-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
200 lines (175 loc) · 7.64 KB
/
Program.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
using CommandLine;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.IconLib;
using System.IO.Compression;
using System.Net;
using VSCode2Msi;
using WixSharp;
using WixSharp.CommonTasks;
await Parser.Default.ParseArguments<Options>(args)
.WithNotParsed(errors =>
{
if (errors.Any())
{
Console.WriteLine("Error: invalid arguments");
Environment.Exit(-1);
}
})
.WithParsedAsync(Run);
static async Task Run(Options options)
{
if (!options.NoLogo)
{
Console.WriteLine($"VSCode2Msi v{typeof(Program).Assembly.GetName().Version.ToNoRevisionString()}");
Console.WriteLine($"Copyright (C) {DateTime.Now.Year} VSCode2Msi, licensed under LGPL 2.1");
Console.WriteLine();
}
// check if archive is specified
if (string.IsNullOrWhiteSpace(options.ArchivePath))
{
options.IfVerbose(() => Console.WriteLine($"No archive specified, using \"{Constants.DefaultVSCodeArchiveUrl}\""));
options.ArchivePath = Constants.DefaultVSCodeArchiveUrl;
}
// check if archive is a URL, and if so, download it
options.IfVerbose(() => Console.WriteLine("Checking if archive should be downloaded..."));
if (Uri.TryCreate(options.ArchivePath, UriKind.Absolute, out var uri))
{
options.IfVerbose(() => Console.Write($"Downloading archive from \"{options.ArchivePath}\" "));
var filename = Constants.ArchiveDownloadPath;
options.IfVerbose(() => Console.WriteLine($"to \"{filename}\""));
using WebClient webClient = new();
// this is stupid but it works
object locker = new();
webClient.DownloadProgressChanged += (_, e) =>
{
lock (locker)
{
Console.Write($"\rDownloading... {e.ProgressPercentage}%");
options.IfVerbose(() => Console.Write($" ({e.BytesReceived}/{e.TotalBytesToReceive})"));
}
};
webClient.DownloadFileCompleted += (_, e) => { lock (locker) { Console.WriteLine(" Done."); } };
// download file
await webClient.DownloadFileTaskAsync(uri, filename);
options.ArchivePath = filename;
}
options.IfVerbose(() => Console.WriteLine("Checking if archive exists..."));
if (!System.IO.File.Exists(options.ArchivePath))
{
Console.WriteLine($"Error: archive not found at \"{options.ArchivePath}\"");
Environment.Exit(-1);
}
options.IfVerbose(() => Console.WriteLine("Checking for existing extracted archive..."));
// if archive was extracted before, remove old extracted archive
if (Directory.Exists(Constants.ArchiveExtractPath))
{
Console.WriteLine("Removing old extracted archive...");
Directory.Delete(Constants.ArchiveExtractPath, true);
}
Console.Write("Extracting archive...");
ZipFile.ExtractToDirectory(options.ArchivePath, Constants.ArchiveExtractPath);
Console.WriteLine(" Done.");
options.IfVerbose(() => Console.WriteLine($"Archive extracted to \"{Constants.ArchiveExtractPath}\""));
// remove unneeded archive if it was not specified
if (options.ArchivePath == Constants.ArchiveDownloadPath)
{
options.IfVerbose(() => Console.WriteLine("Removing unneeded archive..."));
System.IO.File.Delete(options.ArchivePath);
}
// locating vscode executable
var codeExe = Path.Combine(Constants.ArchiveExtractPath, "Code.exe");
options.IfVerbose(() => Console.WriteLine("Checking for vscode executable..."));
if (!System.IO.File.Exists(codeExe))
{
Console.WriteLine("Error: vscode executable not found");
Environment.Exit(-1);
}
options.IfVerbose(() => Console.WriteLine($"VSCode executable is at \"{codeExe}\""));
// extract icon of vscode executable
options.IfVerbose(() => Console.WriteLine("Extracting icon from vscode executable..."));
var iconFile = ExtractIconFile(codeExe);
if (iconFile is null)
{
Console.WriteLine("Warning: failed to extract icon from vs code executable");
}
options.IfVerbose(() => Console.WriteLine($"Successfully extracted icon to \"{iconFile}\""));
// get vscode attributes (version, name, etc.)
options.IfVerbose(() => Console.WriteLine("Extracting attributes from vscode executable..."));
var attributes = FileVersionInfo.GetVersionInfo(codeExe);
// computing correct output path as expected by WiX
if (string.IsNullOrWhiteSpace(options.OutputPath))
{
options.OutputPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + $"VSCode-{attributes.ProductVersion}-x64";
options.IfVerbose(() => Console.WriteLine($"No output path specified, using \"{options.OutputPath}\""));
}
if (!Directory.Exists(Path.GetDirectoryName(options.OutputPath)))
{
Console.WriteLine($"Error: directory \"{Path.GetDirectoryName(options.OutputPath)}\" does not exist");
Environment.Exit(-1);
}
if (options.OutputPath!.EndsWith(".msi"))
{
options.OutputPath = options.OutputPath[..^4];
}
options.IfVerbose(() => Console.WriteLine("Configuring msi..."));
// base structure
Project msi = new(attributes.ProductName,
new Dir(@"%ProgramFiles%\Microsoft VS Code",
new Files(Constants.ArchiveExtractPath + Path.DirectorySeparatorChar + "*.*")));
msi.ResolveWildCards();
// add desktop and app menu shortcuts
msi.FindFile(f => f.Name.EndsWith("Code.exe"))[0]
.AddShortcuts(
new FileShortcut(attributes.ProductName, "ProgramMenuFolder"),
new FileShortcut(attributes.ProductName, "%Desktop%"));
msi.MajorUpgradeStrategy = MajorUpgradeStrategy.Default;
// add vscode binaries to PATH
msi.Add(new EnvironmentVariable("PATH", @"[INSTALLDIR]\bin") { Part = EnvVarPart.last });
// basic attributes
msi.Version = new(attributes.ProductMajorPart, attributes.ProductMinorPart, attributes.ProductBuildPart);
msi.Platform = Platform.x64;
msi.OutFileName = options.OutputPath;
msi.LicenceFile = Constants.ArchiveExtractPath + Path.DirectorySeparatorChar + @"resources\app\LICENSE.rtf";
// set GUID of installer
msi.GUID = new Guid("fcd5a47f-9d70-4c32-8c3a-ae65c9b17a64");
// advanced attributes
msi.ControlPanelInfo.NoModify = true;
msi.ControlPanelInfo.Comments = attributes.FileDescription;
msi.ControlPanelInfo.Readme = "https://code.visualstudio.com/learn";
msi.ControlPanelInfo.HelpLink = "https://code.visualstudio.com/docs";
msi.ControlPanelInfo.UrlInfoAbout = "https://code.visualstudio.com";
msi.ControlPanelInfo.Manufacturer = attributes.CompanyName;
msi.ControlPanelInfo.InstallLocation = "[INSTALLDIR]";
// if icon file was extracted use it as product icon
if (iconFile is not null)
{
msi.ControlPanelInfo.ProductIcon = iconFile;
}
Console.Write("Building msi...");
Compiler.BuildMsi(msi);
Console.WriteLine("Successfully built MSI installer.");
}
static string? ExtractIconFile(string path)
{
try
{
// we try to use IconLib first
MultiIcon multiIcon = [];
multiIcon.Load(path);
multiIcon.Save(Constants.VSCodeIconPath, MultiIconFormat.ICO);
}
catch
{
Console.WriteLine("Warning: failed to extract icon via IconLib, falling back to System.Drawing");
// if this fails we try to use System.Drawing, this icon won't be as good but it's better than nothing
using var icon = Icon.ExtractAssociatedIcon(path);
if (icon is null)
{
return null;
}
using var stream = System.IO.File.OpenWrite(Constants.VSCodeIconPath);
icon.Save(stream);
}
return Constants.VSCodeIconPath;
}