前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >How to parse version range

How to parse version range

作者头像
林德熙
发布2018-09-18 17:34:15
5360
发布2018-09-18 17:34:15
举报
文章被收录于专栏:林德熙的博客林德熙的博客

Now we are making a solution that has to get the package reference. But the version of package reference is a range and the default version parser need input a version but not a version range. This post will tell you how to parse the version range string to reference version.

The format for reference version is like this

代码语言:javascript
复制
[2.1.0.293,3.0)
[1.1.0.34,2.0)
(1.1.0.34,2.0]
2.1

For parse the reference version string, we should make some property.

代码语言:javascript
复制
    public class ReferenceVersion
    {
        public ReferenceVersion(Version version)
        {
            Version = version;
            MinVersion = version;
            MaxVersion = version;
            IsIncludeMaxVersion = true;
            IsIncludeMinVersion = true;
        }

        public ReferenceVersion(Version minVersion, Version maxVersion, bool isIncludeMinVersion,
            bool isIncludeMaxVersion)
        {
            Version = null;
            MinVersion = minVersion;
            MaxVersion = maxVersion;
            IsIncludeMinVersion = isIncludeMinVersion;
            IsIncludeMaxVersion = isIncludeMaxVersion;
        }

        public Version Version { get; }

        public Version MinVersion { get; }

        public Version MaxVersion { get; }

        public bool IsIncludeMinVersion { get; }

        public bool IsIncludeMaxVersion { get; }
}

I will use regex to get the string and parse the string to version.

代码语言:javascript
复制
      public static ReferenceVersion Parser(string str)
        {
            if (_regex == null)
            {
                _regex = new Regex(@"([(|\[])([\d|.]*),([\d|.]*)([)|\]])", RegexOptions.Compiled);
            }

            var res = _regex.Match(str);

            if (res.Success)
            {
                var isIncludeMinVersion = res.Groups[1].Value;
                var minVersion = res.Groups[2].Value;
                var maxVersion = res.Groups[3].Value;
                var isIncludeMaxVersion = res.Groups[4].Value;

                return new ReferenceVersion
                (
                    string.IsNullOrEmpty(minVersion) ? null : Version.Parse(minVersion),
                    string.IsNullOrEmpty(maxVersion) ? null : Version.Parse(maxVersion),
                    isIncludeMinVersion.Equals("["),
                    isIncludeMaxVersion.Equals("]")
                );
            }

            return new ReferenceVersion(Version.Parse(str));
        }

        private static Regex _regex;

We can get the reference version in the solution file and know the solution reference package.

Full code:

代码语言:javascript
复制
    /// <summary>
    ///     引用的版本
    /// 用来转换  [2.1.0.293,3.0)、 (1.1.0.3,2.0]、 5.2 的版本
    /// </summary>
    public class ReferenceVersion
    {
        /// <summary>
        ///     创建引用版本
        /// </summary>
        /// <param name="version">版本</param>
        public ReferenceVersion(Version version)
        {
            Version = version;
            MinVersion = version;
            MaxVersion = version;
            IsIncludeMaxVersion = true;
            IsIncludeMinVersion = true;
        }

        /// <summary>
        ///     创建引用版本
        /// </summary>
        /// <param name="minVersion">最低版本</param>
        /// <param name="maxVersion">最高版本</param>
        /// <param name="isIncludeMinVersion">是否包括最低版本</param>
        /// <param name="isIncludeMaxVersion">是否包括最高版本</param>
        public ReferenceVersion(Version minVersion, Version maxVersion, bool isIncludeMinVersion,
            bool isIncludeMaxVersion)
        {
            Version = null;
            MinVersion = minVersion;
            MaxVersion = maxVersion;
            IsIncludeMinVersion = isIncludeMinVersion;
            IsIncludeMaxVersion = isIncludeMaxVersion;
        }


        /// <summary>
        ///     版本
        /// </summary>
        public Version Version { get; }

        /// <summary>
        ///     最低版本
        /// </summary>
        public Version MinVersion { get; }

        /// <summary>
        ///     最高版本
        /// </summary>
        public Version MaxVersion { get; }

        /// <summary>
        ///     是否包括最低版本
        /// </summary>
        public bool IsIncludeMinVersion { get; }

        /// <summary>
        ///     是否包括最高版本
        /// </summary>
        public bool IsIncludeMaxVersion { get; }

        /// <summary>
        ///     转换版本
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static ReferenceVersion Parser(string str)
        {
            if (_regex == null)
            {
                _regex = new Regex(@"([(|\[])([\d|.]*),([\d|.]*)([)|\]])", RegexOptions.Compiled);
            }

            var res = _regex.Match(str);

            if (res.Success)
            {
                var isIncludeMinVersion = res.Groups[1].Value;
                var minVersion = res.Groups[2].Value;
                var maxVersion = res.Groups[3].Value;
                var isIncludeMaxVersion = res.Groups[4].Value;

                return new ReferenceVersion
                (
                    string.IsNullOrEmpty(minVersion) ? null : Version.Parse(minVersion),
                    string.IsNullOrEmpty(maxVersion) ? null : Version.Parse(maxVersion),
                    isIncludeMinVersion.Equals("["),
                    isIncludeMaxVersion.Equals("]")
                );
            }

            return new ReferenceVersion(Version.Parse(str));
        }

        private static Regex _regex;
    }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档