-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.csx
106 lines (89 loc) · 3.35 KB
/
tasks.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Text.RegularExpressions;
private static readonly string Version = File.ReadAllText("version");
private static readonly Dictionary<string, Action<string[]>> Tasks = new Dictionary<string, Action<string[]>>(StringComparer.OrdinalIgnoreCase)
{
[nameof(SetVersion)] = SetVersion,
[nameof(Build)] = Build,
[nameof(Pack)] = Pack,
[nameof(Clean)] = Clean,
[nameof(NugetPush)] = NugetPush,
[nameof(NugetPushLocal)] = NugetPushLocal
};
var task = Args.FirstOrDefault();
if (string.IsNullOrWhiteSpace(task))
return Fail("Task not specified.");
if (!Tasks.ContainsKey(task))
return Fail($"Invalid task '{task}' specified");
var now = DateTime.Now;
Console.WriteLine($"[{DateTime.Now:O}] Executing task '{task}'...");
Tasks[task](Args.Skip(1).ToArray());
return Succeed($"[{DateTime.Now:O}] Task '{task}' executed successfully.");
private static void SetVersion(params string[] args)
{
var version = args.FirstOrDefault() ?? Version;
RunForEachProject((projectFolder, projectName, csprojFile) =>
File.WriteAllText(
csprojFile,
Regex.Replace(
File.ReadAllText(csprojFile),
@"\<PackageVersion\>\d+\.\d+\.\d+(?:-(?:alpha|beta|rc)\d+)?\<\/PackageVersion\>",
$"<PackageVersion>{version}</PackageVersion>")));
if (version != Version)
File.WriteAllText("version", version);
}
private static void Build(params string[] args)
=>
RunDotNetCmd("build");
private static void Pack(params string[] args)
=>
RunDotNetCmd("pack");
private static void Clean(params string[] args)
=>
RunDotNetCmd("clean");
private static void NugetPush(params string[] args)
{
if (RunDotNetCmd("pack") == 0)
RunForEachProject((projectFolder, projectName, csprojFile) =>
RunDotNetCmd(
"nuget push",
$"{projectFolder}/bin/packages/{projectName}.{Version}.nupkg",
"-s=https://api.nuget.org/v3/index.json",
$"-k={Environment.GetEnvironmentVariable("NUGET_API_KEY")}"));
}
private static void NugetPushLocal(params string[] args)
{
if (RunDotNetCmd("pack") == 0)
RunForEachProject((projectFolder, projectName, csprojFile) =>
RunDotNetCmd(
"nuget push",
$"{projectFolder}/bin/packages/{projectName}.{Version}.nupkg",
"-s=Local"));
}
private static int RunDotNetCmd(string cmd, params string[] args)
{
var process = Process.Start("dotnet", cmd + " " + string.Join(" ", args));
process.WaitForExit();
return process.ExitCode;
}
private static int RunForEachProject(Action<string, string, string> run)
{
foreach (var csprojFile in Directory.GetFiles(".", "*.csproj", SearchOption.AllDirectories))
run(Path.GetDirectoryName(csprojFile), Path.GetFileNameWithoutExtension(csprojFile), csprojFile);
return 0;
}
private static int Fail(string message)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(message);
Console.ForegroundColor = originalColor;
return -1;
}
private static int Succeed(string message)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(message);
Console.ForegroundColor = originalColor;
return 0;
}