public void TestProjectTemplate() { var projectTemplate = ProjectTemplate.Load(@"..\..\Test\TestProjectTemplate.ttproj"); var outputDir = Environment.CurrentDirectory + "\\OutputTemp"; try { Directory.Delete(outputDir, true); } catch (Exception) { } var result = projectTemplate.Generate(outputDir, "TestProject", Guid.NewGuid()); Assert.IsFalse(result.HasErrors); Assert.IsTrue(File.Exists(Path.Combine(outputDir, "TestProject.cs"))); Assert.IsTrue(File.Exists(Path.Combine(outputDir, @"SubFolder\TextRaw.txt"))); Assert.IsTrue(File.Exists(Path.Combine(outputDir, @"SubFolder\TextTemplate1.cs"))); Assert.AreEqual("This is a test with the file name using the property $ProjectName$ = \"TestProject\"", File.ReadAllText(Path.Combine(outputDir, @"TestProject.cs")).Trim()); Assert.AreEqual(File.ReadAllText(@"..\..\Test\SubFolder\TextRaw.txt"), File.ReadAllText(Path.Combine(outputDir, @"SubFolder\TextRaw.txt"))); Assert.AreEqual("This is a test of template with the project name TestProject and 5", File.ReadAllText(Path.Combine(outputDir, @"SubFolder\TextTemplate1.cs")).Trim()); }
public static ProjectTemplate PrepareTemplate(TemplateGeneratorParameters parameters, Package package, UFile templateRelativePath, PlatformType platformType, GraphicsPlatform? graphicsPlatform, ProjectType projectType) { if (platformType != PlatformType.Shared && !graphicsPlatform.HasValue) { throw new ArgumentException(@"Expecting a value for GraphicsPlatform when platformType is specified.", nameof(graphicsPlatform)); } var rootTemplateDir = parameters.Description.TemplateDirectory; var templateFilePath = UPath.Combine(rootTemplateDir, templateRelativePath); var projectTemplate = ProjectTemplate.Load(templateFilePath); // TODO: Assemblies are not configurable from the outside projectTemplate.Assemblies.Add(typeof(ProjectType).Assembly.FullName); projectTemplate.Assemblies.Add(typeof(StrideConfig).Assembly.FullName); projectTemplate.Assemblies.Add(typeof(GraphicsPlatform).Assembly.FullName); projectTemplate.Assemblies.Add(typeof(DisplayOrientation).Assembly.FullName); AddOption(parameters, "Package", package); AddOption(parameters, "PackageName", package.Meta.Name); // PackageNameCode, same as PackageName without '.' and ' '. AddOption(parameters, "PackageNameCode", package.Meta.Name.Replace(" ", string.Empty) .Replace(".", string.Empty)); AddOption(parameters, "PackageDisplayName", package.Meta.Title ?? package.Meta.Name); // Escape illegal characters for the short name AddOption(parameters, "PackageNameShort", Utilities.BuildValidClassName(package.Meta.Name.Replace(" ", string.Empty))); AddOption(parameters, "CurrentPlatform", platformType); if (platformType != PlatformType.Shared) { AddOption(parameters, "CurrentGraphicsPlatform", graphicsPlatform); } AddOption(parameters, "ProjectType", projectType); AddOption(parameters, "Namespace", parameters.Namespace ?? Utilities.BuildValidNamespaceName(package.Meta.Name)); if (platformType == PlatformType.Windows) { var isNETFramework = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"); AddOption(parameters, "TargetFramework", isNETFramework ? "net461" : "net5.0-windows"); } return projectTemplate; }
private static void GenerateUnitTestProject(string outputDirectory, string templateFile, string name) { var projectTemplate = ProjectTemplate.Load(templateFile); // Force reference to Paradox.Assets (to have acess to SolutionPlatform) projectTemplate.Assemblies.Add(typeof(GraphicsProfile).Assembly.FullName); projectTemplate.Assemblies.Add(typeof(ParadoxConfig).Assembly.FullName); var options = new Dictionary <string, object>(); var session = new PackageSession(); var result = new LoggerResult(); var templateGeneratorParameters = new TemplateGeneratorParameters(); templateGeneratorParameters.OutputDirectory = outputDirectory; templateGeneratorParameters.Session = session; templateGeneratorParameters.Name = name; templateGeneratorParameters.Logger = result; templateGeneratorParameters.Description = new TemplateDescription(); PackageUnitTestGenerator.Default.Generate(templateGeneratorParameters); if (result.HasErrors) { Console.WriteLine("Error generating package: {0}", result.ToText()); return; } var package = templateGeneratorParameters.Package; var previousCurrent = session.CurrentPackage; session.CurrentPackage = package; // Compute Paradox Sdk relative path // We are supposed to be in standard output binary folder, so Paradox root should be at ..\.. var paradoxPath = UDirectory.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UDirectory(@"..\..")); var paradoxRelativePath = new UDirectory(paradoxPath) .MakeRelative(outputDirectory) .ToString() .Replace('/', '\\'); paradoxRelativePath = paradoxRelativePath.TrimEnd('\\'); options["Namespace"] = name; options["Package"] = package; options["Platforms"] = new List <SolutionPlatform>(AssetRegistry.SupportedPlatforms); options["ParadoxSdkRelativeDir"] = paradoxRelativePath; // Generate project template var projectGuid = Guid.NewGuid(); result = projectTemplate.Generate(outputDirectory, name, projectGuid, options); if (result.HasErrors) { Console.WriteLine("Error generating solution: {0}", result.ToText()); return; } var sharedProfile = package.Profiles[PlatformType.Shared]; // Setup the assets folder Directory.CreateDirectory(UPath.Combine(outputDirectory, (UDirectory)"Assets/Shared")); // Add Windows test as Shared library var projectWindowsRef = new ProjectReference(); projectWindowsRef.Id = projectGuid; projectWindowsRef.Location = UPath.Combine(outputDirectory, (UFile)(name + ".Windows.csproj")); projectWindowsRef.Type = SiliconStudio.Assets.ProjectType.Library; sharedProfile.ProjectReferences.Add(projectWindowsRef); // Generate executable projects for each platform foreach (var platform in AssetRegistry.SupportedPlatforms) { var platformProfile = new PackageProfile(platform.Name) { Platform = platform.Type }; platformProfile.AssetFolders.Add(new AssetFolder("Assets/" + platform.Name)); // Log progress var projectName = name + "." + platform.Type; // Create project reference var projectPlatformRef = new ProjectReference(); projectPlatformRef.Id = projectGuid; projectPlatformRef.Location = UPath.Combine(outputDirectory, (UFile)(projectName + ".csproj")); projectPlatformRef.Type = SiliconStudio.Assets.ProjectType.Executable; platformProfile.ProjectReferences.Add(projectPlatformRef); // Add build configuration per platform platform.Properties.CopyTo(platformProfile.Properties, true); package.Profiles.Add(platformProfile); } session.CurrentPackage = previousCurrent; result = session.Save(); if (result.HasErrors) { Console.WriteLine("Error saving package: {0}", result.ToText()); return; } }
private static void GenerateUnitTestProject(string outputDirectory, string templateFile, string name, string projectNamespace) { var projectTemplate = ProjectTemplate.Load(templateFile); // Force reference to Stride.Assets (to have acess to SolutionPlatform) projectTemplate.Assemblies.Add(typeof(GraphicsProfile).Assembly.FullName); projectTemplate.Assemblies.Add(typeof(StrideConfig).Assembly.FullName); var options = new Dictionary <string, object>(); // When generating over an existing set of files, retrieve the existing IDs // for better incrementality Guid projectGuid, assetId; GetExistingGuid(outputDirectory, name + ".Windows.csproj", out projectGuid); GetExistingAssetId(outputDirectory, name + ".sdpkg", out assetId); var session = new PackageSession(); var result = new LoggerResult(); var templateGeneratorParameters = new SessionTemplateGeneratorParameters(); templateGeneratorParameters.OutputDirectory = outputDirectory; templateGeneratorParameters.Session = session; templateGeneratorParameters.Name = name; templateGeneratorParameters.Logger = result; templateGeneratorParameters.Description = new TemplateDescription(); templateGeneratorParameters.Id = assetId; templateGeneratorParameters.Namespace = projectNamespace; if (!PackageUnitTestGenerator.Default.PrepareForRun(templateGeneratorParameters).Result) { Console.WriteLine(@"Error generating package: PackageUnitTestGenerator.PrepareForRun returned false"); return; } if (!PackageUnitTestGenerator.Default.Run(templateGeneratorParameters)) { Console.WriteLine(@"Error generating package: PackageUnitTestGenerator.Run returned false"); return; } if (result.HasErrors) { Console.WriteLine($"Error generating package: {result.ToText()}"); return; } var project = session.Projects.OfType <SolutionProject>().Single(); var previousCurrent = session.CurrentProject; session.CurrentProject = project; // Compute Stride Sdk relative path // We are supposed to be in standard output binary folder, so Stride root should be at ..\.. var stridePath = UPath.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UDirectory(@"..\..")); var strideRelativePath = new UDirectory(stridePath) .MakeRelative(outputDirectory) .ToString() .Replace('/', '\\'); strideRelativePath = strideRelativePath.TrimEnd('\\'); options["Namespace"] = projectNamespace ?? name; options["Package"] = project.Package; options["Platforms"] = new List <SolutionPlatform>(AssetRegistry.SupportedPlatforms); options["StrideSdkRelativeDir"] = strideRelativePath; // Generate project template result = projectTemplate.Generate(outputDirectory, name, projectGuid, options); if (result.HasErrors) { Console.WriteLine("Error generating solution: {0}", result.ToText()); return; } // Setup the assets folder Directory.CreateDirectory(UPath.Combine(outputDirectory, (UDirectory)"Assets/Shared")); session.CurrentProject = previousCurrent; session.Save(result); if (result.HasErrors) { Console.WriteLine("Error saving package: {0}", result.ToText()); return; } }
private static void GenerateUnitTestProject(string outputDirectory, string templateFile, string name, string projectNamespace) { var projectTemplate = ProjectTemplate.Load(templateFile); // Force reference to Xenko.Assets (to have acess to SolutionPlatform) projectTemplate.Assemblies.Add(typeof(GraphicsProfile).Assembly.FullName); projectTemplate.Assemblies.Add(typeof(XenkoConfig).Assembly.FullName); var options = new Dictionary <string, object>(); // When generating over an existing set of files, retrieve the existing IDs // for better incrementality Guid projectGuid, assetId; GetExistingGuid(outputDirectory, name + ".Windows.csproj", out projectGuid); GetExistingAssetId(outputDirectory, name + ".xkpkg", out assetId); var session = new PackageSession(); var result = new LoggerResult(); var templateGeneratorParameters = new SessionTemplateGeneratorParameters(); templateGeneratorParameters.OutputDirectory = outputDirectory; templateGeneratorParameters.Session = session; templateGeneratorParameters.Name = name; templateGeneratorParameters.Logger = result; templateGeneratorParameters.Description = new TemplateDescription(); templateGeneratorParameters.Id = assetId; if (!PackageUnitTestGenerator.Default.PrepareForRun(templateGeneratorParameters).Result) { Console.WriteLine(@"Error generating package: PackageUnitTestGenerator.PrepareForRun returned false"); return; } if (!PackageUnitTestGenerator.Default.Run(templateGeneratorParameters)) { Console.WriteLine(@"Error generating package: PackageUnitTestGenerator.Run returned false"); return; } if (result.HasErrors) { Console.WriteLine($"Error generating package: {result.ToText()}"); return; } var package = session.LocalPackages.Single(); var previousCurrent = session.CurrentPackage; session.CurrentPackage = package; // Compute Xenko Sdk relative path // We are supposed to be in standard output binary folder, so Xenko root should be at ..\.. var xenkoPath = UPath.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new UDirectory(@"..\..")); var xenkoRelativePath = new UDirectory(xenkoPath) .MakeRelative(outputDirectory) .ToString() .Replace('/', '\\'); xenkoRelativePath = xenkoRelativePath.TrimEnd('\\'); options["Namespace"] = projectNamespace ?? name; options["Package"] = package; options["Platforms"] = new List <SolutionPlatform>(AssetRegistry.SupportedPlatforms); options["XenkoSdkRelativeDir"] = xenkoRelativePath; // Generate project template result = projectTemplate.Generate(outputDirectory, name, projectGuid, options); if (result.HasErrors) { Console.WriteLine("Error generating solution: {0}", result.ToText()); return; } var sharedProfile = package.Profiles.FindSharedProfile(); // Setup the assets folder Directory.CreateDirectory(UPath.Combine(outputDirectory, (UDirectory)"Assets/Shared")); // Add Windows test as Shared library var projectWindowsRef = new ProjectReference(projectGuid, UPath.Combine(outputDirectory, (UFile)(name + ".Windows.csproj")), SiliconStudio.Assets.ProjectType.Library); sharedProfile.ProjectReferences.Add(projectWindowsRef); // Generate executable projects for each platform foreach (var platform in AssetRegistry.SupportedPlatforms) { var platformProfile = new PackageProfile(platform.Name) { Platform = platform.Type }; platformProfile.AssetFolders.Add(new AssetFolder("Assets/" + platform.Name)); // Log progress var projectName = name + "." + platform.Type; // Create project reference var projectPlatformRef = new ProjectReference(projectGuid, UPath.Combine(outputDirectory, (UFile)(projectName + ".csproj")), SiliconStudio.Assets.ProjectType.Executable); platformProfile.ProjectReferences.Add(projectPlatformRef); package.Profiles.Add(platformProfile); } session.CurrentPackage = previousCurrent; session.Save(result); if (result.HasErrors) { Console.WriteLine("Error saving package: {0}", result.ToText()); return; } }