Datei: Tools/PatchNdoVersion/Program.cs
Last Commit (f707e1c)
1 | using System.Text.RegularExpressions; |
2 | using System.Xml.Linq; |
3 | |
4 | namespace PatchNdoVersion |
5 | { |
6 | ····internal class Program |
7 | ····{ |
8 | ········static int Main(string[] args) |
9 | ········{ |
10 | ············if (args.Length < 2) |
11 | ············{ |
12 | ················Console.WriteLine( "usage: PatchNdoVersion <ProjFile> <Version>" ); |
13 | ················return -1; |
14 | ············} |
15 | |
16 | ············var projFile = args[0]; |
17 | ············var version = args[1]; |
18 | |
19 | ············Regex regex = new Regex(@"\d+\.\d+\.\d+"); |
20 | ············if (!regex.Match(version).Success) |
21 | ············{ |
22 | ················Console.WriteLine( "Version must be \\d+\\.\\d+\\.\\d+" ); |
23 | ················return -2; |
24 | ············} |
25 | |
26 | ············if (!File.Exists(projFile)) |
27 | ············{ |
28 | ················Console.WriteLine( $"File doesn't exist: '{projFile}'" ); |
29 | ················return -3; |
30 | ············} |
31 | |
32 | ············try |
33 | ············{ |
34 | ················string ndoRootPath = AppDomain.CurrentDomain.BaseDirectory; |
35 | |
36 | ················do |
37 | ················{ |
38 | ····················ndoRootPath = Path.GetFullPath( Path.Combine( ndoRootPath, ".." ) ); |
39 | ················} while (!Directory.Exists( Path.Combine( ndoRootPath, ".git" ) )); |
40 | |
41 | ················var revision = GetRevision(ndoRootPath); |
42 | |
43 | ················XDocument doc = XDocument.Load( projFile ); |
44 | ················var project = doc.Root!; |
45 | ················bool hasVersionElement = false; |
46 | |
47 | ················//Version 5.0.0 |
48 | ················//FileVersion 5.0.0.0 |
49 | ················//AssemblyVersion 5.0.0.0 |
50 | ················//SourceRevisionId abc0123 |
51 | ················foreach (var pg in project.Elements("PropertyGroup")) |
52 | ················{ |
53 | var element = pg. Element( "Version") ; |
54 | ····················if (element != null)··// We are in the right property group |
55 | ····················{ |
56 | ························hasVersionElement = true; |
57 | |
58 | ························element.Value = version; |
59 | ························var assemblyVersion = pg.Element("AssemblyVersion"); |
60 | ························var longVersion = version + ".0"; |
61 | ························if (assemblyVersion != null) |
62 | ····························assemblyVersion.Value = longVersion; |
63 | ························else |
64 | ····························pg.Add( new XElement( "AssemblyVersion", longVersion ) ); |
65 | |
66 | ························var fileVersion = pg.Element("FileVersion"); |
67 | ························if (fileVersion != null) |
68 | ····························fileVersion.Value = longVersion; |
69 | ························else |
70 | ····························pg.Add( new XElement( "FileVersion", longVersion ) ); |
71 | |
72 | ························var sourceRevisionId = pg.Element("SourceRevisionId"); |
73 | ························if (sourceRevisionId != null) |
74 | ····························sourceRevisionId.Value = revision; |
75 | ························else |
76 | ····························pg.Add( new XElement( "SourceRevisionId", revision ) ); |
77 | |
78 | ························break; |
79 | ····················} |
80 | ················} |
81 | |
82 | ················if (!hasVersionElement) |
83 | ····················throw new Exception( "Project file. doesn't have a Version tag. Add a version tag to the first PropertyGroup element in the project file." ); |
84 | |
85 | ················doc.Save(projFile); |
86 | ················return 0; |
87 | ············} |
88 | ············catch (Exception ex) |
89 | ············{ |
90 | ················Console.WriteLine( ex.ToString() ); |
91 | ················return -4; |
92 | ············} |
93 | ········} |
94 | |
95 | ········static string GetRevision(string ndoPath) |
96 | ········{ |
97 | ············string? head = null; |
98 | ············char[] buf = new char[7]; |
99 | ············var gitDir = Path.Combine(ndoPath, ".git"); |
100 | ············using (StreamReader sr = new StreamReader( Path.Combine( gitDir, "HEAD" ) )) |
101 | ············{ |
102 | ················sr.Read( buf, 0, 5 ); |
103 | ················head = sr.ReadToEnd().Trim(); |
104 | ············} |
105 | |
106 | ············var refHeadPath = Path.Combine( gitDir, head!.Replace( "/", "\\" ) ); |
107 | ············if (!File.Exists( refHeadPath )) |
108 | ················throw new Exception( "Ref head doesn't exist: " + refHeadPath ); |
109 | |
110 | ············using (StreamReader sr = new StreamReader( refHeadPath )) |
111 | ············{ |
112 | ················sr.Read( buf, 0, 7 );··// first 7 chars of the head················ |
113 | ············} |
114 | |
115 | ············return new string( buf ); |
116 | ········} |
117 | ····} |
118 | } |
119 |
New Commit (259cc0c)
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 |