2016-10-10 14:45:28 +00:00
#region = = = = = = = = = = = = = = = = = = = = = = = = Namespaces
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Globalization ;
using System.IO ;
using System.Reflection ;
#endregion
2016-12-26 23:05:53 +00:00
namespace mxd.VersionFromGIT
2016-10-10 14:45:28 +00:00
{
public static class Program
{
#region = = = = = = = = = = = = = = = = = = = = = = = = Constants
2019-03-29 18:40:55 +00:00
private const string GIT_INFO = "@echo off\r\ngit.exe rev-list --count origin/master\r\ngit.exe rev-parse --short=7 origin/master" ;
2016-10-10 14:45:28 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = = = = = = = Main
[STAThread]
private static int Main ( string [ ] args )
{
bool showusageinfo = true ;
bool dorevisionlookup = true ;
bool reverttargets = false ;
string revision = "" ;
2016-12-28 12:24:58 +00:00
string shorthash = "" ;
2016-10-10 14:45:28 +00:00
string apppath = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
string revisionoutputfile = "" ;
List < string > targetfiles = new List < string > ( ) ;
Queue < string > queue = new Queue < string > ( args ) ;
if ( string . IsNullOrEmpty ( apppath ) )
{
Console . WriteLine ( "Invalid working directory: '" + apppath + "'" ) ;
return 2 ;
}
while ( queue . Count > 0 )
{
string arg = queue . Dequeue ( ) ;
if ( string . Compare ( arg , "-F" , true ) = = 0 )
{
if ( queue . Count > 0 )
{
revision = queue . Dequeue ( ) ;
dorevisionlookup = false ;
}
else
{
showusageinfo = true ;
break ;
}
}
else if ( string . Compare ( arg , "-O" , true ) = = 0 )
{
if ( queue . Count > 0 ) revisionoutputfile = queue . Dequeue ( ) ;
}
else if ( string . Compare ( arg , "-R" , true ) = = 0 )
{
dorevisionlookup = false ;
reverttargets = true ;
}
else
{
targetfiles . Add ( arg ) ;
showusageinfo = false ;
}
}
if ( showusageinfo )
{
ShowUsageInfo ( ) ;
Console . ReadKey ( ) ;
return 0 ;
}
if ( dorevisionlookup )
{
2016-12-26 23:05:53 +00:00
Console . WriteLine ( "Looking up latest git revision..." ) ;
2016-10-10 14:45:28 +00:00
2016-12-26 23:05:53 +00:00
string batpath = Path . Combine ( apppath , "versionfromgit.bat" ) ;
File . WriteAllText ( batpath , GIT_INFO ) ;
2016-10-10 14:45:28 +00:00
2016-12-26 23:05:53 +00:00
// Redirect the output stream of the child process.
Process p = new Process
2016-10-10 14:45:28 +00:00
{
2016-12-26 23:05:53 +00:00
StartInfo =
2016-10-10 14:45:28 +00:00
{
2016-12-26 23:05:53 +00:00
UseShellExecute = false ,
RedirectStandardOutput = true ,
WindowStyle = ProcessWindowStyle . Hidden ,
FileName = batpath ,
WorkingDirectory = apppath
2016-10-10 14:45:28 +00:00
}
2016-12-26 23:05:53 +00:00
} ;
// Redirect the output stream of the child process.
p . Start ( ) ;
2016-10-10 14:45:28 +00:00
2016-12-26 23:05:53 +00:00
// Read the output stream first and then wait.
2016-12-28 12:24:58 +00:00
string output = p . StandardOutput . ReadToEnd ( ) . Trim ( ) ; //mxd. first line is revision, second - short hash
2016-12-26 23:05:53 +00:00
p . WaitForExit ( ) ;
2016-10-10 14:45:28 +00:00
File . Delete ( batpath ) ;
2016-12-28 12:24:58 +00:00
string [ ] parts = output . Split ( new [ ] { '\n' } , StringSplitOptions . RemoveEmptyEntries ) ;
if ( parts . Length ! = 2 )
{
Console . WriteLine ( "Unable to get Git info from string \"" + output + "\". You must install Git from https://git-scm.com" ) ;
return 3 ;
}
//mxd. Check hash
shorthash = parts [ 1 ] ;
if ( shorthash . Length ! = 7 )
{
Console . WriteLine ( "Unable to get Git hash from string \"" + shorthash + "\". You must install Git from https://git-scm.com" ) ;
return 4 ;
}
//mxd. Check revision
2016-12-26 23:05:53 +00:00
int unused ;
2016-12-28 12:24:58 +00:00
revision = parts [ 0 ] ;
2016-12-26 23:05:53 +00:00
if ( string . IsNullOrEmpty ( revision ) | | ! int . TryParse ( revision , out unused ) | | unused < 1 )
{
Console . WriteLine ( "Unable to get Git commits count from string \"" + revision + "\". You must install Git from https://git-scm.com" ) ;
return 1 ;
}
2016-10-10 14:45:28 +00:00
}
if ( ! string . IsNullOrEmpty ( revisionoutputfile ) )
2016-12-28 12:24:58 +00:00
File . AppendAllText ( revisionoutputfile , "SET REVISIONNUMBER=" + revision + "\nSET REVISIONHASH=" + shorthash ) ;
2016-10-10 14:45:28 +00:00
if ( reverttargets )
{
Console . WriteLine ( "Reverting changes in version files..." ) ;
string contents = "" ;
foreach ( string str in targetfiles )
2016-12-26 23:05:53 +00:00
contents + = "git checkout \"" + str + "\"\r\n" ;
2016-10-10 14:45:28 +00:00
2016-12-26 23:05:53 +00:00
string path = Path . Combine ( apppath , "versionfromgit.bat" ) ;
2016-10-10 14:45:28 +00:00
File . WriteAllText ( path , contents ) ;
ProcessStartInfo info = new ProcessStartInfo
{
FileName = path ,
CreateNoWindow = false ,
ErrorDialog = false ,
UseShellExecute = true ,
WindowStyle = ProcessWindowStyle . Hidden ,
WorkingDirectory = apppath
} ;
Process . Start ( info ) . WaitForExit ( ) ;
File . Delete ( path ) ;
}
else
{
2016-12-28 12:24:58 +00:00
Console . WriteLine ( "Writing revision " + revision + ", hash " + shorthash + " to target files..." ) ;
2016-10-10 14:45:28 +00:00
foreach ( string file in targetfiles )
{
bool changed = false ;
string [ ] contents = File . ReadAllLines ( file ) ;
for ( int i = 0 ; i < contents . Length ; + + i )
{
string line = contents [ i ] ;
if ( line . Trim ( ) . StartsWith ( "[assembly: AssemblyVersion" , true , CultureInfo . InvariantCulture ) )
{
int startbracepos = line . IndexOf ( "\"" , 0 ) ;
int endbracepos = line . IndexOf ( "\"" , startbracepos + 1 ) ;
int revisiondotpos = line . LastIndexOf ( "." , endbracepos , endbracepos - startbracepos ) ;
string linestart = line . Substring ( 0 , revisiondotpos + 1 ) ;
string lineend = line . Substring ( endbracepos ) ;
string result = linestart + revision + lineend ;
2016-12-28 12:24:58 +00:00
if ( string . Compare ( contents [ i ] , result , true ) ! = 0 )
{
contents [ i ] = result ;
changed = true ;
}
}
//mxd. Apply hash
else if ( line . Trim ( ) . StartsWith ( "[assembly: AssemblyHash" , true , CultureInfo . InvariantCulture ) )
{
int startbracepos = line . IndexOf ( "\"" , 0 ) ;
int endbracepos = line . IndexOf ( "\"" , startbracepos + 1 ) ;
string linestart = line . Substring ( 0 , startbracepos + 1 ) ;
string lineend = line . Substring ( endbracepos ) ;
string result = linestart + shorthash + lineend ;
2016-10-10 14:45:28 +00:00
if ( string . Compare ( contents [ i ] , result , true ) ! = 0 )
{
contents [ i ] = result ;
changed = true ;
}
}
}
if ( changed )
{
File . Copy ( file , file + ".backup" ) ;
File . WriteAllLines ( file , contents ) ;
File . Delete ( file + ".backup" ) ;
}
}
}
Console . WriteLine ( "Done." ) ;
return 0 ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = = = = = = = Usage info
private static void ShowUsageInfo ( )
{
2016-12-26 23:05:53 +00:00
Console . WriteLine ( "Version From GIT is a tool programmed by MaxED.\r\nThis tool requires that Git commandline client is installed.\r\nSee https://git-scm.com\r\n" ) ;
Console . WriteLine ( "Usage: versionfromgit.exe targetfile [targetfile ...] [options]" ) ;
Console . WriteLine ( "Where targetfile is AssemblyInfo.cs file in which AssemblyVersion property value must be replaced with the commits count of the GIT master branch.\r\n" ) ;
2016-10-10 14:45:28 +00:00
Console . WriteLine ( "Options:\r\n" ) ;
Console . WriteLine ( "-F number" ) ;
2016-12-26 23:05:53 +00:00
Console . WriteLine ( "When this is used, the given number will be used as commits count and this program will not use Git to look for the real commits count (faster).\r\n" ) ;
2016-10-10 14:45:28 +00:00
Console . WriteLine ( "-R" ) ;
2016-12-26 23:05:53 +00:00
Console . WriteLine ( "This will revert all changes in the specified target files (same as GIT checkout function). This will not apply commits count to the target files.\r\n" ) ;
2016-10-10 14:45:28 +00:00
Console . WriteLine ( "-O filename" ) ;
2016-12-28 12:24:58 +00:00
Console . WriteLine ( "Creates a bath file, which sets REVISIONNUMBER environment variable to the GIT revision number and REVISIONHASH environment variable to the GIT revision short hash.\r\n" ) ;
2016-10-10 14:45:28 +00:00
Console . WriteLine ( "Press any key to quit." ) ;
}
#endregion
}
}