-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitCommand.cs
More file actions
110 lines (81 loc) · 3.82 KB
/
InitCommand.cs
File metadata and controls
110 lines (81 loc) · 3.82 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
#region copyright
/*
InitCommand.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;
namespace SimpleMSI;
[CliCommand(Description = "Initialize new config file")]
internal class InitCommand : CommonCommand
{
[CliArgument(ValidationPattern = Config.GeneralConfig.NameValidationRegex,
Description = "Application identifying name")]
public string AppName { get; set; } = null!;
[CliOption(Description = "Application identifying GUID", Required = false)]
public Guid? Guid { get; set; }
[CliOption(Description = "Executable name used for default Shortcut", Required = false,
ValidationRules = CliValidationRules.LegalFileName)]
public string? ExecutableName { 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 template config...");
var resource = SimpleMsiCli.Assembly.GetManifestResourceStream(typeof(InitCommand), "Template.msi.toml")
?? throw new FileNotFoundException("Embedded template not found");
using StreamReader reader = new(resource);
var template = await reader.ReadToEndAsync();
var config = Config.FromToml(template)
?? throw new InvalidDataException("Failed to parse embedded template");
print.VerboseLine("Adjusting template...");
config.General.Name = AppName;
config.General.Guid = (Guid ?? System.Guid.NewGuid()).ToString();
if (Version is not null)
{
config.General.Version = Version.ToString();
}
if (Platform is not null)
{
config.General.Platform = Platform;
}
if (ExecutableName is not null)
{
var shortcut = config.Installation?.Shortcuts[0]!;
shortcut.TargetFile = ExecutableName;
}
if (SourceDirectories.Count > 0)
{
config.Installation!.Dirs.Clear();
config.Installation!.Dirs.AddRange(SourceDirectories);
}
if (SourceFiles.Count > 0)
{
config.Installation!.Files.Clear();
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;
}
if (OutputFile?.EndsWith(".msi.toml") == false)
{
print.OutLine($"Warning: Config file should have the extension '.msi.toml' or '.v{SimpleMsiCli.AssemblyMajor}.msi.toml' for SimpleMSI to locate it automatically");
}
var outputPath = OutputFile ??
$"{AppName}.v{SimpleMsiCli.AssemblyMajor}.msi.toml";
print.OutLine($"Writing config to {outputPath}...");
var outText = config.ToToml();
await File.WriteAllTextAsync(outputPath, outText);
return ExitCodes.Success;
}
}