diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index e59ab36f6..926f13c8f 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -385,8 +385,22 @@ private List InstallPackage(IEnumerable 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 @@ -432,9 +446,17 @@ private List InstallPackage(IEnumerable 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)); + } } } } @@ -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, @@ -660,4 +702,4 @@ private void MoveFilesIntoInstallPath( } } } -} \ No newline at end of file +} diff --git a/src/code/Utils.cs b/src/code/Utils.cs index b4881b3c8..891482d0e 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -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)) @@ -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 { @@ -423,7 +430,7 @@ public static Hashtable ParseModuleManifest(string moduleFileInfo, PSCmdlet cmdl } } - return parsedMetadataHash; + return successfullyParsed; } #endregion