public ToolInstaller.UnzipToolReturnAccumulator UnpackZipTool(string pathToZip, IUnpackZipToolSupport unpackSupport) { return(ToolInstaller.UnpackZipTool(pathToZip, unpackSupport)); }
public ToolInstaller.UnzipToolReturnAccumulator UnpackZipTool(string pathToZip, IUnpackZipToolSupport unpackSupport) { return ToolInstaller.UnpackZipTool(pathToZip, unpackSupport); }
/// <summary> /// Function for unpacking zipped External tools. /// </summary> /// <param name="pathToZip">Path to the zipped file that contains the tool and all its assicaited files.</param> /// <param name="unpackSupport"> Interface that implements required functions that are dependent on context.</param> /// <returns></returns> public static UnzipToolReturnAccumulator UnpackZipTool(string pathToZip, IUnpackZipToolSupport unpackSupport) { if (!Directory.Exists(pathToZip) && !File.Exists(pathToZip)) { throw new FileNotFoundException(pathToZip); } //Removes any old folders that dont have Tools associated with them CheckToolDirConsistency(); var retval = new UnzipToolReturnAccumulator(); string name = Path.GetFileNameWithoutExtension(pathToZip); if (name == null) { throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_Invalid_file_selected__No_tools_added_); } // This helps with zipfiles that have spaces in the titles. // Consider: We may want to add quotes around usages of the $(ToolDir) macro incase the Tool directory has spaces in one of its directory names. name = name.Replace(' ', '_'); string outerToolsFolderPath = ToolDescriptionHelpers.GetToolsDirectory(); if (string.IsNullOrEmpty(outerToolsFolderPath)) { throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_Error_unpacking_zipped_tools); } string tempFolderPath = Path.Combine(outerToolsFolderPath, @"Temp"); var toolDir = new DirectoryInfo(tempFolderPath); if (!toolDir.Exists) { toolDir.Create(); } // This naming conflict shouldn't happen. The temp file should be empty. // Consider: Try to delete the existing directory in the temp directory. string tempToolPath = Path.Combine(tempFolderPath, name); if (Directory.Exists(tempToolPath)) { tempToolPath = DirectoryEx.GetUniqueName(tempToolPath); } using (new TemporaryDirectory(tempToolPath)) { if (Directory.Exists(pathToZip)) { DirectoryCopy(pathToZip, tempToolPath); } else { using (var zipFile = new ZipFile(pathToZip)) { try { zipFile.ExtractAll(tempToolPath, ExtractExistingFileAction.OverwriteSilently); } catch (Exception) { throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_There_is_a_naming_conflict_in_unpacking_the_zip__Tool_importing_canceled_); } } } var dirInfo = new DirectoryInfo(tempToolPath); if (!dirInfo.Exists) { // Case where they try to load tools from an empty zipfile then the folder is never created. dirInfo.Create(); } var toolInfDir = new DirectoryInfo(Path.Combine(tempToolPath, TOOL_INF)); if (!toolInfDir.Exists) { throw new ToolExecutionException(TextUtil.LineSeparate( Resources.ToolInstaller_UnpackZipTool_The_selected_zip_file_is_not_an_installable_tool_, string.Format(Resources.ToolInstaller_UnpackZipTool_Error__It_does_not_contain_the_required__0__directory_, TOOL_INF))); } // Handle info.properties var toolInfo = GetToolInfo(toolInfDir, retval); if (!HandleAnnotations(unpackSupport.ShouldOverwriteAnnotations, toolInfDir)) { return(null); } HandleLegacyQuaSAR(toolInfo); var toolsToBeOverwritten = GetToolsToBeOverwritten(toolInfo.PackageIdentifier); List <ReportOrViewSpec> newReports; var existingReports = FindReportConflicts(toolInfDir, tempToolPath, out newReports); bool?overwrite = IsOverwrite(unpackSupport.ShouldOverwrite, toolsToBeOverwritten, existingReports, toolInfo); if (!overwrite.HasValue) { // User canceled installation. return(null); } string DirectoryToRemove = null; if (overwrite.Value) { // Delete the tools and their containing folder if (toolsToBeOverwritten.Count > 0) { foreach (var tool in toolsToBeOverwritten) { Settings.Default.ToolList.Remove(tool); } // The tools are all guarenteed to be from the same directory by GetToolsToBeOverwritten // and all toolDescriptions in a directory come from the same installation DirectoryToRemove = toolsToBeOverwritten.First().ToolDirPath; } // Overwrite all existing reports. foreach (ReportOrViewSpec item in existingReports) { ReportSharing.SaveReport(PersistedViews.ExternalToolsGroup, item); } } // Add all new reports. foreach (ReportOrViewSpec item in newReports) { ReportSharing.SaveReport(PersistedViews.ExternalToolsGroup, item); } var reportRenameMapping = new Dictionary <string, string>(); if (overwrite == false) // Dont overwrite so rename reports. { // Deal with renaming reports! foreach (ReportOrViewSpec item in existingReports) { string oldname = item.GetKey(); string newname = GetUniqueReportName(oldname); reportRenameMapping.Add(oldname, newname); ReportSharing.SaveReportAs(PersistedViews.ExternalToolsGroup, item, newname); } } foreach (FileInfo file in toolInfDir.GetFiles(@"*.properties")) { // We will replace the tool Directory value (null below) later when we know the import is sucessful. AddToolFromProperties(file, retval, toolInfo, null, tempToolPath, reportRenameMapping); } // Check if we need to install a program if (retval.Installations.Count > 0) { foreach (var ppc in retval.Installations.Keys) { string pathToPackageInstallScript = null; if (ppc.ProgramName.Equals(@"R") && retval.Installations[ppc].Count != 0) { pathToPackageInstallScript = Path.Combine(tempToolPath, TOOL_INF, INSTALL_R_PACKAGES); if (!File.Exists(pathToPackageInstallScript)) { throw new ToolExecutionException(TextUtil.LineSeparate(string.Format(Resources.ToolInstaller_UnpackZipTool_Error__There_is_a_file_missing_the__0__zip, name), string.Empty, string.Format(Resources.ToolInstaller_UnpackZipTool_Tool_Uses_R_and_specifies_Packages_without_an__0__file_in_the_tool_inf_directory_, INSTALL_R_PACKAGES))); } } string path = unpackSupport.InstallProgram(ppc, retval.Installations[ppc], pathToPackageInstallScript); if (path == null) { // Cancel installation return(null); } else if (path != string.Empty) { if (Settings.Default.ToolFilePaths.ContainsKey(ppc)) { Settings.Default.ToolFilePaths.Remove(ppc); } Settings.Default.ToolFilePaths.Add(ppc, path); } } } // We don't decide the final toolDirPath until we make it to here. // This will require some fixing of the tooldir and path to dll in each of the tools in retval.validtoolsfound // It also enables us to not delete the tools from the tool list unless we have a sucessful installation. // Decide the permToolPath. if (DirectoryToRemove != null) { DirectoryEx.SafeDelete(DirectoryToRemove); } // Final Directory Location. string permToolPath = DirectoryEx.GetUniqueName(Path.Combine(outerToolsFolderPath, name)); foreach (var tool in retval.ValidToolsFound) { tool.ToolDirPath = permToolPath; if (!string.IsNullOrEmpty(tool.ArgsCollectorDllPath)) { tool.ArgsCollectorDllPath = Path.Combine(permToolPath, tool.ArgsCollectorDllPath); } Settings.Default.ToolList.Add(tool); } if (retval.ValidToolsFound.Count != 0) { Helpers.TryTwice(() => Directory.Move(tempToolPath, permToolPath)); } } return(retval); }
/// <summary> /// Function for unpacking zipped External tools. /// </summary> /// <param name="pathToZip">Path to the zipped file that contains the tool and all its assicaited files.</param> /// <param name="unpackSupport"> Interface that implements required functions that are dependent on context.</param> /// <returns></returns> public static UnzipToolReturnAccumulator UnpackZipTool(string pathToZip, IUnpackZipToolSupport unpackSupport) { if (!Directory.Exists(pathToZip) && !File.Exists(pathToZip)) throw new FileNotFoundException(pathToZip); //Removes any old folders that dont have Tools associated with them CheckToolDirConsistency(); var retval = new UnzipToolReturnAccumulator(); string name = Path.GetFileNameWithoutExtension(pathToZip); if (name == null) { throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_Invalid_file_selected__No_tools_added_); } // This helps with zipfiles that have spaces in the titles. // Consider: We may want to add quotes around usages of the $(ToolDir) macro incase the Tool directory has spaces in one of its directory names. name = name.Replace(' ', '_'); string outerToolsFolderPath = ToolDescriptionHelpers.GetToolsDirectory(); if (string.IsNullOrEmpty(outerToolsFolderPath)) { throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_Error_unpacking_zipped_tools); } string tempFolderPath = Path.Combine(outerToolsFolderPath, "Temp"); // Not L10N var toolDir = new DirectoryInfo(tempFolderPath); if (!toolDir.Exists) toolDir.Create(); // This naming conflict shouldn't happen. The temp file should be empty. // Consider: Try to delete the existing directory in the temp directory. string tempToolPath = Path.Combine(tempFolderPath, name); if (Directory.Exists(tempToolPath)) tempToolPath = DirectoryEx.GetUniqueName(tempToolPath); using (new TemporaryDirectory(tempToolPath)) { if (Directory.Exists(pathToZip)) { DirectoryCopy(pathToZip, tempToolPath); } else using (var zipFile = new ZipFile(pathToZip)) { try { zipFile.ExtractAll(tempToolPath, ExtractExistingFileAction.OverwriteSilently); } catch (Exception) { throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_There_is_a_naming_conflict_in_unpacking_the_zip__Tool_importing_canceled_); } } var dirInfo = new DirectoryInfo(tempToolPath); if (!dirInfo.Exists) // Case where they try to load tools from an empty zipfile then the folder is never created. dirInfo.Create(); var toolInfDir = new DirectoryInfo(Path.Combine(tempToolPath, TOOL_INF)); if (!toolInfDir.Exists) { throw new ToolExecutionException(TextUtil.LineSeparate( Resources.ToolInstaller_UnpackZipTool_The_selected_zip_file_is_not_an_installable_tool_, string.Format(Resources.ToolInstaller_UnpackZipTool_Error__It_does_not_contain_the_required__0__directory_, TOOL_INF))); } // Handle info.properties var toolInfo = GetToolInfo(toolInfDir, retval); if (!HandleAnnotations(unpackSupport.ShouldOverwriteAnnotations, toolInfDir)) return null; HandleLegacyQuaSAR(toolInfo); var toolsToBeOverwritten = GetToolsToBeOverwritten(toolInfo.PackageIdentifier); List<ReportOrViewSpec> newReports; var existingReports = FindReportConflicts(toolInfDir, tempToolPath, out newReports); bool? overwrite = IsOverwrite(unpackSupport.ShouldOverwrite, toolsToBeOverwritten, existingReports, toolInfo); if (!overwrite.HasValue) { // User canceled installation. return null; } string DirectoryToRemove = null; if (overwrite.Value) { // Delete the tools and their containing folder if (toolsToBeOverwritten.Count > 0) { foreach (var tool in toolsToBeOverwritten) { Settings.Default.ToolList.Remove(tool); } // The tools are all guarenteed to be from the same directory by GetToolsToBeOverwritten // and all toolDescriptions in a directory come from the same installation DirectoryToRemove = toolsToBeOverwritten.First().ToolDirPath; } // Overwrite all existing reports. foreach (ReportOrViewSpec item in existingReports) { ReportSharing.SaveReport(PersistedViews.ExternalToolsGroup, item); } } // Add all new reports. foreach (ReportOrViewSpec item in newReports) { ReportSharing.SaveReport(PersistedViews.ExternalToolsGroup, item); } var reportRenameMapping = new Dictionary<string, string>(); if (overwrite == false) // Dont overwrite so rename reports. { // Deal with renaming reports! foreach (ReportOrViewSpec item in existingReports) { string oldname = item.GetKey(); string newname = GetUniqueReportName(oldname); reportRenameMapping.Add(oldname, newname); ReportSharing.SaveReportAs(PersistedViews.ExternalToolsGroup, item, newname); } } foreach (FileInfo file in toolInfDir.GetFiles("*.properties")) // Not L10N { // We will replace the tool Directory value (null below) later when we know the import is sucessful. AddToolFromProperties(file, retval, toolInfo, null, tempToolPath, reportRenameMapping); } // Check if we need to install a program if (retval.Installations.Count > 0) { foreach (var ppc in retval.Installations.Keys) { string pathToPackageInstallScript = null; if (ppc.ProgramName.Equals("R") && retval.Installations[ppc].Count != 0) // Not L10N { pathToPackageInstallScript = Path.Combine(tempToolPath, TOOL_INF, INSTALL_R_PACKAGES); if (!File.Exists(pathToPackageInstallScript)) { throw new ToolExecutionException(TextUtil.LineSeparate(string.Format(Resources.ToolInstaller_UnpackZipTool_Error__There_is_a_file_missing_the__0__zip, name), string.Empty, string.Format(Resources.ToolInstaller_UnpackZipTool_Tool_Uses_R_and_specifies_Packages_without_an__0__file_in_the_tool_inf_directory_, INSTALL_R_PACKAGES))); } } string path = unpackSupport.InstallProgram(ppc, retval.Installations[ppc], pathToPackageInstallScript); if (path == null) { // Cancel installation return null; } else if (path != string.Empty) { if (Settings.Default.ToolFilePaths.ContainsKey(ppc)) Settings.Default.ToolFilePaths.Remove(ppc); Settings.Default.ToolFilePaths.Add(ppc, path); } } } // We don't decide the final toolDirPath until we make it to here. // This will require some fixing of the tooldir and path to dll in each of the tools in retval.validtoolsfound // It also enables us to not delete the tools from the tool list unless we have a sucessful installation. // Decide the permToolPath. if (DirectoryToRemove != null) DirectoryEx.SafeDelete(DirectoryToRemove); // Final Directory Location. string permToolPath = DirectoryEx.GetUniqueName(Path.Combine(outerToolsFolderPath, name)); foreach (var tool in retval.ValidToolsFound) { tool.ToolDirPath = permToolPath; if (!string.IsNullOrEmpty(tool.ArgsCollectorDllPath)) tool.ArgsCollectorDllPath = Path.Combine(permToolPath, tool.ArgsCollectorDllPath); Settings.Default.ToolList.Add(tool); } if (retval.ValidToolsFound.Count != 0) { Helpers.TryTwice(() => Directory.Move(tempToolPath, permToolPath)); } } return retval; }