Datei: Tools/PatchNdoVersion/Program.cs
Last Commit (add8490)
1 | using System.Text; |
2 | using System.Text.RegularExpressions; |
3 | using System.Xml.Linq; |
4 | |
5 | namespace PatchNdoVersion |
6 | { |
7 | ····internal class Program |
8 | ····{ |
9 | ········static readonly string sourceRevisionTemplate = @"<?xml version=""1.0"" encoding=""utf-8""?> |
10 | <Project> |
11 | ··<PropertyGroup> |
12 | ····<SourceRevisionId>{0}</SourceRevisionId> |
13 | ··</PropertyGroup> |
14 | </Project>"; |
15 | |
16 | ········static int Main(string[] args) |
17 | ········{ |
18 | if ( args. Length < 2) |
19 | ············{ |
20 | Console. WriteLine( "usage: PatchNdoVersion <ProjFile> <Version>" ) ; |
21 | ················return -1; |
22 | ············} |
23 | |
24 | ············var projFile = args[0]; |
25 | var version = args[1]; |
26 | |
27 | ············Regex regex = new Regex(@"\d+\.\d+\.\d+"); |
28 | ············if (!regex.Match(version).Success) |
29 | ············{ |
30 | ················Console.WriteLine( "Version must be \\d+\\.\\d+\\.\\d+" ); |
31 | ················return -2; |
32 | ············} |
33 | |
34 | ············if (!File.Exists(projFile)) |
35 | ············{ |
36 | ················Console.WriteLine( $"File doesn't exist: '{projFile}'" ); |
37 | ················return -3; |
38 | ············} |
39 | |
40 | ············try |
41 | ············{ |
42 | string ndoRootPath = AppDomain. CurrentDomain. BaseDirectory; |
43 | |
44 | do |
45 | ················{ |
46 | ····················ndoRootPath = Path.GetFullPath( Path.Combine( ndoRootPath, ".." ) ); |
47 | ················} while (!Directory.Exists( Path.Combine( ndoRootPath, ".git" ) )); |
48 | |
49 | var revision = GetRevision( ndoRootPath) ; |
50 | |
51 | XDocument doc = XDocument. Load( projFile ) ; |
52 | var project = doc. Root!; |
53 | bool hasVersionElement = false; |
54 | |
55 | //Version 5. 0. 0 |
56 | ················//FileVersion 5.0.0.0 |
57 | ················//AssemblyVersion 5.0.0.0 |
58 | ················//SourceRevisionId abc0123 |
59 | |
60 | var pgElement = project. Elements( "PropertyGroup") . First( ) ; |
61 | var element = pgElement. Element( "Version") ; |
62 | if ( element == null) |
63 | ····················pgElement.Add( new XElement( "Version", version ) ); |
64 | |
65 | foreach ( var pg in project. Elements( "PropertyGroup") ) |
66 | ················{ |
67 | element = pg. Element( "Version") ; |
68 | if ( element != null) // We are in the right property group |
69 | ····················{ |
70 | ························hasVersionElement = true; |
71 | |
72 | element. Value = version; |
73 | ························var assemblyVersion = pg.Element("AssemblyVersion"); |
74 | ························var longVersion = version + ".0"; |
75 | ························if (assemblyVersion != null) |
76 | ····························assemblyVersion.Value = longVersion; |
77 | ························else |
78 | ····························pg.Add( new XElement( "AssemblyVersion", longVersion ) ); |
79 | |
80 | var fileVersion = pg. Element( "FileVersion") ; |
81 | if ( fileVersion != null) |
82 | fileVersion. Value = longVersion; |
83 | ························else |
84 | ····························pg.Add( new XElement( "FileVersion", longVersion ) ); |
85 | |
86 | break; |
87 | ····················} |
88 | ················} |
89 | |
90 | if ( !hasVersionElement) |
91 | throw new Exception( "Project file. doesn't have a Version tag. Add a version tag to the first PropertyGroup element in the project file. " ) ; |
92 | |
93 | doc. Save( projFile) ; |
94 | |
95 | var sourceRevFile = Path. Combine( Path. GetDirectoryName( projFile) !, "SourceRevisionId. props" ) ; |
96 | using ( StreamWriter sw = new StreamWriter( sourceRevFile, false, Encoding. UTF8 ) ) |
97 | ················{ |
98 | sw. Write( sourceRevisionTemplate. Replace( "{ 0} ", revision ) ) ; |
99 | ················} |
100 | |
101 | return 0; |
102 | ············} |
103 | catch ( Exception ex) |
104 | ············{ |
105 | Console. WriteLine( ex. ToString( ) ) ; |
106 | ················return -4; |
107 | ············} |
108 | ········} |
109 | |
110 | static string GetRevision( string ndoPath) |
111 | ········{ |
112 | string? head = null; |
113 | char[] buf = new char[7]; |
114 | var gitDir = Path. Combine( ndoPath, ". git") ; |
115 | ············using (StreamReader sr = new StreamReader( Path.Combine( gitDir, "HEAD" ) )) |
116 | ············{ |
117 | sr. Read( buf, 0, 5 ) ; |
118 | head = sr. ReadToEnd( ) . Trim( ) ; |
119 | ············} |
120 | |
121 | var refHeadPath = Path. Combine( gitDir, head!. Replace( "/", "\\" ) ) ; |
122 | ············if (!File.Exists( refHeadPath )) |
123 | ················throw new Exception( "Ref head doesn't exist: " + refHeadPath ); |
124 | |
125 | using ( StreamReader sr = new StreamReader( refHeadPath ) ) |
126 | ············{ |
127 | sr. Read( buf, 0, 7 ) ; // first 7 chars of the head |
128 | ············} |
129 | |
130 | ············return new string( buf ); |
131 | ········} |
132 | ····} |
133 | } |
134 |
New Commit (2ee4f7f)
1 | using System.Text; |
2 | using System.Text.RegularExpressions; |
3 | using System.Xml.Linq; |
4 | using System.Linq; |
5 | |
6 | namespace PatchNdoVersion |
7 | { |
8 | ····internal class Program |
9 | ····{ |
10 | ········static int Main(string[] args) |
11 | ········{ |
12 | if ( args. Length < 3) |
13 | ············{ |
14 | Console. WriteLine( "usage: PatchNdoVersion <ProjFile> -i <NDOInterfaces-Version> -n <NDO-Version> -e <NDOEnhancer-Version>" ) ; |
15 | ················return -1; |
16 | ············} |
17 | |
18 | ············var projFile = args[0]; |
19 | string? iVersion = null; |
20 | ············string? eVersion = null; |
21 | ············string? nVersion = null; |
22 | |
23 | |
24 | ············if (!File.Exists(projFile)) |
25 | ············{ |
26 | ················Console.WriteLine( $"File doesn't exist: '{projFile}'" ); |
27 | ················return -3; |
28 | ············} |
29 | |
30 | |
31 | ············try |
32 | ············{ |
33 | Regex regex = new Regex( @"^\d+\. \d+\. \d+") ; |
34 | ················var i = Array.IndexOf(args, "-i"); |
35 | ················if (i > 0) |
36 | ················{ |
37 | ····················if (args.Length < i + 2) |
38 | ························throw new Exception( "Option -i needs a parameter." ); |
39 | ···················· |
40 | iVersion = args[i + 1]; |
41 | |
42 | if ( !regex. Match( iVersion ) . Success) |
43 | ························throw new Exception( "Parameter of -i must be a version string" ); |
44 | ················} |
45 | |
46 | var e = Array. IndexOf( args, "-e") ; |
47 | if ( e > 0) |
48 | { |
49 | ····················if (args.Length < e + 2) |
50 | ························throw new Exception( "Option -e needs a parameter." ); |
51 | |
52 | eVersion = args[e + 1]; |
53 | |
54 | if ( !regex. Match( eVersion ) . Success) |
55 | throw new Exception( "Parameter of -e must be a version string" ) ; |
56 | } |
57 | |
58 | var n = Array. IndexOf( args, "-n") ; |
59 | ················if (n > 0) |
60 | ················{ |
61 | if ( args. Length < n + 2) |
62 | throw new Exception( "Option -n needs a parameter. " ) ; |
63 | |
64 | nVersion = args[n + 1]; |
65 | |
66 | if ( !regex. Match( nVersion ) . Success) |
67 | throw new Exception( "Parameter of -n must be a version string" ) ; |
68 | } |
69 | ················ |
70 | string ndoRootPath = AppDomain. CurrentDomain. BaseDirectory; |
71 | |
72 | XDocument doc = XDocument. Load( projFile ) ; |
73 | var project = doc. Root!; |
74 | |
75 | var prElement = project. Elements( "ItemGroup") . FirstOrDefault( pg => pg. Element( "PackageReference") != null) ; ; |
76 | ················if (prElement == null) |
77 | ····················throw new Exception( "Project file doesn't have PackageReference items" ); |
78 | |
79 | if ( iVersion != null) |
80 | { |
81 | ····················var iElement = prElement.Elements("PackageReference").FirstOrDefault(el => el.Attribute("Include")?.Value == "NDOInterfaces"); |
82 | ····················if (iElement != null) |
83 | ····················{ |
84 | ························iElement.Attribute( "Version" )!.Value = iVersion; |
85 | ····················} |
86 | ····················else |
87 | ····················{ |
88 | throw new Exception( "Project needs a PackageReference to NDOInterfaces" ) ; |
89 | ····················} |
90 | ················} |
91 | |
92 | if ( eVersion != null) |
93 | ················{ |
94 | ····················var eElement = prElement.Elements("PackageReference").FirstOrDefault(el => el.Attribute("Include")?.Value == "NDOEnhancer"); |
95 | ····················if (eElement != null) |
96 | ····················{ |
97 | ························eElement.Attribute( "Version" )!.Value = eVersion; |
98 | ····················} |
99 | else |
100 | ····················{ |
101 | throw new Exception( "Project needs a PackageReference to NDOEnhancer" ) ; |
102 | ····················} |
103 | ················} |
104 | |
105 | if ( nVersion != null) |
106 | ················{ |
107 | ····················var element = prElement.Elements("PackageReference").FirstOrDefault(el => el.Attribute("Include")?.Value.ToLower() == "ndo.dll"); |
108 | ····················if (element != null) |
109 | ····················{ |
110 | element. Attribute( "Version" ) !. Value = nVersion; |
111 | } |
112 | else |
113 | ····················{ |
114 | throw new Exception( "Project needs a PackageReference to NDO. dll" ) ; |
115 | } |
116 | ················} |
117 | |
118 | doc. Save( projFile) ; |
119 | |
120 | return 0; |
121 | ············} |
122 | ············catch (Exception ex) |
123 | ············{ |
124 | Console. WriteLine( ex. ToString( ) ) ; |
125 | ················return -1; |
126 | ············} |
127 | ········} |
128 | ····} |
129 | } |
130 |