public static string GeneratePackage(PackageDef definition) { foreach (var packageFile in definition.Files) { File.CreateText(packageFile.FileName).Close(); } string defFileName = "generated_package.xml"; using (var stream = File.Create(defFileName)) definition.SaveTo(stream); var proc = OpenTap.Engine.UnitTests.TapProcessContainer.StartFromArgs("package create " + defFileName, TimeSpan.FromMinutes(5)); proc.WaitForEnd(); string output = proc.ConsoleOutput; string outputFile = definition.Name + "." + definition.Version + ".TapPackage"; if (File.Exists(outputFile)) { return(outputFile); } else { throw new Exception(output); } }
/// <summary> /// Compresses the files to a zip package. /// </summary> static private void Compress(this PackageDef pkg, string outputPath, IEnumerable <PackageFile> inputPaths) { var dir = Path.GetDirectoryName(outputPath); if (String.IsNullOrWhiteSpace(dir) == false) { Directory.CreateDirectory(dir); } using (var zip = new System.IO.Compression.ZipArchive(File.Open(outputPath, FileMode.Create), System.IO.Compression.ZipArchiveMode.Create)) { foreach (PackageFile file in inputPaths) { var sw = Stopwatch.StartNew(); var relFileName = file.RelativeDestinationPath; var ZipPart = zip.CreateEntry(relFileName, System.IO.Compression.CompressionLevel.Optimal); using (var instream = File.OpenRead(file.FileName)) { using (var outstream = ZipPart.Open()) { var compressTask = instream.CopyToAsync(outstream, 2048, TapThread.Current.AbortToken); ConsoleUtils.PrintProgressTillEnd(compressTask, "Compressing", () => instream.Position, () => instream.Length); } } log.Debug(sw, "Compressed '{0}'", file.FileName); } // add the metadata xml file: var metaPart = zip.CreateEntry(String.Join("/", PackageDef.PackageDefDirectory, pkg.Name, PackageDef.PackageDefFileName), System.IO.Compression.CompressionLevel.Optimal); using (var str = metaPart.Open()) pkg.SaveTo(str); } }
public void SaveTo_Simple() { string inputFilename = "Packages/Package/package.xml"; // this package contains the old XML Schema URL string outputFileContent = ""; PackageDef pkg = PackageDef.FromXml(inputFilename); using (Stream str = new MemoryStream()) { pkg.SaveTo(str); using (StreamReader reader = new StreamReader(str)) { reader.BaseStream.Seek(0, 0); outputFileContent = reader.ReadToEnd(); } } string inputContent = File.ReadAllText(inputFilename); // check that the package now contains the new XML Schema URL StringAssert.Contains("xmlns=\"http://opentap.io/schemas/package\"", outputFileContent); }
public void SaveTo_FromXmlFile_Dependency() { string inputXml = @"<?xml version='1.0' encoding='utf-8' ?> <Package Name='Test3' xmlns ='http://opentap.io/schemas/package'> <Files> <File Path='OpenTap.Package.UnitTests.dll'> <UseVersion/> </File> </Files> </Package> "; string inputFilename = "test3.package.xml"; File.WriteAllText(inputFilename, inputXml); string outputFileContent = ""; PackageDef pkg = PackageDefExt.FromInputXml(inputFilename, Directory.GetCurrentDirectory()); pkg.Files.First().IgnoredDependencies.AddRange(new[] { "abc", "test" }); using (Stream str = new MemoryStream()) { pkg.SaveTo(str); using (StreamReader reader = new StreamReader(str)) { reader.BaseStream.Seek(0, 0); outputFileContent = reader.ReadToEnd(); } } StringAssert.Contains("<PackageDependency Package=\"Test1\" Version=\"^1.2", outputFileContent); // the Test2 package is a release version, so it should have 2 version numbers specified //StringAssert.Contains("<PackageDependency Package=\"Test2\" Version=\"1.2.3", outputFileContent); // the Test2 package is from an integration branch, so it should have 3 version numbers specified StringAssert.Contains("<PackageDependency Package=\"OpenTAP\"", outputFileContent); StringAssert.Contains("<IgnoreDependency>test</IgnoreDependency>", outputFileContent); // check that the dependency to XSeries is not there twice: Assert.IsFalse(Regex.IsMatch(outputFileContent, "(Test1).+\n.+(Test1)")); }
private int Process(string[] OutputPaths) { try { PackageDef pkg = null; if (!File.Exists(PackageXmlFile)) { log.Error("Cannot locate XML file '{0}'", PackageXmlFile); return((int)ExitCodes.FileSystemError); } if (!Directory.Exists(ProjectDir)) { log.Error("Project directory '{0}' does not exist.", ProjectDir); return((int)ExitCodes.FileSystemError); } try { var fullpath = Path.GetFullPath(PackageXmlFile); pkg = PackageDefExt.FromInputXml(fullpath, ProjectDir); // Check if package name has invalid characters or is not a valid path var illegalCharacter = pkg.Name.IndexOfAny(Path.GetInvalidFileNameChars()); if (illegalCharacter >= 0) { log.Error("Package name cannot contain invalid file path characters: '{0}'", pkg.Name[illegalCharacter]); return((int)ExitCodes.InvalidPackageName); } } catch (AggregateException aex) { foreach (var ex in aex.InnerExceptions) { if (ex is FileNotFoundException) { log.Error("File not found: '{0}'", ((FileNotFoundException)ex).FileName); } else { log.Error(ex.ToString()); } } log.Error("Caught errors while loading package definition."); return(4); } var tmpFile = Path.GetTempFileName(); // If user omitted the Version XML attribute or put Version="", lets inform. if (string.IsNullOrEmpty(pkg.RawVersion)) { log.Warning($"Package version is {pkg.Version} due to blank or missing 'Version' XML attribute in 'Package' element"); } pkg.CreatePackage(tmpFile); if (OutputPaths == null || OutputPaths.Length == 0) { OutputPaths = new string[1] { "" } } ; foreach (var outputPath in OutputPaths) { var path = outputPath; if (String.IsNullOrEmpty(path)) { path = GetRealFilePathFromName(pkg.Name, pkg.Version.ToString(), DefaultEnding); } Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))); ProgramHelper.FileCopy(tmpFile, path); log.Info("OpenTAP plugin package '{0}' containing '{1}' successfully created.", path, pkg.Name); } if (FakeInstall) { log.Warning("--fake-install argument is obsolete, use --install instead"); Install = FakeInstall; } if (Install) { var path = PackageDef.GetDefaultPackageMetadataPath(pkg, Directory.GetCurrentDirectory()); Directory.CreateDirectory(Path.GetDirectoryName(path)); using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { pkg.SaveTo(fs); } log.Info($"Installed '{pkg.Name}' ({Path.GetFullPath(path)})"); } } catch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); return((int)ExitCodes.GeneralPackageCreateError); } catch (InvalidDataException ex) { log.Error("Caught invalid data exception: {0}", ex.Message); return((int)ExitCodes.InvalidPackageDefinition); } return(0); }
public void CreatePackageDepReuse() { if (Directory.Exists("Packages2")) { Directory.Delete("Packages2", true); } Directory.Move("Packages", "Packages2"); File.Copy("Packages2/DependencyTest.dll", "DependencyTest.dll", true); try { Directory.CreateDirectory("Packages"); { PackageDef def = new PackageDef() { Name = "pkg1", Version = SemanticVersion.Parse("1.2") }; def.AddFile("OpenTap.dll"); Directory.CreateDirectory("Packages/pkg1"); using (var f = File.OpenWrite("Packages/pkg1/package.xml")) def.SaveTo(f); } { PackageDef def = new PackageDef() { Name = "gui", Version = SemanticVersion.Parse("1.2") }; def.AddFile("Keysight.OpenTap.Wpf.dll"); Directory.CreateDirectory("Packages/gui"); using (var f = File.OpenWrite("Packages/gui/package.xml")) def.SaveTo(f); } { PackageDef def = new PackageDef() { Name = "rv", Version = SemanticVersion.Parse("1.2") }; def.AddFile("Keysight.OpenTap.Wpf.dll"); Directory.CreateDirectory("Packages/rv"); using (var f = File.OpenWrite("Packages/rv/package.xml")) def.SaveTo(f); } { PackageDef def = new PackageDef(); def.Name = "test"; def.InfoLink = "a"; def.Date = DateTime.Today; def.Dependencies.Add(new PackageDependency("rv", VersionSpecifier.Parse("1.2"))); def.AddFile("DependencyTest.dll"); def.Files[0].DependentAssemblies.AddRange(PluginManager.GetSearcher().Assemblies.First(f => f.Name == "DependencyTest").References); var searcher = new PluginSearcher(); searcher.Search(Directory.GetCurrentDirectory()); List <AssemblyData> assemblies = searcher.Assemblies.ToList(); def.findDependencies(new List <string>(), assemblies); //Assert.AreEqual(3, def.Dependencies.Count); Assert.IsTrue(def.Dependencies.Any(d => d.Name == "rv")); Assert.IsTrue(def.Dependencies.Any(d => d.Name == "pkg1")); } { PackageDef def = new PackageDef(); def.Name = "test"; def.InfoLink = "a"; def.Date = DateTime.Today; def.Dependencies.Add(new PackageDependency("gui", VersionSpecifier.Parse("1.2"))); def.AddFile("DependencyTest.dll"); def.Files[0].DependentAssemblies.AddRange(PluginManager.GetSearcher().Assemblies.First(f => f.Name == "DependencyTest").References); var searcher = new PluginSearcher(); searcher.Search(Directory.GetCurrentDirectory()); List <AssemblyData> assemblies = searcher.Assemblies.ToList(); def.findDependencies(new List <string>(), assemblies); //Assert.AreEqual(2, def.Dependencies.Count); Assert.IsTrue(def.Dependencies.Any(d => d.Name == "gui")); Assert.IsTrue(def.Dependencies.Any(d => d.Name == "pkg1")); } } finally { if (Directory.Exists("Packages")) { Directory.Delete("Packages", true); } Directory.Move("Packages2", "Packages"); } }