Пример #1
0
 private IEnumerable<ParseResult> YieldRun(RunningContext context)
 {
     foreach (var command in Commands)
     {
         yield return command.Exec(context);
     }
 }
Пример #2
0
 private IEnumerable <ParseResult> YieldRun(RunningContext context)
 {
     foreach (var command in Commands)
     {
         yield return(command.Exec(context));
     }
 }
Пример #3
0
 public ParseResult Exec(RunningContext context)
 {
     string text = HelpTextGenerator.GetHelpMessage(_rootOptions, _options.Command);
     _rootOptions.CurrentSubCommand = CommandType.Help;
     Console.WriteLine(text);
     return ParseResult.SuccessResult;
 }
Пример #4
0
        public ParseResult Exec(RunningContext context)
        {
            string text = HelpTextGenerator.GetHelpMessage(_rootOptions, _options.Command);

            _rootOptions.CurrentSubCommand = CommandType.Help;
            Console.WriteLine(text);
            return(ParseResult.SuccessResult);
        }
Пример #5
0
        static int Main(string[] args)
        {
            try
            {
                var consoleLogListener = new ConsoleLogListener();
                Logger.RegisterListener(consoleLogListener);
                Options options;
                var result = TryGetOptions(args, out options);

                if (!string.IsNullOrWhiteSpace(options.Log))
                {
                    Logger.RegisterListener(new ReportLogListener(options.Log));
                }

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

                if (!string.IsNullOrEmpty(result.Message))
                {
                    Logger.Log(result);
                }
                if (result.ResultLevel == ResultLevel.Error)
                {
                    return 1;
                }

                var replayListener = new ReplayLogListener();
                replayListener.AddListener(consoleLogListener);
                Logger.RegisterListener(replayListener);
                Logger.UnregisterListener(consoleLogListener);

                var context = new RunningContext();
                result = Exec(options, context);
                if (!string.IsNullOrEmpty(result.Message))
                {
                    Logger.Log(result);
                }

                if (result.ResultLevel == ResultLevel.Error)
                {
                    return 1;
                }
                if (result.ResultLevel == ResultLevel.Warning)
                {
                    return 2;
                }
                return 0;
            }
            finally
            {
                Logger.Flush();
                Logger.UnregisterAllListeners();
            }
        }
Пример #6
0
        static int Main(string[] args)
        {
            try
            {
                var consoleLogListener = new ConsoleLogListener();
                Logger.RegisterListener(consoleLogListener);
                Options options;
                var     result = TryGetOptions(args, out options);

                if (!string.IsNullOrWhiteSpace(options.Log))
                {
                    Logger.RegisterListener(new ReportLogListener(options.Log));
                }

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

                if (!string.IsNullOrEmpty(result.Message))
                {
                    Logger.Log(result);
                }
                if (result.ResultLevel == ResultLevel.Error)
                {
                    return(1);
                }

                var replayListener = new ReplayLogListener();
                replayListener.AddListener(consoleLogListener);
                Logger.RegisterListener(replayListener);
                Logger.UnregisterListener(consoleLogListener);

                var context = new RunningContext();
                result = Exec(options, context);
                if (!string.IsNullOrEmpty(result.Message))
                {
                    Logger.Log(result);
                }

                if (result.ResultLevel == ResultLevel.Error)
                {
                    return(1);
                }
                if (result.ResultLevel == ResultLevel.Warning)
                {
                    return(2);
                }
                return(0);
            }
            finally
            {
                Logger.Flush();
                Logger.UnregisterAllListeners();
            }
        }
Пример #7
0
        public ParseResult Exec(RunningContext context)
        {
            if (_helpMessage != null)
            {
                Console.WriteLine(_helpMessage);
                return(ParseResult.SuccessResult);
            }

            return(InternalExec(Config, context));
        }
Пример #8
0
        public ParseResult Exec(RunningContext context)
        {
            if (_helpMessage != null)
            {
                Console.WriteLine(_helpMessage);
                return ParseResult.SuccessResult;
            }

            return InternalExec(Config, context);
        }
Пример #9
0
 /// <summary>
 /// TODO: catch exception 
 /// </summary>
 /// <param name="configs"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 private IEnumerable<ParseResult> YieldExec(RunningContext context)
 {
     foreach (var inputModel in InputModels)
     {
         // TODO: Use plugin to generate metadata for files with different extension?
         using (var worker = new ExtractMetadataWorker(inputModel, inputModel.ForceRebuild))
         {
             // Use task.run to get rid of current context (causing deadlock in xunit)
             var task = Task.Run(worker.ExtractMetadataAsync);
             yield return task.Result;
         }
     }
 }
