-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
213 lines (181 loc) · 7.99 KB
/
Program.cs
File metadata and controls
213 lines (181 loc) · 7.99 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using CommandLine;
using System.Diagnostics;
using System.Drawing;
using System.IO.Compression;
using VSCode2Msi;
using WixSharp;
using WixSharp.CommonTasks;
using File = System.IO.File;
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))
{
var filename = Constants.ArchiveDownloadPath;
options.IfVerbose(() => Console.WriteLine($"Downloading archive from \"{options.ArchivePath}\" to \"{filename}\"... "));
using HttpClient client = new();
await using var file = File.OpenWrite(filename);
using var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
if (response.Content.Headers.ContentLength is {} contentLength)
{
await using var stream = await response.Content.ReadAsStreamAsync();
var buffer = new byte[1024 * 8];
long totalRead = 0;
int read;
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await file.WriteAsync(buffer, 0, read);
totalRead += read;
var progress = (int)((totalRead * 100) / contentLength);
Console.Write($"\rDownloading... {progress}% ");
options.IfVerbose(() => Console.Write($"({totalRead}/{contentLength}) "));
}
}
else
{
await response.Content.CopyToAsync(file);
}
Console.WriteLine("Done downloading.");
options.ArchivePath = filename;
}
options.IfVerbose(() => Console.WriteLine("Checking if archive exists..."));
if (!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..."));
File.Delete(options.ArchivePath);
}
// locating vscode executable
var codeExe = Path.Combine(Constants.ArchiveExtractPath, "Code.exe");
options.IfVerbose(() => Console.WriteLine("Checking for vscode executable..."));
if (!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;
var license = msi.FindFile(f => f.Name.EndsWith("LICENSE.rtf"))[0].Name;
if (!string.IsNullOrWhiteSpace(license) && File.Exists(license))
{
options.IfVerbose(() => Console.WriteLine($"Found license file at \"{license}\""));
msi.LicenceFile = license;
}
// 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)
{
var icon = Icon.ExtractIcon(path, 0);
if (icon is null)
{
Console.WriteLine("Warning: high quality icon extraction failed, falling pack to low quality");
// use lower quality icon extraction if ExtractIcon fails
icon = Icon.ExtractAssociatedIcon(path);
// if this also fails we just skip the icon extraction
if (icon is null)
return null;
}
using var stream = System.IO.File.OpenWrite(Constants.VsCodeIconPath);
icon.Save(stream);
icon.Dispose();
return Constants.VsCodeIconPath;
}