forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildFPMToolPreReqs.cs
352 lines (317 loc) · 15.9 KB
/
BuildFPMToolPreReqs.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Build.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Microsoft.DotNet.Build.Tasks
{
/// <summary>
/// This task prepares the command line parameters for running a RPM build using FPM tool and also updates the copyright and changelog file tokens.
/// If parses various values from the config json by first reading it into a model and then builds the required string for parameters and passes it back.
///
/// </summary>
public class BuildFPMToolPreReqs : BuildTask
{
[Required]
public string InputDir { get; set; }
[Required]
public string OutputDir { get; set; }
[Required]
public string PackageVersion { get; set; }
[Required]
public string ConfigJsonFile { get; set; }
[Output]
public string FPMParameters { get; set; }
public override bool Execute()
{
try
{
if (!File.Exists(ConfigJsonFile))
{
throw new FileNotFoundException($"Expected file {ConfigJsonFile} was not found.");
}
// Open the Config Json and read the values into the model
TextReader projectFileReader = File.OpenText(ConfigJsonFile);
if (projectFileReader != null)
{
string jsonFileText = projectFileReader.ReadToEnd();
ConfigJson configJson = JsonConvert.DeserializeObject<ConfigJson>(jsonFileText);
// Update the Changelog and Copyright files by replacing tokens with values from config json
UpdateChangelog(configJson, PackageVersion);
UpdateCopyRight(configJson);
// Build the full list of parameters
FPMParameters = BuildCmdParameters(configJson, PackageVersion);
Log.LogMessage(MessageImportance.Normal, "Generated RPM paramters: " + FPMParameters);
}
else
{
throw new IOException($"Could not open the file {ConfigJsonFile} for reading.");
}
}
catch (Exception e)
{
Log.LogErrorFromException(e, true);
}
return !Log.HasLoggedErrors;
}
// Update the tokens in the changelog file from the config Json
private void UpdateChangelog(ConfigJson configJson, string package_version)
{
try
{
string changelogFile = Path.Combine(InputDir, "templates", "changelog");
if (!File.Exists(changelogFile))
{
throw new FileNotFoundException($"Expected file {changelogFile} was not found.");
}
string str = File.ReadAllText(changelogFile);
str = str.Replace("{PACKAGE_NAME}", configJson.Package_Name);
str = str.Replace("{PACKAGE_VERSION}", package_version);
str = str.Replace("{PACKAGE_REVISION}", configJson.Release.Package_Revision);
str = str.Replace("{URGENCY}", configJson.Release.Urgency);
str = str.Replace("{CHANGELOG_MESSAGE}", configJson.Release.Changelog_Message);
str = str.Replace("{MAINTAINER_NAME}", configJson.Maintainer_Name);
str = str.Replace("{MAINTAINER_EMAIL}", configJson.Maintainer_Email);
// The date format needs to be like Wed May 17 2017
str = str.Replace("{DATE}", DateTime.UtcNow.ToString("ddd MMM dd yyyy"));
File.WriteAllText(changelogFile, str);
}
catch (Exception e)
{
Log.LogError("Exception while updating the changelog file: " + e.Message);
}
}
private void UpdateCopyRight(ConfigJson configJson)
{
try
{
// Update the tokens in the copyright file from the config Json
string copyrightFile = Path.Combine(InputDir, "templates", "copyright");
if (!File.Exists(copyrightFile))
{
throw new FileNotFoundException($"Expected file {copyrightFile} was not found.");
}
string str = File.ReadAllText(copyrightFile);
str = str.Replace("{COPYRIGHT_TEXT}", configJson.CopyRight);
str = str.Replace("{LICENSE_NAME}", configJson.License.Type);
str = str.Replace("{LICENSE_NAME}", configJson.License.Type);
str = str.Replace("{LICENSE_TEXT}", configJson.License.Full_Text);
File.WriteAllText(copyrightFile, str);
}
catch (Exception e)
{
Log.LogError("Exception while updating the copyright file: " + e.Message);
}
}
private string BuildCmdParameters(ConfigJson configJson, string package_version)
{
// Parameter list that needs to be passed to FPM tool:
// -s : is the input source type(dir) --Static
// -t : is the type of package(rpm) --Static
// -n : is for the name of the package --JSON
// -v : is the version to give to the package --ARG
// -a : architecture --JSON
// -d : is for all dependent packages. This can be used multiple times to specify the dependencies of the package. --JSON
// --rpm-os : the operating system to target this rpm --Static
// --rpm-changelog : the changelog from FILEPATH contents --ARG
// --rpm-summary : it is the RPM summary that shows in the Title --JSON
// --description : it is the description for the package --JSON
// -p : The actual package name (with path) for your package. --ARG+JSON
// --conflicts : Other packages/versions this package conflicts with provided as CSV --JSON
// --directories : Recursively add directories as being owned by the package. --JSON
// --after-install : FILEPATH to the script to be run after install of the package --JSON
// --after-remove : FILEPATH to the script to be run after package removal --JSON
// --license : the licensing name for the package. This will include the license type in the meta-data for the package, but will not include the associated license file within the package itself. --JSON
// --iteration : the iteration to give to the package. This comes from the package_revision --JSON
// --url : url for this package. --JSON
// --verbose : Set verbose output for FPM tool --Static
// <All folder mappings> : Add all the folder mappings for packge_root, docs, man pages --Static
var parameters = new List<string>();
parameters.Add("-s dir");
parameters.Add("-t rpm");
parameters.Add(string.Concat("-n ", configJson.Package_Name));
parameters.Add(string.Concat("-v ", package_version));
parameters.Add(string.Concat("-a ", configJson.Control.Architecture));
// Build the list of dependencies as -d <dep1> -d <dep2>
if (configJson.Rpm_Dependencies != null)
{
IEnumerable<RpmDependency> dependencies;
switch (configJson.Rpm_Dependencies)
{
case JArray dependencyArray:
dependencies = dependencyArray.ToObject<RpmDependency[]>();
break;
case JObject dependencyDictionary:
dependencies = dependencyDictionary
.ToObject<Dictionary<string, string>>()
.Select(pair => new RpmDependency
{
Package_Name = pair.Key,
Package_Version = pair.Value
});
break;
default:
throw new ArgumentException(
"Expected 'rpm_dependencies' to be JArray or JObject, but found " +
configJson.Rpm_Dependencies.Type);
}
foreach (RpmDependency rpmdep in dependencies)
{
string dependency = "";
if (rpmdep.Package_Name != "")
{
// If no version is specified then the dependency is just the package without >= check
if (rpmdep.Package_Version == "")
{
dependency = rpmdep.Package_Name;
}
else
{
dependency = string.Concat(rpmdep.Package_Name, " >= ", rpmdep.Package_Version);
}
}
if (dependency != "")
{
parameters.Add(string.Concat("-d ", EscapeArg(dependency)));
}
}
}
// Build the list of owned directories
if (configJson.Directories != null)
{
foreach (string dir in configJson.Directories)
{
if (dir != "")
{
parameters.Add(string.Concat("--directories ", EscapeArg(dir)));
}
}
}
parameters.Add("--rpm-os linux");
parameters.Add(string.Concat("--rpm-changelog ", EscapeArg(Path.Combine(InputDir, "templates", "changelog")))); // Changelog File
parameters.Add(string.Concat("--rpm-summary ", EscapeArg(configJson.Short_Description)));
parameters.Add(string.Concat("--description ", EscapeArg(configJson.Long_Description)));
parameters.Add(string.Concat("--maintainer ", EscapeArg(configJson.Maintainer_Name + " <" + configJson.Maintainer_Email + ">")));
parameters.Add(string.Concat("--vendor ", EscapeArg(configJson.Vendor)));
parameters.Add(string.Concat("-p ", Path.Combine(OutputDir, configJson.Package_Name + ".rpm")));
if (configJson.Package_Conflicts != null) parameters.Add(string.Concat("--conflicts ", EscapeArg(string.Join(",", configJson.Package_Conflicts))));
if (configJson.After_Install_Source != null) parameters.Add(string.Concat("--after-install ", Path.Combine(InputDir, EscapeArg(configJson.After_Install_Source))));
if (configJson.After_Remove_Source != null) parameters.Add(string.Concat("--after-remove ", Path.Combine(InputDir, EscapeArg(configJson.After_Remove_Source))));
parameters.Add(string.Concat("--license ", EscapeArg(configJson.License.Type)));
parameters.Add(string.Concat("--iteration ", configJson.Release.Package_Revision));
parameters.Add(string.Concat("--url ", "\"", EscapeArg(configJson.Homepage), "\""));
parameters.Add("--verbose");
// Map all the payload directories as they need to install on the system
if (configJson.Install_Root != null) parameters.Add(string.Concat(Path.Combine(InputDir, "package_root/="), configJson.Install_Root)); // Package Files
if (configJson.Install_Man != null) parameters.Add(string.Concat(Path.Combine(InputDir, "docs", "host/="), configJson.Install_Man)); // Man Pages
if (configJson.Install_Doc != null) parameters.Add(string.Concat(Path.Combine(InputDir, "templates", "copyright="), configJson.Install_Doc)); // CopyRight File
return string.Join(" ", parameters);
}
private string EscapeArg(string arg)
{
var sb = new StringBuilder();
bool quoted = ShouldSurroundWithQuotes(arg);
if (quoted) sb.Append("\"");
for (int i = 0; i < arg.Length; ++i)
{
var backslashCount = 0;
// Consume All Backslashes
while (i < arg.Length && arg[i] == '\\')
{
backslashCount++;
i++;
}
// Escape any backslashes at the end of the arg
// This ensures the outside quote is interpreted as
// an argument delimiter
if (i == arg.Length)
{
sb.Append('\\', 2 * backslashCount);
}
// Escape any preceding backslashes and the quote
else if (arg[i] == '"')
{
sb.Append('\\', (2 * backslashCount) + 1);
sb.Append('"');
}
// Output any consumed backslashes and the character
else
{
sb.Append('\\', backslashCount);
sb.Append(arg[i]);
}
}
if (quoted) sb.Append("\"");
return sb.ToString();
}
private bool ShouldSurroundWithQuotes(string argument)
{
// Don't quote already quoted strings
if (argument.StartsWith("\"", StringComparison.Ordinal) &&
argument.EndsWith("\"", StringComparison.Ordinal))
{
return false;
}
// Only quote if whitespace exists in the string
if (argument.Contains(" ") || argument.Contains("\t") || argument.Contains("\n"))
{
return true;
}
return false;
}
/// <summary>
/// Model classes for reading and storing the JSON.
/// </summary>
private class ConfigJson
{
public string Maintainer_Name { get; set; }
public string Maintainer_Email { get; set; }
public string Vendor { get; set; }
public string Package_Name { get; set; }
public string Install_Root { get; set; }
public string Install_Doc { get; set; }
public string Install_Man { get; set; }
public string Short_Description { get; set; }
public string Long_Description { get; set; }
public string Homepage { get; set; }
public string CopyRight { get; set; }
public Release Release { get; set; }
public Control Control { get; set; }
public License License { get; set; }
public JContainer Rpm_Dependencies { get; set; }
public List<string> Package_Conflicts { get; set; }
public List<string> Directories { get; set; }
public string After_Install_Source { get; set; }
public string After_Remove_Source { get; set; }
}
private class Release
{
public string Package_Version { get; set; }
public string Package_Revision { get; set; }
public string Urgency { get; set; }
public string Changelog_Message { get; set; }
}
private class Control
{
public string Priority { get; set; }
public string Section { get; set; }
public string Architecture { get; set; }
}
private class License
{
public string Type { get; set; }
public string Full_Text { get; set; }
}
private class RpmDependency
{
public string Package_Name { get; set; }
public string Package_Version { get; set; }
}
}
}