/
ChocolateyPackageService.cs
729 lines (621 loc) · 35.2 KB
/
ChocolateyPackageService.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
// Copyright © 2011 - Present 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.infrastructure.app.services
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using commandline;
using configuration;
using domain;
using infrastructure.commands;
using infrastructure.services;
using logging;
using NuGet;
using platforms;
using results;
using tolerance;
using IFileSystem = filesystem.IFileSystem;
public class ChocolateyPackageService : IChocolateyPackageService
{
private readonly INugetService _nugetService;
private readonly IPowershellService _powershellService;
private readonly IShimGenerationService _shimgenService;
private readonly IFileSystem _fileSystem;
private readonly IRegistryService _registryService;
private readonly IChocolateyPackageInformationService _packageInfoService;
private readonly IFilesService _filesService;
private readonly IAutomaticUninstallerService _autoUninstallerService;
private readonly IXmlService _xmlService;
private readonly IConfigTransformService _configTransformService;
public ChocolateyPackageService(INugetService nugetService, IPowershellService powershellService,
IShimGenerationService shimgenService, IFileSystem fileSystem, IRegistryService registryService,
IChocolateyPackageInformationService packageInfoService, IFilesService filesService,
IAutomaticUninstallerService autoUninstallerService, IXmlService xmlService,
IConfigTransformService configTransformService)
{
_nugetService = nugetService;
_powershellService = powershellService;
_shimgenService = shimgenService;
_fileSystem = fileSystem;
_registryService = registryService;
_packageInfoService = packageInfoService;
_filesService = filesService;
_autoUninstallerService = autoUninstallerService;
_xmlService = xmlService;
_configTransformService = configTransformService;
}
public void list_noop(ChocolateyConfiguration config)
{
if (config.Sources.is_equal_to(SpecialSourceType.webpi.to_string()))
{
//todo: webpi
}
else
{
_nugetService.list_noop(config);
}
}
public IEnumerable<PackageResult> list_run(ChocolateyConfiguration config)
{
this.Log().Debug(() => "Searching for package information");
if (config.Sources.is_equal_to(SpecialSourceType.webpi.to_string()))
{
//todo: webpi
//install webpi if not installed
//run the webpi command
this.Log().Warn("Command not yet functional, stay tuned...");
yield break;
}
else
{
var packages = new List<IPackage>();
if (config.ListCommand.LocalOnly)
{
config.Sources = ApplicationParameters.PackagesLocation;
config.Prerelease = true;
}
foreach (var package in _nugetService.list_run(config))
{
if (!config.ListCommand.LocalOnly || !config.ListCommand.IncludeRegistryPrograms)
{
yield return package;
}
if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms && package.Package != null)
{
packages.Add(package.Package);
}
}
if (config.ListCommand.LocalOnly && config.ListCommand.IncludeRegistryPrograms)
{
foreach (var installed in report_registry_programs(config, packages))
{
yield return installed;
}
}
}
}
private IEnumerable<PackageResult> report_registry_programs(ChocolateyConfiguration config, IEnumerable<IPackage> list)
{
var itemsToRemoveFromMachine = list.Select(package => _packageInfoService.get_package_information(package)).
Where(p => p.RegistrySnapshot != null).
Select(p => p.RegistrySnapshot.RegistryKeys.FirstOrDefault()).
Where(p => p != null).
Select(p => p.DisplayName).ToList();
var count = 0;
var machineInstalled = _registryService.get_installer_keys().RegistryKeys.
Where((p) => p.is_in_programs_and_features() && !itemsToRemoveFromMachine.Contains(p.DisplayName)).
OrderBy((p) => p.DisplayName).Distinct();
this.Log().Info(() => "");
foreach (var key in machineInstalled)
{
if (config.RegularOutput)
{
this.Log().Info("{0}|{1}".format_with(key.DisplayName, key.DisplayVersion));
if (config.Verbose) this.Log().Info(" InstallLocation: {0}{1} Uninstall:{2}".format_with(key.InstallLocation.escape_curly_braces(), Environment.NewLine, key.UninstallString.escape_curly_braces()));
}
count++;
yield return new PackageResult(key.DisplayName, key.DisplayName, key.InstallLocation);
}
if (config.RegularOutput)
{
this.Log().Warn(() => @"{0} applications not managed with Chocolatey.".format_with(count));
}
}
public void pack_noop(ChocolateyConfiguration config)
{
_nugetService.pack_noop(config);
}
public void pack_run(ChocolateyConfiguration config)
{
_nugetService.pack_run(config);
}
public void push_noop(ChocolateyConfiguration config)
{
_nugetService.push_noop(config);
}
public void push_run(ChocolateyConfiguration config)
{
_nugetService.push_run(config);
}
public void install_noop(ChocolateyConfiguration config)
{
// each package can specify its own configuration values
foreach (var packageConfig in set_config_from_package_names_and_packages_config(config, new ConcurrentDictionary<string, PackageResult>()).or_empty_list_if_null())
{
_nugetService.install_noop(packageConfig, (pkg) => _powershellService.install_noop(pkg));
}
}
public void handle_package_result(PackageResult packageResult, ChocolateyConfiguration config, CommandNameType commandName)
{
var pkgInfo = _packageInfoService.get_package_information(packageResult.Package);
if (config.AllowMultipleVersions)
{
pkgInfo.IsSideBySide = true;
}
if (packageResult.Success && config.Information.PlatformType == PlatformType.Windows)
{
if (!config.SkipPackageInstallProvider)
{
var before = _registryService.get_installer_keys();
var powerShellRan = _powershellService.install(config, packageResult);
if (powerShellRan)
{
// we don't care about the exit code
if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false);
}
var difference = _registryService.get_differences(before, _registryService.get_installer_keys());
if (difference.RegistryKeys.Count != 0)
{
//todo v1 - determine the installer type and write it to the snapshot
//todo v1 - note keys passed in
pkgInfo.RegistrySnapshot = difference;
var key = difference.RegistryKeys.FirstOrDefault();
if (key != null && key.HasQuietUninstall)
{
pkgInfo.HasSilentUninstall = true;
}
}
}
_configTransformService.run(packageResult, config);
pkgInfo.FilesSnapshot = _filesService.capture_package_files(packageResult, config);
if (packageResult.Success)
{
_shimgenService.install(config, packageResult);
}
}
else
{
if (config.Information.PlatformType != PlatformType.Windows) this.Log().Info(ChocolateyLoggers.Important, () => " Skipping Powershell and shimgen portions of the install due to non-Windows.");
}
if (packageResult.Success)
{
handle_extension_packages(config, packageResult);
}
_packageInfoService.save_package_information(pkgInfo);
ensure_bad_package_path_is_clean(config, packageResult);
if (!packageResult.Success)
{
this.Log().Error(ChocolateyLoggers.Important, "The {0} of {1} was NOT successful.".format_with(commandName.to_string(), packageResult.Name));
handle_unsuccessful_operation(config, packageResult, movePackageToFailureLocation: true, attemptRollback: true);
return;
}
remove_rollback_if_exists(packageResult);
this.Log().Info(ChocolateyLoggers.Important, " The {0} of {1} was successful.".format_with( commandName.to_string(), packageResult.Name));
}
public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfiguration config)
{
//todo:are we installing from an alternate source? If so run that command instead
this.Log().Info(@"Installing the following packages:");
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));
this.Log().Info(@"By installing you accept licenses for the packages.");
var packageInstalls = new ConcurrentDictionary<string, PackageResult>();
foreach (var packageConfig in set_config_from_package_names_and_packages_config(config, packageInstalls).or_empty_list_if_null())
{
var results = _nugetService.install_run(
packageConfig,
(packageResult) => handle_package_result(packageResult, packageConfig, CommandNameType.install)
);
foreach (var result in results)
{
packageInstalls.GetOrAdd(result.Key, result.Value);
}
}
var installFailures = packageInstalls.Count(p => !p.Value.Success);
var installWarnings = packageInstalls.Count(p => p.Value.Warning);
this.Log().Warn(() => @"{0}{1} installed {2}/{3} package(s). {4} package(s) failed.{5}{0} See the log for details ({6}).".format_with(
Environment.NewLine,
ApplicationParameters.Name,
packageInstalls.Count(p => p.Value.Success && !p.Value.Inconclusive),
packageInstalls.Count,
installFailures,
installWarnings == 0 ? string.Empty : "{0} {1} package(s) had warnings.".format_with(Environment.NewLine, installWarnings),
_fileSystem.combine_paths(ApplicationParameters.LoggingLocation,ApplicationParameters.LoggingFile)
));
if (installWarnings != 0)
{
this.Log().Warn(ChocolateyLoggers.Important, "Warnings:");
foreach (var warning in packageInstalls.Where(p => p.Value.Warning).or_empty_list_if_null())
{
this.Log().Warn(ChocolateyLoggers.Important, " - {0}".format_with(warning.Value.Name));
}
}
if (installFailures != 0)
{
this.Log().Error("Failures:");
foreach (var failure in packageInstalls.Where(p => !p.Value.Success).or_empty_list_if_null())
{
this.Log().Error(" - {0}".format_with(failure.Value.Name));
}
}
if (installFailures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
return packageInstalls;
}
public void outdated_noop(ChocolateyConfiguration config)
{
this.Log().Info(@"
Would have determined packages that are out of date based on what is
installed and what versions are available for upgrade.");
}
public void outdated_run(ChocolateyConfiguration config)
{
this.Log().Info(ChocolateyLoggers.Important, @"Outdated Packages
Output is package name | current version | available version | pinned?
");
config.PackageNames = ApplicationParameters.AllPackages;
config.UpgradeCommand.NotifyOnlyAvailableUpgrades = true;
var output = config.RegularOutput;
config.RegularOutput = false;
var oudatedPackages = _nugetService.upgrade_noop(config, null);
config.RegularOutput = output;
if (config.RegularOutput)
{
var upgradeWarnings = oudatedPackages.Count(p => p.Value.Warning);
this.Log().Warn(() => @"{0}{1} has determined {2} package(s) are outdated. {3}.".format_with(
Environment.NewLine,
ApplicationParameters.Name,
oudatedPackages.Count(p => p.Value.Success && !p.Value.Inconclusive),
upgradeWarnings == 0 ? string.Empty : "{0} {1} package(s) had warnings.".format_with(Environment.NewLine, upgradeWarnings)
));
if (upgradeWarnings != 0)
{
this.Log().Warn(ChocolateyLoggers.Important, "Warnings:");
foreach (var warning in oudatedPackages.Where(p => p.Value.Warning).or_empty_list_if_null())
{
this.Log().Warn(ChocolateyLoggers.Important, " - {0}".format_with(warning.Value.Name));
}
}
}
}
private IEnumerable<ChocolateyConfiguration> set_config_from_package_names_and_packages_config(ChocolateyConfiguration config, ConcurrentDictionary<string, PackageResult> packageInstalls)
{
// if there are any .config files, split those off of the config. Then return the config without those package names.
foreach (var packageConfigFile in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Where(p => p.EndsWith(".config")).ToList())
{
config.PackageNames = config.PackageNames.Replace(packageConfigFile, string.Empty);
foreach (var packageConfig in get_packages_from_config(packageConfigFile, config, packageInstalls).or_empty_list_if_null())
{
yield return packageConfig;
}
}
yield return config;
}
private IEnumerable<ChocolateyConfiguration> get_packages_from_config(string packageConfigFile, ChocolateyConfiguration config, ConcurrentDictionary<string, PackageResult> packageInstalls)
{
IList<ChocolateyConfiguration> packageConfigs = new List<ChocolateyConfiguration>();
if (!_fileSystem.file_exists(_fileSystem.get_full_path(packageConfigFile)))
{
var logMessage = "Could not find '{0}' in the location specified.".format_with(packageConfigFile);
this.Log().Error(ChocolateyLoggers.Important, logMessage);
var results = packageInstalls.GetOrAdd(packageConfigFile, new PackageResult(packageConfigFile, null, null));
results.Messages.Add(new ResultMessage(ResultType.Error, logMessage));
return packageConfigs;
}
var settings = _xmlService.deserialize<PackagesConfigFileSettings>(_fileSystem.get_full_path(packageConfigFile));
foreach (var pkgSettings in settings.Packages.or_empty_list_if_null())
{
if (!pkgSettings.Disabled)
{
var packageConfig = config.deep_copy();
packageConfig.PackageNames = pkgSettings.Id;
packageConfig.Sources = string.IsNullOrWhiteSpace(pkgSettings.Source) ? packageConfig.Sources : pkgSettings.Source;
packageConfig.Version = pkgSettings.Version;
packageConfig.InstallArguments = string.IsNullOrWhiteSpace(pkgSettings.InstallArguments) ? packageConfig.InstallArguments : pkgSettings.InstallArguments;
packageConfig.PackageParameters = string.IsNullOrWhiteSpace(pkgSettings.PackageParameters) ? packageConfig.PackageParameters : pkgSettings.PackageParameters;
if (pkgSettings.ForceX86) packageConfig.ForceX86 = true;
if (pkgSettings.AllowMultipleVersions) packageConfig.AllowMultipleVersions = true;
if (pkgSettings.IgnoreDependencies) packageConfig.IgnoreDependencies = true;
packageConfigs.Add(packageConfig);
}
}
return packageConfigs;
}
public void upgrade_noop(ChocolateyConfiguration config)
{
var noopUpgrades = _nugetService.upgrade_noop(config, (pkg) => _powershellService.install_noop(pkg));
if (config.RegularOutput)
{
var upgradeWarnings = noopUpgrades.Count(p => p.Value.Warning);
this.Log().Warn(() => @"{0}{1} can upgrade {2}/{3} package(s). {4}{0} See the log for details ({5}).".format_with(
Environment.NewLine,
ApplicationParameters.Name,
noopUpgrades.Count(p => p.Value.Success && !p.Value.Inconclusive),
noopUpgrades.Count,
upgradeWarnings == 0 ? string.Empty : "{0} {1} package(s) had warnings.".format_with(Environment.NewLine, upgradeWarnings),
_fileSystem.combine_paths(ApplicationParameters.LoggingLocation, ApplicationParameters.LoggingFile)
));
if (upgradeWarnings != 0)
{
this.Log().Warn(ChocolateyLoggers.Important, "Warnings:");
foreach (var warning in noopUpgrades.Where(p => p.Value.Warning).or_empty_list_if_null())
{
this.Log().Warn(ChocolateyLoggers.Important, " - {0}".format_with(warning.Value.Name));
}
}
}
}
public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config)
{
//todo:are we upgrading an alternate source? If so run that command instead
this.Log().Info(@"Upgrading the following packages:");
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));
this.Log().Info(@"By upgrading you accept licenses for the packages.");
foreach (var packageConfigFile in config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Where(p => p.EndsWith(".config")).ToList())
{
throw new ApplicationException("A packages.config file is only used with installs.");
}
var packageUpgrades = _nugetService.upgrade_run(
config,
(packageResult) => handle_package_result(packageResult, config, CommandNameType.upgrade)
);
var upgradeFailures = packageUpgrades.Count(p => !p.Value.Success);
var upgradeWarnings = packageUpgrades.Count(p => p.Value.Warning);
this.Log().Warn(() => @"{0}{1} upgraded {2}/{3} package(s). {4} package(s) failed.{5}{0} See the log for details ({6}).".format_with(
Environment.NewLine,
ApplicationParameters.Name,
packageUpgrades.Count(p => p.Value.Success && !p.Value.Inconclusive),
packageUpgrades.Count,
upgradeFailures,
upgradeWarnings == 0 ? string.Empty : "{0} {1} package(s) had warnings.".format_with(Environment.NewLine, upgradeWarnings),
_fileSystem.combine_paths(ApplicationParameters.LoggingLocation, ApplicationParameters.LoggingFile)
));
if (upgradeWarnings != 0)
{
this.Log().Warn(ChocolateyLoggers.Important, "Warnings:");
foreach (var warning in packageUpgrades.Where(p => p.Value.Warning).or_empty_list_if_null())
{
this.Log().Warn(ChocolateyLoggers.Important, " - {0}".format_with(warning.Value.Name));
}
}
if (upgradeFailures != 0)
{
this.Log().Error("Failures:");
foreach (var failure in packageUpgrades.Where(p => !p.Value.Success).or_empty_list_if_null())
{
this.Log().Error(" - {0}".format_with(failure.Value.Name));
}
}
if (upgradeFailures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
return packageUpgrades;
}
public void uninstall_noop(ChocolateyConfiguration config)
{
_nugetService.uninstall_noop(config, (pkg) => _powershellService.uninstall_noop(pkg));
}
public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config)
{
this.Log().Info(@"Uninstalling the following packages:");
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));
if (config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Any(p => p.EndsWith(".config")))
{
throw new ApplicationException("A packages.config file is only used with installs.");
}
var packageUninstalls = _nugetService.uninstall_run(
config,
(packageResult) =>
{
if (!_fileSystem.directory_exists(packageResult.InstallLocation))
{
packageResult.InstallLocation += ".{0}".format_with(packageResult.Package.Version.to_string());
}
_shimgenService.uninstall(config, packageResult);
if (!config.SkipPackageInstallProvider)
{
_powershellService.uninstall(config, packageResult);
}
if (packageResult.Success)
{
_autoUninstallerService.run(packageResult, config);
}
// we don't care about the exit code
if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false);
if (packageResult.Success)
{
//todo: v2 clean up package information store for things no longer installed (call it compact?)
uninstall_cleanup(config, packageResult);
}
else
{
this.Log().Error(ChocolateyLoggers.Important, "{0} {1} not successful.".format_with(packageResult.Name, "uninstall"));
handle_unsuccessful_operation(config, packageResult, movePackageToFailureLocation: false, attemptRollback: false);
}
if (!packageResult.Success)
{
// throw an error so that NuGet Service doesn't attempt to continue with package removal
throw new ApplicationException("{0} {1} not successful.".format_with(packageResult.Name, "uninstall"));
}
});
var uninstallFailures = packageUninstalls.Count(p => !p.Value.Success);
this.Log().Warn(() => @"{0}{1} uninstalled {2}/{3} packages. {4} packages failed.{0} See the log for details ({5}).".format_with(
Environment.NewLine,
ApplicationParameters.Name,
packageUninstalls.Count(p => p.Value.Success && !p.Value.Inconclusive),
packageUninstalls.Count,
uninstallFailures,
_fileSystem.combine_paths(ApplicationParameters.LoggingLocation, ApplicationParameters.LoggingFile)
));
if (uninstallFailures != 0)
{
this.Log().Error("Failures");
foreach (var failure in packageUninstalls.Where(p => !p.Value.Success).or_empty_list_if_null())
{
this.Log().Error(" - {0}".format_with(failure.Value.Name));
}
}
if (uninstallFailures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
return packageUninstalls;
}
private void uninstall_cleanup(ChocolateyConfiguration config, PackageResult packageResult)
{
_packageInfoService.remove_package_information(packageResult.Package);
ensure_bad_package_path_is_clean(config, packageResult);
remove_rollback_if_exists(packageResult);
handle_extension_packages(config, packageResult);
if (config.Force)
{
var packageDirectory = _fileSystem.combine_paths(packageResult.InstallLocation);
if (string.IsNullOrWhiteSpace(packageDirectory) || !_fileSystem.directory_exists(packageDirectory)) return;
if (packageDirectory.is_equal_to(ApplicationParameters.InstallLocation) || packageDirectory.is_equal_to(ApplicationParameters.PackagesLocation))
{
packageResult.Messages.Add(
new ResultMessage(
ResultType.Error,
"Install location is not specific enough, cannot force remove directory:{0} Erroneous install location captured as '{1}'".format_with(Environment.NewLine, packageResult.InstallLocation)
)
);
return;
}
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.delete_directory_if_exists(packageDirectory, recursive: true),
"Attempted to remove '{0}' but had an error".format_with(packageDirectory),
logWarningInsteadOfError: true);
}
}
private void handle_extension_packages(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult == null) return;
_fileSystem.create_directory_if_not_exists(ApplicationParameters.ExtensionsLocation);
if (!packageResult.Name.to_lower().EndsWith(".extension")) return;
var pkgExtensions = _fileSystem.combine_paths(ApplicationParameters.ExtensionsLocation, packageResult.Name);
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.delete_directory_if_exists(pkgExtensions, recursive: true),
"Attempted to remove '{0}' but had an error".format_with(pkgExtensions));
if (!config.CommandName.is_equal_to(CommandNameType.uninstall.to_string()))
{
if (packageResult.InstallLocation == null) return;
_fileSystem.create_directory_if_not_exists(pkgExtensions);
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.copy_directory(packageResult.InstallLocation, pkgExtensions, overwriteExisting: true),
"Attempted to copy{0} '{1}'{0} to '{2}'{0} but had an error".format_with(Environment.NewLine, packageResult.InstallLocation, pkgExtensions));
string logMessage = "Installed/updated extension for {0}. You will be able to use it on next run.".format_with(packageResult.Name);
this.Log().Warn(logMessage);
packageResult.Messages.Add(new ResultMessage(ResultType.Note, logMessage));
}
}
private void ensure_bad_package_path_is_clean(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult.InstallLocation == null) return;
FaultTolerance.try_catch_with_logging_exception(
() =>
{
string badPackageInstallPath = packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation);
if (_fileSystem.directory_exists(badPackageInstallPath))
{
_fileSystem.delete_directory(badPackageInstallPath, recursive: true);
}
},
"Attempted to delete bad package install path if existing. Had an error");
}
private void handle_unsuccessful_operation(ChocolateyConfiguration config, PackageResult packageResult, bool movePackageToFailureLocation, bool attemptRollback)
{
Environment.ExitCode = 1;
foreach (var message in packageResult.Messages.Where(m => m.MessageType == ResultType.Error))
{
this.Log().Error(message.Message);
}
if (attemptRollback || movePackageToFailureLocation)
{
var packageDirectory = packageResult.InstallLocation;
if (packageDirectory.is_equal_to(ApplicationParameters.InstallLocation) || packageDirectory.is_equal_to(ApplicationParameters.PackagesLocation))
{
this.Log().Error(ChocolateyLoggers.Important, @"
Package location is not specific enough, cannot move bad package or
rollback previous version. Erroneous install location captured as
'{0}'
ATTENTION: You must take manual action to remove {1} from
{2}. It will show incorrectly as installed
until you do. To remove you can simply delete the folder in question.
".format_with(packageResult.InstallLocation, packageResult.Name, ApplicationParameters.PackagesLocation));
}
else
{
if (movePackageToFailureLocation) move_bad_package_to_failure_location(packageResult);
if (attemptRollback) rollback_previous_version(config, packageResult);
}
}
}
private void move_bad_package_to_failure_location(PackageResult packageResult)
{
_fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageFailuresLocation);
if (packageResult.InstallLocation != null && _fileSystem.directory_exists(packageResult.InstallLocation))
{
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.move_directory(packageResult.InstallLocation, packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation)),
"Could not move bad package to failure directory It will show as installed.{0} {1}{0} The error".format_with(Environment.NewLine,packageResult.InstallLocation));
}
}
private void rollback_previous_version(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult.InstallLocation == null) return;
var rollbackDirectory = packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageBackupLocation);
if (!_fileSystem.directory_exists(rollbackDirectory))
{
//search for folder
var possibleRollbacks = _fileSystem.get_directories(ApplicationParameters.PackageBackupLocation, packageResult.Name + "*");
if (possibleRollbacks != null && possibleRollbacks.Count() != 0)
{
rollbackDirectory = possibleRollbacks.OrderByDescending(p => p).DefaultIfEmpty(string.Empty).FirstOrDefault();
}
}
if (string.IsNullOrWhiteSpace(rollbackDirectory) || !_fileSystem.directory_exists(rollbackDirectory)) return;
this.Log().Debug("Attempting rollback");
var rollback = true;
if (config.PromptForConfirmation)
{
var selection = InteractivePrompt.prompt_for_confirmation(" Unsuccessful operation for {0}.{1} Do you want to rollback to previous version (package files only)?".format_with(packageResult.Name, Environment.NewLine), new[] { "yes", "no" }, defaultChoice: null, requireAnswer: true);
if (selection.is_equal_to("no")) rollback = false;
}
if (rollback)
{
_fileSystem.move_directory(rollbackDirectory, rollbackDirectory.Replace(ApplicationParameters.PackageBackupLocation, ApplicationParameters.PackagesLocation));
}
remove_rollback_if_exists(packageResult);
}
private void remove_rollback_if_exists(PackageResult packageResult)
{
_nugetService.remove_rollback_directory_if_exists(packageResult.Name);
}
}
}