Пример #10
0
 /// <summary>
 /// TODO: catch exception
 /// </summary>
 /// <param name="configs"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 private IEnumerable <ParseResult> YieldExec(RunningContext context)
 {
     foreach (var inputModel in InputModels)
     {
         // TODO: Use plugin to generate metadata for files with different extension?
         using (var worker = new ExtractMetadataWorker(inputModel, inputModel.ForceRebuild))
         {
             // Use task.run to get rid of current context (causing deadlock in xunit)
             var task = Task.Run(worker.ExtractMetadataAsync);
             yield return(task.Result);
         }
     }
 }
Пример #11
0
        private ParseResult InternalExec(BuildJsonConfig config, RunningContext context)
        {
            var parameters = ConfigToParameter(config);

            if (parameters.Files.Count == 0)
            {
                return(new ParseResult(ResultLevel.Warning, "No files found, nothing is to be generated"));
            }
            try
            {
                _builder.Build(parameters);
            }
            catch (AggregateDocumentException aggEx)
            {
                return(new ParseResult(ResultLevel.Warning, "following document error:" + Environment.NewLine + string.Join(Environment.NewLine, from ex in aggEx.InnerExceptions select ex.Message)));
            }
            catch (DocumentException ex)
            {
                return(new ParseResult(ResultLevel.Warning, "document error:" + ex.Message));
            }
            var documentContext = DocumentBuildContext.DeserializeFrom(parameters.OutputBaseDir);
            var assembly        = typeof(Program).Assembly;

            if (config.Templates == null || config.Templates.Count == 0)
            {
                config.Templates = new ListWithStringFallback {
                    Constants.DefaultTemplateName
                };
            }

            // If RootOutput folder is specified from command line, use it instead of the base directory
            var outputFolder = Path.Combine(config.OutputFolder ?? config.BaseDirectory ?? string.Empty, config.Destination ?? string.Empty);

            using (var manager = new TemplateManager(assembly, "Template", config.Templates, config.Themes, config.BaseDirectory))
            {
                manager.ProcessTemplateAndTheme(documentContext, outputFolder, true);
            }

            // TODO: SEARCH DATA

            if (config.Serve)
            {
                ServeCommand.Serve(outputFolder, config.Port);
            }

            return(ParseResult.SuccessResult);
        }
Пример #12
0
        public ParseResult Exec(RunningContext context)
        {
            var outputFile = Path.Combine(_options.OutputFolder ?? Environment.CurrentDirectory, _options.Name ?? "externalreference.rpk");

            try
            {
                var baseUri = new Uri(_options.BaseUrl);
                if (!baseUri.IsAbsoluteUri)
                {
                    return(new ParseResult(ResultLevel.Error, "BaseUrl should be absolute url."));
                }
                var source = _options.Source.TrimEnd('/', '\\');
                using (var package = _options.AppendMode ? ExternalReferencePackageWriter.Append(outputFile, baseUri) : ExternalReferencePackageWriter.Create(outputFile, baseUri))
                {
                    var files = FileGlob.GetFiles(source, new string[] { _options.Glob }, null).ToList();
                    if (_options.FlatMode)
                    {
                        package.AddFiles(string.Empty, files);
                    }
                    else
                    {
                        foreach (var g in from f in files
                                 group f by Path.GetDirectoryName(f) into g
                                 select new
                        {
                            Folder = g.Key.Substring(source.Length).Replace('\\', '/').Trim('/'),
                            Files = g.ToList(),
                        })
                        {
                            if (g.Folder.Length == 0)
                            {
                                package.AddFiles(string.Empty, g.Files);
                            }
                            else
                            {
                                package.AddFiles(g.Folder + "/", g.Files);
                            }
                        }
                    }
                }
                return(ParseResult.SuccessResult);
            }
            catch (Exception ex)
            {
                return(new ParseResult(ResultLevel.Error, ex.ToString()));
            }
        }
