/
ChocolateyPackageInformationService.cs
180 lines (159 loc) · 8.45 KB
/
ChocolateyPackageInformationService.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
// 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.IO;
using System.Text;
using NuGet;
using domain;
using tolerance;
using IFileSystem = filesystem.IFileSystem;
internal class ChocolateyPackageInformationService : IChocolateyPackageInformationService
{
private readonly IFileSystem _fileSystem;
private readonly IRegistryService _registryService;
private readonly IFilesService _filesService;
private const string REGISTRY_SNAPSHOT_FILE = ".registry";
private const string FILES_SNAPSHOT_FILE = ".files";
private const string SILENT_UNINSTALLER_FILE = ".silentUninstaller";
private const string SIDE_BY_SIDE_FILE = ".sxs";
private const string PIN_FILE = ".pin";
private const string ARGS_FILE = ".arguments";
private const string VERSION_OVERRIDE_FILE = ".version";
public ChocolateyPackageInformationService(IFileSystem fileSystem, IRegistryService registryService, IFilesService filesService)
{
_fileSystem = fileSystem;
_registryService = registryService;
_filesService = filesService;
}
public ChocolateyPackageInformation get_package_information(IPackage package)
{
var packageInformation = new ChocolateyPackageInformation(package);
if (package == null)
{
this.Log().Debug("No package information as package is null.");
return packageInformation;
}
var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));
if (!_fileSystem.directory_exists(pkgStorePath))
{
return packageInformation;
}
FaultTolerance.try_catch_with_logging_exception(
() =>
{
packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
},
"Unable to read registry snapshot file for {0} (located at {1})".format_with(package.Id, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE)),
throwError: false,
logWarningInsteadOfError: true
);
FaultTolerance.try_catch_with_logging_exception(
() =>
{
packageInformation.FilesSnapshot = _filesService.read_from_file(_fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
},
"Unable to read files snapshot file",
throwError: false,
logWarningInsteadOfError: true
);
packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
packageInformation.IsSideBySide = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
packageInformation.IsPinned = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);
if (_fileSystem.file_exists(argsFile)) packageInformation.Arguments = _fileSystem.read_file(argsFile);
var versionOverrideFile = _fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE);
if (_fileSystem.file_exists(versionOverrideFile))
{
FaultTolerance.try_catch_with_logging_exception(
() =>
{
packageInformation.VersionOverride = new SemanticVersion(_fileSystem.read_file(versionOverrideFile).trim_safe());
},
"Unable to read version override file",
throwError: false,
logWarningInsteadOfError: true
);
}
return packageInformation;
}
public void save_package_information(ChocolateyPackageInformation packageInformation)
{
_fileSystem.create_directory_if_not_exists(ApplicationParameters.ChocolateyPackageInfoStoreLocation);
_fileSystem.ensure_file_attribute_set(ApplicationParameters.ChocolateyPackageInfoStoreLocation, FileAttributes.Hidden);
if (packageInformation.Package == null)
{
this.Log().Debug("No package information to save as package is null.");
return;
}
var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(packageInformation.Package.Id, packageInformation.Package.Version.to_string()));
_fileSystem.create_directory_if_not_exists(pkgStorePath);
if (packageInformation.RegistrySnapshot != null)
{
_registryService.save_to_file(packageInformation.RegistrySnapshot, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
}
if (packageInformation.FilesSnapshot != null)
{
_filesService.save_to_file(packageInformation.FilesSnapshot, _fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
}
if (!string.IsNullOrWhiteSpace(packageInformation.Arguments))
{
var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);
if (_fileSystem.file_exists(argsFile)) _fileSystem.delete_file(argsFile);
_fileSystem.write_file(argsFile, packageInformation.Arguments);
}
else
{
_fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, ARGS_FILE));
}
if (packageInformation.VersionOverride != null)
{
var versionOverrideFile = _fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE);
if (_fileSystem.file_exists(versionOverrideFile)) _fileSystem.delete_file(versionOverrideFile);
_fileSystem.write_file(versionOverrideFile, packageInformation.VersionOverride.to_string());
}
else
{
_fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE));
}
if (packageInformation.HasSilentUninstall)
{
_fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE), string.Empty, Encoding.ASCII);
}
if (packageInformation.IsSideBySide)
{
_fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE), string.Empty, Encoding.ASCII);
}
else
{
_fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
}
if (packageInformation.IsPinned)
{
_fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE), string.Empty, Encoding.ASCII);
}
else
{
_fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
}
}
public void remove_package_information(IPackage package)
{
var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));
_fileSystem.delete_directory_if_exists(pkgStorePath, recursive: true);
}
}
}