diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 15998e30e..ed5d89a47 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -376,9 +376,12 @@ private List InstallPackage(IEnumerable pkgsToInstall, s var version4digitNoPrerelease = pkgIdentity.Version.Version.ToString(); string moduleManifestVersion = string.Empty; var scriptPath = Path.Combine(tempDirNameVersion, (p.Name + ".ps1")); - var isScript = File.Exists(scriptPath) ? true : false; + var modulePath = Path.Combine(tempDirNameVersion, (p.Name + ".psd1")); + // Check if the package is a module or a script + var isModule = File.Exists(modulePath); - if (!isScript) + + if (isModule) { var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1"); if (!File.Exists(moduleManifest)) @@ -420,16 +423,16 @@ private List InstallPackage(IEnumerable pkgsToInstall, s /// ./Modules /// ./Scripts /// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list - installPath = isScript ? _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase)) - : _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase)); + installPath = isModule ? _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase)) + : _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase)); } if (_includeXML) { - CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isScript); + CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isModule); } - MoveFilesIntoInstallPath(p, isScript, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath); + MoveFilesIntoInstallPath(p, isModule, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath); _cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", p.Name, installPath)); pkgsSuccessfullyInstalled.Add(p.Name); @@ -535,12 +538,12 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t return success; } - private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isScript) + private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isModule) { // Script will have a metadata file similar to: "TestScript_InstalledScriptInfo.xml" // Modules will have the metadata file: "PSGetModuleInfo.xml" - var metadataXMLPath = isScript ? Path.Combine(dirNameVersion, (pkg.Name + "_InstalledScriptInfo.xml")) - : Path.Combine(dirNameVersion, "PSGetModuleInfo.xml"); + var metadataXMLPath = isModule ? Path.Combine(dirNameVersion, "PSGetModuleInfo.xml") + : Path.Combine(dirNameVersion, (pkg.Name + "_InstalledScriptInfo.xml")); pkg.InstalledDate = DateTime.Now; pkg.InstalledLocation = installPath; @@ -627,7 +630,7 @@ private bool TryDeleteDirectory( private void MoveFilesIntoInstallPath( PSResourceInfo p, - bool isScript, + bool isModule, bool isLocalRepo, string dirNameVersion, string tempInstallPath, @@ -639,18 +642,42 @@ private void MoveFilesIntoInstallPath( string scriptPath) { // Creating the proper installation path depending on whether pkg is a module or script - var newPathParent = isScript ? installPath : Path.Combine(installPath, p.Name); - var finalModuleVersionDir = isScript ? installPath : Path.Combine(installPath, p.Name, moduleManifestVersion); // versionWithoutPrereleaseTag + var newPathParent = isModule ? Path.Combine(installPath, p.Name) : installPath; + var finalModuleVersionDir = isModule ? Path.Combine(installPath, p.Name, moduleManifestVersion) : installPath; // versionWithoutPrereleaseTag // If script, just move the files over, if module, move the version directory over - var tempModuleVersionDir = (isScript || isLocalRepo) ? dirNameVersion + var tempModuleVersionDir = (!isModule || isLocalRepo) ? dirNameVersion : Path.Combine(tempInstallPath, p.Name.ToLower(), newVersion); _cmdletPassedIn.WriteVerbose(string.Format("Installation source path is: '{0}'", tempModuleVersionDir)); - _cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir)); + _cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir)); - if (isScript) + if (isModule) { + // If new path does not exist + if (!Directory.Exists(newPathParent)) + { + _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); + Directory.CreateDirectory(newPathParent); + Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); + } + else + { + _cmdletPassedIn.WriteVerbose(string.Format("Temporary module version directory is: '{0}'", tempModuleVersionDir)); + + // At this point if + if (Directory.Exists(finalModuleVersionDir)) + { + // Delete the directory path before replacing it with the new module + _cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", finalModuleVersionDir)); + Directory.Delete(finalModuleVersionDir, true); + } + + _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); + Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); + } + } + else { if (!_savePkg) { // Need to delete old xml files because there can only be 1 per script @@ -677,31 +704,6 @@ private void MoveFilesIntoInstallPath( _cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1"))); Utils.MoveFiles(scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1")); } - else - { - // If new path does not exist - if (!Directory.Exists(newPathParent)) - { - _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); - Directory.CreateDirectory(newPathParent); - Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); - } - else - { - _cmdletPassedIn.WriteVerbose(string.Format("Temporary module version directory is: '{0}'", tempModuleVersionDir)); - - // At this point if - if (Directory.Exists(finalModuleVersionDir)) - { - // Delete the directory path before replacing it with the new module - _cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", finalModuleVersionDir)); - Directory.Delete(finalModuleVersionDir, true); - } - - _cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir)); - Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir); - } - } } } } diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 4a61f1549..9dcfa1f30 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -243,6 +243,14 @@ Describe 'Test Install-PSResource for Module' { $res = Get-Module "TestModule" -ListAvailable $res | Should -BeNullOrEmpty } + + It "Validates that a module with module-name script files (like Pester) installs under Modules path" { + + Install-PSResource -Name "testModuleWithScript" -Repository $TestGalleryName + + $res = Get-Module "testModuleWithScript" -ListAvailable + $res.Path.Contains("Modules") | Should -Be $true + } } <# Temporarily commented until -Tag is implemented for this Describe block