private void CreateTemplatePackFile(List <TemplatePack> templatePacks, string templateReportJsonPath, string resultsPath)
        {
            // write the results to a temp file, and then copy to the final destination
            var tempfilepath = Path.GetTempFileName();

            //var templatePacks = TemplatePack.CreateFromFile(templateReportJsonPath);
            using var writer = new StreamWriter(tempfilepath);
            writer.WriteLine("package name, version, has lib folder, packagetype,num-downloads");
            foreach (var tp in templatePacks)
            {
                var info = new TemplatePackReportInternalSummaryInfo(_remoteFile.CacheFolderpath, tp);
                var line = GetTemplatePackReportLineFor(info);
                Console.WriteLine(line);
                writer.WriteLine(line);
            }
            writer.Flush();
            writer.Close();

            // string resultsPath = optionAnalysisResultFilePath.HasValue() ? optionAnalysisResultFilePath.Value() : "template-pack-analysis.csv";
            Console.WriteLine($"writing analysis file to {resultsPath}");
            File.Copy(tempfilepath, resultsPath, true);

            // now create the template-details.csv file
            var allTemplates     = new List <Template>();
            var allTemplateInfos = new List <TemplateReportSummaryInfo>();

            foreach (var tp in templatePacks)
            {
                var extractFolderPath = Path.Combine(_remoteFile.CacheFolderpath, "extracted", ($"{tp.Package}.{tp.Version}.nupkg").ToLowerInvariant());
                var templates         = TemplatePack.GetTemplateFilesUnder(extractFolderPath);
                foreach (var template in templates)
                {
                    var templateObj = Template.CreateFromFile(template);
                    allTemplates.Add(templateObj);
                    allTemplateInfos.Add(new TemplateReportSummaryInfo {
                        Template = templateObj
                    });
                }
            }
        }
        public override void Setup(CommandLineApplication command)
        {
            base.Setup(command);

            var optionTemplateReportJsonPath = command.Option <string>(
                "-trp|--templateReportPath",
                "the path to the template-report.json file",
                CommandOptionType.SingleValue);

            optionTemplateReportJsonPath.IsRequired();

            //var optionAnalysisResultFilePath = command.Option<string>(
            //    "-arp|--analysisResultPath",
            //    "path to where the results will be written to",
            //    CommandOptionType.SingleValue);

            var optionOutputDir = command.Option <string>(
                "-od|--output-dir",
                "folder path where files will be written",
                CommandOptionType.SingleValue);

            OnExecute = () => {
                EnableVerboseOption = OptionVerbose.HasValue();

                var templateReportJsonPath = optionTemplateReportJsonPath.Value();
                if (!File.Exists(templateReportJsonPath))
                {
                    throw new FileNotFoundException($"template-report.json file not found at {templateReportJsonPath}");
                }

                var           templatePacks = TemplatePack.CreateFromFile(templateReportJsonPath);
                List <string> createdFiles  = new List <string>();

                string outdir = optionOutputDir.HasValue() ? optionOutputDir.Value() : Directory.GetCurrentDirectory();

                string templatePackFile = Path.Combine(outdir, "template-pack-analysis.csv");
                CreateTemplatePackFile(templatePacks, templateReportJsonPath, templatePackFile);
                createdFiles.Add(templatePackFile);

                // create the json file that contains all the templates
                var allTemplates     = new List <Template>();
                var allTemplateInfos = new List <TemplateReportSummaryInfo>();
                var allHostFiles     = new List <TemplateHostFile>();
                foreach (var tp in templatePacks)
                {
                    var extractFolderPath = Path.Combine(_remoteFile.CacheFolderpath, "extracted", ($"{tp.Package}.{tp.Version}.nupkg").ToLowerInvariant());

                    // populate the HostFiles property of the template pack
                    var templates = TemplatePack.GetTemplateFilesUnder(extractFolderPath);
                    foreach (var template in templates)
                    {
                        var templateObj = Template.CreateFromFile(template);
                        templateObj.TemplatePackId = tp.Package;
                        templateObj.InitHostFilesFrom(Path.GetDirectoryName(template), templateObj.TemplatePackId, templateObj.Name);

                        allTemplates.Add(templateObj);
                        allTemplateInfos.Add(new TemplateReportSummaryInfo {
                            Template = templateObj
                        });
                        if (templateObj.HostFiles != null && templateObj.HostFiles.Count > 0)
                        {
                            allHostFiles.AddRange(templateObj.HostFiles);
                        }
                    }
                }

                var allTemplatesJsonPath = Path.Combine(outdir, "template-all.json");
                CreateAllTemplatesJsonFile(allTemplates, allTemplatesJsonPath);
                createdFiles.Add(allTemplatesJsonPath);

                // create the template-details.csv file now
                var templateDetailsCsvPath = Path.Combine(outdir, "template-details.csv");
                CreateTemplateDetailsCsvFile(allTemplateInfos, templateDetailsCsvPath);
                createdFiles.Add(templateDetailsCsvPath);

                var hostFileDetailsCsvPath = Path.Combine(outdir, "template-host-files.csv");
                CreateHostFilesDetailsCsvFile(allHostFiles, hostFileDetailsCsvPath);
                createdFiles.Add(hostFileDetailsCsvPath);

                Console.WriteLine("Created files:");
                foreach (var cf in createdFiles)
                {
                    Console.WriteLine($"    {cf}");
                }

                return(1);
            };
        }
        private void CreateTemplatePackFile(List <TemplatePack> templatePacks, string templateReportJsonPath, string resultsPath)
        {
            // write the results to a temp file, and then copy to the final destination
            var tempfilepath = Path.GetTempFileName();

            //var templatePacks = TemplatePack.CreateFromFile(templateReportJsonPath);
            using var writer = new StreamWriter(tempfilepath);
            writer.WriteLine("package name, version, has lib folder, packagetype,num-downloads");

            // list of template packs that could not be initalized for some reason
            // they need to be removed from the list to prevent futher issues
            var tpToRemove = new List <TemplatePack>();

            foreach (var tp in templatePacks)
            {
                TemplatePackReportInternalSummaryInfo info = null;
                try {
                    info = new TemplatePackReportInternalSummaryInfo(_remoteFile.CacheFolderpath, tp);
                }
                catch (TemplateInitException tie) {
                    // TODO: Reporter should be used instead of writing directly to the console
                    Console.WriteLine($"ERROR: Unable to initalize template pack from '{tp.Package}', skipping this one. Error: {tie.ToString()}");
                    tpToRemove.Add(tp);
                    continue;
                }
                var line = GetTemplatePackReportLineFor(info);
                Console.WriteLine(line);
                writer.WriteLine(line);
            }
            writer.Flush();
            writer.Close();

            if (tpToRemove.Count > 0)
            {
                foreach (var tp in tpToRemove)
                {
                    templatePacks.Remove(tp);
                }
            }

            // string resultsPath = optionAnalysisResultFilePath.HasValue() ? optionAnalysisResultFilePath.Value() : "template-pack-analysis.csv";
            var dir = Path.GetDirectoryName(resultsPath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            Console.WriteLine($"writing analysis file to {resultsPath}");
            File.Copy(tempfilepath, resultsPath, true);

            // now create the template-details.csv file
            var allTemplates     = new List <Template>();
            var allTemplateInfos = new List <TemplateReportSummaryInfo>();

            foreach (var tp in templatePacks)
            {
                var extractFolderPath = Path.Combine(_remoteFile.CacheFolderpath, "extracted", ($"{tp.Package}.{tp.Version}.nupkg").ToLowerInvariant());
                var templates         = TemplatePack.GetTemplateFilesUnder(extractFolderPath);
                foreach (var template in templates)
                {
                    // TODO: null check is needed here
                    var templateObj = Template.CreateFromFile(template);
                    if (templateObj != null)
                    {
                        allTemplates.Add(templateObj);
                        allTemplateInfos.Add(new TemplateReportSummaryInfo {
                            Template = templateObj
                        });
                    }
                    else
                    {
                        Console.WriteLine($"unable to initalize template from path '{template.ToString()}'");
                    }
                }
            }
        }