123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security;
- using System.Security.Cryptography;
- using System.Text;
- using UnityEditor;
- using UnityEditor.Compilation;
- using UnityEngine;
- using UnityEngine.Profiling;
- namespace VSCodeEditor
- {
- public interface IGenerator
- {
- bool SyncIfNeeded(List<string> affectedFiles, string[] reimportedFiles);
- void Sync();
- string SolutionFile();
- string ProjectDirectory { get; }
- IAssemblyNameProvider AssemblyNameProvider { get; }
- void GenerateAll(bool generateAll);
- bool SolutionExists();
- }
- public class ProjectGeneration : IGenerator
- {
- enum ScriptingLanguage
- {
- None,
- CSharp
- }
- public static readonly string MSBuildNamespaceUri = "http://schemas.microsoft.com/developer/msbuild/2003";
- const string k_WindowsNewline = "\r\n";
- const string k_SettingsJson = @"{
- ""files.exclude"":
- {
- ""**/.DS_Store"":true,
- ""**/.git"":true,
- ""**/.gitignore"":true,
- ""**/.gitmodules"":true,
- ""**/*.booproj"":true,
- ""**/*.pidb"":true,
- ""**/*.suo"":true,
- ""**/*.user"":true,
- ""**/*.userprefs"":true,
- ""**/*.unityproj"":true,
- ""**/*.dll"":true,
- ""**/*.exe"":true,
- ""**/*.pdf"":true,
- ""**/*.mid"":true,
- ""**/*.midi"":true,
- ""**/*.wav"":true,
- ""**/*.gif"":true,
- ""**/*.ico"":true,
- ""**/*.jpg"":true,
- ""**/*.jpeg"":true,
- ""**/*.png"":true,
- ""**/*.psd"":true,
- ""**/*.tga"":true,
- ""**/*.tif"":true,
- ""**/*.tiff"":true,
- ""**/*.3ds"":true,
- ""**/*.3DS"":true,
- ""**/*.fbx"":true,
- ""**/*.FBX"":true,
- ""**/*.lxo"":true,
- ""**/*.LXO"":true,
- ""**/*.ma"":true,
- ""**/*.MA"":true,
- ""**/*.obj"":true,
- ""**/*.OBJ"":true,
- ""**/*.asset"":true,
- ""**/*.cubemap"":true,
- ""**/*.flare"":true,
- ""**/*.mat"":true,
- ""**/*.meta"":true,
- ""**/*.prefab"":true,
- ""**/*.unity"":true,
- ""build/"":true,
- ""Build/"":true,
- ""Library/"":true,
- ""library/"":true,
- ""obj/"":true,
- ""Obj/"":true,
- ""ProjectSettings/"":true,
- ""temp/"":true,
- ""Temp/"":true
- }
- }";
-
-
-
- static readonly Dictionary<string, ScriptingLanguage> k_BuiltinSupportedExtensions = new Dictionary<string, ScriptingLanguage>
- {
- { "cs", ScriptingLanguage.CSharp },
- { "uxml", ScriptingLanguage.None },
- { "uss", ScriptingLanguage.None },
- { "shader", ScriptingLanguage.None },
- { "compute", ScriptingLanguage.None },
- { "cginc", ScriptingLanguage.None },
- { "hlsl", ScriptingLanguage.None },
- { "glslinc", ScriptingLanguage.None },
- { "template", ScriptingLanguage.None },
- { "raytrace", ScriptingLanguage.None }
- };
- string m_SolutionProjectEntryTemplate = string.Join("\r\n", @"Project(""{{{0}}}"") = ""{1}"", ""{2}"", ""{{{3}}}""", @"EndProject").Replace(" ", "\t");
- string m_SolutionProjectConfigurationTemplate = string.Join("\r\n", @" {{{0}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU", @" {{{0}}}.Debug|Any CPU.Build.0 = Debug|Any CPU").Replace(" ", "\t");
- static readonly string[] k_ReimportSyncExtensions = { ".dll", ".asmdef" };
- string[] m_ProjectSupportedExtensions = new string[0];
- public string ProjectDirectory { get; }
- IAssemblyNameProvider IGenerator.AssemblyNameProvider => m_AssemblyNameProvider;
- public void GenerateAll(bool generateAll)
- {
- m_AssemblyNameProvider.ToggleProjectGeneration(
- ProjectGenerationFlag.BuiltIn
- | ProjectGenerationFlag.Embedded
- | ProjectGenerationFlag.Git
- | ProjectGenerationFlag.Local
- #if UNITY_2019_3_OR_NEWER
- | ProjectGenerationFlag.LocalTarBall
- #endif
- | ProjectGenerationFlag.PlayerAssemblies
- | ProjectGenerationFlag.Registry
- | ProjectGenerationFlag.Unknown);
- }
- readonly string m_ProjectName;
- readonly IAssemblyNameProvider m_AssemblyNameProvider;
- readonly IFileIO m_FileIOProvider;
- readonly IGUIDGenerator m_GUIDProvider;
- const string k_ToolsVersion = "4.0";
- const string k_ProductVersion = "10.0.20506";
- const string k_BaseDirectory = ".";
- const string k_TargetFrameworkVersion = "v4.7.1";
- const string k_TargetLanguageVersion = "latest";
- public ProjectGeneration(string tempDirectory)
- : this(tempDirectory, new AssemblyNameProvider(), new FileIOProvider(), new GUIDProvider()) { }
- public ProjectGeneration(string tempDirectory, IAssemblyNameProvider assemblyNameProvider, IFileIO fileIO, IGUIDGenerator guidGenerator)
- {
- ProjectDirectory = tempDirectory.Replace('\\', '/');
- m_ProjectName = Path.GetFileName(ProjectDirectory);
- m_AssemblyNameProvider = assemblyNameProvider;
- m_FileIOProvider = fileIO;
- m_GUIDProvider = guidGenerator;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public bool SyncIfNeeded(List<string> affectedFiles, string[] reimportedFiles)
- {
- Profiler.BeginSample("SolutionSynchronizerSync");
- SetupProjectSupportedExtensions();
-
- if (SolutionExists() && HasFilesBeenModified(affectedFiles, reimportedFiles))
- {
- var assemblies = m_AssemblyNameProvider.GetAssemblies(ShouldFileBePartOfSolution);
- var allProjectAssemblies = RelevantAssembliesForMode(assemblies).ToList();
- var allAssetProjectParts = GenerateAllAssetProjectParts();
- var affectedNames = affectedFiles.Select(asset => m_AssemblyNameProvider.GetAssemblyNameFromScriptPath(asset)).Where(name => !string.IsNullOrWhiteSpace(name)).Select(name => name.Split(new [] {".dll"}, StringSplitOptions.RemoveEmptyEntries)[0]);
- var reimportedNames = reimportedFiles.Select(asset => m_AssemblyNameProvider.GetAssemblyNameFromScriptPath(asset)).Where(name => !string.IsNullOrWhiteSpace(name)).Select(name => name.Split(new [] {".dll"}, StringSplitOptions.RemoveEmptyEntries)[0]);
- var affectedAndReimported = new HashSet<string>(affectedNames.Concat(reimportedNames));
- var assemblyNames = new HashSet<string>(allProjectAssemblies.Select(assembly => Path.GetFileName(assembly.outputPath)));
- foreach (var assembly in allProjectAssemblies)
- {
- if (!affectedAndReimported.Contains(assembly.name))
- continue;
- SyncProject(assembly, allAssetProjectParts, ParseResponseFileData(assembly), assemblyNames);
- }
- Profiler.EndSample();
- return true;
- }
- Profiler.EndSample();
- return false;
- }
- bool HasFilesBeenModified(List<string> affectedFiles, string[] reimportedFiles)
- {
- return affectedFiles.Any(ShouldFileBePartOfSolution) || reimportedFiles.Any(ShouldSyncOnReimportedAsset);
- }
- static bool ShouldSyncOnReimportedAsset(string asset)
- {
- return k_ReimportSyncExtensions.Contains(new FileInfo(asset).Extension);
- }
- public void Sync()
- {
- SetupProjectSupportedExtensions();
- GenerateAndWriteSolutionAndProjects();
- }
- public bool SolutionExists()
- {
- return m_FileIOProvider.Exists(SolutionFile());
- }
- void SetupProjectSupportedExtensions()
- {
- m_ProjectSupportedExtensions = m_AssemblyNameProvider.ProjectSupportedExtensions;
- }
- bool ShouldFileBePartOfSolution(string file)
- {
-
- if (m_AssemblyNameProvider.IsInternalizedPackagePath(file))
- {
- return false;
- }
- return HasValidExtension(file);
- }
- bool HasValidExtension(string file)
- {
- string extension = Path.GetExtension(file);
-
- if (extension == ".dll")
- return true;
- if (file.ToLower().EndsWith(".asmdef"))
- return true;
- return IsSupportedExtension(extension);
- }
- bool IsSupportedExtension(string extension)
- {
- extension = extension.TrimStart('.');
- if (k_BuiltinSupportedExtensions.ContainsKey(extension))
- return true;
- if (m_ProjectSupportedExtensions.Contains(extension))
- return true;
- return false;
- }
- static ScriptingLanguage ScriptingLanguageFor(Assembly assembly)
- {
- return ScriptingLanguageFor(GetExtensionOfSourceFiles(assembly.sourceFiles));
- }
- static string GetExtensionOfSourceFiles(string[] files)
- {
- return files.Length > 0 ? GetExtensionOfSourceFile(files[0]) : "NA";
- }
- static string GetExtensionOfSourceFile(string file)
- {
- var ext = Path.GetExtension(file).ToLower();
- ext = ext.Substring(1);
- return ext;
- }
- static ScriptingLanguage ScriptingLanguageFor(string extension)
- {
- return k_BuiltinSupportedExtensions.TryGetValue(extension.TrimStart('.'), out var result)
- ? result
- : ScriptingLanguage.None;
- }
- public void GenerateAndWriteSolutionAndProjects()
- {
-
-
- var assemblies = m_AssemblyNameProvider.GetAssemblies(ShouldFileBePartOfSolution);
- var allAssetProjectParts = GenerateAllAssetProjectParts();
- SyncSolution(assemblies);
- var allProjectAssemblies = RelevantAssembliesForMode(assemblies).ToList();
- var assemblyNames = new HashSet<string>(allProjectAssemblies.Select(assembly => Path.GetFileName(assembly.outputPath)));
- foreach (Assembly assembly in allProjectAssemblies)
- {
- var responseFileData = ParseResponseFileData(assembly);
- SyncProject(assembly, allAssetProjectParts, responseFileData, assemblyNames);
- }
- WriteVSCodeSettingsFiles();
- }
- List<ResponseFileData> ParseResponseFileData(Assembly assembly)
- {
- var systemReferenceDirectories = CompilationPipeline.GetSystemAssemblyDirectories(assembly.compilerOptions.ApiCompatibilityLevel);
- Dictionary<string, ResponseFileData> responseFilesData = assembly.compilerOptions.ResponseFiles.ToDictionary(x => x, x => m_AssemblyNameProvider.ParseResponseFile(
- x,
- ProjectDirectory,
- systemReferenceDirectories
- ));
- Dictionary<string, ResponseFileData> responseFilesWithErrors = responseFilesData.Where(x => x.Value.Errors.Any())
- .ToDictionary(x => x.Key, x => x.Value);
- if (responseFilesWithErrors.Any())
- {
- foreach (var error in responseFilesWithErrors)
- foreach (var valueError in error.Value.Errors)
- {
- Debug.LogError($"{error.Key} Parse Error : {valueError}");
- }
- }
- return responseFilesData.Select(x => x.Value).ToList();
- }
- Dictionary<string, string> GenerateAllAssetProjectParts()
- {
- Dictionary<string, StringBuilder> stringBuilders = new Dictionary<string, StringBuilder>();
- foreach (string asset in m_AssemblyNameProvider.GetAllAssetPaths())
- {
-
-
- if (m_AssemblyNameProvider.IsInternalizedPackagePath(asset))
- {
- continue;
- }
- string extension = Path.GetExtension(asset);
- if (IsSupportedExtension(extension) && ScriptingLanguage.None == ScriptingLanguageFor(extension))
- {
-
- var assemblyName = m_AssemblyNameProvider.GetAssemblyNameFromScriptPath(asset);
- if (string.IsNullOrEmpty(assemblyName))
- {
- continue;
- }
- assemblyName = Path.GetFileNameWithoutExtension(assemblyName);
- if (!stringBuilders.TryGetValue(assemblyName, out var projectBuilder))
- {
- projectBuilder = new StringBuilder();
- stringBuilders[assemblyName] = projectBuilder;
- }
- projectBuilder.Append(" <None Include=\"").Append(EscapedRelativePathFor(asset)).Append("\" />").Append(k_WindowsNewline);
- }
- }
- var result = new Dictionary<string, string>();
- foreach (var entry in stringBuilders)
- result[entry.Key] = entry.Value.ToString();
- return result;
- }
- void SyncProject(
- Assembly assembly,
- Dictionary<string, string> allAssetsProjectParts,
- List<ResponseFileData> responseFilesData,
- HashSet<string> assemblyNames)
- {
- SyncProjectFileIfNotChanged(ProjectFile(assembly), ProjectText(assembly, allAssetsProjectParts, responseFilesData, assemblyNames, GetAllRoslynAnalyzerPaths().ToArray()));
- }
- private IEnumerable<string> GetAllRoslynAnalyzerPaths()
- {
- return m_AssemblyNameProvider.GetRoslynAnalyzerPaths();
- }
- void SyncProjectFileIfNotChanged(string path, string newContents)
- {
- SyncFileIfNotChanged(path, newContents);
- }
- void SyncSolutionFileIfNotChanged(string path, string newContents)
- {
- SyncFileIfNotChanged(path, newContents);
- }
- void SyncFileIfNotChanged(string filename, string newContents)
- {
- if (m_FileIOProvider.Exists(filename))
- {
- var currentContents = m_FileIOProvider.ReadAllText(filename);
- if (currentContents == newContents)
- {
- return;
- }
- }
- m_FileIOProvider.WriteAllText(filename, newContents);
- }
- string ProjectText(
- Assembly assembly,
- Dictionary<string, string> allAssetsProjectParts,
- List<ResponseFileData> responseFilesData,
- HashSet<string> assemblyNames,
- string[] roslynAnalyzerDllPaths)
- {
- var projectBuilder = new StringBuilder();
- ProjectHeader(assembly, responseFilesData, roslynAnalyzerDllPaths, projectBuilder);
- var references = new List<string>();
- foreach (string file in assembly.sourceFiles)
- {
- if (!HasValidExtension(file))
- continue;
- var extension = Path.GetExtension(file).ToLower();
- var fullFile = EscapedRelativePathFor(file);
- if (".dll" != extension)
- {
- projectBuilder.Append(" <Compile Include=\"").Append(fullFile).Append("\" />").Append(k_WindowsNewline);
- }
- else
- {
- references.Add(fullFile);
- }
- }
-
- if (allAssetsProjectParts.TryGetValue(assembly.name, out var additionalAssetsForProject))
- projectBuilder.Append(additionalAssetsForProject);
- var responseRefs = responseFilesData.SelectMany(x => x.FullPathReferences.Select(r => r));
- var internalAssemblyReferences = assembly.assemblyReferences
- .Where(i => !i.sourceFiles.Any(ShouldFileBePartOfSolution)).Select(i => i.outputPath);
- var allReferences =
- assembly.compiledAssemblyReferences
- .Union(responseRefs)
- .Union(references)
- .Union(internalAssemblyReferences)
- .Except(roslynAnalyzerDllPaths);
- foreach (var reference in allReferences)
- {
- string fullReference = Path.IsPathRooted(reference) ? reference : Path.Combine(ProjectDirectory, reference);
- AppendReference(fullReference, projectBuilder);
- }
- if (0 < assembly.assemblyReferences.Length)
- {
- projectBuilder.Append(" </ItemGroup>").Append(k_WindowsNewline);
- projectBuilder.Append(" <ItemGroup>").Append(k_WindowsNewline);
- foreach (Assembly reference in assembly.assemblyReferences.Where(i => i.sourceFiles.Any(ShouldFileBePartOfSolution)))
- {
- var referencedProject = reference.outputPath;
- projectBuilder.Append(" <ProjectReference Include=\"").Append(reference.name).Append(GetProjectExtension()).Append("\">").Append(k_WindowsNewline);
- projectBuilder.Append(" <Project>{").Append(ProjectGuid(reference.name)).Append("}</Project>").Append(k_WindowsNewline);
- projectBuilder.Append(" <Name>").Append(reference.name).Append("</Name>").Append(k_WindowsNewline);
- projectBuilder.Append(" <ReferenceOutputAssembly>false</ReferenceOutputAssembly>").Append(k_WindowsNewline);
- projectBuilder.Append(" </ProjectReference>").Append(k_WindowsNewline);
- }
- }
- projectBuilder.Append(ProjectFooter());
- return projectBuilder.ToString();
- }
- static void AppendReference(string fullReference, StringBuilder projectBuilder)
- {
-
- var escapedFullPath = SecurityElement.Escape(fullReference);
- escapedFullPath = escapedFullPath.Replace("\\\\", "/");
- escapedFullPath = escapedFullPath.Replace("\\", "/");
- projectBuilder.Append(" <Reference Include=\"").Append(Path.GetFileNameWithoutExtension(escapedFullPath)).Append("\">").Append(k_WindowsNewline);
- projectBuilder.Append(" <HintPath>").Append(escapedFullPath).Append("</HintPath>").Append(k_WindowsNewline);
- projectBuilder.Append(" </Reference>").Append(k_WindowsNewline);
- }
- public string ProjectFile(Assembly assembly)
- {
- var fileBuilder = new StringBuilder(assembly.name);
- fileBuilder.Append(".csproj");
- return Path.Combine(ProjectDirectory, fileBuilder.ToString());
- }
- public string SolutionFile()
- {
- return Path.Combine(ProjectDirectory, $"{m_ProjectName}.sln");
- }
- void ProjectHeader(
- Assembly assembly,
- List<ResponseFileData> responseFilesData,
- string[] roslynAnalyzerDllPaths,
- StringBuilder builder
- )
- {
- var otherArguments = GetOtherArgumentsFromResponseFilesData(responseFilesData);
- GetProjectHeaderTemplate(
- builder,
- ProjectGuid(assembly.name),
- assembly.name,
- string.Join(";", new[] { "DEBUG", "TRACE" }.Concat(assembly.defines).Concat(responseFilesData.SelectMany(x => x.Defines)).Concat(EditorUserBuildSettings.activeScriptCompilationDefines).Distinct().ToArray()),
- assembly.compilerOptions.AllowUnsafeCode | responseFilesData.Any(x => x.Unsafe),
- GenerateAnalyserItemGroup(otherArguments["analyzer"].Concat(otherArguments["a"])
- .SelectMany(x => x.Split(';'))
- .Concat(roslynAnalyzerDllPaths)
- .Distinct()
- .ToArray()));
- }
- private static ILookup<string, string> GetOtherArgumentsFromResponseFilesData(List<ResponseFileData> responseFilesData)
- {
- var paths = responseFilesData.SelectMany(x =>
- {
- return x.OtherArguments.Where(a => a.StartsWith("/") || a.StartsWith("-"))
- .Select(b =>
- {
- var index = b.IndexOf(":", StringComparison.Ordinal);
- if (index > 0 && b.Length > index)
- {
- var key = b.Substring(1, index - 1);
- return new KeyValuePair<string, string>(key, b.Substring(index + 1));
- }
- const string warnaserror = "warnaserror";
- if (b.Substring(1).StartsWith(warnaserror))
- {
- return new KeyValuePair<string, string>(warnaserror, b.Substring(warnaserror.Length + 1));
- }
- return default;
- });
- })
- .Distinct()
- .ToLookup(o => o.Key, pair => pair.Value);
- return paths;
- }
- private static string GenerateAnalyserItemGroup(string[] paths)
- {
-
-
-
-
- if (!paths.Any())
- return string.Empty;
- var analyserBuilder = new StringBuilder();
- analyserBuilder.Append(" <ItemGroup>").Append(k_WindowsNewline);
- foreach (var path in paths)
- {
- analyserBuilder.Append($" <Analyzer Include=\"{path}\" />").Append(k_WindowsNewline);
- }
- analyserBuilder.Append(" </ItemGroup>").Append(k_WindowsNewline);
- return analyserBuilder.ToString();
- }
- static string GetSolutionText()
- {
- return string.Join("\r\n", @"", @"Microsoft Visual Studio Solution File, Format Version {0}", @"# Visual Studio {1}", @"{2}", @"Global", @" GlobalSection(SolutionConfigurationPlatforms) = preSolution", @" Debug|Any CPU = Debug|Any CPU", @" EndGlobalSection", @" GlobalSection(ProjectConfigurationPlatforms) = postSolution", @"{3}", @" EndGlobalSection", @" GlobalSection(SolutionProperties) = preSolution", @" HideSolutionNode = FALSE", @" EndGlobalSection", @"EndGlobal", @"").Replace(" ", "\t");
- }
- static string GetProjectFooterTemplate()
- {
- return string.Join("\r\n", @" </ItemGroup>", @" <Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />", @" <!-- To modify your build process, add your task inside one of the targets below and uncomment it.", @" Other similar extension points exist, see Microsoft.Common.targets.", @" <Target Name=""BeforeBuild"">", @" </Target>", @" <Target Name=""AfterBuild"">", @" </Target>", @" -->", @"</Project>", @"");
- }
- static void GetProjectHeaderTemplate(
- StringBuilder builder,
- string assemblyGUID,
- string assemblyName,
- string defines,
- bool allowUnsafe,
- string analyzerBlock
- )
- {
- builder.Append(@"<?xml version=""1.0"" encoding=""utf-8""?>").Append(k_WindowsNewline);
- builder.Append(@"<Project ToolsVersion=""").Append(k_ToolsVersion).Append(@""" DefaultTargets=""Build"" xmlns=""").Append(MSBuildNamespaceUri).Append(@""">").Append(k_WindowsNewline);
- builder.Append(@" <PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(@" <LangVersion>").Append(k_TargetLanguageVersion).Append("</LangVersion>").Append(k_WindowsNewline);
- builder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(@" <PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(@" <Configuration Condition="" '$(Configuration)' == '' "">Debug</Configuration>").Append(k_WindowsNewline);
- builder.Append(@" <Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>").Append(k_WindowsNewline);
- builder.Append(@" <ProductVersion>").Append(k_ProductVersion).Append("</ProductVersion>").Append(k_WindowsNewline);
- builder.Append(@" <SchemaVersion>2.0</SchemaVersion>").Append(k_WindowsNewline);
- builder.Append(@" <RootNamespace>").Append(EditorSettings.projectGenerationRootNamespace).Append("</RootNamespace>").Append(k_WindowsNewline);
- builder.Append(@" <ProjectGuid>{").Append(assemblyGUID).Append("}</ProjectGuid>").Append(k_WindowsNewline);
- builder.Append(@" <OutputType>Library</OutputType>").Append(k_WindowsNewline);
- builder.Append(@" <AppDesignerFolder>Properties</AppDesignerFolder>").Append(k_WindowsNewline);
- builder.Append(@" <AssemblyName>").Append(assemblyName).Append("</AssemblyName>").Append(k_WindowsNewline);
- builder.Append(@" <TargetFrameworkVersion>").Append(k_TargetFrameworkVersion).Append("</TargetFrameworkVersion>").Append(k_WindowsNewline);
- builder.Append(@" <FileAlignment>512</FileAlignment>").Append(k_WindowsNewline);
- builder.Append(@" <BaseDirectory>").Append(k_BaseDirectory).Append("</BaseDirectory>").Append(k_WindowsNewline);
- builder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(@" <PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "">").Append(k_WindowsNewline);
- builder.Append(@" <DebugSymbols>true</DebugSymbols>").Append(k_WindowsNewline);
- builder.Append(@" <DebugType>full</DebugType>").Append(k_WindowsNewline);
- builder.Append(@" <Optimize>false</Optimize>").Append(k_WindowsNewline);
- builder.Append(@" <OutputPath>Temp\bin\Debug\</OutputPath>").Append(k_WindowsNewline);
- builder.Append(@" <DefineConstants>").Append(defines).Append("</DefineConstants>").Append(k_WindowsNewline);
- builder.Append(@" <ErrorReport>prompt</ErrorReport>").Append(k_WindowsNewline);
- builder.Append(@" <WarningLevel>4</WarningLevel>").Append(k_WindowsNewline);
- builder.Append(@" <NoWarn>0169</NoWarn>").Append(k_WindowsNewline);
- builder.Append(@" <AllowUnsafeBlocks>").Append(allowUnsafe).Append("</AllowUnsafeBlocks>").Append(k_WindowsNewline);
- builder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(@" <PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(@" <NoConfig>true</NoConfig>").Append(k_WindowsNewline);
- builder.Append(@" <NoStdLib>true</NoStdLib>").Append(k_WindowsNewline);
- builder.Append(@" <AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>").Append(k_WindowsNewline);
- builder.Append(@" <ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>").Append(k_WindowsNewline);
- builder.Append(@" <ImplicitlyExpandDesignTimeFacades>false</ImplicitlyExpandDesignTimeFacades>").Append(k_WindowsNewline);
- builder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
- builder.Append(analyzerBlock);
- builder.Append(@" <ItemGroup>").Append(k_WindowsNewline);
- }
- void SyncSolution(IEnumerable<Assembly> assemblies)
- {
- SyncSolutionFileIfNotChanged(SolutionFile(), SolutionText(assemblies));
- }
- string SolutionText(IEnumerable<Assembly> assemblies)
- {
- var fileversion = "11.00";
- var vsversion = "2010";
- var relevantAssemblies = RelevantAssembliesForMode(assemblies);
- string projectEntries = GetProjectEntries(relevantAssemblies);
- string projectConfigurations = string.Join(k_WindowsNewline, relevantAssemblies.Select(i => GetProjectActiveConfigurations(ProjectGuid(i.name))).ToArray());
- return string.Format(GetSolutionText(), fileversion, vsversion, projectEntries, projectConfigurations);
- }
- static IEnumerable<Assembly> RelevantAssembliesForMode(IEnumerable<Assembly> assemblies)
- {
- return assemblies.Where(i => ScriptingLanguage.CSharp == ScriptingLanguageFor(i));
- }
-
-
-
-
- string GetProjectEntries(IEnumerable<Assembly> assemblies)
- {
- var projectEntries = assemblies.Select(i => string.Format(
- m_SolutionProjectEntryTemplate,
- SolutionGuid(i),
- i.name,
- Path.GetFileName(ProjectFile(i)),
- ProjectGuid(i.name)
- ));
- return string.Join(k_WindowsNewline, projectEntries.ToArray());
- }
-
-
-
- string GetProjectActiveConfigurations(string projectGuid)
- {
- return string.Format(
- m_SolutionProjectConfigurationTemplate,
- projectGuid);
- }
- string EscapedRelativePathFor(string file)
- {
- var projectDir = ProjectDirectory.Replace('/', '\\');
- file = file.Replace('/', '\\');
- var path = SkipPathPrefix(file, projectDir);
- var packageInfo = m_AssemblyNameProvider.FindForAssetPath(path.Replace('\\', '/'));
- if (packageInfo != null)
- {
-
-
- var absolutePath = Path.GetFullPath(NormalizePath(path)).Replace('/', '\\');
- path = SkipPathPrefix(absolutePath, projectDir);
- }
- return SecurityElement.Escape(path);
- }
- static string SkipPathPrefix(string path, string prefix)
- {
- if (path.StartsWith($@"{prefix}\"))
- return path.Substring(prefix.Length + 1);
- return path;
- }
- static string NormalizePath(string path)
- {
- if (Path.DirectorySeparatorChar == '\\')
- return path.Replace('/', Path.DirectorySeparatorChar);
- return path.Replace('\\', Path.DirectorySeparatorChar);
- }
- string ProjectGuid(string assembly)
- {
- return m_GUIDProvider.ProjectGuid(m_ProjectName, assembly);
- }
- string SolutionGuid(Assembly assembly)
- {
- return m_GUIDProvider.SolutionGuid(m_ProjectName, GetExtensionOfSourceFiles(assembly.sourceFiles));
- }
- static string ProjectFooter()
- {
- return GetProjectFooterTemplate();
- }
- static string GetProjectExtension()
- {
- return ".csproj";
- }
- void WriteVSCodeSettingsFiles()
- {
- var vsCodeDirectory = Path.Combine(ProjectDirectory, ".vscode");
- if (!m_FileIOProvider.Exists(vsCodeDirectory))
- m_FileIOProvider.CreateDirectory(vsCodeDirectory);
- var vsCodeSettingsJson = Path.Combine(vsCodeDirectory, "settings.json");
- if (!m_FileIOProvider.Exists(vsCodeSettingsJson))
- m_FileIOProvider.WriteAllText(vsCodeSettingsJson, k_SettingsJson);
- }
- }
- public static class SolutionGuidGenerator
- {
- static MD5 mD5 = MD5CryptoServiceProvider.Create();
- public static string GuidForProject(string projectName)
- {
- return ComputeGuidHashFor(projectName + "salt");
- }
- public static string GuidForSolution(string projectName, string sourceFileExtension)
- {
- if (sourceFileExtension.ToLower() == "cs")
-
- return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
- return ComputeGuidHashFor(projectName);
- }
- static string ComputeGuidHashFor(string input)
- {
- var hash = mD5.ComputeHash(Encoding.Default.GetBytes(input));
- return new Guid(hash).ToString();
- }
- }
- }
|