public async Task CheckDeveloperTargetRedirects(string id, PackageVersion version, string realPath) { var nupkgFile = Path.Combine(Environment.ExpandEnvironmentVariables(store.DevSource), $"{id}.{version}.nupkg"); var isRedirectToCurrentPath = false; var installedPackage = store.GetLocalPackages(id).FirstOrDefault(x => x.Version == version); if (installedPackage != null) { var redirectFile = Path.Combine(installedPackage.Path, $@"{id}.redirect"); if (File.Exists(redirectFile) && String.Compare(File.ReadAllText(redirectFile), realPath, StringComparison.OrdinalIgnoreCase) == 0) { isRedirectToCurrentPath = true; } } // Note: later, we could do better and check existing package contents and scan for differences? // We could also delete older packages using same path if (File.Exists(nupkgFile) && isRedirectToCurrentPath) { return; } Directory.CreateDirectory(Path.GetDirectoryName(nupkgFile)); var builder = new NugetPackageBuilder(); var meta = new PackageMeta { Name = id, Version = version, Authors = { $"{id} developers" }, Description = $"{id} developer package using {realPath}", }; var nugetMeta = new ManifestMetadata(); ToNugetManifest(meta, nugetMeta); builder.Populate(nugetMeta); // Note: path must exist (created in NugetStore ctor) using (var tempDirectory = new TemporaryDirectory()) { // Generate fake files Directory.CreateDirectory(Path.Combine(tempDirectory.DirectoryPath, "build")); var files = new List <ManifestFile>(); foreach (var buildFileExtension in new[] { "targets", "props" }) { var source = Path.Combine(tempDirectory.DirectoryPath, $@"build\{id}.{buildFileExtension}"); File.WriteAllText(source, $@"<?xml version=""1.0"" encoding=""utf-8"" ?> <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" > <Import Project=""{realPath}\Targets\Xenko.{buildFileExtension}"" /> </Project> "); files.Add(new ManifestFile { Source = source, Target = $@"build\{id}.{buildFileExtension}" }); } var redirectFile = Path.Combine(tempDirectory.DirectoryPath, $"{id}.redirect"); File.WriteAllText(redirectFile, realPath); files.Add(new ManifestFile { Source = redirectFile, Target = $@"{id}.redirect" }); builder.PopulateFiles(".", files); using (var nupkgStream = File.OpenWrite(nupkgFile)) { builder.Save(nupkgStream); } } // If package is already installed in cache so that it will force reinstallation. if (installedPackage != null) { await store.UninstallPackage(installedPackage, null); } await store.InstallPackage(id, version, null); }
public static void Build(ILogger log, Package package, string outputDirectory = null) { if (package == null) { throw new ArgumentNullException(nameof(package)); } var meta = new ManifestMetadata(); PackageStore.ToNugetManifest(package.Meta, meta); // Sanity check: Xenko version should be same between NuGet package and Xenko package var nugetVersion = new PackageVersion(XenkoVersion.NuGetVersion).Version; var packageVersion = package.Meta.Version.Version; if (nugetVersion != packageVersion) { log.Error($"Package has mismatching version: NuGet package version is {nugetVersion} and Xenko Package version is {packageVersion}"); return; } if (nugetVersion.Revision <= 0) { // If 0, we have special cases with NuGet dropping it in install path, could be dangerous and lead to bugs if not properly tested. log.Error($"Package has revision {nugetVersion} but 4th digit needs to be at least 1."); return; } // Override version with NuGet version (4th number is different in Xenko package) meta.Version = XenkoVersion.NuGetVersion; var builder = new NugetPackageBuilder(); builder.Populate(meta); var currentAssemblyLocation = Assembly.GetExecutingAssembly().Location; var mainPlatformDirectory = Path.GetFileName(Path.GetDirectoryName(currentAssemblyLocation)); // TODO this is not working // We are excluding everything that is in a folder that starts with a dot (ie. .shadow, .vs) var files = new List <ManifestFile>() { NewFile(@"Bin\**\*.exe", "Bin", @"Bin\**\.*\**\*.exe;Bin\**\Tools\**.exe"), NewFile(@"Bin\**\*.so", "Bin", @"Bin\**\.*\**\*.so;Bin\Windows\lib\**\*.so"), NewFile(@"Bin\**\*.ssdeps", "Bin", @"Bin\**\.*\**\*.ssdeps"), NewFile(@"Bin\**\*.a", "Bin", @"Bin\**\.*\**\*.a"), NewFile(@"Bin\**\*.md", "Bin", @"Bin\**\.*\**\*.md"), NewFile(@"Bin\**\*.html", "Bin", @"Bin\**\.*\**\*.html"), NewFile(@"Bin\**\*.config", "Bin", @"Bin\**\.*\**\*.config"), NewFile(@"Bin\**\*.dll", "Bin", @"Bin\**\.*\**\*.dll;Bin\Windows\lib\**\*.dll"), NewFile(@"Bin\**\*.xml", "Bin", @"Bin\**\.*\**\*.xml"), NewFile(@"Bin\**\*.usrdoc", "Bin", @"Bin\**\.*\**\*.usrdoc"), NewFile(@"Bin\**\*.winmd", "Bin", @"Bin\**\.*\**\*.winmd"), NewFile(@"Bin\**\*.sh", "Bin", @"Bin\**\.*\**\*.sh"), NewFile(@"Bin\**\*.json", "Bin", @"Bin\**\.*\**\*.json"), NewFile(@"deps\AssemblyProcessor\*.exe", @"deps/AssemblyProcessor"), NewFile(@"deps\AssemblyProcessor\*.dll", @"deps/AssemblyProcessor"), NewFile($@"Bin\{mainPlatformDirectory}\ios-tcprelay\*.py", $@"Bin\{mainPlatformDirectory}\ios-tcprelay"), NewFile(@"Targets\*.targets", "Targets"), NewFile(@"Targets\*.props", "Targets"), NewFile($@"Bin\**\Xenko*.pdb", $@"Bin", @"Bin\**\.*\**\*.pdb;Bin\**\Xenko.Importer*.pdb;Bin\**\Xenko.Assimp.Translation.pdb"), NewFile(@"build\Xenko.targets", @"build"), NewFile(@"build\Xenko.props", @"build"), NewFile(@"tools\**\*.exe", "tools"), NewFile(@"tools\**\*.dll", "tools"), NewFile(@"LICENSE.md", @""), NewFile(@"THIRD PARTY.md", @""), NewFile(@"BACKERS.md", @""), }; // Handle Assets var rootDir = package.RootDirectory; var newPackage = new Package { Meta = AssetCloner.Clone(package.Meta) }; newPackage.Meta.Version = new PackageVersion(meta.Version); foreach (var profile in package.Profiles) { var target = "Assets/" + profile.Name; foreach (var assetFolder in profile.AssetFolders) { files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xk*", target)); } foreach (var resourceFolder in profile.ResourceFolders) { files.Add(NewFile(resourceFolder.MakeRelative(rootDir) + "/**/*.*", "Resources")); } var targetProfile = new PackageProfile(profile.Name); if (profile.AssetFolders.Count > 0) { targetProfile.AssetFolders.Add(new AssetFolder(target)); } if (profile.ResourceFolders.Count > 0) { targetProfile.ResourceFolders.Add("Resources"); } newPackage.Profiles.Add(targetProfile); } //Handle RootAssets foreach (var rootAsset in package.RootAssets) { newPackage.RootAssets.Add(rootAsset); } // Handle templates var targetFolder = new TemplateFolder("Templates"); foreach (var templateFolder in package.TemplateFolders) { var source = templateFolder.Path.MakeRelative(rootDir) + "/**"; UDirectory target = targetFolder.Path; if (templateFolder.Group != null) { target = UPath.Combine(target, templateFolder.Group); } var excludeFiles = templateFolder.Exclude; files.Add(NewFile(source, target, excludeFiles)); // Add template files foreach (var templateFile in templateFolder.Files) { var newTemplateFile = templateFile.MakeRelative(templateFolder.Path); if (templateFolder.Group != null) { newTemplateFile = UPath.Combine(templateFolder.Group, newTemplateFile); } newTemplateFile = UPath.Combine(targetFolder.Path, newTemplateFile); targetFolder.Files.Add(newTemplateFile); } } // Create temp package for archive newPackage.TemplateFolders.Add(targetFolder); var newPackageFileName = "temp" + Guid.NewGuid() + ".xkpkg"; newPackage.FullPath = package.RootDirectory + "/" + newPackageFileName; var result = new LoggerResult(); newPackage.Save(result); if (result.HasErrors) { throw new InvalidOperationException(result.ToText()); // TODO throw error } files.Add(NewFile(newPackageFileName, package.Meta.Name + Package.PackageFileExtension)); // Add files builder.PopulateFiles(package.RootDirectory, files); outputDirectory = outputDirectory ?? Environment.CurrentDirectory; // Save the nupkg var outputPath = GetOutputPath(builder, outputDirectory); bool isExistingPackage = File.Exists(outputPath); if (isExistingPackage) { File.Delete(outputPath); } try { using (Stream stream = File.Create(outputPath)) { builder.Save(stream); } } catch { if (!isExistingPackage && File.Exists(outputPath)) { File.Delete(outputPath); } throw; } File.Delete(newPackage.FullPath); }
public static void Build(ILogger log, Package package, string outputDirectory = null) { if (package == null) { throw new ArgumentNullException(nameof(package)); } var meta = new ManifestMetadata(); PackageStore.ToNugetManifest(package.Meta, meta); // Sanity check: Xenko version should be same between NuGet package and Xenko package var nugetVersion = new PackageVersion(XenkoVersion.NuGetVersion).Version; var packageVersion = package.Meta.Version.Version; if (nugetVersion != packageVersion) { log.Error($"Package has mismatching version: NuGet package version is {nugetVersion} and Xenko Package version is {packageVersion}"); return; } if (nugetVersion.Revision <= 0) { // If 0, we have special cases with NuGet dropping it in install path, could be dangerous and lead to bugs if not properly tested. log.Error($"Package has revision {nugetVersion} but 4th digit needs to be at least 1."); return; } // Override version with NuGet version (4th number is different in Xenko package) meta.Version = XenkoVersion.NuGetVersion; var builder = new NugetPackageBuilder(); builder.Populate(meta); var currentAssemblyLocation = Assembly.GetExecutingAssembly().Location; var mainPlatformDirectory = Path.GetFileName(Path.GetDirectoryName(currentAssemblyLocation)); // TODO this is not working // We are excluding everything that is in a folder that starts with a dot (ie. .shadow, .vs) var files = new List <ManifestFile>() { NewFile(@"Bin\**\*.exe", "Bin", @"Bin\**\.*\**\*.exe;Bin\**\Tools\**.exe"), NewFile(@"Bin\**\*.so", "Bin", @"Bin\**\.*\**\*.so;Bin\Windows\lib\**\*.so"), NewFile(@"Bin\**\*.ssdeps", "Bin", @"Bin\**\.*\**\*.ssdeps"), NewFile(@"Bin\**\*.a", "Bin", @"Bin\**\.*\**\*.a"), NewFile(@"Bin\**\*.md", "Bin", @"Bin\**\.*\**\*.md"), NewFile(@"Bin\**\*.html", "Bin", @"Bin\**\.*\**\*.html"), NewFile(@"Bin\**\*.config", "Bin", @"Bin\**\.*\**\*.config"), NewFile(@"Bin\**\*.dll", "Bin", @"Bin\**\.*\**\*.dll;Bin\Windows\lib\**\*.dll"), NewFile(@"Bin\**\*.xml", "Bin", @"Bin\**\.*\**\*.xml"), NewFile(@"Bin\**\*.usrdoc", "Bin", @"Bin\**\.*\**\*.usrdoc"), NewFile(@"Bin\**\*.winmd", "Bin", @"Bin\**\.*\**\*.winmd"), NewFile(@"Bin\**\*.sh", "Bin", @"Bin\**\.*\**\*.sh"), NewFile(@"Bin\**\*.json", "Bin", @"Bin\**\.*\**\*.json"), NewFile(@"deps\AssemblyProcessor\*.exe", @"deps/AssemblyProcessor"), NewFile(@"deps\AssemblyProcessor\*.dll", @"deps/AssemblyProcessor"), NewFile(@"deps\CoreFX\**\*.*", @"deps\CoreFX"), NewFile($@"Bin\{mainPlatformDirectory}\ios-tcprelay\*.py", $@"Bin\{mainPlatformDirectory}\ios-tcprelay"), NewFile(@"Targets\*.targets", "Targets"), NewFile($@"Bin\{mainPlatformDirectory}\SiliconStudio.*.pdb", $@"Bin\{mainPlatformDirectory}", @"Bin\**\SiliconStudio.Xenko.Importer*.pdb;Bin\**\SiliconStudio.Xenko.Assimp.Translation.pdb"), }; // Handle Assets var rootDir = package.RootDirectory; var newPackage = new Package { Meta = package.Meta }; foreach (var profile in package.Profiles) { var target = "Assets/" + profile.Name; foreach (var assetFolder in profile.AssetFolders) { // TODO: handle exclude in asset folders //files.Add(NewFile(source, target, @"**\*.cs;**\*.hlsl;**\*.csproj;**\*.csproj.user;**\obj\**")); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xksl", target)); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xkfx", target)); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xkfnt", target)); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xksheet", target)); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xkuilib", target)); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xkgfxcomp", target)); files.Add(NewFile(assetFolder.Path.MakeRelative(rootDir) + "/**/*.xktex", target)); var resourceFolder = UPath.Combine(assetFolder.Path, new UDirectory("../../Resources")); if (Directory.Exists(resourceFolder.ToWindowsPath())) { files.Add(NewFile(resourceFolder.MakeRelative(rootDir) + "/**/*.*", "Resources")); } } var targetProfile = new PackageProfile(profile.Name); targetProfile.AssetFolders.Add(new AssetFolder(target)); newPackage.Profiles.Add(targetProfile); } //Handle RootAssets foreach (var rootAsset in package.RootAssets) { newPackage.RootAssets.Add(rootAsset); } // Handle templates var targetFolder = new TemplateFolder("Templates"); foreach (var templateFolder in package.TemplateFolders) { var source = templateFolder.Path.MakeRelative(rootDir) + "/**"; UDirectory target = targetFolder.Path; if (templateFolder.Group != null) { target = UPath.Combine(target, templateFolder.Group); } var excludeFiles = templateFolder.Exclude; files.Add(NewFile(source, target, excludeFiles)); // Add template files foreach (var templateFile in templateFolder.Files) { var newTemplateFile = templateFile.MakeRelative(templateFolder.Path); if (templateFolder.Group != null) { newTemplateFile = UPath.Combine(templateFolder.Group, newTemplateFile); } newTemplateFile = UPath.Combine(targetFolder.Path, newTemplateFile); targetFolder.Files.Add(newTemplateFile); } } // Add files builder.PopulateFiles(package.RootDirectory, files); files.Clear(); var dataFiles = builder.Files.ToList(); builder.ClearFiles(); // Create temp package for archive newPackage.TemplateFolders.Add(targetFolder); var newPackageFileName = "temp" + Guid.NewGuid() + ".xkpkg"; newPackage.FullPath = package.RootDirectory + "/" + newPackageFileName; var result = new LoggerResult(); newPackage.Save(result); if (result.HasErrors) { throw new InvalidOperationException(result.ToText()); // TODO throw error } // Add the package file files.Add(NewFile(newPackageFileName, package.Meta.Name + Package.PackageFileExtension)); // Add entry point to decompress LZMA files.Add(NewFile(@"tools\**\*.exe", "tools")); files.Add(NewFile(@"tools\**\*.dll", "tools")); // Add an empty .xz file so that it gets added to [Content_Types].xml // This file will be removed later files.Add(NewFile(@"tools\data_empty.xz", string.Empty)); // Repopulate with .xkpkg file builder.PopulateFiles(package.RootDirectory, files); outputDirectory = outputDirectory ?? Environment.CurrentDirectory; // Save the nupkg var outputPath = GetOutputPath(builder, outputDirectory); bool isExistingPackage = File.Exists(outputPath); if (isExistingPackage) { File.Delete(outputPath); } try { using (Stream stream = File.Create(outputPath)) { builder.Save(stream); stream.Position = 0; // Add LZMA file as update so that it is stored without compression using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, true)) { // Delete data_empty.xz var dataEntry = archive.GetEntry("data_empty.xz"); dataEntry.Delete(); // Create data.xz (no compression since .xz is already compressed) dataEntry = archive.CreateEntry("data.xz", CompressionLevel.NoCompression); using (var dataStream = dataEntry.Open()) { // Generate LZMA using (var indexedArchive = new IndexedArchive()) { foreach (var file in dataFiles) { indexedArchive.AddFile(Path.Combine(package.RootDirectory, file.SourcePath), file.Path); } indexedArchive.Save(dataStream, new ConsoleProgressReport()); } } } } } catch { if (!isExistingPackage && File.Exists(outputPath)) { File.Delete(outputPath); } throw; } File.Delete(newPackage.FullPath); }