public void RenameTag(IJournalReader journalReader, string oldTag, string newTag) { if (string.IsNullOrWhiteSpace(oldTag) || string.IsNullOrWhiteSpace(newTag)) { throw new ArgumentNullException($"'{nameof(oldTag)}' cannot be null, empty, or whitespace.", nameof(oldTag)); } if (string.IsNullOrWhiteSpace(newTag)) { throw new ArgumentNullException($"'{nameof(newTag)}' cannot be null, empty, or whitespace.", nameof(newTag)); } if (!journalReader.FrontMatter.HasTags()) { throw new InvalidOperationException("No tags exist in this journal."); } var currentTags = journalReader.FrontMatter.Tags.ToList(); var oldItemIndex = currentTags.IndexOf(oldTag); if (oldItemIndex < 0) { throw new InvalidOperationException($"The tag '{oldTag}' does not exist."); } currentTags[oldItemIndex] = newTag; var readmeExpression = new ReadmeParser(journalReader.FrontMatter.Readme).ToExpression(journalReader.EntryDate); var frontMatter = new JournalFrontMatter(currentTags, readmeExpression); var newEntry = frontMatter.ToString(asFrontMatter: true) + journalReader.RawBody; _fileSystem.File.WriteAllText(journalReader.FilePath, newEntry); }
protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics) { var exp = arguments.ToString(); var parser = new ReadmeParser(arguments.ToString()); if (parser.IsValid) { return; } var message = $"{Environment.NewLine}The value you entered '{exp}' is not a valid readme expression. Valid expressions are specific dates, " + "such as 12/19/2021, or a time period written in the form of '{integer} {time period}', where 'integer' " + "is any positive number greater than zero and 'time period' is either 'days', 'weeks', 'months', or 'years'. " + "For example, '15 weeks' or '12 years'."; throw new PSArgumentException(message.Wrap()); }