Ejemplo n.º 1
0
        /// <summary>
        /// <para>s - Default SemVer [1.2.3-beta.4+5]</para>
        /// <para>f - Full SemVer [1.2.3-beta.4+5]</para>
        /// <para>i - Informational SemVer [1.2.3-beta.4+5.Branch.master.BranchType.Master.Sha.000000]</para>
        /// <para>j - Just the SemVer part [1.2.3]</para>
        /// <para>t - SemVer with the tag [1.2.3-beta.4]</para>
        /// <para>l - Legacy SemVer tag for systems which do not support SemVer 2.0 properly [1.2.3-beta4]</para>
        /// <para>lp - Legacy SemVer tag for systems which do not support SemVer 2.0 properly (padded) [1.2.3-beta0004]</para>
        /// </summary>
        public string ToString(string format, IFormatProvider formatProvider = null)
        {
            if (string.IsNullOrEmpty(format))
            {
                format = "s";
            }

            if (formatProvider != null)
            {
                var formatter = formatProvider.GetFormat(GetType()) as ICustomFormatter;

                if (formatter != null)
                {
                    return(formatter.Format(format, this, formatProvider));
                }
            }

            // Check for lp first because the param can vary
            format = format.ToLower();
            if (format.StartsWith("lp", StringComparison.Ordinal))
            {
                // handle the padding
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString(format)) : ToString("j"));
            }

            switch (format)
            {
            case "j":
                return(string.Format("{0}.{1}.{2}", Major, Minor, Patch));

            case "s":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag) : ToString("j"));

            case "t":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("t")) : ToString("j"));

            case "l":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("l")) : ToString("j"));

            case "f":
            {
                var buildMetadata = BuildMetaData.ToString();

                return(!string.IsNullOrEmpty(buildMetadata) ? string.Format("{0}+{1}", ToString("s"), buildMetadata) : ToString("s"));
            }

            case "i":
            {
                var buildMetadata = BuildMetaData.ToString("f");

                return(!string.IsNullOrEmpty(buildMetadata) ? string.Format("{0}+{1}", ToString("s"), buildMetadata) : ToString("s"));
            }

            default:
                throw new ArgumentException(string.Format("Unrecognised format '{0}'", format), "format");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <para>s - Default SemVer [1.2.3-beta.4+5]</para>
        /// <para>f - Full SemVer [1.2.3-beta.4+5]</para>
        /// <para>i - Informational SemVer [1.2.3-beta.4+5.Branch.master.BranchType.Master.Sha.000000]</para>
        /// <para>j - Just the SemVer part [1.2.3]</para>
        /// <para>t - SemVer with the tag [1.2.3-beta.4]</para>
        /// <para>l - Legacy SemVer tag for systems which do not support SemVer 2.0 properly [1.2.3-beta4]</para>
        /// <para>lp - Legacy SemVer tag for systems which do not support SemVer 2.0 properly (padded) [1.2.3-beta0004]</para>
        /// </summary>
        public string ToString(string format, IFormatProvider formatProvider = null)
        {
            if (string.IsNullOrEmpty(format))
            {
                format = "s";
            }

            if (formatProvider != null)
            {
                if (formatProvider.GetFormat(GetType()) is ICustomFormatter formatter)
                {
                    return(formatter.Format(format, this, formatProvider));
                }
            }

            // Check for lp first because the param can vary
            format = format.ToLower();
            if (format.StartsWith("lp", StringComparison.Ordinal))
            {
                // handle the padding
                return(PreReleaseTag.HasTag() ? $"{ToString("j")}-{PreReleaseTag.ToString(format)}" : ToString("j"));
            }

            switch (format)
            {
            case "j":
                return($"{Major}.{Minor}.{Patch}");

            case "s":
                return(PreReleaseTag.HasTag() ? $"{ToString("j")}-{PreReleaseTag}" : ToString("j"));

            case "t":
                return(PreReleaseTag.HasTag() ? $"{ToString("j")}-{PreReleaseTag.ToString("t")}" : ToString("j"));

            case "l":
                return(PreReleaseTag.HasTag() ? $"{ToString("j")}-{PreReleaseTag.ToString("l")}" : ToString("j"));

            case "f":
            {
                var buildMetadata = BuildMetaData.ToString();

                return(!string.IsNullOrEmpty(buildMetadata) ? $"{ToString("s")}+{buildMetadata}" : ToString("s"));
            }

            case "i":
            {
                var buildMetadata = BuildMetaData.ToString("f");

                return(!string.IsNullOrEmpty(buildMetadata) ? $"{ToString("s")}+{buildMetadata}" : ToString("s"));
            }

            default:
                throw new ArgumentException($"Unrecognised format '{format}'", nameof(format));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// <para>s - Default SemVer [1.2.3-beta.4+5]</para>
        /// <para>f - Full SemVer [1.2.3-beta.4+5]</para>
        /// <para>i - Informational SemVer [1.2.3-beta.4+5.Branch.master.BranchType.Master.Sha.000000]</para>
        /// <para>j - Just the SemVer part [1.2.3]</para>
        /// <para>t - SemVer with the tag [1.2.3-beta.4]</para>
        /// <para>l - Legacy SemVer tag for systems which do not support SemVer 2.0 properly [1.2.3-beta4]</para>
        /// <para>lp - Legacy SemVer tag for systems which do not support SemVer 2.0 properly (padded) [1.2.3-beta0004]</para>
        /// </summary>
        public string ToString(string format, IFormatProvider formatProvider = null)
        {
            if (string.IsNullOrEmpty(format))
            {
                format = "s";
            }

            if (formatProvider != null)
            {
                var formatter = formatProvider.GetFormat(GetType()) as ICustomFormatter;

                if (formatter != null)
                {
                    return(formatter.Format(format, this, formatProvider));
                }
            }

            switch (format.ToLower())
            {
            case "j":
                return(string.Format("{0}.{1}.{2}", Major, Minor, Patch));

            case "s":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag) : ToString("j"));

            case "t":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("t")) : ToString("j"));

            case "l":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("l")) : ToString("j"));

            case "lp":
                return(PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("lp")) : ToString("j"));

            case "f":
            {
                var buildMetadata = BuildMetaData.ToString();

                return(!string.IsNullOrEmpty(buildMetadata) ? string.Format("{0}+{1}", ToString("s"), buildMetadata) : ToString("s"));
            }

            case "i":
            {
                var buildMetadata = BuildMetaData.ToString("f");

                return(!string.IsNullOrEmpty(buildMetadata) ? string.Format("{0}+{1}", ToString("s"), buildMetadata) : ToString("s"));
            }

            default:
                throw new ArgumentException(string.Format("Unrecognised format '{0}'", format), "format");
            }
        }