void AppendReference(string fullReference, StringBuilder projectBuilder)
        {
            var escapedFullPath = EscapedRelativePathFor(fullReference);

            projectBuilder.Append("    <Reference Include=\"").Append(FileUtility.FileNameWithoutExtension(escapedFullPath)).Append("\">").Append(k_WindowsNewline);
            projectBuilder.Append("      <HintPath>").Append(escapedFullPath).Append("</HintPath>").Append(k_WindowsNewline);
            projectBuilder.Append("    </Reference>").Append(k_WindowsNewline);
        }
        IEnumerable <SolutionProjectEntry> ToProjectEntries(IEnumerable <Assembly> islands)
        {
            foreach (var island in islands)
            {
                yield return new SolutionProjectEntry()
                       {
                           ProjectFactoryGuid = SolutionGuid(island),
                           Name        = FileUtility.FileNameWithoutExtension(island.outputPath),
                           FileName    = Path.GetFileName(ProjectFile(island)),
                           ProjectGuid = ProjectGuid(island.outputPath)
                       }
            }
            ;
        }

        /// <summary>
        /// Generate the active configuration string for a given project guid
        /// </summary>
        string GetProjectActiveConfigurations(string projectGuid)
        {
            return(string.Format(
                       m_SolutionProjectConfigurationTemplate,
                       projectGuid));
        }

        string EscapedRelativePathFor(string file)
        {
            var projectDir = FileUtility.Normalize(ProjectDirectory);

            file = FileUtility.Normalize(file);
            var path = SkipPathPrefix(file, projectDir);

            var packageInfo = m_AssemblyNameProvider.FindForAssetPath(path.Replace('\\', '/'));

            if (packageInfo != null)
            {
                // We have to normalize the path, because the PackageManagerRemapper assumes
                // dir seperators will be os specific.
                var absolutePath = Path.GetFullPath(FileUtility.Normalize(path));

                path = SkipPathPrefix(absolutePath, projectDir);
            }

            return(XmlFilename(path));
        }
        Dictionary <string, string> GenerateAllAssetProjectParts()
        {
            Dictionary <string, StringBuilder> stringBuilders = new Dictionary <string, StringBuilder>();

            foreach (string asset in m_AssemblyNameProvider.GetAllAssetPaths())
            {
                // Exclude files coming from packages except if they are internalized.
                if (!m_ShouldGenerateAll && IsInternalizedPackagePath(asset))
                {
                    continue;
                }

                if (IsSupportedFile(asset) && ScriptingLanguage.None == ScriptingLanguageFor(asset))
                {
                    // Find assembly the asset belongs to by adding script extension and using compilation pipeline.
                    var assemblyName = m_AssemblyNameProvider.GetAssemblyNameFromScriptPath(asset + ".cs");

                    if (string.IsNullOrEmpty(assemblyName))
                    {
                        continue;
                    }

                    assemblyName = FileUtility.FileNameWithoutExtension(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);
        }
        string ProjectHeader(
            Assembly island,
            IEnumerable <ResponseFileData> responseFilesData
            )
        {
            var          toolsVersion   = "4.0";
            var          productVersion = "10.0.20506";
            const string baseDirectory  = ".";

            var targetFrameworkVersion = "v4.7.1";
            var targetLanguageVersion  = "latest";

            var projectType = ProjectTypeOf(island.outputPath);

            var arguments = new object[]
            {
                toolsVersion, productVersion, ProjectGuid(island.outputPath),
                XmlFilename(FileUtility.Normalize(InternalEditorUtility.GetEngineAssemblyPath())),
                XmlFilename(FileUtility.Normalize(InternalEditorUtility.GetEditorAssemblyPath())),
                string.Join(";", new[] { "DEBUG", "TRACE" }.Concat(EditorUserBuildSettings.activeScriptCompilationDefines).Concat(island.defines).Concat(responseFilesData.SelectMany(x => x.Defines)).Distinct().ToArray()),
                MSBuildNamespaceUri,
                FileUtility.FileNameWithoutExtension(island.outputPath),
                EditorSettings.projectGenerationRootNamespace,
                targetFrameworkVersion,
                targetLanguageVersion,
                baseDirectory,
                island.compilerOptions.AllowUnsafeCode | responseFilesData.Any(x => x.Unsafe),
                // flavoring
                projectType + ":" + (int)projectType,
                EditorUserBuildSettings.activeBuildTarget + ":" + (int)EditorUserBuildSettings.activeBuildTarget,
                Application.unityVersion,
            };

            try
            {
                return(string.Format(GetProjectHeaderTemplate(), arguments));
            }
            catch (Exception)
            {
                throw new NotSupportedException("Failed creating c# project because the c# project header did not have the correct amount of arguments, which is " + arguments.Length);
            }
        }
 public string ProjectFile(Assembly assembly)
 {
     return(Path.Combine(ProjectDirectory, $"{FileUtility.FileNameWithoutExtension(assembly.outputPath)}.csproj"));
 }
        string ProjectText(Assembly assembly,
                           Dictionary <string, string> allAssetsProjectParts,
                           IEnumerable <ResponseFileData> responseFilesData,
                           List <Assembly> allProjectIslands)
        {
            var projectBuilder    = new StringBuilder(ProjectHeader(assembly, responseFilesData));
            var references        = new List <string>();
            var projectReferences = new List <Match>();

            projectBuilder.Append(@"  <ItemGroup>").Append(k_WindowsNewline);
            foreach (string file in assembly.sourceFiles)
            {
                if (!ShouldFileBePartOfSolution(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);
                }
            }
            projectBuilder.Append(@"  </ItemGroup>").Append(k_WindowsNewline);

            var assemblyName = FileUtility.FileNameWithoutExtension(assembly.outputPath);

            projectBuilder.Append(@"  <ItemGroup>").Append(k_WindowsNewline);
            // Append additional non-script files that should be included in project generation.
            if (allAssetsProjectParts.TryGetValue(assemblyName, out var additionalAssetsForProject))
            {
                projectBuilder.Append(additionalAssetsForProject);
            }

            var islandRefs = references.Union(assembly.allReferences);

            foreach (string reference in islandRefs)
            {
                if (reference.EndsWith("/UnityEditor.dll", StringComparison.Ordinal) ||
                    reference.EndsWith("/UnityEngine.dll", StringComparison.Ordinal) ||
                    reference.EndsWith("\\UnityEditor.dll", StringComparison.Ordinal) ||
                    reference.EndsWith("\\UnityEngine.dll", StringComparison.Ordinal))
                {
                    continue;
                }

                var match = k_ScriptReferenceExpression.Match(reference);
                if (match.Success)
                {
                    // assume csharp language
                    // Add a reference to a project except if it's a reference to a script assembly
                    // that we are not generating a project for. This will be the case for assemblies
                    // coming from .assembly.json files in non-internalized packages.
                    var dllName = match.Groups["dllname"].Value;
                    if (allProjectIslands.Any(i => Path.GetFileName(i.outputPath) == dllName))
                    {
                        projectReferences.Add(match);
                        continue;
                    }
                }

                string fullReference = Path.IsPathRooted(reference) ? reference : Path.Combine(ProjectDirectory, reference);

                AppendReference(fullReference, projectBuilder);
            }

            var responseRefs = responseFilesData.SelectMany(x => x.FullPathReferences.Select(r => r));

            foreach (var reference in responseRefs)
            {
                AppendReference(reference, projectBuilder);
            }
            projectBuilder.Append(@"  </ItemGroup>").Append(k_WindowsNewline);

            if (0 < projectReferences.Count)
            {
                projectBuilder.Append(@"  <ItemGroup>").Append(k_WindowsNewline);
                foreach (Match reference in projectReferences)
                {
                    var referencedProject = reference.Groups["project"].Value;

                    projectBuilder.Append("    <ProjectReference Include=\"").Append(referencedProject).Append(GetProjectExtension()).Append("\">").Append(k_WindowsNewline);
                    projectBuilder.Append("      <Project>{").Append(ProjectGuid(Path.Combine("Temp", reference.Groups["project"].Value + ".dll"))).Append("}</Project>").Append(k_WindowsNewline);
                    projectBuilder.Append("      <Name>").Append(referencedProject).Append("</Name>").Append(k_WindowsNewline);
                    projectBuilder.Append("    </ProjectReference>").Append(k_WindowsNewline);
                }
                projectBuilder.Append(@"  </ItemGroup>").Append(k_WindowsNewline);
            }

            projectBuilder.Append(ProjectFooter());
            return(projectBuilder.ToString());
        }
 string ProjectGuid(string assembly)
 {
     return(SolutionGuidGenerator.GuidForProject(m_ProjectName + FileUtility.FileNameWithoutExtension(assembly)));
 }