public void ReadInitial_OK()
        {
            var expected = AdrEntry.CreateInitial();

            expected.When = new DateTime(2019, 9, 19);

            var entry = new AdrEntryReader(new StringReader(Examples.InitialRecordMarkdown)).Read();

            AssertEntries(expected, entry);
        }
Ejemplo n.º 2
0
        public void Write(AdrEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            WriteHeading(1, $"{entry.Number}. {entry.Title}");

            WriteMeta(new[] { ("Date", entry.When.ToString("yyyy-MM-dd")) });
 private static void AssertEntries(AdrEntry expected, AdrEntry actual)
 {
     Assert.Equal(expected.Title, actual.Title);
     Assert.Equal(expected.Number, actual.Number);
     Assert.Equal(expected.Status, actual.Status);
     Assert.Equal(expected.Context, actual.Context);
     Assert.Equal(expected.Decision, actual.Decision);
     Assert.Equal(expected.Consequences, actual.Consequences);
     Assert.Equal(expected.When, actual.When);
 }
Ejemplo n.º 4
0
        public void WriteInitial_OK()
        {
            var entry = AdrEntry.CreateInitial();

            entry.When = new DateTime(2019, 9, 19);

            var actual = new StringWriter();

            new AdrEntryWriter(actual).Write(entry);

            Assert.Equal(Examples.InitialRecordMarkdown, actual.ToString());
        }
Ejemplo n.º 5
0
        private static int Init(InitOptions opts)
        {
            if (!Directory.Exists(opts.Directory))
            {
                Directory.CreateDirectory(opts.Directory);
            }

            var file = AdrFile.Save(new AdrDirectory(opts.Directory), AdrEntry.CreateInitial());

            Console.WriteLine("Initialized and created {0}", file.FilePath);
            return(0);
        }
Ejemplo n.º 6
0
        public static AdrFile Save(AdrDirectory directory, AdrEntry entry)
        {
            entry.Number = directory.GetNextAdrNo();
            var filePath = directory.GetAdrFilePath(entry.Number, entry.Title);

            using (var stream = File.OpenWrite(filePath))
            {
                new AdrEntryWriter(stream).Write(entry);
            }

            return(new AdrFile(filePath, entry));
        }
Ejemplo n.º 7
0
        private static int New(NewOptions opts)
        {
            var directory = new AdrDirectory(opts.Directory);
            var newEntry  = AdrEntry.CreateNew(opts.Title);
            var current   = AdrFile.Save(directory, newEntry);

            Console.WriteLine("Created {0}", current.FilePath);

            if (opts.SupersedesAdr != null)
            {
                var referenced = directory.GetRecord(opts.SupersedesAdr.Value);
                if (referenced != null)
                {
                    current.Entry.AppendStatus($"Supersedes {referenced.AsLink()}");
                    referenced.Entry.AppendStatus($"Superseded by {current.AsLink()}");
                    current.Save();
                    referenced.Save();
                }
                else
                {
                    Console.WriteLine("{0} not found", opts.SupersedesAdr);
                    return(1);
                }
            }

            if (opts.AmendsAdr != null)
            {
                var referenced = directory.GetRecord(opts.AmendsAdr.Value);
                if (referenced != null)
                {
                    current.Entry.AppendStatus($"Amends {referenced.AsLink()}");
                    referenced.Entry.AppendStatus($"Amended by {current.AsLink()}");
                    current.Save();
                    referenced.Save();
                }
                else
                {
                    Console.WriteLine("{0} not found", opts.AmendsAdr);
                    return(1);
                }
            }

            return(0);
        }
Ejemplo n.º 8
0
        public AdrEntry Read()
        {
            var firstLine = _reader.ReadLine();

            if (firstLine == null)
            {
                return(null);
            }
            if (!firstLine.StartsWith("# "))
            {
                throw new InvalidDataException("Invalid file");
            }

            var dotIndex = firstLine.IndexOf('.');
            var number   = int.Parse(firstLine.AsSpan(2, dotIndex - 2));
            var title    = firstLine.Substring(dotIndex + 1).TrimStart();

            var entry = new AdrEntry(title)
            {
                Number = number
            };
            var sections = ReadSections().ToDictionary(n => n.Item1, n => n.Item2);
            var meta     = sections.GetValueOrDefault("")
                           .Split('\n', StringSplitOptions.RemoveEmptyEntries)
                           .Where(l => !string.IsNullOrEmpty(l))
                           .Select(l => l.Split(": "))
                           .ToDictionary(n => n[0], n => n.ElementAtOrDefault(1));

            entry.When         = DateTime.TryParse(meta.GetValueOrDefault("Date"), out var when) ? when : DateTime.Today;
            entry.Status       = sections.GetValueOrDefault("## Status");
            entry.Context      = sections.GetValueOrDefault("## Context");
            entry.Decision     = sections.GetValueOrDefault("## Decision");
            entry.Consequences = sections.GetValueOrDefault("## Consequences");

            return(entry);
        }
Ejemplo n.º 9
0
 public AdrFile(string path, AdrEntry entry)
 {
     FilePath = path;
     Entry    = entry;
 }