Ejemplo n.º 1
0
        public static LogKeyValueItem ScaffoldProjFile(
            FileInfo projectCsProjFile,
            bool createAsWeb,
            bool createAsTestProject,
            string projectName,
            bool useNullableReferenceTypes,
            List<string>? frameworkReferences,
            List<Tuple<string, string, string?>>? packageReferences,
            List<FileInfo>? projectReferences,
            bool includeApiSpecification)
        {
            if (projectCsProjFile == null)
            {
                throw new ArgumentNullException(nameof(projectCsProjFile));
            }

            var sb = new StringBuilder();
            sb.AppendLine(createAsWeb
                ? "<Project Sdk=\"Microsoft.NET.Sdk.Web\">"
                : "<Project Sdk=\"Microsoft.NET.Sdk\">");
            sb.AppendLine();
            sb.AppendLine(2, "<PropertyGroup>");
            sb.AppendLine(4, "<TargetFramework>netcoreapp3.1</TargetFramework>");
            if (!createAsTestProject)
            {
                sb.AppendLine(4, "<IsPackable>false</IsPackable>");
            }

            sb.AppendLine(2, "</PropertyGroup>");
            sb.AppendLine();
            if (!createAsTestProject)
            {
                sb.AppendLine(2, "<PropertyGroup>");
                if (useNullableReferenceTypes)
                {
                    sb.AppendLine(4, "<Nullable>enable</Nullable>");
                }

                sb.AppendLine(4, "<LangVersion>8.0</LangVersion>");
                sb.AppendLine(2, "</PropertyGroup>");
                sb.AppendLine();
                sb.AppendLine(2, "<PropertyGroup>");
                sb.AppendLine(4, "<GenerateDocumentationFile>true</GenerateDocumentationFile>");
                sb.AppendLine(2, "</PropertyGroup>");
                sb.AppendLine();
                sb.AppendLine(2, "<PropertyGroup>");
                sb.AppendLine(4, $"<DocumentationFile>bin\\Debug\\netcoreapp3.1\\{projectName}.xml</DocumentationFile>");
                sb.AppendLine(4, "<NoWarn>1573;1591;1701;1702;1712;8618</NoWarn>");
                sb.AppendLine(2, "</PropertyGroup>");
                sb.AppendLine();

                if (includeApiSpecification)
                {
                    sb.AppendLine(2, "<ItemGroup>");
                    sb.AppendLine(4, "<None Remove=\"Resources\\ApiSpecification.yaml\" />");
                    sb.AppendLine(4, "<EmbeddedResource Include=\"Resources\\ApiSpecification.yaml\" />");
                    sb.AppendLine(2, "</ItemGroup>");
                    sb.AppendLine();
                }
            }

            if (frameworkReferences != null && frameworkReferences.Count > 0)
            {
                sb.AppendLine(2, "<ItemGroup>");
                foreach (var frameworkReference in frameworkReferences.OrderBy(x => x))
                {
                    sb.AppendLine(4, $"<FrameworkReference Include=\"{frameworkReference}\" />");
                }

                sb.AppendLine(2, "</ItemGroup>");
                sb.AppendLine();
            }

            if (packageReferences != null && packageReferences.Count > 0)
            {
                sb.AppendLine(2, "<ItemGroup>");
                foreach (var (package, version, extra) in packageReferences.OrderBy(x => x.Item1))
                {
                    if (extra == null)
                    {
                        sb.AppendLine(4, $"<PackageReference Include=\"{package}\" Version=\"{version}\" />");
                    }
                    else
                    {
                        sb.AppendLine(4, $"<PackageReference Include=\"{package}\" Version=\"{version}\">");
                        var sa = extra.Split('\n');
                        foreach (var s in sa)
                        {
                            sb.AppendLine(6, $"{s}");
                        }

                        sb.AppendLine(4, "</PackageReference>");
                    }
                }

                sb.AppendLine(2, "</ItemGroup>");
                sb.AppendLine();
            }

            if (projectReferences != null && projectReferences.Count > 0)
            {
                sb.AppendLine(2, "<ItemGroup>");
                foreach (var projectReference in projectReferences.OrderBy(x => x.Name))
                {
                    var packageReferenceValue = GetProjectReference(projectCsProjFile, projectReference);
                    sb.AppendLine(4, $"<ProjectReference Include=\"{packageReferenceValue}\" />");
                }

                sb.AppendLine(2, "</ItemGroup>");
                sb.AppendLine();
            }

            sb.AppendLine("</Project>");
            return TextFileHelper.Save(projectCsProjFile, sb.ToString());
        }
