/// <summary>
 /// Internal Constructor.
 /// </summary>
 /// <param name="result"></param>
 /// <inheritdoc />
 internal BumpResultEventArgs(IBumpResult result)
 {
     Result = result;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="result"></param>
 protected void OnBumpResultFound(IBumpResult result)
 => BumpResultFound?.Invoke(this, new BumpResultEventArgs(result));
Example #3
0
        /// <inheritdoc />
        public bool TryBumpResult(string given, IBumpResult result)
        {
            // TODO: TBD: provide hooks whether to 1) Support Wildcard, and 2) Supports Pre-Release Semantic Version.
            const char dot      = '.';
            const char wildcard = '*';

            var match = VersionRegex.Match(given);

            // ReSharper disable once LocalNameCapturedOnly
            string version;

            if (!(match.Groups.HasGroupName(nameof(version)) ||
                  match.Groups[nameof(version)].Success))
            {
                return(result.DidBump);
            }

            /* Capture this once and once only across the breadth of the request. Yes, we want
             * to capture a whole new collection, because any prior matches in the same file will
             * also need to have been bumped in a consistent manner, including any More Significant
             * Providers in prior sequences. */

            var versionProviders = Descriptor.VersionProviders.ToArray();

            // TODO: TBD: should "wildcard" be a special field? potentially, it could even be its own Version Provider, i.e. WildcardVersionProvider ...
            // TODO: TBD: which would be a long-handed way of saying "*" ... but it might make better sense than reading around the wildcard field ...
            var versionElements = (result.OldVersionString = match.Groups[nameof(version)].Value).Split(dot);
            var hasWildcard     = versionElements.Any(x => x == $"{wildcard}");

            var changedElements = versionElements
                                  .Take(versionElements.Length - (hasWildcard ? 1 : 0))
                                  .Zip(versionProviders.Take(versionElements.Length - (hasWildcard ? 1 : 0))
                                       , (x, p) => p.TryChange(x, out var y) ? y : x).ToArray();

            result.VersionString = version = Join($"{dot}", changedElements);

            if (hasWildcard)
            {
                // ReSharper disable once RedundantAssignment
                result.VersionString = version = Join($"{dot}", version, $"{wildcard}");
            }

            /* TODO: TBD: there is a corner case in here whereby we would not necessarily want a
             * Wildcard to coexist with a Semantic, but we will leave that go for the time being... */
            string semantic;

            // ReSharper disable once InvertIf
            // Having the Group does not necessarily mean Successful Match.
            if (match.Groups.HasGroupName(nameof(semantic)) &&
                match.Groups[nameof(semantic)].Success)
            {
                semantic = result.OldSemanticString = match.Groups[nameof(semantic)].Value;

                /* Semantic simply falls through as-is when there is no Provider given.
                 * However, not being given a Provider is effectively a NoOp. */
                var p = versionProviders.OfType <PreReleaseIncrementVersionProvider>().SingleOrDefault()
                        ?? (IVersionProvider)Registry.NoOp;

                /* The difference here is subtle. We only want to Pre-Release to Bump when there
                 * has been an actual change. However, No-op should pass through regardless. */
                if (p is PreReleaseIncrementVersionProvider preRelease &&
                    preRelease.TryChange(semantic, out var newSemantic))
                {
                    result.SemanticString = newSemantic;
                }
                else
                {
                    p.TryChange(semantic, out var noOpSemantic);
                    result.SemanticString = noOpSemantic;
                }
            }