Exemple #1
0
        internal static VersionCliArgs GetVersionBumpFromRemainingArgs(List <string> remainingArguments,
                                                                       OutputFormat outputFormat, bool doVcs, bool dryRunEnabled)
        {
            if (remainingArguments == null || !remainingArguments.Any())
            {
                var msgEx =
                    "No version bump specified, please specify one of:\n\tmajor | minor | patch | <specific version>";
                // ReSharper disable once NotResolvedInText
                throw new ArgumentException(msgEx, "versionBump");
            }

            var args = new VersionCliArgs {
                OutputFormat = outputFormat, DoVcs = doVcs, DryRun = dryRunEnabled
            };
            var bump = VersionBump.Patch;

            foreach (var arg in remainingArguments)
            {
                if (Enum.TryParse(arg, true, out bump))
                {
                    break;
                }

                var ver = SemVer.FromString(arg);
                args.SpecificVersionToApply = ver.ToVersionString();
                bump = VersionBump.Specific;
            }

            args.VersionBump = bump;
            return(args);
        }
        /// <summary>
        /// Bump the currently parsed version information with the specified <paramref name="bump"/>
        /// </summary>
        /// <param name="bump">The bump to apply to the version</param>
        /// <param name="specificVersionToApply">The specific version to apply if bump is Specific</param>
        public void Bump(VersionBump bump, string specificVersionToApply = "")
        {
            switch (bump)
            {
            case VersionBump.Major:
            {
                Major += 1;
                Minor  = 0;
                Patch  = 0;
                break;
            }

            case VersionBump.Minor:
            {
                Minor += 1;
                Patch  = 0;
                break;
            }

            case VersionBump.Patch:
            {
                Patch += 1;
                break;
            }

            case VersionBump.Specific:
            {
                if (string.IsNullOrEmpty(specificVersionToApply))
                {
                    throw new ArgumentException($"When bump is specific, specificVersionToApply must be provided");
                }
                var specific = SemVer.FromString(specificVersionToApply);
                Major = specific.Major;
                Minor = specific.Minor;
                Patch = specific.Patch;
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException($"VersionBump : {bump} not supported");
            }
            }
        }
        public VersionInfo Execute(VersionCliArgs args)
        {
            if (!args.DryRun && args.DoVcs && !_vcsTool.IsVcsToolPresent())
            {
                throw new OperationCanceledException(
                          $"Unable to find the vcs tool {_vcsTool.ToolName()} in your path");
            }

            if (!args.DryRun && args.DoVcs && !_vcsTool.IsRepositoryClean())
            {
                throw new OperationCanceledException(
                          "You currently have uncomitted changes in your repository, please commit these and try again");
            }

            var csProjXml = _fileDetector.FindAndLoadCsProj(args.CsProjFilePath);

            _fileParser.Load(csProjXml);

            var semVer = SemVer.FromString(_fileParser.Version);

            semVer.Bump(args.VersionBump, args.SpecificVersionToApply);
            var newVersion = semVer.ToVersionString();

            if (!args.DryRun) // if we are not in dry run mode, then we should go ahead
            {
                var patchedCsProjXml = _fileVersionPatcher.Patch(
                    csProjXml,
                    _fileParser.Version,
                    newVersion
                    );
                _fileVersionPatcher.Flush(
                    patchedCsProjXml,
                    _fileDetector.ResolvedCsProjFile
                    );

                if (args.DoVcs)
                {
                    // Run git commands
                    _vcsTool.Commit(_fileDetector.ResolvedCsProjFile, $"v{newVersion}");
                    _vcsTool.Tag($"v{newVersion}");
                }
            }

            var theOutput = new VersionInfo
            {
                Product = new ProductOutputInfo
                {
                    Name    = ProductInfo.Name,
                    Version = ProductInfo.Version
                },
                OldVersion      = _fileParser.Version,
                NewVersion      = newVersion,
                ProjectFile     = _fileDetector.ResolvedCsProjFile,
                VersionStrategy = args.VersionBump.ToString().ToLowerInvariant()
            };


            if (args.OutputFormat == OutputFormat.Json)
            {
                WriteJsonToStdout(theOutput);
            }
            else
            {
                Console.WriteLine($"Bumped {_fileDetector.ResolvedCsProjFile} to version {newVersion}");
            }

            return(theOutput);
        }