Ejemplo n.º 2
0
        public static List<LogKeyValueItem> ScaffoldSlnFile(
            FileInfo slnFile,
            string projectName,
            DirectoryInfo apiPath,
            DirectoryInfo domainPath,
            DirectoryInfo hostPath,
            DirectoryInfo? apiTestPath = null,
            DirectoryInfo? domainTestPath = null,
            DirectoryInfo? hostTestPath = null)
        {
            if (slnFile == null)
            {
                throw new ArgumentNullException(nameof(slnFile));
            }

            if (apiPath == null)
            {
                throw new ArgumentNullException(nameof(apiPath));
            }

            if (domainPath == null)
            {
                throw new ArgumentNullException(nameof(domainPath));
            }

            if (hostPath == null)
            {
                throw new ArgumentNullException(nameof(hostPath));
            }

            var slnId = Guid.NewGuid();
            var apiId = Guid.NewGuid();
            var domainId = Guid.NewGuid();
            var hostId = Guid.NewGuid();
            var apiTestId = Guid.NewGuid();
            var domainTestId = Guid.NewGuid();
            var hostTestId = Guid.NewGuid();

            var apiPrefixPath = GetProjectReference(slnFile, apiPath, projectName);
            var domainPrefixPath = GetProjectReference(slnFile, domainPath, projectName);
            var hostPrefixPath = GetProjectReference(slnFile, hostPath, projectName);

            var codeInspectionExcludeProjects = new List<Guid>();
            var codeInspectionExcludeProjectsFolders = new List<Tuple<Guid, DirectoryInfo, List<DirectoryInfo>>>();
            if (slnFile.Exists)
            {
                var lines = File.ReadAllLines(slnFile.FullName);
                if (TryGetGuidByProject(lines, "Api.Generated.csproj", out Guid idApiGenerated))
                {
                    codeInspectionExcludeProjects.Add(idApiGenerated);
                }

                if (TryGetGuidByProject(lines, "Api.Generated.Tests.csproj", out Guid idApiGeneratedTest))
                {
                    codeInspectionExcludeProjects.Add(idApiGeneratedTest);
                }

                if (hostTestPath != null && TryGetGuidByProject(lines, "Api.Tests.csproj", out Guid idHostTest))
                {
                    var hostTestDirectory = new DirectoryInfo(hostTestPath.FullName + ".Tests");
                    var generatedDirectories = hostTestDirectory.GetDirectories("Generated", SearchOption.AllDirectories).ToList();
                    codeInspectionExcludeProjectsFolders.Add(new Tuple<Guid, DirectoryInfo, List<DirectoryInfo>>(idHostTest, hostTestDirectory, generatedDirectories));
                }

                if (domainTestPath != null && TryGetGuidByProject(lines, "Domain.Tests.csproj", out Guid idDomainTest))
                {
                    var domainTestDirectory = new DirectoryInfo(domainTestPath.FullName + ".Tests");
                    var generatedDirectories = domainTestDirectory.GetDirectories("Generated", SearchOption.AllDirectories).ToList();
                    codeInspectionExcludeProjectsFolders.Add(new Tuple<Guid, DirectoryInfo, List<DirectoryInfo>>(idDomainTest, domainTestDirectory, generatedDirectories));
                }
            }
            else
            {
                codeInspectionExcludeProjects.Add(apiId);
                if (apiTestPath != null)
                {
                    codeInspectionExcludeProjects.Add(apiTestId);
                }

                if (hostTestPath != null)
                {
                    var hostTestDirectory = new DirectoryInfo(hostTestPath.FullName + ".Tests");
                    var generatedDirectories = hostTestDirectory.GetDirectories("Generated", SearchOption.AllDirectories).ToList();
                    codeInspectionExcludeProjectsFolders.Add(new Tuple<Guid, DirectoryInfo, List<DirectoryInfo>>(hostTestId, hostTestDirectory, generatedDirectories));
                }

                if (domainTestPath != null)
                {
                    var domainTestDirectory = new DirectoryInfo(domainTestPath.FullName + ".Tests");
                    var generatedDirectories = domainTestDirectory.GetDirectories("Generated", SearchOption.AllDirectories).ToList();
                    codeInspectionExcludeProjectsFolders.Add(new Tuple<Guid, DirectoryInfo, List<DirectoryInfo>>(domainTestId, domainTestDirectory, generatedDirectories));
                }
            }

            var slnFileContent = CreateSlnFileContent(
                slnFile,
                projectName,
                apiTestPath,
                domainTestPath,
                hostTestPath,
                apiPrefixPath,
                apiId,
                domainPrefixPath,
                domainId,
                hostPrefixPath,
                hostId,
                hostTestId,
                apiTestId,
                domainTestId,
                slnId);

            var slnDotSettingsFile = new FileInfo(slnFile + ".DotSettings");
            var slnDotSettingsFileContent = CreateSlnDotSettingsFileContent(
                codeInspectionExcludeProjects,
                codeInspectionExcludeProjectsFolders);

            bool slnDotSettingsFileOverrideIfExist = true;
            if (slnDotSettingsFile.Exists)
            {
                var lines = File.ReadAllLines(slnDotSettingsFile.FullName);
                if (lines.Any(line => !line.Contains("ResourceDictionary", StringComparison.Ordinal) &&
                                      !line.Contains("/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2", StringComparison.Ordinal)))
                {
                    slnDotSettingsFileOverrideIfExist = false;
                }
            }

            var logItems = new List<LogKeyValueItem>
            {
                TextFileHelper.Save(slnFile, slnFileContent, false),
                TextFileHelper.Save(slnDotSettingsFile, slnDotSettingsFileContent, slnDotSettingsFileOverrideIfExist),
            };

            return logItems;
        }