-
Notifications
You must be signed in to change notification settings - Fork 936
Expand file tree
/
Copy pathProgram.cs
More file actions
317 lines (267 loc) · 13.8 KB
/
Program.cs
File metadata and controls
317 lines (267 loc) · 13.8 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
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
// Copyright © 2017 - 2022 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.
namespace chocolatey.console
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using chocolatey.infrastructure.information;
using infrastructure.app;
using infrastructure.app.builders;
using infrastructure.app.configuration;
using infrastructure.app.runners;
using infrastructure.commandline;
using infrastructure.extractors;
using infrastructure.licensing;
using infrastructure.logging;
using infrastructure.registration;
using infrastructure.tolerance;
using SimpleInjector;
#if !NoResources
using resources;
#endif
using Assembly = infrastructure.adapters.Assembly;
using Console = System.Console;
using Environment = System.Environment;
using IFileSystem = infrastructure.filesystem.IFileSystem;
public sealed class Program
{
// ReSharper disable InconsistentNaming
private static void Main(string[] args)
// ReSharper restore InconsistentNaming
{
ChocolateyConfiguration config = null;
ChocolateyLicense license = null;
try
{
add_assembly_resolver();
string loggingLocation = ApplicationParameters.LoggingLocation;
//no file system at this point
if (!Directory.Exists(loggingLocation)) Directory.CreateDirectory(loggingLocation);
Log4NetAppenderConfiguration.configure(loggingLocation, excludeLoggerNames: ChocolateyLoggers.Trace.to_string());
Bootstrap.initialize();
Bootstrap.startup();
license = License.validate_license();
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 && !is_licensed_assembly_loaded(container))
{
license.AssemblyLoaded = false;
license.IsCompatible = false;
}
ConfigurationBuilder.set_up_configuration(
args,
config,
container,
license,
warning => { warnings.Add(warning); }
);
if (license.AssemblyLoaded && license.is_licensed_version() && !license.IsCompatible && !config.DisableCompatibilityChecks)
{
write_warning_for_incompatible_versions();
}
if (config.Features.LogWithoutColor)
{
ApplicationParameters.Log4NetConfigurationResource = @"chocolatey.infrastructure.logging.log4net.nocolor.config.xml";
Log4NetAppenderConfiguration.configure(loggingLocation, excludeLoggerNames: ChocolateyLoggers.Trace.to_string());
}
if (!string.IsNullOrWhiteSpace(config.AdditionalLogFileLocation))
{
Log4NetAppenderConfiguration.configure_additional_log_file(fileSystem.get_full_path(config.AdditionalLogFileLocation));
}
report_version_and_exit_if_requested(args, config);
trap_exit_scenarios(config);
warn_on_nuspec_or_nupkg_usage(args, config);
if (config.RegularOutput)
{
#if DEBUG
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
#else
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
#endif
if (args.Length == 0)
{
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "Please run 'choco -?' or 'choco <command> -?' for help menu.");
}
}
if (warnings.Count != 0 && config.RegularOutput)
{
foreach (var warning in warnings.or_empty_list_if_null())
{
"chocolatey".Log().Warn(ChocolateyLoggers.Important, warning);
}
}
if (config.HelpRequested || config.UnsuccessfulParsing)
{
pause_execution_if_debug();
Environment.Exit(config.UnsuccessfulParsing ? 1 : 0);
}
var verboseAppenderName = "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string());
var traceAppenderName = "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Trace.to_string());
Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, verboseAppenderName, traceAppenderName);
Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(config.Verbose, config.Debug, verboseAppenderName);
if (config.Information.IsProcessElevated)
{
Log4NetAppenderConfiguration.set_trace_logger_when_trace(config.Trace, traceAppenderName);
}
else
{
var logger = ChocolateyLoggers.Normal;
if (!config.RegularOutput)
{
logger = ChocolateyLoggers.LogFileOnly;
}
"chocolatey".Log().Warn(logger, "Usage of the --trace option is only allowed when running from an elevated session.");
}
"chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".format_with(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.to_string()));
//"chocolatey".Log().Debug(() => "Command Line: {0}".format_with(Environment.CommandLine));
remove_old_chocolatey_exe(fileSystem);
AssemblyFileExtractor.extract_all_resources_to_relative_directory(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.extract_all_resources_to_relative_directory(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.is_debug_mode_cli_primitive())
{
"chocolatey".Log().Error(() => "{0} had an error occur:{1}{2}".format_with(
ApplicationParameters.Name,
Environment.NewLine,
ex.ToString()));
}
else
{
"chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".format_with(ex.Message));
"chocolatey".Log().Error(ChocolateyLoggers.LogFileOnly, () => "More Details: {0}".format_with(ex.ToString()));
}
if (Environment.ExitCode == 0) Environment.ExitCode = 1;
}
finally
{
if (license != null && license.AssemblyLoaded && license.is_licensed_version() && !license.IsCompatible && config != null && !config.DisableCompatibilityChecks)
{
write_warning_for_incompatible_versions();
}
"chocolatey".Log().Debug(() => "Exiting with {0}".format_with(Environment.ExitCode));
#if DEBUG
"chocolatey".Log().Info(() => "Exiting with {0}".format_with(Environment.ExitCode));
#endif
pause_execution_if_debug();
Bootstrap.shutdown();
Environment.Exit(Environment.ExitCode);
}
}
private static bool is_licensed_assembly_loaded(Container container)
{
var allExtensions = container.GetAllInstances<ExtensionInformation>();
foreach (var extension in allExtensions)
{
if (extension.Name.is_equal_to("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 warn_on_nuspec_or_nupkg_usage(string[] args, ChocolateyConfiguration config)
{
var commandLine = Environment.CommandLine;
if (!(commandLine.contains(" pack ") || commandLine.contains(" push ") || commandLine.contains("convert")) && (commandLine.contains(".nupkg") || commandLine.contains(".nuspec")))
{
if (config.RegularOutput) "chocolatey".Log().Warn("The use of .nupkg or .nuspec in for package name or source is known to cause issues. Please use the package id from the nuspec `<id />` with `-s .` (for local folder where nupkg is found).");
}
}
private static void add_assembly_resolver()
{
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolution.resolve_extension_or_merged_assembly;
}
private static void report_version_and_exit_if_requested(string[] args, ChocolateyConfiguration config)
{
if (args == null || args.Length == 0) return;
var firstArg = args.FirstOrDefault();
if (firstArg.is_equal_to("-v") || firstArg.is_equal_to("--version"))
{
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0}".format_with(config.Information.ChocolateyProductVersion));
pause_execution_if_debug();
"chocolatey".Log().Debug(() => "Exiting with 0");
Environment.Exit(0);
}
}
private static void trap_exit_scenarios(ChocolateyConfiguration config)
{
ExitScenarioHandler.SetHandler();
}
private static void remove_old_chocolatey_exe(IFileSystem fileSystem)
{
FaultTolerance.try_catch_with_logging_exception(
() =>
{
fileSystem.delete_file(fileSystem.get_current_assembly_path() + ".old");
fileSystem.delete_file(fileSystem.combine_paths(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
);
}
private static void pause_execution_if_debug()
{
#if DEBUG
Console.WriteLine("Press enter to continue...");
Console.ReadKey();
#endif
}
private static void write_warning_for_incompatible_versions()
{
"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.");
}
}
}