Пример #13
0
 public ParseResult Exec(RunningContext context)
 {
     var outputFile = Path.Combine(_options.OutputFolder ?? Environment.CurrentDirectory, _options.Name ?? "externalreference.rpk");
     try
     {
         var baseUri = new Uri(_options.BaseUrl);
         if (!baseUri.IsAbsoluteUri)
         {
             return new ParseResult(ResultLevel.Error, "BaseUrl should be absolute url.");
         }
         var source = _options.Source.TrimEnd('/', '\\');
         using (var package = _options.AppendMode ? ExternalReferencePackageWriter.Append(outputFile, baseUri) : ExternalReferencePackageWriter.Create(outputFile, baseUri))
         {
             var files = FileGlob.GetFiles(source, new string[] { _options.Glob }, null).ToList();
             if (_options.FlatMode)
             {
                 package.AddFiles(string.Empty, files);
             }
             else
             {
                 foreach (var g in from f in files
                                   group f by Path.GetDirectoryName(f) into g
                                   select new
                                   {
                                       Folder = g.Key.Substring(source.Length).Replace('\\', '/').Trim('/'),
                                       Files = g.ToList(),
                                   })
                 {
                     if (g.Folder.Length == 0)
                     {
                         package.AddFiles(string.Empty, g.Files);
                     }
                     else
                     {
                         package.AddFiles(g.Folder + "/", g.Files);
                     }
                 }
             }
         }
         return ParseResult.SuccessResult;
     }
     catch (Exception ex)
     {
         return new ParseResult(ResultLevel.Error, ex.ToString());
     }
 }
Пример #14
0
        public ParseResult Exec(RunningContext context)
        {
            if (_helpMessage != null)
            {
                Console.WriteLine(_helpMessage);
                return(ParseResult.SuccessResult);
            }

            var extractMetadataResult = _metadataCommand.Exec(context);

            Logger.Log(extractMetadataResult);
            if (extractMetadataResult.ResultLevel == ResultLevel.Error)
            {
                return(extractMetadataResult);
            }

            // 2. convert.
            var inputModels = _metadataCommand.InputModels;
            var outputFile  = Path.Combine(_options.OutputFolder ?? Environment.CurrentDirectory, _options.Name ?? "externalreference.rpk");

            if (string.IsNullOrWhiteSpace(_options.BaseUrl))
            {
                return(new ParseResult(ResultLevel.Error, "BaseUrl cannot be empty."));
            }
            try
            {
                var baseUri = new Uri(_options.BaseUrl);
                if (!baseUri.IsAbsoluteUri)
                {
                    return(new ParseResult(ResultLevel.Error, "BaseUrl should be absolute url."));
                }
                using (var package = _options.AppendMode ? ExternalReferencePackageWriter.Append(outputFile, baseUri) : ExternalReferencePackageWriter.Create(outputFile, baseUri))
                {
                    package.AddProjects(inputModels.SelectMany(s => s.Items).Select(s => s.Key).ToList());
                }

                return(ParseResult.SuccessResult);
            }
            catch (Exception ex)
            {
                return(new ParseResult(ResultLevel.Error, ex.ToString()));
            }
        }
Пример #15
0
        public ParseResult Exec(RunningContext context)
        {
            if (_helpMessage != null)
            {
                Console.WriteLine(_helpMessage);
                return ParseResult.SuccessResult;
            }

            var extractMetadataResult = _metadataCommand.Exec(context);

            Logger.Log(extractMetadataResult);
            if (extractMetadataResult.ResultLevel == ResultLevel.Error)
            {
                return extractMetadataResult;
            }

            // 2. convert.
            var inputModels = _metadataCommand.InputModels;
            var outputFile = Path.Combine(_options.OutputFolder ?? Environment.CurrentDirectory, _options.Name ?? "externalreference.rpk");
            if (string.IsNullOrWhiteSpace(_options.BaseUrl))
            {
                return new ParseResult(ResultLevel.Error, "BaseUrl cannot be empty.");
            }
            try
            {
                var baseUri = new Uri(_options.BaseUrl);
                if (!baseUri.IsAbsoluteUri)
                {
                    return new ParseResult(ResultLevel.Error, "BaseUrl should be absolute url.");
                }
                using (var package = _options.AppendMode ? ExternalReferencePackageWriter.Append(outputFile, baseUri) : ExternalReferencePackageWriter.Create(outputFile, baseUri))
                {
                    package.AddProjects(inputModels.SelectMany(s => s.Items).Select(s => s.Key).ToList());
                }

                return ParseResult.SuccessResult;
            }
            catch (Exception ex)
            {
                return new ParseResult(ResultLevel.Error, ex.ToString());
            }
        }
