// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;
namespace Flax.Build.Projects.VisualStudio
{
///
/// The Visual Studio project generator for C# projects (.NET SDK .csproj).
///
///
public class CSSDKProjectGenerator : VisualStudioProjectGenerator
{
///
public CSSDKProjectGenerator(VisualStudioVersion version) : base(version)
{
}
///
public override string ProjectFileExtension => "csproj";
///
public override TargetType? Type => TargetType.DotNetCore;
///
public override void GenerateProject(Project project)
{
var csProjectFileContent = new StringBuilder();
var vsProject = (VisualStudioProject)project;
var projectFileToolVersion = ProjectFileToolVersion;
var projectDirectory = Path.GetDirectoryName(project.Path);
var defaultTarget = project.Targets[0];
foreach (var target in project.Targets)
{
// Pick the Editor-related target
if (target.IsEditor)
{
defaultTarget = target;
break;
}
}
var defaultConfiguration = project.Configurations.First();
foreach (var e in project.Configurations)
{
if (e.Configuration == defaultConfiguration.Configuration && e.Target == defaultTarget && e.Platform == Platform.BuildTargetPlatform)
{
defaultConfiguration = e;
break;
}
}
// Header
csProjectFileContent.AppendLine("");
csProjectFileContent.AppendLine("");
// Properties
csProjectFileContent.AppendLine(" ");
// List supported platforms and configurations
var allConfigurations = project.Configurations.Select(x => x.Text).Distinct().ToArray();
var allPlatforms = project.Configurations.Select(x => x.ArchitectureName).Distinct().ToArray();
csProjectFileContent.AppendLine(string.Format(" {0}", string.Join(";", allConfigurations)));
csProjectFileContent.AppendLine(string.Format(" {0}", string.Join(";", allPlatforms)));
// Provide default platform and configuration
csProjectFileContent.AppendLine(string.Format(" {0}", defaultConfiguration.Text));
csProjectFileContent.AppendLine(string.Format(" {0}", defaultConfiguration.ArchitectureName));
switch (project.OutputType ?? defaultTarget.OutputType)
{
case TargetOutputType.Executable:
csProjectFileContent.AppendLine(" Exe");
break;
case TargetOutputType.Library:
csProjectFileContent.AppendLine(" Library");
break;
default: throw new ArgumentOutOfRangeException();
}
var baseConfiguration = project.Configurations.First();
var baseOutputDir = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? baseConfiguration.TargetBuildOptions.OutputFolder, projectDirectory);
var baseIntermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(baseConfiguration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory);
csProjectFileContent.AppendLine(" net7.0");
csProjectFileContent.AppendLine(" disable");
csProjectFileContent.AppendLine(" annotations");
csProjectFileContent.AppendLine(" false");
csProjectFileContent.AppendLine(" false");
csProjectFileContent.AppendLine(" false");
csProjectFileContent.AppendLine(" false");
csProjectFileContent.AppendLine(" false");
csProjectFileContent.AppendLine(" false");
csProjectFileContent.AppendLine(string.Format(" {0}", project.BaseName));
csProjectFileContent.AppendLine(string.Format(" {0}.CSharp", project.BaseName));
csProjectFileContent.AppendLine(" 11.0");
csProjectFileContent.AppendLine(" 512");
// Needed for Hostfxr
csProjectFileContent.AppendLine(" true");
csProjectFileContent.AppendLine(" true");
//csProjectFileContent.AppendLine(" false"); // TODO: use it to reduce burden of framework libs
// This needs to be set here to fix errors in VS
csProjectFileContent.AppendLine(string.Format(" {0}", baseOutputDir));
csProjectFileContent.AppendLine(string.Format(" {0}", baseIntermediateOutputPath));
csProjectFileContent.AppendLine(" ");
csProjectFileContent.AppendLine("");
// Default configuration
{
var configuration = defaultConfiguration;
var defines = string.Join(";", project.Defines);
if (configuration.TargetBuildOptions.ScriptingAPI.Defines.Count != 0)
{
if (defines.Length != 0)
defines += ";";
defines += string.Join(";", configuration.TargetBuildOptions.ScriptingAPI.Defines);
}
var outputPath = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? configuration.TargetBuildOptions.OutputFolder, projectDirectory);
var intermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(configuration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory);
csProjectFileContent.AppendLine(string.Format(" ", configuration.Name));
csProjectFileContent.AppendLine(" true");
csProjectFileContent.AppendLine(" portable");
csProjectFileContent.AppendLine(string.Format(" {0}", configuration.Configuration == TargetConfiguration.Debug ? "false" : "true"));
csProjectFileContent.AppendLine(string.Format(" {0}\\", outputPath));
csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" {0}", defines));
csProjectFileContent.AppendLine(" prompt");
csProjectFileContent.AppendLine(" 4");
csProjectFileContent.AppendLine(" true");
if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings)
csProjectFileContent.AppendLine(" 1591");
csProjectFileContent.AppendLine(string.Format(" {0}\\{1}.CSharp.xml", outputPath, project.BaseName));
csProjectFileContent.AppendLine(" true");
csProjectFileContent.AppendLine(" ");
csProjectFileContent.AppendLine("");
}
// Configurations
foreach (var configuration in project.Configurations)
{
var defines = string.Join(";", project.Defines);
if (configuration.TargetBuildOptions.ScriptingAPI.Defines.Count != 0)
{
if (defines.Length != 0)
defines += ";";
defines += string.Join(";", configuration.TargetBuildOptions.ScriptingAPI.Defines);
}
var outputPath = Utilities.MakePathRelativeTo(project.CSharp.OutputPath ?? configuration.TargetBuildOptions.OutputFolder, projectDirectory);
var intermediateOutputPath = Utilities.MakePathRelativeTo(project.CSharp.IntermediateOutputPath ?? Path.Combine(configuration.TargetBuildOptions.IntermediateFolder, "CSharp"), projectDirectory);
csProjectFileContent.AppendLine(string.Format(" ", configuration.Name));
csProjectFileContent.AppendLine(" true");
csProjectFileContent.AppendLine(" portable");
csProjectFileContent.AppendLine(string.Format(" {0}", configuration.Configuration == TargetConfiguration.Release ? "true" : "false"));
csProjectFileContent.AppendLine(string.Format(" {0}\\", outputPath));
csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" {0}\\", intermediateOutputPath));
csProjectFileContent.AppendLine(string.Format(" {0}", defines));
csProjectFileContent.AppendLine(" prompt");
csProjectFileContent.AppendLine(" 4");
csProjectFileContent.AppendLine(" true");
if (configuration.TargetBuildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings)
csProjectFileContent.AppendLine(" 1591");
csProjectFileContent.AppendLine(string.Format(" {0}\\{1}.CSharp.xml", outputPath, project.BaseName));
csProjectFileContent.AppendLine(" true");
csProjectFileContent.AppendLine(" ");
csProjectFileContent.AppendLine("");
}
// References
csProjectFileContent.AppendLine(" ");
// Unused when using explicitly NetCore7 ?
//foreach (var reference in project.CSharp.SystemReferences)
{
//csProjectFileContent.AppendLine(string.Format(" ", reference));
}
foreach (var reference in project.CSharp.FileReferences)
{
csProjectFileContent.AppendLine(string.Format(" ", Path.GetFileNameWithoutExtension(reference)));
csProjectFileContent.AppendLine(string.Format(" {0}", Utilities.MakePathRelativeTo(reference, projectDirectory)));
csProjectFileContent.AppendLine(" ");
}
foreach (var dependency in project.Dependencies)
{
csProjectFileContent.AppendLine(string.Format(" ", Utilities.MakePathRelativeTo(dependency.Path, projectDirectory)));
csProjectFileContent.AppendLine(string.Format(" {0}", ((VisualStudioProject)dependency).ProjectGuid.ToString("B").ToUpperInvariant()));
csProjectFileContent.AppendLine(string.Format(" {0}", dependency.BaseName));
csProjectFileContent.AppendLine(" ");
}
csProjectFileContent.AppendLine(" ");
csProjectFileContent.AppendLine("");
// Files and folders
csProjectFileContent.AppendLine(" ");
var files = new List();
if (project.SourceFiles != null)
files.AddRange(project.SourceFiles);
if (project.SourceDirectories != null)
{
foreach (var folder in project.SourceDirectories)
{
files.AddRange(Directory.GetFiles(folder, "*", SearchOption.AllDirectories));
}
}
foreach (var file in files)
{
string fileType;
if (file.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
fileType = "Compile";
else
fileType = "None";
var projectPath = Utilities.MakePathRelativeTo(file, projectDirectory);
csProjectFileContent.AppendLine(string.Format(" <{0} Include=\"{1}\" />", fileType, projectPath));
}
if (project.GeneratedSourceFiles != null)
{
foreach (var file in project.GeneratedSourceFiles)
{
string fileType;
if (file.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
fileType = "Compile";
else
fileType = "None";
csProjectFileContent.AppendLine(string.Format(" <{0} Visible=\"false\" Include=\"{1}\" />", fileType, file));
}
}
csProjectFileContent.AppendLine(" ");
// End
csProjectFileContent.AppendLine("");
if (defaultTarget.CustomExternalProjectFilePath == null)
{
// Save the files
Utilities.WriteFileIfChanged(project.Path, csProjectFileContent.ToString());
}
}
}
}