Example #1
0
        private static void ProcessTemplate(string ns, Template template, string outputPath)
        {
            var files = Directory.GetFiles(template.Path, "*.*", SearchOption.AllDirectories);
            var sourceRoot = template.Path;
            var destRoot = outputPath;
            if (!sourceRoot.EndsWith("\\"))
                sourceRoot += "\\";
            if (!destRoot.EndsWith("\\"))
                destRoot += "\\";

            foreach (var file in files)
            {
                var info = new FileInfo(file);
                var sourcePath = file;
                var destPath = info.FullName.Replace(sourceRoot, destRoot);
                destPath = destPath.Replace(template.Replace, ns);

                var dirName = Path.GetDirectoryName(destPath);
                if (!Directory.Exists(dirName))
                    Directory.CreateDirectory(dirName);

                if (m_Replacable.Contains(info.Extension))
                {
                    var contents = File.ReadAllText(sourcePath);
                    contents = contents.Replace(template.Replace, ns);
                    File.WriteAllText(destPath, contents);
                }
                else
                {
                    File.Copy(sourcePath, destPath, true);
                }
            }
        }
        private async Task UpdateRootVsTemplateProject(Template template, IEnumerable <string> projectNamespaces, FileInfo destinationFile)
        {
            var vsTemplateFileInfo = new FileInfo(destinationFile.FullName.Replace(".csproj", ".vstemplate"));

            if (!vsTemplateFileInfo.Exists)
            {
                var vsTemplates = destinationFile.Directory.GetFiles("*.vstemplate");
                if (vsTemplates?.Count() == 1)
                {
                    vsTemplateFileInfo = vsTemplates[0];
                }
                else
                {
                    Console.WriteLine("Unable to determine which vs template file to update.");
                    return;
                }
            }

            var icon = destinationFile.Name.Replace(".csproj", ".ico");

            var templateText = string.Empty;

            templateText += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
                            "<VSTemplate Version=\"3.0.0\" Type=\"ProjectGroup\" xmlns=\"http://schemas.microsoft.com/developer/vstemplate/2005\" xmlns:sdk=\"http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010\">\r\n" +
                            "  <TemplateData>\r\n" +
                            "    <Name>Unit4 Wanda Chatbot</Name>\r\n" +
                            "    <Description>Unit4 Wanda Chatbot Template containing a default template for creating a Unit4 Chatbot.</Description>\r\n" +
                            $"    <Icon>{icon}</Icon>\r\n" +
                            "    <ProjectType>CSharp</ProjectType>\r\n" +
                            "    <RequiredFrameworkVersion>4.6.1</RequiredFrameworkVersion>\r\n" +
                            "    <SortOrder>1000</SortOrder>\r\n" +
                            "    <TemplateID>74f1f6dd-7858-4ef8-907c-f43724265abe</TemplateID>\r\n" +
                            "    <CreateNewFolder>false</CreateNewFolder>\r\n" +
                            "    <DefaultName>U4.Area.Chatbot</DefaultName>\r\n" +
                            "    <ProvideDefaultName>true</ProvideDefaultName>\r\n" +
                            "    <CreateInPlace>true</CreateInPlace>\r\n" +
                            "  </TemplateData>\r\n" +
                            "  <TemplateContent>\r\n" +
                            "    <ProjectCollection>\r\n";

            foreach (var projectNamespace in projectNamespaces)
            {
                templateText += $"      <ProjectTemplateLink ProjectName=\"{ projectNamespace.Replace(template.RootNamespace, "$safeprojectname$")}\" CopyParameters=\"true\">{projectNamespace}\\{projectNamespace}.vstemplate</ProjectTemplateLink>\r\n";
            }

            templateText += "    </ProjectCollection>\r\n" +
                            "    <CustomParameters>\r\n" +
                            "      <CustomParameter Name=\"$company$\" Value=\"Unit4\"/>\r\n" +
                            "      <CustomParameter Name=\"$year$\" Value=\"2018\"/>\r\n" +
                            "    </CustomParameters>\r\n" +
                            "  </TemplateContent>\r\n" +
                            "</VSTemplate>";

            vsTemplateFileInfo.Delete();

            using (var fileStream = vsTemplateFileInfo.OpenWrite())
            {
                using (var streamWriter = new StreamWriter(fileStream))
                {
                    await streamWriter.WriteAsync(templateText).ConfigureAwait(false);
                }
            }
        }