Пример #16
0
        public ParseResult Exec(RunningContext context)
        {
            string name = null;
            string path = null;

            try
            {
                DefaultConfigModel config = new DefaultConfigModel();
                if (_options.Quiet)
                {
                    // Generate a default Config
                    foreach (var question in _questions)
                    {
                        question.SetDefault(config);
                    }
                }
                else
                {
                    foreach (var question in _questions)
                    {
                        if (question is SingleQuestion)
                        {
                            ProcessSingleQuestion((SingleQuestion)question, config);
                        }
                        else
                        {
                            ProcessMultiQuestion((MultiQuestion)question, config);
                        }
                    }
                }

                name = string.IsNullOrEmpty(_options.Name) ? ConfigName : _options.Name;
                path = string.IsNullOrEmpty(_options.OutputFolder) ? name : Path.Combine(_options.OutputFolder, name);

                JsonUtility.Serialize(path, config, Formatting.Indented);
                return(new ParseResult(ResultLevel.Success, $"Generated {name} to {path}"));
            }
            catch (Exception e)
            {
                return(new ParseResult(ResultLevel.Error, $"Error init { name ?? ConfigName}: {e.Message}"));
            }
        }
Пример #17
0
        private static ParseResult Exec(Options options, RunningContext context)
        {
            ICommand command;

            try
            {
                command = CommandFactory.GetCommand(options);
            }
            catch (Exception e)
            {
                return(new ParseResult(ResultLevel.Error, $"Fails to get config file: {e.Message}"));
            }
            try
            {
                return(command.Exec(context));
            }
            catch (Exception e)
            {
                return(new ParseResult(ResultLevel.Error, $"Error running program: {e.ToString()}"));
            }
        }
Пример #18
0
        private ParseResult InternalExec(BuildJsonConfig config, RunningContext context)
        {
            var parameters = ConfigToParameter(config);
            if (parameters.Files.Count == 0) return new ParseResult(ResultLevel.Warning, "No files found, nothing is to be generated");
            try
            {
                _builder.Build(parameters);
            }
            catch (AggregateDocumentException aggEx)
            {
                return new ParseResult(ResultLevel.Warning, "following document error:" + Environment.NewLine + string.Join(Environment.NewLine, from ex in aggEx.InnerExceptions select ex.Message));
            }
            catch (DocumentException ex)
            {
                return new ParseResult(ResultLevel.Warning, "document error:" + ex.Message);
            }
            var documentContext = DocumentBuildContext.DeserializeFrom(parameters.OutputBaseDir);
            var assembly = typeof(Program).Assembly;

            if (config.Templates == null || config.Templates.Count == 0)
            {
                config.Templates = new ListWithStringFallback { Constants.DefaultTemplateName };
            }

            // If RootOutput folder is specified from command line, use it instead of the base directory
            var outputFolder = Path.Combine(config.OutputFolder ?? config.BaseDirectory ?? string.Empty, config.Destination ?? string.Empty);
            using (var manager = new TemplateManager(assembly, "Template", config.Templates, config.Themes, config.BaseDirectory))
            {
                manager.ProcessTemplateAndTheme(documentContext, outputFolder, true);
            }

            // TODO: SEARCH DATA

            if (config.Serve)
            {
                ServeCommand.Serve(outputFolder, config.Port);
            }

            return ParseResult.SuccessResult;
        }
Пример #19
0
 public ParseResult Exec(RunningContext context)
 {
     return(AggregateParseResult(YieldRun(context)));
 }
Пример #20
0
 private ParseResult InternalExec(RunningContext context)
 {
     // TODO: can we do it parallelly?
     return(CompositeCommand.AggregateParseResult(YieldExec(context)));
 }
Пример #21
0
 private ParseResult InternalExec(RunningContext context)
 {
     // TODO: can we do it parallelly?
     return CompositeCommand.AggregateParseResult(YieldExec(context));
 }
Пример #22
0
 public ParseResult Exec(RunningContext context)
 {
     return AggregateParseResult(YieldRun(context));
 }
Пример #23
0
 public ParseResult Exec(RunningContext context)
 {
     Serve(_options.Folder, _options.Port.HasValue ? _options.Port.Value.ToString() : null);
     return ParseResult.SuccessResult;
 }
Пример #24
0
 private static ParseResult Exec(Options options, RunningContext context)
 {
     ICommand command;
     try
     {
         command = CommandFactory.GetCommand(options);
     }
     catch (Exception e)
     {
         return new ParseResult(ResultLevel.Error, $"Fails to get config file: {e.Message}");
     }
     try
     {
         return command.Exec(context);
     }
     catch (Exception e)
     {
         return new ParseResult(ResultLevel.Error, $"Error running program: {e.ToString()}");
     }
 }
Пример #25
0
 public ParseResult Exec(RunningContext context)
 {
     Serve(_options.Folder, _options.Port.HasValue ? _options.Port.Value.ToString() : null);
     return(ParseResult.SuccessResult);
 }