-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-packages.csx
171 lines (137 loc) · 5.79 KB
/
build-packages.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Requires `dotnet script` tool. `dotnet tool install -g dotnet-script`
using System.Runtime.CompilerServices;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO.Compression;
static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path);
static readonly string ArtifactsFolder = Path.Combine(GetScriptFolder(), ".artifacts");
static void ReplaceVersion(string fileName, string version) {
const string pattern = "<Version>.*</Version>";
// Use regex as xml document will change formatting and empty lines.
var content = File.ReadAllText(fileName);
var match = Regex.Match(content, pattern);
if (!match.Success)
{
throw new InvalidOperationException($"File {fileName} does not have a version tag");
}
content = Regex.Replace(content, pattern, $"<Version>{version}</Version>");
File.WriteAllText(fileName, content);
}
static void BuildNugetPackages(string project) {
var packProcess = Process.Start($"dotnet", $"pack -c Release {project}");
packProcess.WaitForExit();
}
static void BuildProject(string project) {
var process = Process.Start("dotnet", $"build -c Release {project}");
process.WaitForExit();
}
static void MoveNugetArtifacts(string project) {
if (!Directory.Exists(ArtifactsFolder)) {
Directory.CreateDirectory(ArtifactsFolder);
}
var projectPath = Path.GetDirectoryName(project);
var nugetPath = Path.Combine(projectPath, "bin", "Release");
var nugetFiles = Directory.GetFiles(nugetPath, "*.nupkg");
if (nugetFiles.Length == 0) {
throw new InvalidOperationException($"No nuget packages found in '{nugetPath}'");
}
foreach (var file in nugetFiles) {
File.Move(file, Path.Combine(ArtifactsFolder, Path.GetFileName(file)), true);
}
}
static void CreateZipArchive(string project, string version) {
if (!Directory.Exists(ArtifactsFolder)) {
Directory.CreateDirectory(ArtifactsFolder);
}
var projectName = Path.GetFileNameWithoutExtension(project);
var projectPath = Path.GetDirectoryName(project);
var folderToZip = Path.Combine(projectPath, "bin", "Release", "netcoreapp3.1");
var resultingZip = Path.Combine(ArtifactsFolder, $"{projectName}.{version}.zip");
if (File.Exists(resultingZip)) {
File.Delete(resultingZip);
}
ZipFile.CreateFromDirectory(folderToZip, resultingZip, CompressionLevel.Optimal, false);
}
static void CreateGluePluginArchive(string project, string version) {
if (!Directory.Exists(ArtifactsFolder)) {
Directory.CreateDirectory(ArtifactsFolder);
}
var projectName = Path.GetFileNameWithoutExtension(project);
var projectPath = Path.GetDirectoryName(project);
var binaryFolder = Path.Combine(projectPath, "bin", "Release", "netcoreapp3.1");
var pluginFolder = Path.Combine(binaryFolder, projectName);
var innerFolder = Path.Combine(pluginFolder, projectName);
var resultingZip = Path.Combine(ArtifactsFolder, $"{projectName}.{version}.zip");
if (File.Exists(resultingZip)) {
File.Delete(resultingZip);
}
if (Directory.Exists(pluginFolder))
{
Directory.Delete(pluginFolder, true);
}
Directory.CreateDirectory(innerFolder);
var includeList = new string[] {
"Parme.Core.dll", "Parme.Core.pdb", "Parme.CSharp.dll", "Parme.CSharp.pdb", "Parme.Frb.GluePlugin.dll",
"Parme.Frb.GluePlugin.pdb"
};
foreach (var file in includeList)
{
var path = Path.Combine(binaryFolder, file);
if (!File.Exists(path))
{
throw new InvalidOperationException($"File '{path}' does not exist but is expected to");
}
File.Copy(path, Path.Combine(innerFolder, file));
}
ZipFile.CreateFromDirectory(pluginFolder, resultingZip, CompressionLevel.Optimal, false);
}
static void RemoveCurrentArtifacts() {
if (!Directory.Exists(ArtifactsFolder)) {
return;
}
var files = Directory.GetFiles(ArtifactsFolder);
foreach (var file in files) {
File.Delete(file);
}
}
if (Args.Count < 1) {
Console.WriteLine("No version passed in");
return;
}
var version = Args[0];
Console.WriteLine($"Version: {version}");
var nugetProjects = new[] {
Path.Combine(GetScriptFolder(), "Parme.Core\\Parme.Core.csproj"),
Path.Combine(GetScriptFolder(), "Parme.CSharp\\Parme.CSharp.csproj"),
Path.Combine(GetScriptFolder(), "Parme.MonoGame\\Parme.MonoGame.csproj"),
Path.Combine(GetScriptFolder(), "Parme.Frb\\Parme.Frb.csproj"),
};
var gluePluginProject = Path.Combine(GetScriptFolder(), "Parme.Frb.GluePlugin\\Parme.Frb.GluePlugin.csproj");
var editorProject = Path.Combine(GetScriptFolder(), "Parme.Editor\\Parme.Editor.csproj");
var cliProject = Path.Combine(GetScriptFolder(), "Parme.Cli\\Parme.Cli.csproj");
RemoveCurrentArtifacts();
Console.WriteLine("Building Glue Plugin");
ReplaceVersion(gluePluginProject, version);
Directory.SetCurrentDirectory(Path.GetDirectoryName(gluePluginProject));
BuildProject(gluePluginProject);
CreateGluePluginArchive(gluePluginProject, version);
Console.WriteLine("Building Editor");
ReplaceVersion(editorProject, version);
Directory.SetCurrentDirectory(Path.GetDirectoryName(editorProject));
BuildProject(editorProject);
CreateZipArchive(editorProject, version);
Console.WriteLine("Building Cli Tool");
ReplaceVersion(cliProject, version);
Directory.SetCurrentDirectory(Path.GetDirectoryName(cliProject));
BuildProject(cliProject);
CreateZipArchive(cliProject, version);
foreach (var project in nugetProjects)
{
Console.WriteLine("Processing " + project);
Directory.SetCurrentDirectory(Path.GetDirectoryName(project));
ReplaceVersion(project, version);
BuildNugetPackages(project);
MoveNugetArtifacts(project);
}