forked from mestiez/Walgelijk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
increment_version.csx
83 lines (72 loc) · 2.34 KB
/
increment_version.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml.Linq;
using System.Text.RegularExpressions;
/*
Usage:
Increment version of projects with changed files (according to git)
> dotnet script increment_version.csx
Increment version of all projects
> dotnet script increment_version.csx all
*/
List<string> GetChangedFiles()
{
var changedFiles = new List<string>();
using var process = new Process();
process.StartInfo.FileName = "git";
process.StartInfo.Arguments = "diff --name-only --diff-filter=ACM";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string line;
while ((line = process.StandardOutput.ReadLine()) != null)
changedFiles.Add(line);
process.WaitForExit();
return changedFiles;
}
HashSet<string> projectDirs = new();
bool doAll = Args.Count == 1 && Args[0] == "all";
if (doAll)
{
foreach (var p in Directory.EnumerateFiles(".", "*.csproj", SearchOption.AllDirectories))
{
if (!Regex.IsMatch(p, @"(Walgelijk)\.?\w+"))
continue;
var dir = Path.GetDirectoryName(p);
projectDirs.Add(dir);
Console.WriteLine("Project found {0}, {1}", p, dir);
}
}
else
{
foreach (var path in GetChangedFiles())
{
var p = new FileInfo(path);
var relativeDir = Path.GetRelativePath(Environment.CurrentDirectory, p.DirectoryName);
relativeDir = relativeDir.Split(new[]{Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar})[0];
projectDirs.Add(relativeDir);
Console.WriteLine("Change detected: {0} in solution {1}", path, relativeDir);
}
}
foreach (var project in projectDirs)
{
var csProj = Directory.EnumerateFiles(project, "*.csproj").FirstOrDefault() ?? $"{project}/{project}.csproj";
if (File.Exists(csProj))
{
Console.WriteLine(csProj);
var doc = XDocument.Load(csProj);
var versionElement = doc.Root.Element("PropertyGroup").Element("Version");
if (versionElement == null)
{
versionElement = new XElement("Version", "1.0.0");
doc.Root.Element("PropertyGroup").Add(versionElement);
}
var version = Version.Parse((string)versionElement);
var newVersion = new Version(version.Major, version.Minor, version.Build + 1);
Console.WriteLine("{0} >> {1}", version, newVersion);
versionElement.SetValue(newVersion.ToString());
doc.Save(csProj);
}else
Console.Error.WriteLine("csproj does not exist: {0}", csProj);
}