-
Notifications
You must be signed in to change notification settings - Fork 13
/
dotnetversionsuffix.cs
130 lines (105 loc) · 4.49 KB
/
dotnetversionsuffix.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.Build.Definition;
using Microsoft.Build.Evaluation;
public static class Program
{
public static void Main(string[] args)
{
var parameters = new Parameters(args);
Console.Out.WriteLine(
$"Setting version {parameters.VersionPart} '{parameters.VersionValue}' for all projects of solutions located in '{parameters.WorkingDirectory}'.");
var solutionFiles = Directory.GetFiles(parameters.WorkingDirectory, "*.sln");
if (solutionFiles.Length == 0)
{
Console.Out.WriteLine("No solution files found.");
return;
}
Console.Out.WriteLine(
$"Found solution files: {Environment.NewLine}\t{string.Join(Environment.NewLine + "\t", solutionFiles)}");
foreach (var solutionFile in solutionFiles)
HandleSolution(solutionFile, parameters);
}
private static void HandleSolution(string solutionFile, Parameters parameters)
{
var parsedSolution = SolutionFile.Parse(solutionFile);
Console.Out.WriteLine($"Working with '{solutionFile}.");
Console.Out.WriteLine($"Working with '{parameters.SolutionConfiguration}' solution configuration.");
var projects = FilterProjectsByConfiguration(parsedSolution.ProjectsInOrder, parameters.SolutionConfiguration);
var projectFiles = projects
.Select(project => project.AbsolutePath)
.ToArray();
if (projectFiles.Length == 0)
{
Console.Out.WriteLine("No projects found in solutions.");
return;
}
Console.Out.WriteLine(
$"Found project files: {Environment.NewLine}\t{string.Join(Environment.NewLine + "\t", projectFiles)}");
foreach (var projectFile in projectFiles)
{
if (!File.Exists(projectFile))
{
Console.Out.WriteLine($"Project file '{projectFile}' doesn't exists.");
continue;
}
Console.Out.WriteLine($"Working with project '{Path.GetFileName(projectFile)}'..");
var project = Project.FromFile(projectFile, new ProjectOptions
{
LoadSettings = ProjectLoadSettings.IgnoreMissingImports
});
project.SetProperty(parameters.CsProjProperty, parameters.VersionValue);
project.Save();
}
}
private static IEnumerable<ProjectInSolution> FilterProjectsByConfiguration(
IEnumerable<ProjectInSolution> projects,
string configuration)
{
var keyPrefix = configuration + "|";
foreach (var project in projects)
{
var configurations = project.ProjectConfigurations;
var enabledConfigurations = configurations.Where(x => x.Value.IncludeInBuild);
if (enabledConfigurations.Any(x => x.Key.StartsWith(keyPrefix, StringComparison.OrdinalIgnoreCase)))
yield return project;
}
}
private class Parameters
{
public string VersionPart { get; }
public string CsProjProperty { get; }
public string VersionValue { get; }
public string WorkingDirectory { get; }
public string SolutionConfiguration { get; }
public Parameters(string[] args)
{
VersionPart = args.Contains("--version")
? "version"
: args.Contains("--prefix")
? "prefix"
: "suffix";
CsProjProperty = args.Contains("--version")
? "Version"
: args.Contains("--prefix")
? "VersionPrefix"
: "VersionSuffix";
var positionalArgs = args.Where(x => !x.StartsWith("--")).ToArray();
if (positionalArgs.Length <= 0)
throw new Exception($"Missing required argument: version {VersionPart}.");
VersionValue = positionalArgs[0];
WorkingDirectory = positionalArgs.Length > 1 ? positionalArgs[1] : Environment.CurrentDirectory;
SolutionConfiguration =
GetArgsByKey(args, "--solutionConfiguration:").FirstOrDefault() ?? "Release";
}
private static IEnumerable<string> GetArgsByKey(string[] args, string key)
{
return args
.Where(x => x.StartsWith(key))
.Select(x => x.Substring(key.Length).Trim());
}
}
}