From 23cc8110ce51a6079ceb8b35cf0d5710e2e7725c Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Mon, 4 Oct 2021 12:35:52 -0700 Subject: [PATCH 1/4] Remove .nupkg.metadata after module installation --- src/code/InstallHelper.cs | 69 ++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 926f13c8f..c2342900c 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -362,20 +362,16 @@ private List InstallPackage(IEnumerable pkgsToInstall, s _cmdletPassedIn.WriteVerbose(string.Format("Successfully able to download package from source to: '{0}'", tempInstallPath)); // Prompt if module requires license acceptance (need to read info license acceptance info from the module manifest) - // pkgIdentity.Version.Version gets the version without metadata or release labels. - - + // pkgIdentity.Version.Version gets the version without metadata or release labels. string newVersion = pkgIdentity.Version.ToNormalizedString(); - // string normalizedVersionNoPrereleaseLabel = newVersion; if (pkgIdentity.Version.IsPrerelease) { - // 2.0.2 + // eg: 2.0.2 normalizedVersionNoPrereleaseLabel = pkgIdentity.Version.ToNormalizedString().Substring(0, pkgIdentity.Version.ToNormalizedString().IndexOf('-')); } - //p.Version = new System.Version(normalizedVersionNoPrereleaseLabel); - + string tempDirNameVersion = isLocalRepo ? tempInstallPath : Path.Combine(tempInstallPath, pkgIdentity.Id.ToLower(), newVersion); var version4digitNoPrerelease = pkgIdentity.Version.Version.ToString(); string moduleManifestVersion = string.Empty; @@ -402,6 +398,28 @@ private List InstallPackage(IEnumerable pkgsToInstall, s } moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string; + + //string commandsAsStr = string.Empty; + object[] metaHashObj = parsedMetadataHashtable["CmdletsToExport"] as Object[]; + int hashLength = metaHashObj.Length; + string[] commands = new string[hashLength]; + + /*foreach (var a in (parsedMetadataHashtable["FunctionsToExport"] as Object[])) + { + commands = commands.Append + commandsAsStr += (", " + a as string); + } */ + + for (int i = 0; i < hashLength; i++) + { + commands[i] = metaHashObj[i] as string; + } + + // Clobber verification + if (_noClobber && commands.Length > 0 && !CallClobberCheck(p.Name, commands)) + { + continue; + } // Accept License verification if (!_savePkg && !CallAcceptLicense(p, moduleManifest, tempInstallPath, newVersion)) @@ -463,7 +481,36 @@ private List InstallPackage(IEnumerable pkgsToInstall, s return pkgsSuccessfullyInstalled; } - + + private bool CallClobberCheck(string pkgName, string[] commands) + { + var success = true; + + try + { + var results = _cmdletPassedIn.InvokeCommand.InvokeScript( + script: $"param ([string[]] $commands) Get-Command $commands", + useNewScope: true, + writeToPipeline: System.Management.Automation.Runspaces.PipelineResultTypes.None, + input: null, + args: new object[] { commands }); + + if (results != null) + { + // The following commands already exist on the system. + var resultsAsStr = string.Join(", ", results); + _cmdletPassedIn.WriteVerbose(string.Format("The resource '{0}' cannot be installed because the following commands already exist on the system '{1}'", pkgName, resultsAsStr)); + + success = false; + } + } + catch (Exception e) { + success = false; + } + + return success; + } + private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string tempInstallPath, string newVersion) { var requireLicenseAcceptance = false; @@ -566,6 +613,7 @@ private void DeleteExtraneousFiles(string tempInstallPath, PackageIdentity pkgId var nupkgSHAToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.sha512").ToLower()); var nuspecToDelete = Path.Combine(dirNameVersion, (pkgIdentity.Id + ".nuspec").ToLower()); var nupkgToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg").ToLower()); + var nupkgMetadataToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.metadata").ToLower()); var contentTypesToDelete = Path.Combine(dirNameVersion, "[Content_Types].xml"); var relsDirToDelete = Path.Combine(dirNameVersion, "_rels"); var packageDirToDelete = Path.Combine(dirNameVersion, "package"); @@ -586,6 +634,11 @@ private void DeleteExtraneousFiles(string tempInstallPath, PackageIdentity pkgId _cmdletPassedIn.WriteVerbose(string.Format("Deleting '{0}'", nupkgToDelete)); File.Delete(nupkgToDelete); } + if (File.Exists(nupkgMetadataToDelete)) + { + _cmdletPassedIn.WriteVerbose(string.Format("Deleting '{0}'", nupkgMetadataToDelete)); + File.Delete(nupkgMetadataToDelete); + } if (File.Exists(contentTypesToDelete)) { _cmdletPassedIn.WriteVerbose(string.Format("Deleting '{0}'", contentTypesToDelete)); From 4bf09e50a1a79c1bc54d9396866ec44b9e0ecf10 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Mon, 4 Oct 2021 12:42:49 -0700 Subject: [PATCH 2/4] Remove unneeded code --- src/code/InstallHelper.cs | 53 --------------------------------------- 1 file changed, 53 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index c2342900c..c839bb7b6 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -397,30 +397,6 @@ private List InstallPackage(IEnumerable pkgsToInstall, s continue; } - moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string; - - //string commandsAsStr = string.Empty; - object[] metaHashObj = parsedMetadataHashtable["CmdletsToExport"] as Object[]; - int hashLength = metaHashObj.Length; - string[] commands = new string[hashLength]; - - /*foreach (var a in (parsedMetadataHashtable["FunctionsToExport"] as Object[])) - { - commands = commands.Append - commandsAsStr += (", " + a as string); - } */ - - for (int i = 0; i < hashLength; i++) - { - commands[i] = metaHashObj[i] as string; - } - - // Clobber verification - if (_noClobber && commands.Length > 0 && !CallClobberCheck(p.Name, commands)) - { - continue; - } - // Accept License verification if (!_savePkg && !CallAcceptLicense(p, moduleManifest, tempInstallPath, newVersion)) { @@ -482,35 +458,6 @@ private List InstallPackage(IEnumerable pkgsToInstall, s return pkgsSuccessfullyInstalled; } - private bool CallClobberCheck(string pkgName, string[] commands) - { - var success = true; - - try - { - var results = _cmdletPassedIn.InvokeCommand.InvokeScript( - script: $"param ([string[]] $commands) Get-Command $commands", - useNewScope: true, - writeToPipeline: System.Management.Automation.Runspaces.PipelineResultTypes.None, - input: null, - args: new object[] { commands }); - - if (results != null) - { - // The following commands already exist on the system. - var resultsAsStr = string.Join(", ", results); - _cmdletPassedIn.WriteVerbose(string.Format("The resource '{0}' cannot be installed because the following commands already exist on the system '{1}'", pkgName, resultsAsStr)); - - success = false; - } - } - catch (Exception e) { - success = false; - } - - return success; - } - private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string tempInstallPath, string newVersion) { var requireLicenseAcceptance = false; From 3c584524c9c55dce7f42d53fd0e07a5abece1450 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Mon, 4 Oct 2021 12:48:53 -0700 Subject: [PATCH 3/4] revert line accidentally removed --- src/code/InstallHelper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index c839bb7b6..863786d88 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -397,6 +397,8 @@ private List InstallPackage(IEnumerable pkgsToInstall, s continue; } + moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string; + // Accept License verification if (!_savePkg && !CallAcceptLicense(p, moduleManifest, tempInstallPath, newVersion)) { From 4da5ef1bca25251713624ea3d9c9edcd675d2d48 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Tue, 5 Oct 2021 09:58:04 -0700 Subject: [PATCH 4/4] Remove unnecessary .Lower() from file paths being deleted --- src/code/InstallHelper.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 863786d88..15998e30e 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -559,10 +559,10 @@ private void DeleteExtraneousFiles(string tempInstallPath, PackageIdentity pkgId { // Deleting .nupkg SHA file, .nuspec, and .nupkg after unpacking the module var pkgIdString = pkgIdentity.ToString(); - var nupkgSHAToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.sha512").ToLower()); - var nuspecToDelete = Path.Combine(dirNameVersion, (pkgIdentity.Id + ".nuspec").ToLower()); - var nupkgToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg").ToLower()); - var nupkgMetadataToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.metadata").ToLower()); + var nupkgSHAToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.sha512")); + var nuspecToDelete = Path.Combine(dirNameVersion, (pkgIdentity.Id + ".nuspec")); + var nupkgToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg")); + var nupkgMetadataToDelete = Path.Combine(dirNameVersion, (pkgIdString + ".nupkg.metadata")); var contentTypesToDelete = Path.Combine(dirNameVersion, "[Content_Types].xml"); var relsDirToDelete = Path.Combine(dirNameVersion, "_rels"); var packageDirToDelete = Path.Combine(dirNameVersion, "package");