Skip to content

Commit 65872b1

Browse files
authored
Fix bug with some modules installing as scripts (#504)
1 parent 19c1d70 commit 65872b1

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

src/code/InstallHelper.cs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,12 @@ private List<string> InstallPackage(IEnumerable<PSResourceInfo> pkgsToInstall, s
376376
var version4digitNoPrerelease = pkgIdentity.Version.Version.ToString();
377377
string moduleManifestVersion = string.Empty;
378378
var scriptPath = Path.Combine(tempDirNameVersion, (p.Name + ".ps1"));
379-
var isScript = File.Exists(scriptPath) ? true : false;
379+
var modulePath = Path.Combine(tempDirNameVersion, (p.Name + ".psd1"));
380+
// Check if the package is a module or a script
381+
var isModule = File.Exists(modulePath);
380382

381-
if (!isScript)
383+
384+
if (isModule)
382385
{
383386
var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1");
384387
if (!File.Exists(moduleManifest))
@@ -420,16 +423,16 @@ private List<string> InstallPackage(IEnumerable<PSResourceInfo> pkgsToInstall, s
420423
/// ./Modules
421424
/// ./Scripts
422425
/// _pathsToInstallPkg is sorted by desirability, Find will pick the pick the first Script or Modules path found in the list
423-
installPath = isScript ? _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase))
424-
: _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase));
426+
installPath = isModule ? _pathsToInstallPkg.Find(path => path.EndsWith("Modules", StringComparison.InvariantCultureIgnoreCase))
427+
: _pathsToInstallPkg.Find(path => path.EndsWith("Scripts", StringComparison.InvariantCultureIgnoreCase));
425428
}
426429

427430
if (_includeXML)
428431
{
429-
CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isScript);
432+
CreateMetadataXMLFile(tempDirNameVersion, installPath, repoName, p, isModule);
430433
}
431434

432-
MoveFilesIntoInstallPath(p, isScript, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath);
435+
MoveFilesIntoInstallPath(p, isModule, isLocalRepo, tempDirNameVersion, tempInstallPath, installPath, newVersion, moduleManifestVersion, normalizedVersionNoPrereleaseLabel, version4digitNoPrerelease, scriptPath);
433436

434437
_cmdletPassedIn.WriteVerbose(String.Format("Successfully installed package '{0}' to location '{1}'", p.Name, installPath));
435438
pkgsSuccessfullyInstalled.Add(p.Name);
@@ -535,12 +538,12 @@ private bool CallAcceptLicense(PSResourceInfo p, string moduleManifest, string t
535538
return success;
536539
}
537540

