Skip to content
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
48 changes: 45 additions & 3 deletions src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,22 @@ private List<string> InstallPackage(IEnumerable<PSResourceInfo> pkgsToInstall, s
if (!isScript)
{
var moduleManifest = Path.Combine(tempDirNameVersion, pkgIdentity.Id + ".psd1");
if (!File.Exists(moduleManifest))
{
var message = String.Format("Module manifest file: {0} does not exist. This is not a valid PowerShell module.", moduleManifest);

var ex = new ArgumentException(message);
var psdataFileDoesNotExistError = new ErrorRecord(ex, "psdataFileNotExistError", ErrorCategory.ReadError, null);
_cmdletPassedIn.WriteError(psdataFileDoesNotExistError);
continue;
}

if (!Utils.TryParseModuleManifest(moduleManifest, _cmdletPassedIn, out Hashtable parsedMetadataHashtable))
{
// Ran into errors parsing the module manifest file which was found in Utils.ParseModuleManifest() and written.
continue;
}

var parsedMetadataHashtable = Utils.ParseModuleManifest(moduleManifest, this);
moduleManifestVersion = parsedMetadataHashtable["ModuleVersion"] as string;

// Accept License verification
Expand Down Expand Up @@ -432,9 +446,17 @@ private List<string> InstallPackage(IEnumerable<PSResourceInfo> pkgsToInstall, s
{
// Delete the temp directory and all its contents
_cmdletPassedIn.WriteVerbose(string.Format("Attempting to delete '{0}'", tempInstallPath));

if (Directory.Exists(tempInstallPath))
{
Directory.Delete(tempInstallPath, true);
if (!TryDeleteDirectory(tempInstallPath, out ErrorRecord errorMsg))
{
_cmdletPassedIn.WriteError(errorMsg);
}
else
{
_cmdletPassedIn.WriteVerbose(String.Format("Successfully deleted '{0}'", tempInstallPath));
}
}
}
}
Expand Down Expand Up @@ -581,6 +603,26 @@ private void DeleteExtraneousFiles(string tempInstallPath, PackageIdentity pkgId
}
}

private bool TryDeleteDirectory(
string tempInstallPath,
out ErrorRecord errorMsg)
{
errorMsg = null;

try
{
Utils.DeleteDirectory(tempInstallPath);
}
catch (Exception e)
{
var TempDirCouldNotBeDeletedError = new ErrorRecord(e, "errorDeletingTempInstallPath", ErrorCategory.InvalidResult, null);
errorMsg = TempDirCouldNotBeDeletedError;
return false;
}

return true;
}

private void MoveFilesIntoInstallPath(
PSResourceInfo p,
bool isScript,
Expand Down Expand Up @@ -660,4 +702,4 @@ private void MoveFilesIntoInstallPath(
}
}
}
}
}
15 changes: 11 additions & 4 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,14 @@ private static void GetStandardPlatformPaths(

#region Manifest methods

public static Hashtable ParseModuleManifest(string moduleFileInfo, PSCmdlet cmdletPassedIn)
public static bool TryParseModuleManifest(
string moduleFileInfo,
PSCmdlet cmdletPassedIn,
out Hashtable parsedMetadataHashtable)
{
Hashtable parsedMetadataHash = new Hashtable();
parsedMetadataHashtable = new Hashtable();
bool successfullyParsed = false;

// A script will already have the metadata parsed into the parsedMetadatahash,
// a module will still need the module manifest to be parsed.
if (moduleFileInfo.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase))
Expand All @@ -405,13 +410,15 @@ public static Hashtable ParseModuleManifest(string moduleFileInfo, PSCmdlet cmdl
var ex = new ArgumentException(message);
var psdataParseError = new ErrorRecord(ex, "psdataParseError", ErrorCategory.ParserError, null);
cmdletPassedIn.WriteError(psdataParseError);
return successfullyParsed;
}
else
{
var data = ast.Find(a => a is HashtableAst, false);
if (data != null)
{
parsedMetadataHash = (Hashtable)data.SafeGetValue();
parsedMetadataHashtable = (Hashtable)data.SafeGetValue();
successfullyParsed = true;
}
else
{
Expand All @@ -423,7 +430,7 @@ public static Hashtable ParseModuleManifest(string moduleFileInfo, PSCmdlet cmdl
}
}

return parsedMetadataHash;
return successfullyParsed;
}

#endregion
Expand Down