-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildCommand.cs
More file actions
193 lines (152 loc) · 6.46 KB
/
BuildCommand.cs
File metadata and controls
193 lines (152 loc) · 6.46 KB
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
#region copyright
/*
BuildCommand.cs is part of SimpleMSI.
Copyright (C) 2025 Julian Rossbach
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#endregion
using DotMake.CommandLine;
using System.Diagnostics;
namespace SimpleMSI;
[CliCommand(Description = "Build MSI from config file")]
internal class BuildCommand : CommonCommand
{
[CliOption(Description = "Path to configuration file", Required = false,
Name = "config", Alias = "c",
ValidationRules = CliValidationRules.LegalPath | CliValidationRules.ExistingFile)]
public string? ConfigFile { get; set; }
[CliOption(Description = "EXE/DLL File to grab Version from", Required = false,
Name = "grab-version-from-file",
ValidationRules = CliValidationRules.LegalPath | CliValidationRules.ExistingFile)]
public string? VersionFile { get; set; }
[CliOption(Description = "Name of the code signing certificate to use", Required = false)]
public string? CertificateName { get; set; }
[CliOption(Description = "Password of the code signing certificate", Required = false)]
public string? CertificatePassword { get; set; }
public override async Task<int> RunAsync(CliContext cliContext)
{
if (PrintLogo)
{
cliContext.ShowLogo();
await cliContext.Output.WriteLineAsync();
}
var print = cliContext.ToPrintContext(Verbose);
print.VerboseLine("Loading config file...");
var configPath = ConfigFile;
if (string.IsNullOrEmpty(configPath))
{
var files =
Directory.GetFiles(Environment.CurrentDirectory, $"*.v{SimpleMsiCli.AssemblyMajor}.msi.toml");
if (files.Length > 1)
{
print.ErrLine("Multiple config files found, please specify one using --config");
return ExitCodes.InvalidArguments;
}
if (files.Length <= 0)
{
print.VerboseLine("No versioned config file found, trying without...");
files = Directory.GetFiles(Environment.CurrentDirectory, "*.msi.toml");
}
if (files.Length > 1)
{
print.ErrLine(
$"Multiple config files found, please specify one using --config. Also consider using versioned config files (config.v{SimpleMsiCli.AssemblyMajor}.msi.toml).");
return ExitCodes.InvalidArguments;
}
if (files.Length <= 0)
{
print.ErrLine("No config file found, please specify one using --config");
return ExitCodes.FileNotFound;
}
configPath = files[0];
print.OutLine($"No config file specified, using '{configPath}'");
}
string configText;
try
{
configText = await File.ReadAllTextAsync(configPath);
}
catch (Exception ex)
{
print.ErrLine($"Error: Failed to read config file '{configPath}': {ex.Message}");
return ExitCodes.FileNotFound;
}
var config = Config.FromToml(configText);
if (config is null)
{
print.ErrLine("Error in config file. Please check the names and values of all fields.");
return ExitCodes.InvalidConfig;
}
print.VerboseLine("Adjusting config...");
if (VersionFile is not null && Version is not null)
{
print.ErrLine("Error: You may not specify both a version and version file");
return ExitCodes.InvalidArguments;
}
if (Version is not null)
{
config.General.Version = Version.ToString();
}
if (VersionFile is not null)
{
var attributes = FileVersionInfo.GetVersionInfo(VersionFile);
config.General.Version = attributes.FileVersion ?? throw new InvalidDataException("File does not contain valid version information");
}
if (Platform is not null)
{
config.General.Platform = Platform;
}
if (SourceDirectories.Count > 0)
{
config.Installation ??= new();
config.Installation.Dirs.AddRange(SourceDirectories);
}
if (SourceFiles.Count > 0)
{
config.Installation ??= new();
config.Installation.Files.AddRange(SourceDirectories);
}
if (OutputFile is { } file &&
string.IsNullOrWhiteSpace(Path.GetFileName(file)))
{
print.ErrLine($"Error: Output path '{file}' must contain a file name");
return ExitCodes.InvalidArguments;
}
config.General.OutFileName = OutputFile ?? config.General.OutFileName;
if (CertificateName is not null)
{
config.Installation ??= new();
config.Installation.Signing ??= new();
config.Installation.Signing.CertificateName = CertificateName;
}
if (CertificatePassword is not null)
{
config.Installation ??= new();
config.Installation.Signing ??= new();
config.Installation.Signing.Password = CertificatePassword;
}
print.VerboseLine("Configuring WiX...");
MsiEngine.ApplyGlobalConfiguration(Verbose);
print.OutLine("Configuring MSI package...");
MsiEngine engine = new(print);
try
{
engine.ConfigureMsi(config);
}
catch (ArgumentException e)
{
print.ErrLine($"Config error while configuring MSI: {e.Message}");
return ExitCodes.InvalidConfig;
}
catch (FileNotFoundException e)
{
print.ErrLine($"Error: Could not find file '{e.FileName}': {e.Message}");
return ExitCodes.FileNotFound;
}
print.OutLine("Building MSI package (this may take a while)...");
engine.BuildMsi();
return ExitCodes.Success;
}
}