Esempio n. 1
0
        /// <summary>
        /// Checks if all versions in a profile are equal. Throws an Exception if not.
        /// </summary>
        /// <param name="arguments">A custom set of arguments</param>
        public void Ensure(BumpyArguments arguments)
        {
            var configEntries      = _fileUtil.ReadConfigFile(arguments.ConfigFile, arguments.Profile);
            var versionsPerProfile = new Dictionary <string, List <BumpyVersion> >();

            foreach (var config in configEntries)
            {
                var glob  = new Glob(config.Glob);
                var files = _fileUtil.GetFiles(arguments.WorkingDirectory, 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.Regex, out var version, out _);

                        if (success)
                        {
                            versionsPerProfile[config.Profile].Add(version);
                        }
                    }
                }
            }

            foreach (KeyValuePair <string, List <BumpyVersion> > entry in versionsPerProfile)
            {
                var distinctVersions = entry.Value.Distinct().ToList();

                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}).");
                }

                if (string.IsNullOrWhiteSpace(entry.Key))
                {
                    _writeLine(entry.Value.First().ToString());
                }
                else
                {
                    _writeLine($"{entry.Key}: {entry.Value.First()}");
                }
            }
        }
Esempio n. 2
0
        /// <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");
                    }
                }
            }
        }
Esempio n. 3
0
        /// <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);
                    }
                }
            }
        }