public static void InstallPackage(Package package) { // Path of the zip file to be saved to string cachePath = MakePkgCachePath(package.Name); Console.WriteLine("Downloading from '{0}' to '{1}'", package.URL, cachePath); try { new WebClient().DownloadFile(package.URL, cachePath); } catch (WebException e) { Console.WriteLine("Could not download package: {0}", e.Message); return; } Console.WriteLine("Extracting..."); // Path of the zip file to be extracted to string archiveExtractPath = MakePkgArchiveCachePath(package.Name); ZipFile.ExtractToDirectory(cachePath, archiveExtractPath); // String arrays containing the files which are copied string[] incFiles = null; string[] libFiles = null; // Copy the include dir if it is set if(!string.IsNullOrEmpty(package.IncludeDir)) { // Either use the package name for the output directory // or the name set by IncludeOutputDir string includeDirName; if(!string.IsNullOrEmpty(package.IncludeOutputDir)) { includeDirName = package.IncludeOutputDir; } else { includeDirName = package.Name; } incFiles = Directory.GetFiles(archiveExtractPath + "\\" + package.IncludeDir, "*.*", SearchOption.AllDirectories); string includeDirPath = config.IncludePath + "\\" + includeDirName; DirectoryCopy(archiveExtractPath + "\\" + package.IncludeDir, includeDirPath, true); } // Copy the library dir if it is set if (!string.IsNullOrEmpty(package.LibraryDir)) { libFiles = Directory.GetFiles(archiveExtractPath + "\\" + package.LibraryDir, "*.*", SearchOption.AllDirectories); DirectoryCopy(archiveExtractPath + "\\" + package.LibraryDir, config.LibraryPath, true); } // Reformat the file paths // For example: Change C:\Users\User\AppData\Roaming\NSPkgMgr\pkg.temp.d\include\lala.h // to C:\Include\pkg\lala.h if(incFiles != null) { for(int i = 0; i < incFiles.Length; i++) { int index = incFiles[i].IndexOf(configFolder); string cleanPath = (index < 0) ? configFolder : incFiles[i].Remove(index, configFolder.Length); // Now we have something like this \\pkg.temp.d\\SomeFolder\\include\\lala.h // if we have IncludeOutputDir set then use that name // or else we use the package name // now format the string to that string includeDir = "\\" + package.Name + ".temp.d" + "\\" + package.IncludeDir; index = cleanPath.IndexOf(includeDir); string reallyCleanPath = (index < 0) ? includeDir : cleanPath.Remove(index, includeDir.Length); // Now it's just lala.h // Append the config's include directory now and we're done string includeOutputName; if(!string.IsNullOrEmpty(package.IncludeOutputDir)) { includeOutputName = package.IncludeOutputDir; } else { includeOutputName = package.Name; } includeOutputName = config.IncludePath + "\\" + includeOutputName + reallyCleanPath; incFiles[i] = includeOutputName; } } if (libFiles != null) { for (int i = 0; i < libFiles.Length; i++) { int index = libFiles[i].IndexOf(configFolder); string cleanPath = (index < 0) ? configFolder : libFiles[i].Remove(index, configFolder.Length); libFiles[i] = cleanPath; string libDir = "\\" + package.Name + ".temp.d" + "\\" + package.LibraryDir; index = cleanPath.IndexOf(libDir); string reallyCleanPath = (index < 0) ? libDir : cleanPath.Remove(index, libDir.Length); string libOutputName = config.LibraryPath + "\\" + reallyCleanPath; libFiles[i] = libOutputName; } } // All file uris need to be copied in this array string[] files; if(incFiles != null && libFiles == null) { files = incFiles; } else if(incFiles == null && libFiles != null) { files = libFiles; } else { // Merge the two file arrays files = new string[incFiles.Length + libFiles.Length]; Array.Copy(incFiles, files, incFiles.Length); Array.Copy(libFiles, 0, files, incFiles.Length, libFiles.Length); } // Notify that the package is installed WritePackageIsInstalled(package.Name, files); // Delete temp files Console.WriteLine("Cleaning up temp files"); File.Delete(cachePath); Directory.Delete(archiveExtractPath, true); }
public static void RemovePackage(Package package) { Console.WriteLine("Removing package '{0}'", package.Name); XmlDocument doc = new XmlDocument(); doc.Load(pkgInstalledPath); XmlNode packagesNode = doc.FirstChild; foreach(XmlNode node in packagesNode.ChildNodes) { if(node.Attributes["Name"].InnerText == package.Name) { // Split by ; string[] files = node.ChildNodes[0].InnerText.Split(';'); foreach(string s in files) { // Sometimes this bugs out so ignore these if(string.IsNullOrWhiteSpace(s)) { continue; } Console.WriteLine("Removing {0}", s); File.Delete(s); } packagesNode.RemoveChild(node); } } doc.Save(pkgInstalledPath); }