Octopus.Client.Model.SemanticVersion.TryParse C# (CSharp) Method

TryParse() public static method

Parses a version string using loose semantic versioning rules that allows 2-4 version components followed by an optional special version.
public static TryParse ( string value, SemanticVersion &version, bool preserveMissingComponents = false ) : bool
value string
version SemanticVersion
preserveMissingComponents bool
return bool
        public static bool TryParse(string value, out SemanticVersion version, bool preserveMissingComponents = false)
        {
            version = null;

            if (value != null)
            {
                Version systemVersion = null;

                // trim the value before passing it in since we not strict here
                var sections = ParseSections(value.Trim());

                // null indicates the string did not meet the rules
                if (sections != null
                    && !string.IsNullOrEmpty(sections.Item1))
                {
                    var versionPart = sections.Item1;

                    if (versionPart.IndexOf('.') < 0)
                    {
                        // System.Version requires at least a 2 part version to parse.
                        versionPart += ".0";
                    }

                    if (Version.TryParse(versionPart, out systemVersion))
                    {
                        // labels
                        if (sections.Item2 != null
                            && !sections.Item2.All(s => IsValidPart(s, false)))
                        {
                            return false;
                        }

                        // build metadata
                        if (sections.Item3 != null
                            && !IsValid(sections.Item3, true))
                        {
                            return false;
                        }

                        var ver = preserveMissingComponents  
                            ? systemVersion 
                            : NormalizeVersionValue(systemVersion);

                        var originalVersion = value;

                        if (originalVersion.IndexOf(' ') > -1)
                        {
                            originalVersion = value.Replace(" ", "");
                        }

                        version = new SemanticVersion(version: ver,
                            releaseLabels: sections.Item2,
                            metadata: sections.Item3 ?? string.Empty,
                            originalVersion: originalVersion);

                        return true;
                    }
                }
            }

            return false;
        }

Usage Example

Esempio n. 1
0
        public void Add(string stepNameOrPackageIdAndVersion)
        {
            var split = stepNameOrPackageIdAndVersion.Split(Delimiters);

            if (split.Length < 2)
            {
                throw new CommandException("The package argument '" + stepNameOrPackageIdAndVersion + "' does not use expected format of : {Step Name}:{Version}");
            }

            var stepNameOrPackageId  = split[0];
            var packageReferenceName = split.Length > 2 ? split[1] : WildCard;
            var version = split.Length > 2 ? split[2] : split[1];

            if (string.IsNullOrWhiteSpace(stepNameOrPackageId) || string.IsNullOrWhiteSpace(version))
            {
                throw new CommandException("The package argument '" + stepNameOrPackageIdAndVersion + "' does not use expected format of : {Step Name}:{Version}");
            }

            if (!SemanticVersion.TryParse(version, out var parsedVersion))
            {
                throw new CommandException("The version portion of the package constraint '" + stepNameOrPackageIdAndVersion + "' is not a valid semantic version number.");
            }

            Add(stepNameOrPackageId, packageReferenceName, version);
        }