예제 #1
0
        public ISubCommand Create()
        {
            var args = _args;

            if (args.Length > 0)
            {
                // Drawback: do not support such case: docfx --force <subcommands>
                var subCommandName = args[0];
                ISubCommandCreator command;
                if (TryGetCommandCreator(subCommandName, out command))
                {
                    var subArgs = args.Skip(1).ToArray();
                    return(command.Create(subArgs, this, SubCommandParseOption.Strict));
                }
            }

            // TODO: comment: also handle log and loglevel like in build command
            // WONT FIX: log and loglevel will be handled in each sub-command as passed in by options
            var  options = new CompositeOptions();
            var  parser  = CommandUtility.GetParser(SubCommandParseOption.Loose);
            bool parsed  = parser.ParseArguments(args, options);

            if (options.ShouldShowVersion)
            {
                return(new HelpCommand(GetVersionText()));
            }
            if (options.IsHelp)
            {
                return(new HelpCommand(GetHelpText()));
            }
            return(new CompositeCommand(args, this, options));
        }
예제 #2
0
        private static PdfJsonConfig ParseOptions(PdfCommandOptions options)
        {
            var           configFile = BuildCommand.GetConfigFilePath(options);
            PdfJsonConfig config;

            if (configFile == null)
            {
                if (options.Content == null && options.Resource == null)
                {
                    throw new OptionParserException("Either provide config file or specify content files to start building documentation.");
                }

                config = new PdfJsonConfig()
                {
                    BaseDirectory = EnvironmentContext.BaseDirectory
                };
                MergeOptionsToConfig(options, config);
                return(config);
            }

            config = CommandUtility.GetConfig <PdfConfig>(configFile).Item;
            if (config == null)
            {
                throw new DocumentException($"Unable to find pdf subcommand config in file '{configFile}'.");
            }

            config.BaseDirectory = Path.GetDirectoryName(configFile);

            MergeOptionsToConfig(options, config);
            return(config);
        }
예제 #3
0
        public virtual ISubCommand Create(string[] args, ISubCommandController controller, SubCommandParseOption option)
        {
            var  parser  = CommandUtility.GetParser(option);
            var  options = Activator.CreateInstance <TOptions>();
            bool parsed  = parser.ParseArguments(args, options);

            if (!parsed && option == SubCommandParseOption.Strict)
            {
                throw new OptionParserException();
            }
            var helpOption = options as ICanPrintHelpMessage;

            if (helpOption != null && helpOption.PrintHelpMessage)
            {
                return(new HelpCommand(GetHelpText()));
            }
            var logOption = options as ILoggable;

            if (logOption != null)
            {
                if (!string.IsNullOrWhiteSpace(logOption.LogFilePath))
                {
                    Logger.AddOrReplaceListener(new ReportLogListener(logOption.LogFilePath), TypeEqualityComparer.Default);
                }

                if (logOption.LogLevel.HasValue)
                {
                    Logger.LogLevelThreshold = logOption.LogLevel.Value;
                }
            }

            return(CreateCommand(options, controller));
        }
예제 #4
0
파일: BuildCommand.cs 프로젝트: saul/docfx
        /// <summary>
        /// TODO: refactor
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private static BuildJsonConfig ParseOptions(BuildCommandOptions options)
        {
            var             configFile = GetConfigFilePath(options);
            BuildJsonConfig config;

            if (configFile == null)
            {
                if (options.Content == null && options.Resource == null)
                {
                    throw new OptionParserException("Either provide config file or specify content files to start building documentation.");
                }

                config = new BuildJsonConfig();
                MergeOptionsToConfig(options, config);
                return(config);
            }

            config = CommandUtility.GetConfig <BuildConfig>(configFile).Item;
            if (config == null)
            {
                var message = $"Unable to find build subcommand config in file '{configFile}'.";
                Logger.LogError(message, code: ErrorCodes.Config.BuildConfigNotFound);
                throw new DocumentException(message);
            }
            config.BaseDirectory = Path.GetDirectoryName(configFile);

            MergeOptionsToConfig(options, config);
            return(config);
        }
예제 #5
0
        private IEnumerable <KeyValuePair <string, JToken> > ToOrderedKeyValuePair(string path)
        {
            var jObject = CommandUtility.GetConfig <JObject>(path);

            foreach (var item in jObject)
            {
                yield return(item);
            }
        }
