Skip to content

Fix bug with some modules installing as scripts #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 42 additions & 40 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,12 @@ private List<string> InstallPackage(IEnumerable<PSResourceInfo> 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))
Expand Down Expand Up @@ -420,16 +423,16 @@ private List<string> InstallPackage(IEnumerable<PSResourceInfo> 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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -627,7 +630,7 @@ private bool TryDeleteDirectory(

private void MoveFilesIntoInstallPath(
PSResourceInfo p,
bool isScript,
bool isModule,
bool isLocalRepo,
string dirNameVersion,
string tempInstallPath,
Expand All @@ -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
Expand All @@ -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);
}
}
}
}
}
8 changes: 8 additions & 0 deletions test/InstallPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down