Ejemplo n.º 1
0
        public AdrIndex(string docFolder)
        {
            Directory.CreateDirectory(docFolder);

            _indexDocFolder = docFolder;
            _indexFileName  = $"{fileNumber.ToString().PadLeft(4, '0')}-{AdrEntry.SanitizeFileName(IndexFileName)}.md";
            _indexFile      = Path.Combine(docFolder, _indexFileName);
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            var app = new CommandLineApplication();

            app.Name        = "adr-cli";
            app.Description = "A .Net equivalent of adr-tools (https://github.com/npryce/adr-tools)";

            app.HelpOption(HelpOption);

            app.VersionOption("-v|--version", () =>
            {
                var assemblyName = typeof(AdrEntry).Assembly.GetName();

                return($"{assemblyName.Name} {assemblyName.Version}");
            });

            app.Command("init", (command) =>
            {
                command.Description = "Initialize an ADR repository in the given directory";
                var directory       = command.Argument("[directory]", "");
                command.HelpOption(HelpOption);
                command.OnExecute(() =>
                {
                    var settings       = AdrSettings.Current;
                    settings.DocFolder = directory.Value ?? settings.DocFolder;
                    settings.Write();
                    new AdrEntry(TemplateType.Adr)
                    .Write()
                    .Launch();
                    return((int)ExitCode.Success);
                });
            });

            app.Command("list", (command) =>
            {
                command.Description = "Show a list of ADRs";
                command.HelpOption(HelpOption);

                command.OnExecute(() =>
                {
                    var docFolder = AdrSettings.Current.DocFolder;

                    var log = new ArchitectureDecisionLog(System.IO.Path.GetFullPath(docFolder));

                    var records = log.GetRecords();

                    foreach (var record in records)
                    {
                        app.Out.WriteLine(record.FullName);
                    }

                    return((int)ExitCode.Success);
                });
            });

            app.Command("new", (command) =>
            {
                command.Description = "Create a new ADR file";
                var title           = command.Argument("title", "ADR title");
                var supercedes      = command.Option("-s|--supercedes", "", CommandOptionType.MultipleValue);
                var links           = command.Option("-l|--links", "", CommandOptionType.MultipleValue);
                command.HelpOption(HelpOption);

                command.OnExecute(() =>
                {
                    var docFolder = AdrSettings.Current.DocFolder;

                    var manager = new ArchitectureDecisionLog(System.IO.Path.GetFullPath(docFolder));

                    var newEntry = new AdrEntry(TemplateType.New)
                    {
                        Title = title.Value ?? ""
                    }
                    .Write();

                    if (supercedes?.Values.Count > 0)
                    {
                        foreach (var sup in supercedes.Values)
                        {
                            int number = 0;

                            if (int.TryParse(sup, out number))
                            {
                                manager.SupercedesAdr(number, newEntry);
                            }
                        }
                    }

                    if (links?.Values.Count > 0)
                    {
                        foreach (var linkDetails in links.Values)
                        {
                            AdrLink link = null;

                            if (AdrLink.TryParse(linkDetails, out link))
                            {
                                manager.LinksAdr(newEntry, link);
                            }
                        }
                    }

                    newEntry.Launch();

                    return((int)ExitCode.Success);
                });
            });

            app.Command("link", (command) =>
            {
                command.Description = "(Command not implemented)";
                command.OnExecute(() =>
                {
                    app.Out.WriteLine("Command not implemented");

                    return((int)ExitCode.NotImplemented);
                });
            });

            app.Command("generate", (command) =>
            {
                command.Description = "(Command not implemented)";
                command.OnExecute(() =>
                {
                    app.Out.WriteLine("Command not implemented");

                    return((int)ExitCode.NotImplemented);
                });
            });

            app.OnExecute(() =>
            {
                app.ShowHelp();
                return((int)ExitCode.Success);
            });

            try
            {
                Environment.ExitCode = app.Execute(args);
            }
            catch (CommandParsingException ex)
            {
                app.Out.WriteLine($"[ERROR] Cannot parse or recognize the given command: {ex.Message}");
                app.ShowHelp();

                Environment.ExitCode = (int)ExitCode.ParsingError;
            }
            catch (Exception ex)
            {
                app.Out.WriteLine($"[ERROR] Cannot execute the given command: {ex.Message}");
                app.ShowHelp();

                Environment.ExitCode = (int)ExitCode.Error;
            }
        }