@@ -11,11 +11,10 @@ namespace GitVersion
1111 class Program
1212 {
1313 static StringBuilder log = new StringBuilder ( ) ;
14- const string MsBuild = @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" ;
1514
1615 static void Main ( )
1716 {
18- var exitCode = Run ( ) ;
17+ var exitCode = VerifyArgumentsAndRun ( ) ;
1918
2019 if ( Debugger . IsAttached )
2120 {
@@ -31,10 +30,12 @@ static void Main()
3130 Environment . Exit ( exitCode ) ;
3231 }
3332
34- static int Run ( )
33+ static int VerifyArgumentsAndRun ( )
3534 {
3635 try
3736 {
37+ var fileSystem = new FileSystem ( ) ;
38+
3839 Arguments arguments ;
3940 var argumentsWithoutExeName = GetArgumentsWithoutExeName ( ) ;
4041 try
@@ -48,108 +49,32 @@ static int Run()
4849 HelpWriter . Write ( ) ;
4950 return 1 ;
5051 }
51-
5252 if ( arguments . IsHelp )
5353 {
5454 HelpWriter . Write ( ) ;
5555 return 0 ;
5656 }
5757
58- if ( ! string . IsNullOrEmpty ( arguments . Proj ) || ! string . IsNullOrEmpty ( arguments . Exec ) )
59- {
60- arguments . Output = OutputType . BuildServer ;
61- }
62-
63- ConfigureLogging ( arguments ) ;
64-
65- var gitPreparer = new GitPreparer ( arguments ) ;
66- var gitDirectory = gitPreparer . Prepare ( ) ;
67- if ( string . IsNullOrEmpty ( gitDirectory ) )
68- {
69- Console . Error . WriteLine ( "Failed to prepare or find the .git directory in path '{0}'" , arguments . TargetPath ) ;
70- return 1 ;
71- }
72-
73- var fileSystem = new FileSystem ( ) ;
7458 if ( arguments . Init )
7559 {
76- ConfigurationProvider . WriteSample ( gitDirectory , fileSystem ) ;
60+ ConfigurationProvider . WriteSample ( arguments . TargetPath , fileSystem ) ;
7761 return 0 ;
7862 }
7963 if ( arguments . ShowConfig )
8064 {
81- Console . WriteLine ( ConfigurationProvider . GetEffectiveConfigAsString ( gitDirectory , fileSystem ) ) ;
65+ Console . WriteLine ( ConfigurationProvider . GetEffectiveConfigAsString ( arguments . TargetPath , fileSystem ) ) ;
8266 return 0 ;
8367 }
8468
85- var workingDirectory = Directory . GetParent ( gitDirectory ) . FullName ;
86- Logger . WriteInfo ( "Working directory: " + workingDirectory ) ;
87- var applicableBuildServers = GetApplicableBuildServers ( arguments . Authentication ) . ToList ( ) ;
88-
89- foreach ( var buildServer in applicableBuildServers )
90- {
91- buildServer . PerformPreProcessingSteps ( gitDirectory ) ;
92- }
93- VersionVariables variables ;
94- var versionFinder = new GitVersionFinder ( ) ;
95- var configuration = ConfigurationProvider . Provide ( gitDirectory , fileSystem ) ;
96-
97- using ( var repo = RepositoryLoader . GetRepo ( gitDirectory ) )
98- {
99- var gitVersionContext = new GitVersionContext ( repo , configuration , commitId : arguments . CommitId ) ;
100- var semanticVersion = versionFinder . FindVersion ( gitVersionContext ) ;
101- var config = gitVersionContext . Configuration ;
102- variables = VariableProvider . GetVariablesFor ( semanticVersion , config . AssemblyVersioningScheme , config . VersioningMode , config . ContinuousDeploymentFallbackTag , gitVersionContext . IsCurrentCommitTagged ) ;
103- }
104-
105- if ( arguments . Output == OutputType . BuildServer )
106- {
107- foreach ( var buildServer in applicableBuildServers )
108- {
109- buildServer . WriteIntegration ( Console . WriteLine , variables ) ;
110- }
111- }
112-
113- if ( arguments . Output == OutputType . Json )
69+ if ( ! string . IsNullOrEmpty ( arguments . Proj ) || ! string . IsNullOrEmpty ( arguments . Exec ) )
11470 {
115- switch ( arguments . ShowVariable )
116- {
117- case null :
118- Console . WriteLine ( JsonOutputFormatter . ToJson ( variables ) ) ;
119- break ;
120-
121- default :
122- string part ;
123- if ( ! variables . TryGetValue ( arguments . ShowVariable , out part ) )
124- {
125- throw new WarningException ( string . Format ( "'{0}' variable does not exist" , arguments . ShowVariable ) ) ;
126- }
127- Console . WriteLine ( part ) ;
128- break ;
129- }
71+ arguments . Output = OutputType . BuildServer ;
13072 }
13173
132- using ( var assemblyInfoUpdate = new AssemblyInfoFileUpdate ( arguments , workingDirectory , variables , fileSystem ) )
133- {
134- var execRun = RunExecCommandIfNeeded ( arguments , workingDirectory , variables ) ;
135- var msbuildRun = RunMsBuildIfNeeded ( arguments , workingDirectory , variables ) ;
136- if ( ! execRun && ! msbuildRun )
137- {
138- assemblyInfoUpdate . DoNotRestoreAssemblyInfo ( ) ;
139- //TODO Put warning back
140- //if (!context.CurrentBuildServer.IsRunningInBuildAgent())
141- //{
142- // Console.WriteLine("WARNING: Not running in build server and /ProjectFile or /Exec arguments not passed");
143- // Console.WriteLine();
144- // Console.WriteLine("Run GitVersion.exe /? for help");
145- //}
146- }
147- }
74+ ConfigureLogging ( arguments ) ;
75+ Logger . WriteInfo ( "Working directory: " + arguments . TargetPath ) ;
14876
149- if ( gitPreparer . IsDynamicGitRepository )
150- {
151- DeleteHelper . DeleteGitRepository ( gitPreparer . DynamicGitRepositoryPath ) ;
152- }
77+ SpecifiedArgumentRunner . Run ( arguments , fileSystem ) ;
15378 }
15479 catch ( WarningException exception )
15580 {
@@ -167,11 +92,6 @@ static int Run()
16792 return 0 ;
16893 }
16994
170- static IEnumerable < IBuildServer > GetApplicableBuildServers ( Authentication authentication )
171- {
172- return BuildServerList . GetApplicableBuildServers ( authentication ) ;
173- }
174-
17595 static void ConfigureLogging ( Arguments arguments )
17696 {
17797 var writeActions = new List < Action < string > >
@@ -223,43 +143,5 @@ static List<string> GetArgumentsWithoutExeName()
223143 . Skip ( 1 )
224144 . ToList ( ) ;
225145 }
226-
227- static bool RunMsBuildIfNeeded ( Arguments args , string workingDirectory , VersionVariables variables )
228- {
229- if ( string . IsNullOrEmpty ( args . Proj ) ) return false ;
230-
231- Logger . WriteInfo ( string . Format ( "Launching {0} \" {1}\" {2}" , MsBuild , args . Proj , args . ProjArgs ) ) ;
232- var results = ProcessHelper . Run (
233- Logger . WriteInfo , Logger . WriteError ,
234- null , MsBuild , string . Format ( "\" {0}\" {1}" , args . Proj , args . ProjArgs ) , workingDirectory ,
235- GetEnvironmentalVariables ( variables ) ) ;
236-
237- if ( results != 0 )
238- throw new WarningException ( "MsBuild execution failed, non-zero return code" ) ;
239-
240- return true ;
241- }
242-
243- static bool RunExecCommandIfNeeded ( Arguments args , string workingDirectory , VersionVariables variables )
244- {
245- if ( string . IsNullOrEmpty ( args . Exec ) ) return false ;
246-
247- Logger . WriteInfo ( string . Format ( "Launching {0} {1}" , args . Exec , args . ExecArgs ) ) ;
248- var results = ProcessHelper . Run (
249- Logger . WriteInfo , Logger . WriteError ,
250- null , args . Exec , args . ExecArgs , workingDirectory ,
251- GetEnvironmentalVariables ( variables ) ) ;
252- if ( results != 0 )
253- throw new WarningException ( string . Format ( "Execution of {0} failed, non-zero return code" , args . Exec ) ) ;
254-
255- return true ;
256- }
257-
258- static KeyValuePair < string , string > [ ] GetEnvironmentalVariables ( VersionVariables variables )
259- {
260- return variables
261- . Select ( v => new KeyValuePair < string , string > ( "GitVersion_" + v . Key , v . Value ) )
262- . ToArray ( ) ;
263- }
264146 }
265147}
0 commit comments