예제 #6
0
        public virtual ISubCommand Create(string[] args, ISubCommandController controller, SubCommandParseOption option)
        {
            var  parser  = CommandUtility.GetParser(option);
            var  options = Activator.CreateInstance <TOptions>();
            bool parsed  = parser.ParseArguments(args, options);

            if (!parsed && option == SubCommandParseOption.Strict)
            {
                throw new OptionParserException();
            }
            var helpOption = options as ICanPrintHelpMessage;

            if (helpOption != null && helpOption.PrintHelpMessage)
            {
                return(new HelpCommand(GetHelpText()));
            }
            string root        = string.Empty;
            var    buildOption = options as BuildCommandOptions;

            if (buildOption != null)
            {
                root = Path.GetDirectoryName(buildOption.ConfigFile);
            }
            var logOption = options as ILoggable;

            if (logOption != null)
            {
                if (!string.IsNullOrWhiteSpace(logOption.LogFilePath) && Logger.FindListener(l => l is ReportLogListener) == null)
                {
                    if (string.IsNullOrWhiteSpace(logOption.RepoRoot))
                    {
                        Logger.RegisterListener(new ReportLogListener(logOption.LogFilePath, string.Empty, root));
                    }
                    else
                    {
                        Logger.RegisterListener(new ReportLogListener(logOption.LogFilePath, logOption.RepoRoot, root));
                    }
                }

                if (logOption.LogLevel.HasValue)
                {
                    Logger.LogLevelThreshold = logOption.LogLevel.Value;
                }
            }

            return(CreateCommand(options, controller));
        }
예제 #7
0
        public virtual ISubCommand Create(string[] args, ISubCommandController controller, SubCommandParseOption option)
        {
            var  parser  = CommandUtility.GetParser(option);
            var  options = Activator.CreateInstance <TOptions>();
            bool parsed  = parser.ParseArguments(args, options);

            if (!parsed && option == SubCommandParseOption.Strict)
            {
                throw new OptionParserException();
            }
            var helpOption = options as ICanPrintHelpMessage;

            if (helpOption != null && helpOption.PrintHelpMessage)
            {
                return(new HelpCommand(GetHelpText()));
            }

            var    buildOption = options as BuildCommandOptions;
            string root        = Path.GetDirectoryName(buildOption?.ConfigFile ?? Directory.GetCurrentDirectory());

            var logOption = options as LogOptions;

            if (logOption != null)
            {
                if (!string.IsNullOrWhiteSpace(logOption.LogFilePath) && Logger.FindAsyncListener(l => l is ReportLogListener) == null)
                {
                    Logger.RegisterAsyncListener(new ReportLogListener(logOption.LogFilePath, logOption.RepoRoot ?? string.Empty, root));
                }

                if (logOption.LogLevel.HasValue)
                {
                    Logger.LogLevelThreshold = logOption.LogLevel.Value;
                }

                Logger.WarningsAsErrors = logOption.WarningsAsErrors;

                if (!string.IsNullOrEmpty(logOption.CorrelationId))
                {
                    if (AmbientContext.CurrentContext == null)
                    {
                        AmbientContext.InitializeAmbientContext(logOption.CorrelationId);
                    }
                }
            }

            return(CreateCommand(options, controller));
        }
예제 #8
0
        private static MergeJsonConfig ParseOptions(MergeCommandOptions options)
        {
            var             configFile = options.ConfigFile;
            MergeJsonConfig config;

            if (string.IsNullOrEmpty(configFile))
            {
                if (!File.Exists(DocAsCode.Constants.ConfigFileName))
                {
                    if (options.Content == null)
                    {
                        throw new ArgumentException("Either provide config file or specify content files to start building documentation.");
                    }
                    else
                    {
                        config = new MergeJsonConfig();
                        var item = new MergeJsonItemConfig();
                        MergeOptionsToConfig(options, ref item);
                        config.Add(item);
                        return(config);
                    }
                }
                else
                {
                    Logger.Log(LogLevel.Verbose, $"Config file {DocAsCode.Constants.ConfigFileName} is found.");
                    configFile = DocAsCode.Constants.ConfigFileName;
                }
            }

            config = CommandUtility.GetConfig <MergeConfig>(configFile).Item;
            if (config == null)
            {
                var message = $"Unable to find build subcommand config in file '{configFile}'.";
                Logger.LogError(message, code: ErrorCodes.Config.BuildConfigNotFound);
                throw new DocumentException(message);
            }

            for (int i = 0; i < config.Count; i++)
            {
                var round = config[i];
                round.BaseDirectory = Path.GetDirectoryName(configFile);

                MergeOptionsToConfig(options, ref round);
            }

            return(config);
        }
