22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . Linq ;
5+ using System . Text ;
56
67namespace LuaInstaller . Core
78{
89 public sealed class VisualStudioCompiler : ICompiler
910 {
1011 private string _buildDirectory ;
1112 private readonly string _path ;
12- private readonly ICollection < CompilerOption > _options ;
13- private readonly ICollection < CompilerOption > _sourceFiles ;
13+ private readonly IDictionary < int , Queue < CompilerOption > > _options ;
1414
1515 public VisualStudioCompiler ( string path )
1616 {
@@ -20,8 +20,7 @@ public VisualStudioCompiler(string path)
2020 }
2121
2222 _path = path ;
23- _options = new List < CompilerOption > ( ) ;
24- _sourceFiles = new List < CompilerOption > ( ) ;
23+ _options = new Dictionary < int , Queue < CompilerOption > > ( ) ;
2524 }
2625
2726 public string BuildDirectory
@@ -78,15 +77,13 @@ public void AddCompilerOption(CompilerOption option)
7877 {
7978 throw new ArgumentNullException ( ) ;
8079 }
81-
82- if ( option is VisualStudioSourceFileCompilerOption )
83- {
84- _sourceFiles . Add ( option ) ;
85- }
86- else
80+ Queue < CompilerOption > queue ;
81+ if ( ! _options . TryGetValue ( option . CommandLineSortOrder , out queue ) )
8782 {
88- _options . Add ( option ) ;
83+ queue = new Queue < CompilerOption > ( ) ;
84+ _options . Add ( option . CommandLineSortOrder , queue ) ;
8985 }
86+ queue . Enqueue ( option ) ;
9087 }
9188
9289 private static string FormatCompilerOption ( CompilerOption option )
@@ -98,11 +95,22 @@ public int Execute()
9895 {
9996 int result = 1 ;
10097
101- string arguments = string . Format (
102- "{0} {1}" ,
103- string . Join ( " " , _options . Select ( FormatCompilerOption ) . ToArray ( ) ) ,
104- string . Join ( " " , _sourceFiles . Select ( FormatCompilerOption ) . ToArray ( ) )
105- ) ;
98+ int key ;
99+ ICollection < int > keys = _options . Keys ;
100+ int [ ] orderedKeys = new int [ keys . Count ] ;
101+ keys . CopyTo ( orderedKeys , 0 ) ;
102+ Array . Sort < int > ( orderedKeys ) ;
103+ StringBuilder argumentsBuilder = new StringBuilder ( Math . Max ( 2 * orderedKeys . Length - 1 , 4 ) ) ;
104+ for ( int i = 0 ; i < orderedKeys . Length ; i ++ )
105+ {
106+ key = orderedKeys [ i ] ;
107+ argumentsBuilder . Append ( string . Join ( " " , _options [ key ] . Select ( FormatCompilerOption ) ) ) ;
108+ if ( i < orderedKeys . Length - 1 )
109+ {
110+ argumentsBuilder . Append ( " " ) ;
111+ }
112+ }
113+ string arguments = argumentsBuilder . ToString ( ) ;
106114
107115 ProcessStartInfo compilePsi = new ProcessStartInfo ( ) ;
108116 compilePsi . UseShellExecute = false ;
@@ -127,7 +135,6 @@ public void Reset()
127135 {
128136 _buildDirectory = null ;
129137 _options . Clear ( ) ;
130- _sourceFiles . Clear ( ) ;
131138 }
132139 }
133140}
0 commit comments