538-
private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isScript)
541+
private void CreateMetadataXMLFile(string dirNameVersion, string installPath, string repoName, PSResourceInfo pkg, bool isModule)
539542
{
540543
// Script will have a metadata file similar to: "TestScript_InstalledScriptInfo.xml"
541544
// Modules will have the metadata file: "PSGetModuleInfo.xml"
542-
var metadataXMLPath = isScript ? Path.Combine(dirNameVersion, (pkg.Name + "_InstalledScriptInfo.xml"))
543-
: Path.Combine(dirNameVersion, "PSGetModuleInfo.xml");
545+
var metadataXMLPath = isModule ? Path.Combine(dirNameVersion, "PSGetModuleInfo.xml")
546+
: Path.Combine(dirNameVersion, (pkg.Name + "_InstalledScriptInfo.xml"));
544547

545548
pkg.InstalledDate = DateTime.Now;
546549
pkg.InstalledLocation = installPath;
@@ -627,7 +630,7 @@ private bool TryDeleteDirectory(
627630

628631
private void MoveFilesIntoInstallPath(
629632
PSResourceInfo p,
630-
bool isScript,
633+
bool isModule,
631634
bool isLocalRepo,
632635
string dirNameVersion,
633636
string tempInstallPath,
@@ -639,18 +642,42 @@ private void MoveFilesIntoInstallPath(
639642
string scriptPath)
640643
{
641644
// Creating the proper installation path depending on whether pkg is a module or script
642-
var newPathParent = isScript ? installPath : Path.Combine(installPath, p.Name);
643-
var finalModuleVersionDir = isScript ? installPath : Path.Combine(installPath, p.Name, moduleManifestVersion); // versionWithoutPrereleaseTag
645+
var newPathParent = isModule ? Path.Combine(installPath, p.Name) : installPath;
646+
var finalModuleVersionDir = isModule ? Path.Combine(installPath, p.Name, moduleManifestVersion) : installPath; // versionWithoutPrereleaseTag
644647

645648
// If script, just move the files over, if module, move the version directory over
646-
var tempModuleVersionDir = (isScript || isLocalRepo) ? dirNameVersion
649+
var tempModuleVersionDir = (!isModule || isLocalRepo) ? dirNameVersion
647650
: Path.Combine(tempInstallPath, p.Name.ToLower(), newVersion);
648651

649652
_cmdletPassedIn.WriteVerbose(string.Format("Installation source path is: '{0}'", tempModuleVersionDir));
650-
_cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir));
653+
_cmdletPassedIn.WriteVerbose(string.Format("Installation destination path is: '{0}'", finalModuleVersionDir));
651654

652-
if (isScript)
655+
if (isModule)
653656
{
657+
// If new path does not exist
658+
if (!Directory.Exists(newPathParent))
659+
{
660+
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir));
661+
Directory.CreateDirectory(newPathParent);
662+
Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir);
663+
}
664+
else
665+
{
666+
_cmdletPassedIn.WriteVerbose(string.Format("Temporary module version directory is: '{0}'", tempModuleVersionDir));
667+
668+
// At this point if
669+
if (Directory.Exists(finalModuleVersionDir))
670+
{
671+
// Delete the directory path before replacing it with the new module
672+
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", finalModuleVersionDir));
673+
Directory.Delete(finalModuleVersionDir, true);
674+
}
675+
676+
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir));
677+
Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir);
678+
}
679+
}
680+
else {
654681
if (!_savePkg)
655682
{
656683
// Need to delete old xml files because there can only be 1 per script
@@ -677,31 +704,6 @@ private void MoveFilesIntoInstallPath(
677704
_cmdletPassedIn.WriteVerbose(string.Format("Moving '{0}' to '{1}'", scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1")));
678705
Utils.MoveFiles(scriptPath, Path.Combine(finalModuleVersionDir, p.Name + ".ps1"));
679706
}
680-
else
681-
{
682-
// If new path does not exist
683-
if (!Directory.Exists(newPathParent))
684-
{
685-
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir));
686-
Directory.CreateDirectory(newPathParent);
687-
Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir);
688-
}
689-
else
690-
{
691-
_cmdletPassedIn.WriteVerbose(string.Format("Temporary module version directory is: '{0}'", tempModuleVersionDir));
692-
693-
// At this point if
694-
if (Directory.Exists(finalModuleVersionDir))
695-
{
696-
// Delete the directory path before replacing it with the new module
697-
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", finalModuleVersionDir));
698-
Directory.Delete(finalModuleVersionDir, true);
699-
}
700-
701-
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to move '{0}' to '{1}'", tempModuleVersionDir, finalModuleVersionDir));
702-
Utils.MoveDirectory(tempModuleVersionDir, finalModuleVersionDir);
703-
}
704-
}
705707
}
706708
}
707709
}

test/InstallPSResource.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ Describe 'Test Install-PSResource for Module' {
243243
$res = Get-Module "TestModule" -ListAvailable
244244
$res | Should -BeNullOrEmpty
245245
}
246+
247+
It "Validates that a module with module-name script files (like Pester) installs under Modules path" {
248+
249+
Install-PSResource -Name "testModuleWithScript" -Repository $TestGalleryName
250+
251+
$res = Get-Module "testModuleWithScript" -ListAvailable
252+
$res.Path.Contains("Modules") | Should -Be $true
253+
}
246254
}
247255

248256
<# Temporarily commented until -Tag is implemented for this Describe block

0 commit comments

Comments
 (0)