Ejemplo n.º 1
0
        public static async Task <TemplateFile> CreateTemplateFile(FileInfo file, DirectoryInfo sourceRootDirectory, DirectoryInfo destinationRootDirectory, string projectNamespace, string solutionRootNamespace, string projectGuid)
        {
            var templateFile = new TemplateFile();

            var isMarkdown = file.Extension == ".md";
            var isHtml     = file.Extension == ".html";
            var isCs       = file.Extension == ".cs";
            var isCsProj   = file.Extension == ".csproj";

            templateFile.SourceLocation      = file.FullName;
            templateFile.DestinationLocation = templateFile.SourceLocation.Replace(sourceRootDirectory.FullName, destinationRootDirectory.FullName + "\\" + projectNamespace);

            var targetFileName = templateFile.SourceLocation.Replace(sourceRootDirectory.FullName, "").TrimStart('\\');

            templateFile.RelativePath = projectNamespace + "\\" + targetFileName;

            templateFile.ItemGroupEntry  = $"<None Include=\"{templateFile.RelativePath}\" />";
            templateFile.VsTemplateEntry = $"<ProjectItem ReplaceParameters=\"true\">{targetFileName}</ProjectItem>";

            templateFile.Content = string.Empty;

            templateFile.IsImage = _isImageRegex.Match(file.Extension).Success;

            if (!templateFile.IsImage)
            {
                using (var streamReader = file.OpenText())
                {
                    var line = await streamReader.ReadLineAsync().ConfigureAwait(false);

                    while (line != null)
                    {
                        if (isCsProj)
                        {
                            if (_projectGuidRegex.Match(line).Success)
                            {
                                line = "    <ProjectGuid>$guid1$</ProjectGuid>";
                            }
                            else if (_targetFrameworkVersionRegex.Match(line).Success)
                            {
                                line = "    <TargetFrameworkVersion>v$targetframeworkversion$</TargetFrameworkVersion>";
                            }
                            else if (_rootNamespaceRegex.Match(line).Success)
                            {
                                var match = _rootNamespaceRegex.Match(line);
                                var ns    = match.Groups[1].Value;
                                line = $"    <RootNamespace>{ns.Replace(solutionRootNamespace, "$ext_safeprojectname$")}</RootNamespace>";
                            }
                            else if (_assemblyNamepaceRegex.Match(line).Success)
                            {
                                var match = _assemblyNamepaceRegex.Match(line);
                                var ns    = match.Groups[1].Value;
                                line = $"    <AssemblyName>{ns.Replace(solutionRootNamespace, "$ext_safeprojectname$")}</AssemblyName>";
                            }
                            else if (_packagesRegex.Match(line).Success)
                            {
                                var match        = _packagesRegex.Match(line);
                                var relativePath = match.Groups[1].Value;
                                var rest         = match.Groups[2].Value;
                                line = $"{line.Substring(0, match.Groups[1].Index)}{relativePath}..\\{rest}";
                            }
                            else if (line.IndexOf(solutionRootNamespace) >= 0)
                            {
                                line = line.Replace(solutionRootNamespace, "$ext_safeprojectname$");
                            }
                        }

                        else if (isCs)
                        {
                            //These might need to be more robust
                            if (line.IndexOf("[assembly: Guid(") >= 0)
                            {
                                line = $"[assembly: Guid(\"$guid1$\")]";
                            }
                            else if (line.IndexOf("AssemblyCompany(\"\")") >= 0)
                            {
                                line = line.Replace("AssemblyCompany(\"\")", "AssemblyCompany(\"$ext_company$\")");
                            }
                            else if (line.IndexOf("AssemblyCopyright(\"Copyright ©  2018\")") >= 0)
                            {
                                line = line.Replace("AssemblyCopyright(\"Copyright ©  2018\")", "AssemblyCopyright(\"Copyright © $ext_company$ $ext_year$\")");
                            }
                        }

                        if (line.IndexOf(solutionRootNamespace) >= 0)
                        {
                            line = line.Replace(solutionRootNamespace, $"$ext_safeprojectname$");
                        }

                        templateFile.Content += line += "\r\n";
                        line = await streamReader.ReadLineAsync().ConfigureAwait(false);
                    }
                }
            }

            return(templateFile);
        }