-
Notifications
You must be signed in to change notification settings - Fork 36
/
Collectors.cs
252 lines (224 loc) · 10.7 KB
/
Collectors.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// IL2C - A translator for ECMA-335 CIL/MSIL to C language.
// Copyright (c) 2016-2019 Kouji Matsui (@kozy_kekyo, @kekyo2)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace IL2C.ArtifactCollector
{
internal static class Collectors
{
public static Task RecreateDirectoryAsync(string path)
{
return Task.Run(() =>
{
if (Directory.Exists(path))
{
Directory.Move(path, path + ".old");
Directory.Delete(path + ".old", true);
}
Directory.CreateDirectory(path);
});
}
private static Task CopyFileAsync(string from, string to)
{
return Task.Run(() =>
{
var toDirectory = Path.GetDirectoryName(to);
var count = 10;
while (!Directory.Exists(toDirectory))
{
try
{
Directory.CreateDirectory(toDirectory);
}
catch (IOException)
{
count--;
if (count <= 0)
{
throw;
}
}
}
File.Copy(from, to, true);
});
}
private static async Task CopyArtifactsAsync(string artifactsDir, string targetDirectoryPath)
{
var version = typeof(Collectors).Assembly.GetName().Version;
var versionString = $"{version.Major}.{version.Minor}.{version.Build}";
var nupkgPaths = Directory.GetFiles(targetDirectoryPath, "*.nupkg", SearchOption.AllDirectories).
Where(p => p.Contains(versionString)).
GroupBy(p => p).
Select(g => Tuple.Create(g.Key, g.OrderByDescending(p => p.Length).ToArray())).
ToDictionary(entry => entry.Item1, entry => entry.Item2);
foreach (var nupkgPath in nupkgPaths.Select(entry => entry.Value.First()))
{
var targetNupkgFileName = Path.GetFileName(nupkgPath);
var targetPath = Path.Combine(artifactsDir, targetNupkgFileName);
if (File.Exists(targetPath))
{
File.Move(targetPath, targetPath + ".orig");
File.Delete(targetPath + ".orig");
}
await CopyFileAsync(nupkgPath, targetPath);
Program.WriteLine(
"AritfactCollector: Package collected: File={0}, Built={1}",
targetNupkgFileName,
Path.GetFileName(nupkgPath));
}
}
public static async Task BuildCsprojAndCollectArtifactsAsync(
string solutionDir, string artifactsDir, string buildIdentifier, IEnumerable<string> csprojPaths)
{
foreach (var path in csprojPaths)
{
var workingPath = Path.GetDirectoryName(path);
var result = await Executor.ExecuteAsync(
workingPath,
new string[0],
"dotnet",
"pack",
"--no-build",
"--configuration", "Release",
"-p:Platform=", // BUG WORKAROUND: dotnet pack is misunderstanding Platform variable sets to "Any CPU", so have to set blank.
$"-p:BuildIdentifier={buildIdentifier}",
$"\"{path}\"");
Program.WriteLine(result.Item2);
if (result.Item1 != 0)
{
throw new Exception($"Csproj failed: Path={path}, ExitCode={result.Item2}");
}
}
await Task.WhenAll(csprojPaths.
Select(path => Path.GetDirectoryName(Path.GetFullPath(path))).
Distinct().
Select(path => CopyArtifactsAsync(artifactsDir, path)));
}
public static async Task BuildNuspecAndCollectArtifactsAsync(
string solutionDir, string artifactsDir, string buildIdentifier, IEnumerable<string> nuspecPaths)
{
var nugetPath = Path.Combine(solutionDir, ".nuget", "nuget.exe");
var version = typeof(Collectors).Assembly.GetName().Version;
var versionString = $"{version.Major}.{version.Minor}.{version.Build}";
foreach (var path in nuspecPaths)
{
var outputDirectory = Path.Combine(Path.GetDirectoryName(path), "bin", "Release");
var workingPath = Path.GetDirectoryName(path);
var result = await Executor.ExecuteAsync(
workingPath,
new string[0],
nugetPath,
"pack",
"-Version", versionString,
"-NoPackageAnalysis",
"-Prop", $"Configuration=Release",
"-Prop", $"BuildIdentifier={buildIdentifier}",
"-OutputDirectory", $"\"{outputDirectory}\"",
$"\"{path}\"");
Program.WriteLine(result.Item2);
if (result.Item1 != 0)
{
throw new Exception($"Nuspec failed: Path={path}, ExitCode={result.Item1}");
}
}
await Task.WhenAll(nuspecPaths.
Select(path => Path.GetDirectoryName(Path.GetFullPath(path))).
Distinct().
Select(path => CopyArtifactsAsync(artifactsDir, path)));
}
public static async Task BuildZipFromCollectArtifactsAsync(
string artifactsDir, IEnumerable<string> zipArtifactsPaths)
{
var version = typeof(Collectors).Assembly.GetName().Version;
var versionString = $"{version.Major}.{version.Minor}.{version.Build}";
await Task.WhenAll(zipArtifactsPaths.Select(zipArtifactsPath => Task.Run(() =>
{
var zipArtifactsName = Path.GetFileNameWithoutExtension(zipArtifactsPath);
var zipArtifactsFullPath = Path.Combine(artifactsDir, $"{zipArtifactsName}.{versionString}.zip");
var zipArtifactsBasePath = Path.GetDirectoryName(Path.GetFullPath(zipArtifactsPath));
var zipArtifactsDocument = XDocument.Load(zipArtifactsPath);
var filePaths =
zipArtifactsDocument.Root.Elements("file").
Where(file => !string.IsNullOrWhiteSpace((string)file.Attribute("src"))).
Select(file => Path.Combine(zipArtifactsBasePath, (string)file.Attribute("src"))).
ToArray();
if (File.Exists(zipArtifactsFullPath))
{
File.Move(zipArtifactsFullPath, zipArtifactsFullPath + ".tmp");
File.Delete(zipArtifactsFullPath);
}
using (var zip = ZipFile.Open(zipArtifactsFullPath, ZipArchiveMode.Create))
{
foreach (var filePath in filePaths.
SelectMany(path => Directory.EnumerateFiles(Path.GetDirectoryName(path), Path.GetFileName(path), SearchOption.AllDirectories)))
{
var entry = zip.CreateEntryFromFile(filePath, filePath.Substring(zipArtifactsBasePath.Length + 1));
}
}
Program.WriteLine(zipArtifactsFullPath);
})));
}
private static async Task CopyResourceWithReplacementsAsync(
string resourceName, string path, IReadOnlyDictionary<string, string> replacements)
{
using (var sr = new StreamReader(
typeof(Collectors).Assembly.GetManifestResourceStream(resourceName),
Encoding.UTF8, true))
{
var arduinoProperties = new StringBuilder(await sr.ReadToEndAsync());
foreach (var entry in replacements)
{
arduinoProperties.Replace(entry.Key, entry.Value);
}
File.WriteAllText(path, arduinoProperties.ToString(), Encoding.UTF8);
}
}
public static async Task CollectArduinoArtifactsAsync(string solutionDir, string artifactsDir)
{
var arduinoBasePath = Path.Combine(artifactsDir, "Arduino");
await RecreateDirectoryAsync(arduinoBasePath);
await CopyResourceWithReplacementsAsync(
"IL2C.ArtifactCollector.Arduino.properties",
Path.Combine(arduinoBasePath, "library.properties"),
new Dictionary<string, string>
{
{ "{version}", typeof(Program).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version },
{ "{semver2}", typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion }
});
var fromIncludeDir = Path.Combine(solutionDir, "IL2C.Runtime", "include");
var toIncludeDir = Path.Combine(arduinoBasePath, "include");
await Task.WhenAll(
Directory.EnumerateFiles(fromIncludeDir, "*.h", SearchOption.AllDirectories).
Select(path => CopyFileAsync(path, Path.Combine(toIncludeDir, path.Substring(fromIncludeDir.Length + 1)))));
var fromSrcDir = Path.Combine(solutionDir, "IL2C.Runtime", "src");
var toSrcDir = Path.Combine(arduinoBasePath, "src");
await Task.WhenAll(
Directory.EnumerateFiles(fromSrcDir, "*.c", SearchOption.AllDirectories).
Concat(Directory.EnumerateFiles(fromSrcDir, "*.h", SearchOption.AllDirectories)).
Select(path => CopyFileAsync(path, Path.Combine(toSrcDir, path.Substring(fromSrcDir.Length + 1)))));
}
}
}