-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathProgram.cs
329 lines (281 loc) · 14.1 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
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
// Copyright © 2017 - 2023 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Win32;
using chocolatey.infrastructure.information;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.app.builders;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.runners;
using chocolatey.infrastructure.commandline;
using chocolatey.infrastructure.extractors;
using chocolatey.infrastructure.licensing;
using chocolatey.infrastructure.logging;
using chocolatey.infrastructure.platforms;
using chocolatey.infrastructure.registration;
using chocolatey.infrastructure.tolerance;
using SimpleInjector;
#if !NoResources
using chocolatey.resources;
#endif
using Assembly = chocolatey.infrastructure.adapters.Assembly;
using Console = System.Console;
using Environment = System.Environment;
using IFileSystem = chocolatey.infrastructure.filesystem.IFileSystem;
namespace chocolatey.console
{
public sealed class Program
{
private static void Main(string[] args)
{
ChocolateyConfiguration config = null;
ChocolateyLicense license = null;
try
{
AddAssemblyResolver();
var loggingLocation = ApplicationParameters.LoggingLocation;
//no file system at this point
if (!Directory.Exists(loggingLocation))
{
Directory.CreateDirectory(loggingLocation);
}
Log4NetAppenderConfiguration.Configure(loggingLocation, excludeLoggerNames: ChocolateyLoggers.Trace.ToStringSafe());
Bootstrap.Initialize();
Bootstrap.Startup();
license = License.ValidateLicense();
var container = SimpleInjectorContainer.Container;
"LogFileOnly".Log().Info(() => "".PadRight(60, '='));
config = container.GetInstance<ChocolateyConfiguration>();
var fileSystem = container.GetInstance<IFileSystem>();
var warnings = new List<string>();
if (license.AssemblyLoaded && !IsLicensedAssemblyLoaded(container))
{
license.AssemblyLoaded = false;
license.IsCompatible = false;
}
ConfigurationBuilder.SetupConfiguration(
args,
config,
container,
license,
warning => { warnings.Add(warning); }
);
if (license.AssemblyLoaded && license.IsLicensedVersion() && !license.IsCompatible && !config.DisableCompatibilityChecks)
{
WriteWarningForIncompatibleExtensionVersion();
}
if (config.Features.LogWithoutColor)
{
ApplicationParameters.Log4NetConfigurationResource = @"chocolatey.infrastructure.logging.log4net.nocolor.config.xml";
Log4NetAppenderConfiguration.Configure(loggingLocation, excludeLoggerNames: ChocolateyLoggers.Trace.ToStringSafe());
}
if (!string.IsNullOrWhiteSpace(config.AdditionalLogFileLocation))
{
Log4NetAppenderConfiguration.SetupAdditionalLogFile(fileSystem.GetFullPath(config.AdditionalLogFileLocation));
}
ReportVersionAndExitIfRequested(args, config);
TrapExitScenarios();
if (config.RegularOutput)
{
#if DEBUG
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".FormatWith(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.IsLicensedVersion() ? " {0}".FormatWith(license.LicenseType) : string.Empty));
#else
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2}".FormatWith(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.IsLicensedVersion() ? " {0}".FormatWith(license.LicenseType) : string.Empty));
#endif
if (args.Length == 0)
{
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "Please run 'choco -?' or 'choco <command> -?' for help menu.");
}
}
ThrowIfNotDotNet48();
if (warnings.Count != 0 && config.RegularOutput)
{
foreach (var warning in warnings.OrEmpty())
{
"chocolatey".Log().Warn(ChocolateyLoggers.Important, warning);
}
}
if (config.HelpRequested || config.UnsuccessfulParsing)
{
PauseIfDebug();
Environment.Exit(config.UnsuccessfulParsing ? 1 : 0);
}
var verboseAppenderName = "{0}LoggingColoredConsoleAppender".FormatWith(ChocolateyLoggers.Verbose.ToStringSafe());
var traceAppenderName = "{0}LoggingColoredConsoleAppender".FormatWith(ChocolateyLoggers.Trace.ToStringSafe());
Log4NetAppenderConfiguration.EnableDebugLoggingIf(config.Debug, verboseAppenderName, traceAppenderName);
Log4NetAppenderConfiguration.EnableVerboseLoggingIf(config.Verbose, config.Debug, verboseAppenderName);
Log4NetAppenderConfiguration.EnableTraceLoggingIf(config.Trace, traceAppenderName);
"chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".FormatWith(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.ToStringSafe()));
//"chocolatey".Log().Debug(() => "Command Line: {0}".FormatWith(Environment.CommandLine));
RemoveOldChocoExe(fileSystem);
AssemblyFileExtractor.ExtractAssemblyResourcesToRelativeDirectory(fileSystem, Assembly.GetAssembly(typeof(Program)), ApplicationParameters.InstallLocation, new List<string>(), "chocolatey.console", throwError: false);
//refactor - thank goodness this is temporary, cuz manifest resource streams are dumb
IList<string> folders = new List<string>
{
"helpers",
"functions",
"redirects",
"tools"
};
#if !NoResources
AssemblyFileExtractor.ExtractAssemblyResourcesToRelativeDirectory(fileSystem, Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources, throwError: false);
#endif
var application = new ConsoleApplication();
application.Run(args, config, container);
}
catch (Exception ex)
{
if (ApplicationParameters.IsDebugModeCliPrimitive())
{
"chocolatey".Log().Error(() => "{0} had an error occur:{1}{2}".FormatWith(
ApplicationParameters.Name,
Environment.NewLine,
ex.ToString()));
}
else
{
"chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".FormatWith(ex.Message));
"chocolatey".Log().Error(ChocolateyLoggers.LogFileOnly, () => "More Details: {0}".FormatWith(ex.ToString()));
}
if (Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
}
finally
{
if (license != null && license.AssemblyLoaded && license.IsLicensedVersion() && !license.IsCompatible && config != null && !config.DisableCompatibilityChecks)
{
WriteWarningForIncompatibleExtensionVersion();
}
"chocolatey".Log().Debug(() => "Exiting with {0}".FormatWith(Environment.ExitCode));
#if DEBUG
"chocolatey".Log().Info(() => "Exiting with {0}".FormatWith(Environment.ExitCode));
#endif
// Chocolatey Agent runs Chocolatey CLI with --run-actual. If that's the case we don't want to pause on debug.
if (!args.Any(a => a.IsEqualTo("--run-actual")))
{
PauseIfDebug();
}
Bootstrap.Shutdown();
Environment.Exit(Environment.ExitCode);
}
}
private static bool IsLicensedAssemblyLoaded(Container container)
{
var allExtensions = container.GetAllInstances<ExtensionInformation>();
foreach (var extension in allExtensions)
{
if (extension.Name.IsEqualTo("chocolatey.licensed"))
{
return extension.Status == ExtensionStatus.Enabled || extension.Status == ExtensionStatus.Loaded;
}
}
// We will be going by an assumption that it has been loaded in this case.
// This is mostly to prevent that the licensed extension won't be disabled
// if it has been loaded using old method.
return true;
}
private static void AddAssemblyResolver()
{
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolution.ResolveExtensionOrMergedAssembly;
}
private static void ReportVersionAndExitIfRequested(string[] args, ChocolateyConfiguration config)
{
if (args == null || args.Length == 0)
{
return;
}
var firstArg = args.FirstOrDefault();
if (firstArg.IsEqualTo("-v") || firstArg.IsEqualTo("--version"))
{
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0}".FormatWith(config.Information.ChocolateyProductVersion));
PauseIfDebug();
"chocolatey".Log().Debug(() => "Exiting with 0");
Environment.Exit(0);
}
}
private static void TrapExitScenarios()
{
ExitScenarioHandler.SetHandler();
}
private static void RemoveOldChocoExe(IFileSystem fileSystem)
{
FaultTolerance.TryCatchWithLoggingException(
() =>
{
fileSystem.DeleteFile(fileSystem.GetCurrentAssemblyPath() + ".old");
fileSystem.DeleteFile(fileSystem.CombinePaths(AppDomain.CurrentDomain.BaseDirectory, "choco.exe.old"));
},
errorMessage: "Attempting to delete choco.exe.old ran into an issue",
throwError: false,
logWarningInsteadOfError: true,
logDebugInsteadOfError: false,
isSilent: true
);
}
[Conditional("DEBUG")]
private static void PauseIfDebug()
{
#if DEBUG
Console.WriteLine("Press enter to continue...");
Console.ReadKey();
#endif
}
private static void WriteWarningForIncompatibleExtensionVersion()
{
"chocolatey".Log().Warn(
ChocolateyLoggers.Important,
@"WARNING!
You are running a version of Chocolatey that may not be compatible with
the currently installed version of the chocolatey.extension package.
Running Chocolatey with the current version of the chocolatey.extension
package is an unsupported configuration.
See https://ch0.co/compatibility for more information.
If you are in the process of modifying the chocolatey.extension package,
you can ignore this warning.
Additionally, you can ignore these warnings by either setting the
DisableCompatibilityChecks feature:
choco feature enable --name=""'disableCompatibilityChecks'""
Or by passing the --skip-compatibility-checks option when executing a
command.");
}
private static void ThrowIfNotDotNet48()
{
if (Platform.GetPlatform() == PlatformType.Windows)
{
// https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#minimum-version
const int net48ReleaseBuild = 528040;
const string regKey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(regKey))
{
if (ndpKey == null || ndpKey.GetValue("Release") == null || (int)ndpKey.GetValue("Release") < net48ReleaseBuild)
{
throw new ApplicationException(
@".NET 4.8 is not installed or may need a reboot to complete installation.
Please install .NET Framework 4.8 manually and reboot the system.
Download at 'https://download.visualstudio.microsoft.com/download/pr/2d6bb6b2-226a-4baa-bdec-798822606ff1/8494001c276a4b96804cde7829c04d7f/ndp48-x86-x64-allos-enu.exe'"
.FormatWith(Environment.NewLine));
}
}
}
}
}
}