Skip to content

Commit 7efcaa5

Browse files
authored
Code clean up pass (#547)
1 parent 74c45e5 commit 7efcaa5

15 files changed

+187
-173
lines changed

src/code/ArgumentCompleter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.ComponentModel;
2-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
32
// Licensed under the MIT License.
43

54
using Microsoft.PowerShell.PowerShellGet.UtilClasses;

src/code/GetHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Management.Automation;
1010

1111
using Dbg = System.Diagnostics.Debug;
12-
using static Microsoft.PowerShell.PowerShellGet.UtilClasses.PSResourceInfo;
1312

1413
namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
1514
{
@@ -196,7 +195,7 @@ public PSResourceInfo OutputPackageObject(string pkgPath, Dictionary<string,PSRe
196195

197196
// Read metadata from XML and parse into PSResourceInfo object
198197
_cmdletPassedIn.WriteVerbose(string.Format("Reading package metadata from: '{0}'", xmlFilePath));
199-
if (TryRead(xmlFilePath, out PSResourceInfo psGetInfo, out string errorMsg))
198+
if (PSResourceInfo.TryRead(xmlFilePath, out PSResourceInfo psGetInfo, out string errorMsg))
200199
{
201200
return psGetInfo;
202201
}

src/code/GetPSResourceRepository.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
1515
/// By default it will return all registered repositories, or if the -Name parameter argument is specified then it wil return the repository with that name.
1616
/// It returns PSRepositoryInfo objects which describe each resource item found.
1717
/// </summary>
18-
19-
[Cmdlet(VerbsCommon.Get,
20-
"PSResourceRepository")]
21-
public sealed
22-
class GetPSResourceRepository : PSCmdlet
18+
[Cmdlet(VerbsCommon.Get, "PSResourceRepository")]
19+
public sealed class GetPSResourceRepository : PSCmdlet
2320
{
2421
#region Parameters
2522

@@ -40,9 +37,11 @@ protected override void BeginProcessing()
4037
{
4138
RepositorySettings.CheckRepositoryStore();
4239
}
40+
4341
protected override void ProcessRecord()
4442
{
45-
string nameArrayAsString = (Name == null || !Name.Any() || string.Equals(Name[0], "*") || Name[0] == null) ? "all" : string.Join(", ", Name);
43+
string nameArrayAsString = (Name == null || !Name.Any() || string.Equals(Name[0], "*") || Name[0] == null)
44+
? "all" : string.Join(", ", Name);
4645
WriteVerbose(String.Format("reading repository: {0}. Calling Read() API now", nameArrayAsString));
4746
List<PSRepositoryInfo> items = RepositorySettings.Read(Name, out string[] errorList);
4847

src/code/InstallHelper.cs

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private List<string> InstallPackage(
286286
bool isLocalRepo)
287287
{
288288
List<string> pkgsSuccessfullyInstalled = new List<string>();
289-
foreach (PSResourceInfo p in pkgsToInstall)
289+
foreach (PSResourceInfo pkgInfo in pkgsToInstall)
290290
{
291291
var tempInstallPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
292292
try
@@ -300,23 +300,24 @@ private List<string> InstallPackage(
300300
// TODO: are there Linux accommodations we need to consider here?
301301
dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
302302

303-
_cmdletPassedIn.WriteVerbose(string.Format("Begin installing package: '{0}'", p.Name));
303+
_cmdletPassedIn.WriteVerbose(string.Format("Begin installing package: '{0}'", pkgInfo.Name));
304304

305305
// TODO: add progress bar here
306306

307307
// Create PackageIdentity in order to download
308-
string createFullVersion = p.Version.ToString();
309-
if (p.IsPrerelease)
308+
string createFullVersion = pkgInfo.Version.ToString();
309+
if (pkgInfo.IsPrerelease)
310310
{
311-
createFullVersion = p.Version.ToString() + "-" + p.PrereleaseLabel;
311+
createFullVersion = pkgInfo.Version.ToString() + "-" + pkgInfo.PrereleaseLabel;
312312
}
313313

314314
if (!NuGetVersion.TryParse(createFullVersion, out NuGetVersion pkgVersion))
315315
{
316-
_cmdletPassedIn.WriteVerbose(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion", p.Name, p.Version.ToString()));
316+
_cmdletPassedIn.WriteVerbose(string.Format("Error parsing package '{0}' version '{1}' into a NuGetVersion",
317+
pkgInfo.Name, pkgInfo.Version.ToString()));
317318
continue;
318319
}
319-
var pkgIdentity = new PackageIdentity(p.Name, pkgVersion);
320+
var pkgIdentity = new PackageIdentity(pkgInfo.Name, pkgVersion);
320321
var cacheContext = new SourceCacheContext();
321322

322323
if (isLocalRepo)
@@ -352,7 +353,9 @@ private List<string> InstallPackage(
352353
result.PackageReader.CopyFiles(
353354
destination: tempInstallPath,
354355
packageFiles: result.PackageReader.GetFiles(),
355-
extractFile: (new PackageFileExtractor(result.PackageReader.GetFiles(), packageExtractionContext.XmlDocFileSaveMode)).ExtractPackageFile,
356+
extractFile: new PackageFileExtractor(
357+
result.PackageReader.GetFiles(),
358+
packageExtractionContext.XmlDocFileSaveMode).ExtractPackageFile,
356359
logger: NullLogger.Instance,
357360
token: _cancellationToken);
358361
result.Dispose();
@@ -409,8 +412,8 @@ private List<string> InstallPackage(
409412
string tempDirNameVersion = isLocalRepo ? tempInstallPath : Path.Combine(tempInstallPath, pkgIdentity.Id.ToLower(), newVersion);
410413
var version4digitNoPrerelease = pkgIdentity.Version.Version.ToString();
411414
string moduleManifestVersion = string.Empty;
412-
var scriptPath = Path.Combine(tempDirNameVersion, (p.Name + ".ps1"));
413-
var modulePath = Path.Combine(tempDirNameVersion, (p.Name + ".psd1"));
415+
var scriptPath = Path.Combine(tempDirNameVersion, pkgInfo.Name + ".ps1");
416+
var modulePath = Path.Combine(tempDirNameVersion, pkgInfo.Name + ".psd1");
414417
// Check if the package is a module or a script
415418
var isModule = File.Exists(modulePath);
416419

@@ -436,20 +439,20 @@ private List<string> InstallPackage(
436439
moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string;
437440

438441
// Accept License verification
439-
if (!_savePkg && !CallAcceptLicense(p, moduleManifest, tempInstallPath, newVersion))
442+
if (!_savePkg && !CallAcceptLicense(pkgInfo, moduleManifest, tempInstallPath, newVersion))
440443
{
441444
continue;
442445
}
443446

444447
// If NoClobber is specified, ensure command clobbering does not happen
445-
if (_noClobber && !DetectClobber(p.Name, tempDirNameVersion, parsedMetadataHashtable))
448+
if (_noClobber && !DetectClobber(pkgInfo.Name, parsedMetadataHashtable))
446449
{
447450
continue;
448451
}
449452
}
450453

451454
// Delete the extra nupkg related files that are not needed and not part of the module/script
452-
DeleteExtraneousFiles(tempInstallPath, pkgIdentity, tempDirNameVersion);
455+
DeleteExtraneousFiles(pkgIdentity, tempDirNameVersion);
453456

454457
string installPath;
455458
if (_savePkg)
@@ -468,20 +471,29 @@ private List<string> InstallPackage(
468471

469472
if (_includeXML)
470473
{
471-
CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isModule);
474+
CreateMetadataXMLFile(tempDirNameVersion, installPath, pkgInfo, isModule);
472475
}
473476

474-
MoveFilesIntoInstallPath(p, isModule, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath);
477+
MoveFilesIntoInstallPath(
478+
pkgInfo,
479+
isModule,
480+
isLocalRepo,
481+
tempDirNameVersion,
482+
tempInstallPath,
483+
installPath,
484+
newVersion,
485+
moduleManifestVersion,
486+
scriptPath);
475487

476-
_cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", p.Name, installPath));
477-
pkgsSuccessfullyInstalled.Add(p.Name);
488+
_cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", pkgInfo.Name, installPath));
489+
pkgsSuccessfullyInstalled.Add(pkgInfo.Name);
478490
}
479491
catch (Exception e)
480492
{
481493
_cmdletPassedIn.WriteError(
482494
new ErrorRecord(
483495
new PSInvalidOperationException(
484-
message: $"Unable to successfully install package '{p.Name}': '{e.Message}'",
496+
message: $"Unable to successfully install package '{pkgInfo.Name}': '{e.Message}'",
485497
innerException: e),
486498
"InstallPackageFailed",
487499
ErrorCategory.InvalidOperation,
@@ -584,7 +596,7 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t
584596
return success;
585597
}
586598

587-
private bool DetectClobber(string pkgName, string tempDirNameVersion, Hashtable parsedMetadataHashtable)
599+
private bool DetectClobber(string pkgName, Hashtable parsedMetadataHashtable)
588600
{
589601
// Get installed modules, then get all possible paths
590602
bool foundClobber = false;
@@ -608,10 +620,12 @@ private bool DetectClobber(string pkgName, string tempDirNameVersion, Hashtable
608620
duplicateCmdlets = listOfCmdlets.Where(cmdlet => pkg.Includes.Cmdlet.Contains(cmdlet)).ToList();
609621

610622
}
623+
611624
if (pkg.Includes.Command != null && pkg.Includes.Command.Any())
612625
{
613626
duplicateCmds = listOfCmdlets.Where(commands => pkg.Includes.Command.Contains(commands, StringComparer.InvariantCultureIgnoreCase)).ToList();
614627
}
628+
615629
if (duplicateCmdlets.Any() || duplicateCmds.Any())
616630
{
617631

@@ -634,7 +648,7 @@ private bool DetectClobber(string pkgName, string tempDirNameVersion, Hashtable
634648
return foundClobber;
635649
}
636650

637-
private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isModule)
651+
private void CreateMetadataXMLFile(string dirNameVersion, string installPath, PSResourceInfo pkg, bool isModule)
638652
{
639653
// Script will have a metadata file similar to: "TestScript_InstalledScriptInfo.xml"
640654
// Modules will have the metadata file: "PSGetModuleInfo.xml"
@@ -654,14 +668,14 @@ private void CreateMetadataXMLFile(string dirNameVersion, string installPath, st
654668
}
655669
}
656670

657-
private void DeleteExtraneousFiles(string tempInstallPath, PackageIdentity pkgIdentity, string dirNameVersion)
671+
private void DeleteExtraneousFiles(PackageIdentity pkgIdentity, string dirNameVersion)
658672
{
659673
// Deleting .nupkg SHA file, .nuspec, and .nupkg after unpacking the module
660674
var pkgIdString = pkgIdentity.ToString();
661-
var nupkgSHAToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.sha512"));
662-
var nuspecToDelete = Path.Combine(dirNameVersion, (pkgIdentity.Id + ".nuspec"));
663-
var nupkgToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg"));
664-
var nupkgMetadataToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.metadata"));
675+
var nupkgSHAToDelete = Path.Combine(dirNameVersion, pkgIdString + ".nupkg.sha512");
676+
var nuspecToDelete = Path.Combine(dirNameVersion, pkgIdentity.Id + ".nuspec");
677+
var nupkgToDelete = Path.Combine(dirNameVersion, pkgIdString + ".nupkg");
678+
var nupkgMetadataToDelete = Path.Combine(dirNameVersion, pkgIdString + ".nupkg.metadata");
665679
var contentTypesToDelete = Path.Combine(dirNameVersion, "[Content_Types].xml");
666680
var relsDirToDelete = Path.Combine(dirNameVersion, "_rels");
667681
var packageDirToDelete = Path.Combine(dirNameVersion, "package");
@@ -725,25 +739,23 @@ private bool TryDeleteDirectory(
725739
}
726740

727741
private void MoveFilesIntoInstallPath(
728-
PSResourceInfo p,
742+
PSResourceInfo pkgInfo,
729743
bool isModule,
730744
bool isLocalRepo,
731745
string dirNameVersion,
732746
string tempInstallPath,
733747
string installPath,
734748
string newVersion,
735749
string moduleManifestVersion,
736-
string nupkgVersion,
737-
string versionWithoutPrereleaseTag,
738750
string scriptPath)
739751
{
740752
// Creating the proper installation path depending on whether pkg is a module or script
741-
var newPathParent = isModule ? Path.Combine(installPath, p.Name) : installPath;
742-
var finalModuleVersionDir = isModule ? Path.Combine(installPath, p.Name, moduleManifestVersion) : installPath; // versionWithoutPrereleaseTag
753+
var newPathParent = isModule ? Path.Combine(installPath, pkgInfo.Name) : installPath;
754+
var finalModuleVersionDir = isModule ? Path.Combine(installPath, pkgInfo.Name, moduleManifestVersion) : installPath;
743755

744756
// If script, just move the files over, if module, move the version directory over
745757
var tempModuleVersionDir = (!isModule || isLocalRepo) ? dirNameVersion
746-
: Path.Combine(tempInstallPath, p.Name.ToLower(), newVersion);
758+
: Path.Combine(tempInstallPath, pkgInfo.Name.ToLower(), newVersion);
747759

748760
_cmdletPassedIn.WriteVerbose(string.Format("Installation source path is: '{0}'", tempModuleVersionDir));
749761
_cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir));
@@ -774,11 +786,12 @@ private void MoveFilesIntoInstallPath(
774786
Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir);
775787
}
776788
}
777-
else {
789+
else
790+
{
778791
if (!_savePkg)
779792
{
780793
// Need to delete old xml files because there can only be 1 per script
781-
var scriptXML = p.Name + "_InstalledScriptInfo.xml";
794+
var scriptXML = pkgInfo.Name + "_InstalledScriptInfo.xml";
782795
_cmdletPassedIn.WriteVerbose(string.Format("Checking if path '{0}' exists: ", File.Exists(Path.Combine(installPath, "InstalledScriptInfos", scriptXML))));
783796
if (File.Exists(Path.Combine(installPath, "InstalledScriptInfos", scriptXML)))
784797
{
@@ -790,16 +803,16 @@ private void MoveFilesIntoInstallPath(
790803
Utils.MoveFiles(Path.Combine(dirNameVersion, scriptXML), Path.Combine(installPath, "InstalledScriptInfos", scriptXML));
791804

792805
// Need to delete old script file, if that exists
793-
_cmdletPassedIn.WriteVerbose(string.Format("Checking if path '{0}' exists: ", File.Exists(Path.Combine(finalModuleVersionDir, p.Name + ".ps1"))));
794-
if (File.Exists(Path.Combine(finalModuleVersionDir, p.Name + ".ps1")))
806+
_cmdletPassedIn.WriteVerbose(string.Format("Checking if path '{0}' exists: ", File.Exists(Path.Combine(finalModuleVersionDir, pkgInfo.Name + ".ps1"))));
807+
if (File.Exists(Path.Combine(finalModuleVersionDir, pkgInfo.Name + ".ps1")))
795808
{
796809
_cmdletPassedIn.WriteVerbose(string.Format("Deleting script file"));
797-
File.Delete(Path.Combine(finalModuleVersionDir, p.Name + ".ps1"));
810+
File.Delete(Path.Combine(finalModuleVersionDir, pkgInfo.Name + ".ps1"));
798811
}
799812
}
800813

801-
_cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1")));
802-
Utils.MoveFiles(scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1"));
814+
_cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", scriptPath, Path.Combine(finalModuleVersionDir, pkgInfo.Name + ".ps1")));
815+
Utils.MoveFiles(scriptPath, Path.Combine(finalModuleVersionDir, pkgInfo.Name + ".ps1"));
803816
}
804817
}
805818

src/code/NuGetLogger.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using NuGet.Common;
45
using System;
56
using System.Collections.Concurrent;
67
using System.Threading.Tasks;
7-
using NuGet.Common;
88

9-
// This class is needed for
10-
public class NuGetLogger : ILogger
9+
#region NuGetLogger
10+
11+
internal class NuGetLogger : ILogger
1112
{
1213
private readonly ITestOutputHelper _output;
1314

@@ -175,16 +176,21 @@ public async Task LogAsync(ILogMessage message)
175176
}
176177
}
177178

179+
#endregion
180+
181+
#region ITestOutputHelper
182+
178183
// Summary:
179184
// Represents a class which can be used to provide test output.
180-
public interface ITestOutputHelper
185+
internal interface ITestOutputHelper
181186
{
182187
// Summary:
183188
// Adds a line of text to the output.
184189
// Parameters:
185190
// message:
186191
// The message
187192
void WriteLine(string message);
193+
188194
// Summary:
189195
// Formats a line of text and adds it to the output.
190196
// Parameters:
@@ -194,3 +200,5 @@ public interface ITestOutputHelper
194200
// The format arguments
195201
void WriteLine(string format, params object[] args);
196202
}
203+
204+
#endregion

src/code/PSRepositoryInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3+
34
using System;
45
using System.Management.Automation;
56

0 commit comments

Comments
 (0)