public void CommandList(string profile) { var configEntries = _fileUtil.ReadConfigFile(_configFile, profile); var currentProfile = BumpyConfiguration.DefaultProfile; foreach (var config in configEntries) { if (!currentProfile.Equals(config.Profile)) { currentProfile = config.Profile; _writeLine($"[{config.Profile}]"); } var glob = new Glob(config.SearchPattern); var files = _fileUtil.GetFiles(_directory, 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.RegularExpression, out var version, out var marker); if (success) { versionFound = true; if (string.IsNullOrEmpty(marker)) { marker = lineNumber.ToString(); } _writeLine($"{content.File.ToRelativePath(_directory)} ({marker}): {version}"); } lineNumber++; } if (!versionFound) { _writeLine($"{content.File.ToRelativePath(_directory)}: no version found"); } } } }
public void CommandEnsure(string profile) { var configEntries = _fileUtil.ReadConfigFile(_configFile, profile); var versionsPerProfile = new Dictionary <string, List <BumpyVersion> >(); foreach (var config in configEntries) { var glob = new Glob(config.SearchPattern); var files = _fileUtil.GetFiles(_directory, glob); if (!versionsPerProfile.ContainsKey(config.Profile)) { versionsPerProfile.Add(config.Profile, new List <BumpyVersion>()); } foreach (var file in files) { var content = _fileUtil.ReadFileContent(file, config.Encoding); foreach (var line in content.Lines) { var success = VersionFunctions.TryParseVersionInText(line, config.RegularExpression, out var version, out var marker); if (success) { versionsPerProfile[config.Profile].Add(version); } } } } foreach (KeyValuePair <string, List <BumpyVersion> > entry in versionsPerProfile) { var distinctVersions = entry.Value.Distinct(); if (distinctVersions.Count() > 1) { var profileText = string.IsNullOrEmpty(entry.Key) ? string.Empty : $" in profile '{entry.Key}'"; var versions = string.Join(", ", distinctVersions.Select(v => v.ToString())); throw new InvalidDataException($"Found different versions{profileText} ({versions}). See 'bumpy list'."); } } _writeLine("Success"); }
private void WriteTransformation(string profile, Func <BumpyVersion, BumpyVersion> transformFunction) { var configEntries = _fileUtil.ReadConfigFile(_configFile, profile); if (_noOperation) { _writeLine("(NO-OP MODE: Will not persist changes to disk)"); } foreach (var config in configEntries) { var glob = new Glob(config.SearchPattern); var files = _fileUtil.GetFiles(_directory, 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.RegularExpression, 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.RegularExpression, newVersion); dirty = true; } if (string.IsNullOrEmpty(marker)) { marker = lineNumber.ToString(); } _writeLine($"{file.ToRelativePath(_directory)} ({marker}): {oldVersion} -> {newVersion}"); } if (!_noOperation) { newLines.Add(newLine); } lineNumber++; } if (!versionFound) { _writeLine($"{content.File.ToRelativePath(_directory)}: no version found"); } else if (dirty && !_noOperation) { var newContent = new FileContent(file, newLines, content.Encoding); _fileUtil.WriteFileContent(newContent); } } } }
public IEnumerable <FileInfo> GetFiles(DirectoryInfo directory, Glob glob) { return(directory.EnumerateFiles(AllFilesPattern, SearchOption.AllDirectories) .Where(f => glob.IsMatch(f.ToRelativePath(directory)))); }