/// <summary> /// Lists all versions. /// </summary> /// <param name="arguments">A custom set of arguments</param> public void List(BumpyArguments arguments) { var configEntries = _fileUtil.ReadConfigFile(arguments.ConfigFile, arguments.Profile); var currentProfile = BumpyConfigEntry.DefaultProfile; foreach (var config in configEntries) { if (!currentProfile.Equals(config.Profile)) { currentProfile = config.Profile; _writeLine($"[{config.Profile}]"); } var glob = new Glob(config.Glob); var files = _fileUtil.GetFiles(arguments.WorkingDirectory, glob); foreach (var file in files) { var content = _fileUtil.ReadFileContent(file, config.Encoding); var lineNumber = 1; var versionFound = false; foreach (var line in content.Lines) { var success = VersionFunctions.TryParseVersionInText(line, config.Regex, out var version, out var marker); if (success) { versionFound = true; if (string.IsNullOrEmpty(marker)) { marker = lineNumber.ToString(); } _writeLine($"{content.File.ToRelativePath(arguments.WorkingDirectory)} ({marker}): {version}"); } lineNumber++; } if (!versionFound) { _writeLine($"{content.File.ToRelativePath(arguments.WorkingDirectory)}: no version found"); } } } }
/// <inheritdoc/> public IEnumerable <FileInfo> GetFiles(DirectoryInfo directory, Glob glob) { return(directory .EnumerateFiles(AllFilesPattern, SearchOption.AllDirectories) .Where(f => glob.IsMatch(f.ToRelativePath(directory)))); }
/// <summary> /// A generalized transform method which can be used to create custom behavior. /// All file based Bumpy operations (e.g. Increment, Write, ...) are based on this method. /// </summary> /// <param name="transformFunction">A function which creates a new version based on an old version</param> /// <param name="arguments">A custom set of arguments</param> public void ApplyTransformation(Func <BumpyVersion, BumpyVersion> transformFunction, BumpyArguments arguments) { var configEntries = _fileUtil.ReadConfigFile(arguments.ConfigFile, arguments.Profile); var currentProfile = BumpyConfigEntry.DefaultProfile; if (arguments.NoOperation) { _writeLine("(NO-OP MODE: Will not persist changes to disk)"); } foreach (var config in configEntries) { if (!currentProfile.Equals(config.Profile)) { currentProfile = config.Profile; _writeLine($"[{config.Profile}]"); } var glob = new Glob(config.Glob); var files = _fileUtil.GetFiles(arguments.WorkingDirectory, glob); foreach (var file in files) { var content = _fileUtil.ReadFileContent(file, config.Encoding); var lineNumber = 1; var newLines = new List <string>(); var versionFound = false; var dirty = false; foreach (var line in content.Lines) { var newLine = line; var success = VersionFunctions.TryParseVersionInText(line, config.Regex, out var oldVersion, out var marker); if (success) { versionFound = true; var newVersion = transformFunction(oldVersion); if (!newVersion.Equals(oldVersion)) { newLine = line.Replace(oldVersion.ToString(), newVersion.ToString()); VersionFunctions.EnsureExpectedVersion(newLine, config.Regex, newVersion); dirty = true; } if (string.IsNullOrEmpty(marker)) { marker = lineNumber.ToString(); } _writeLine($"{file.ToRelativePath(arguments.WorkingDirectory)} ({marker}): {oldVersion} -> {newVersion}"); } if (!arguments.NoOperation) { newLines.Add(newLine); } lineNumber++; } if (!versionFound) { _writeLine($"{content.File.ToRelativePath(arguments.WorkingDirectory)}: no version found"); } else if (dirty && !arguments.NoOperation) { var newContent = new FileContent(file, newLines, content.Encoding); _fileUtil.WriteFileContent(newContent); } } } }