예제 #9
0
        private MetadataJsonConfig ParseOptions(MetadataCommandOptions options)
        {
            string configFile;

            if (TryGetJsonConfig(options.Projects, out configFile))
            {
                var config = CommandUtility.GetConfig <MetadataConfig>(configFile).Item;
                if (config == null)
                {
                    throw new DocumentException($"Unable to find metadata subcommand config in file '{configFile}'.");
                }
                config.BaseDirectory = Path.GetDirectoryName(configFile);
                config.OutputFolder  = options.OutputFolder;
                foreach (var item in config)
                {
                    item.Raw              |= options.PreserveRawInlineComments;
                    item.Force            |= options.ForceRebuild;
                    item.ShouldSkipMarkup |= options.ShouldSkipMarkup;
                    item.FilterConfigFile  = options.FilterConfigFile ?? item.FilterConfigFile;
                }
                return(config);
            }
            else
            {
                var config = new MetadataJsonConfig();
                config.Add(new MetadataJsonItemConfig
                {
                    Force            = options.ForceRebuild,
                    ShouldSkipMarkup = options.ShouldSkipMarkup,
                    Destination      = options.OutputFolder,
                    Raw    = options.PreserveRawInlineComments,
                    Source = new FileMapping(new FileMappingItem(options.Projects.ToArray()))
                    {
                        Expanded = true
                    },
                    FilterConfigFile = options.FilterConfigFile
                });
                return(config);
            }
        }
예제 #10
0
        private static BuildJsonConfig ParseOptions(BuildCommandOptions options)
        {
            var             configFile = options.ConfigFile;
            BuildJsonConfig config;

            if (string.IsNullOrEmpty(configFile))
            {
                if (!File.Exists(DocAsCode.Constants.ConfigFileName))
                {
                    if (options.Content == null && options.Resource == null)
                    {
                        throw new ArgumentException("Either provide config file or specify content files to start building documentation.");
                    }
                    else
                    {
                        config = new BuildJsonConfig();
                        MergeOptionsToConfig(options, config);
                        return(config);
                    }
                }
                else
                {
                    Logger.Log(LogLevel.Verbose, $"Config file {DocAsCode.Constants.ConfigFileName} is found.");
                    configFile = DocAsCode.Constants.ConfigFileName;
                }
            }

            config = CommandUtility.GetConfig <BuildConfig>(configFile).Item;
            if (config == null)
            {
                throw new DocumentException($"Unable to find build subcommand config in file '{configFile}'.");
            }
            config.BaseDirectory = Path.GetDirectoryName(configFile);

            MergeOptionsToConfig(options, config);
            MergeNewFileRepositoryToConfig(config);
            return(config);
        }
예제 #11
0
        private MetadataJsonConfig ParseOptions(MetadataCommandOptions options, out string baseDirectory, out string outputFolder)
        {
            MetadataJsonConfig config;

            baseDirectory = null;
            if (TryGetJsonConfig(options.Projects, out string configFile))
            {
                config = CommandUtility.GetConfig <MetadataConfig>(configFile).Item;
                if (config == null)
                {
                    throw new DocumentException($"Unable to find metadata subcommand config in file '{configFile}'.");
                }

                baseDirectory = Path.GetDirectoryName(configFile);
            }
            else
            {
                config = new MetadataJsonConfig
                {
                    new MetadataJsonItemConfig
                    {
                        Destination = options.OutputFolder,
                        Source      = new FileMapping(new FileMappingItem(options.Projects.ToArray()))
                        {
                            Expanded = true
                        }
                    }
                };
            }

            var msbuildProperties = ResolveMSBuildProperties(options);

            foreach (var item in config)
            {
                item.Force                |= options.ForceRebuild;
                item.Raw                  |= options.PreserveRawInlineComments;
                item.ShouldSkipMarkup     |= options.ShouldSkipMarkup;
                item.DisableGitFeatures   |= options.DisableGitFeatures;
                item.DisableDefaultFilter |= options.DisableDefaultFilter;
                if (!string.IsNullOrEmpty(options.FilterConfigFile))
                {
                    item.FilterConfigFile = Path.GetFullPath(options.FilterConfigFile);
                }

                if (!string.IsNullOrEmpty(options.GlobalNamespaceId))
                {
                    item.GlobalNamespaceId = options.GlobalNamespaceId;
                }

                if (item.MSBuildProperties == null)
                {
                    item.MSBuildProperties = msbuildProperties;
                }
                else
                {
                    // Command line properties overwrites the one defined in docfx.json
                    foreach (var pair in msbuildProperties)
                    {
                        item.MSBuildProperties[pair.Key] = pair.Value;
                    }
                }
            }

            outputFolder = options.OutputFolder;

            return(config);
        }