Ejemplo n.º 1
0
        public void GenerateTemplate(TemplateOptions options, CancellationToken ct)
        {
            var projectTemplates = options.ProjectTemplates.ToList();

            _logger.Debug($"{nameof(GenerateTemplate)} started: {options} projectTemplates count: {projectTemplates.Count()}");

            var solutionTemplate = options.SolutionTemplate;
            var destFolder       = options.TargetFolder;

            ct.ThrowIfCancellationRequested();

            //Delete any .zip or .vstemplate files
            var destDirInfo   = new DirectoryInfo(destFolder);
            var filesToDelete = destDirInfo.GetFiles("*.zip").ToList();

            filesToDelete.AddRange(destDirInfo.GetFiles("*.vstemplate", SearchOption.AllDirectories).ToList());
            foreach (var fileToDelete in filesToDelete)
            {
                fileToDelete.Delete();
            }

            var multiTemplateFile = new FileInfo(options.TargetTemplatePath);

            if (string.IsNullOrWhiteSpace(solutionTemplate.IconImagePath))
            {
                solutionTemplate.IconImagePath = _defaultIconPath;
            }

            solutionTemplate.CopyTemplateIconsTo(destFolder);

            ct.ThrowIfCancellationRequested();

            var projectTemplatesList = projectTemplates.ToList();

            if (options.UseSolution)
            {
                _logger.Debug($"Creating {projectTemplatesList.Count} project templates...");
                //Create project templates
                foreach (var projectTemplate in projectTemplatesList)
                {
                    _templateGenerator.CreateProjectTemplate(projectTemplate, options.SolutionFolder, destFolder, true, ct);
                }
            }

            _logger.Debug($"Creating solution template...");
            _templateGenerator.CreateSolutionTemplate(multiTemplateFile.FullName, solutionTemplate, projectTemplatesList);

            _logger.Debug($"Creating zip file...");
            //Zip files
            var zipFileName = Path.Combine(destFolder, solutionTemplate.TemplateName + ".zip");

            FastZip zip         = new FastZip();
            var     tempZipFile = Path.GetTempFileName();

            zip.CreateZip(tempZipFile, destFolder, true, null);

            File.Move(tempZipFile, zipFileName);

            if (options.AutoImportToVS)
            {
                _logger.Debug($"Importing zip file to VS template folder...");
                var vsFolder = Path.Combine(FileExtensions.FindVSTemplateFolder(), solutionTemplate.LanguageTag.GetTemplateFolderNameByLanguage());
                if (!vsFolder.DirectoryExists())
                {
                    vsFolder = FileExtensions.FindVSTemplateFolder();
                }

                File.Copy(zipFileName, Path.Combine(vsFolder, Path.GetFileName(zipFileName)), true);
            }

            _logger.Information("Generating templates completed");
        }