/// <summary>
        /// Note that WriteOnce.Writer is not used for AnalyzeCommand which has specific Writer classes for formattig to html, json, text unlike
        /// other command classes to writting to a file here is handled by the AnalyzeCommand FlushAll() method
        /// </summary>
        void ConfigFileOutput()
        {
            WriteOnce.SafeLog("AnalyzeCommand::ConfigOutput", LogLevel.Trace);

            WriteOnce.FlushAll();//in case called more than once

            //error check for no output
            if (string.IsNullOrEmpty(_arg_outputFile) && _arg_consoleVerbosityLevel.ToLower() == "none")
            {
                throw new Exception(ErrMsg.GetString(ErrMsg.ID.CMD_NO_OUTPUT));
            }

            if (_arg_fileFormat == "html")
            {
                if (!string.IsNullOrEmpty(_arg_outputFile)) //dependent local files won't be there; TODO look into dir copy to target!
                {
                    _arg_outputFile = String.Empty;
                    WriteOnce.Info("output file argument ignored for html format");
                }

                if (!_arg_outputUniqueTagsOnly) //fix #183
                {
                    throw new Exception(ErrMsg.GetString(ErrMsg.ID.ANALYZE_NODUPLICATES_HTML_FORMAT));
                }

                if (_arg_simpleTagsOnly) //won't work for html that expects full data
                {
                    throw new Exception(ErrMsg.GetString(ErrMsg.ID.ANALYZE_SIMPLETAGS_HTML_FORMAT));
                }
            }

            //Set outstream
            _outputWriter = WriterFactory.GetWriter(_arg_fileFormat ?? "text", (string.IsNullOrEmpty(_arg_outputFile)) ? null : "text", _arg_outputTextFormat);
            if (!string.IsNullOrEmpty(_arg_outputFile))
            {
                _outputWriter.TextWriter = File.CreateText(_arg_outputFile);//not needed if html output since application controlled
            }
            else
            {
                _outputWriter.TextWriter = Console.Out;
            }
        }
Example #2
0
        void ConfigOutput()
        {
            WriteOnce.SafeLog("AnalyzeCommand::ConfigOutput", LogLevel.Trace);

            //Set output type, format and outstream
            _outputWriter = WriterFactory.GetWriter(_arg_fileFormat ?? "text", (string.IsNullOrEmpty(_arg_outputFile)) ? null : "text", _arg_outputTextFormat);
            if (_arg_fileFormat == "html")
            {
                if (!string.IsNullOrEmpty(_arg_outputFile))
                {
                    WriteOnce.Info("output file ignored for html format");
                }
                _outputWriter.TextWriter = Console.Out;
            }
            else if (!string.IsNullOrEmpty(_arg_outputFile))
            {
                _outputWriter.TextWriter = File.CreateText(_arg_outputFile);//not needed if html output since application controlled
            }
            else
            {
                _outputWriter.TextWriter = Console.Out;
            }
        }
        readonly int MAX_HTML_REPORT_FILE_SIZE = 1024 * 1000 * 3;  //warn about potential slow rendering

        /// <summary>
        /// Registers datatypes with html framework liquid and sets up data for use within it and used
        /// with html partial.liquid files that are embedded as resources
        /// Liquid processing within partial html files only requires data ref to object while JS requires json objects
        /// </summary>
        /// <param name="app"></param>
        public override void WriteApp(AppProfile app)
        {
            var htmlTemplateText = File.ReadAllText(Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "html/index.html"));

            Template.FileSystem = new EmbeddedFileSystem(Assembly.GetEntryAssembly(), "Microsoft.ApplicationInspector.CLI.html.partials");

            RegisterSafeType(typeof(AppProfile));
            RegisterSafeType(typeof(AppMetaData));

            var htmlTemplate = Template.Parse(htmlTemplateText);
            var data         = new Dictionary <string, object>();

            data["AppProfile"] = app;

            var hashData = new Hash();

            hashData["json"] = Newtonsoft.Json.JsonConvert.SerializeObject(data);//json serialization required for [js] access to objects
            hashData["application_version"] = Utils.GetVersionString();

            //add dynamic sets of groups of taginfo read from preferences for Profile page
            List <TagGroup> tagGroupList = app.GetCategoryTagGroups("profile");

            hashData["groups"] = tagGroupList;

            //add summary values for sorted tags lists of taginfo
            foreach (string outerKey in app.KeyedSortedTagInfoLists.Keys)
            {
                hashData.Add(outerKey, app.KeyedSortedTagInfoLists[outerKey]);
            }

            //add summary metadata lists
            hashData["cputargets"]   = app.MetaData.CPUTargets;
            hashData["apptypes"]     = app.MetaData.AppTypes;
            hashData["packagetypes"] = app.MetaData.PackageTypes;
            hashData["ostargets"]    = app.MetaData.OSTargets;
            hashData["outputs"]      = app.MetaData.Outputs;
            hashData["filetypes"]    = app.MetaData.FileExtensions;
            hashData["tagcounters"]  = app.MetaData.TagCountersUI;

            var    htmlResult         = htmlTemplate.Render(hashData);
            string htmlOutputFilePath = Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "output.html");

            File.WriteAllText(htmlOutputFilePath, htmlResult);

            //writes out json report for convenience and linking to from report page(s)
            String jsonReportPath = Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "output.json");
            Writer jsonWriter     = WriterFactory.GetWriter("json", jsonReportPath);

            jsonWriter.TextWriter = File.CreateText(jsonReportPath);
            jsonWriter.WriteApp(app);
            jsonWriter.FlushAndClose();

            //html report size warning
            string outputHTMLPath = Path.Combine(Utils.GetPath(Utils.AppPath.basePath), "output.html");

            if (File.Exists(outputHTMLPath) && new FileInfo(outputHTMLPath).Length > MAX_HTML_REPORT_FILE_SIZE)
            {
                WriteOnce.Info(ErrMsg.GetString(ErrMsg.ID.ANALYZE_REPORTSIZE_WARN));
            }

            if (!app.SuppressBrowserOpen)
            {
                Utils.OpenBrowser(htmlOutputFilePath);
            }
        }