Class: Mixlib::Versioning

Inherits:
Object
  • Object
show all
Defined in:
lib/mixlib/versioning.rb,
lib/mixlib/versioning/format.rb,
lib/mixlib/versioning/version.rb,
lib/mixlib/versioning/exceptions.rb,
lib/mixlib/versioning/format/semver.rb,
lib/mixlib/versioning/format/rubygems.rb,
lib/mixlib/versioning/format/git_describe.rb,
lib/mixlib/versioning/format/opscode_semver.rb,
lib/mixlib/versioning/format/partial_semver.rb

Overview

Author:

Defined Under Namespace

Classes: Error, Format, ParseError, UnknownFormatError

Constant Summary collapse

DEFAULT_FORMATS =
[
  Mixlib::Versioning::Format::GitDescribe,
  Mixlib::Versioning::Format::OpscodeSemVer,
  Mixlib::Versioning::Format::SemVer,
  Mixlib::Versioning::Format::Rubygems,
  Mixlib::Versioning::Format::PartialSemVer,
].freeze
VERSION =
"1.2.12"

Class Method Summary collapse

Class Method Details

.find_target_version(all_versions, filter_version = nil, use_prerelease_versions = false, use_build_versions = false) ⇒ Object

Selects the most recent version from `all_versions` that satisfies the filtering constraints provided by `filter_version`, `use_prerelease_versions`, and `use_build_versions`.

If `filter_version` specifies a release (e.g. 1.0.0), then the target version that is returned will be in the same “release line” (it will have the same major, minor, and patch versions), subject to filtering by `use_prerelease_versions` and `use_build_versions`.

If `filter_version` specifies a pre-release (e.g., 1.0.0-alpha.1), the returned target version will be in the same “pre-release line”, and will only be subject to further filtering by `use_build_versions`; that is, `use_prerelease_versions` is completely ignored.

If `filter_version` specifies a build version (whether it is a pre-release or not), no filtering is performed at all, and `filter_version` is the target version; `use_prerelease_versions` and `use_build_versions` are both ignored.

If `filter_version` is `nil`, then only `use_prerelease_versions` and `use_build_versions` are used for filtering.

In all cases, the returned Format is the most recent one in `all_versions` that satisfies the given constraints.

Examples:

all = %w{ 11.0.0-beta.1
          11.0.0-rc.1
          11.0.0
          11.0.1 }

Mixlib::Versioning.find_target_version(all,
                                       '11.0.1',
                                       true,
                                       true)

Parameters:

  • all_versions (Array<String, Mixlib::Versioning::Format>)

    An array of Format objects. This is the “world” of versions we will be filtering to produce the final target version. Any strings in the array will automatically be converted into instances of Format using parse.

  • filter_version (String, Mixlib::Versioning::Format) (defaults to: nil)

    A version that is used to perform more fine-grained filtering. If a string is passed, parse will be used to instantiate a version.

  • use_prerelease_versions (Boolean) (defaults to: false)

    If true, keep versions with pre-release specifiers. When false, versions in `all_versions` that have a pre-release specifier will be filtered out.

  • use_build_versions (Boolean) (defaults to: false)

    If true, keep versions with build version specifiers. When false, versions in `all_versions` that have a build version specifier will be filtered out.


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/mixlib/versioning.rb', line 131

def self.find_target_version(all_versions,
                             filter_version = nil,
                             use_prerelease_versions = false,
                             use_build_versions = false)

  # attempt to parse a `Mixlib::Versioning::Format` instance if we were
  # passed a string
  unless filter_version.nil? ||
      filter_version.is_a?(Mixlib::Versioning::Format)
    filter_version = Mixlib::Versioning.parse(filter_version)
  end

  all_versions.map! do |v|
    if v.is_a?(Mixlib::Versioning::Format)
      v
    else
      Mixlib::Versioning.parse(v)
    end
  end

  if filter_version && filter_version.build
    # If we've requested a build (whether for a pre-release or release),
    # there's no sense doing any other filtering; just return that version
    filter_version
  elsif filter_version && filter_version.prerelease
    # If we've requested a prerelease version, we only need to see if we
    # want a build version or not.  If so, keep only the build version for
    # that prerelease, and then take the most recent. Otherwise, just
    # return the specified prerelease version
    if use_build_versions
      all_versions.select { |v| v.in_same_prerelease_line?(filter_version) }.max
    else
      filter_version
    end
  else
    # If we've gotten this far, we're either just interested in
    # variations on a specific release, or the latest of all versions
    # (depending on various combinations of prerelease and build status)
    all_versions.select do |v|
      # If we're given a version to filter by, then we're only
      # interested in other versions that share the same major, minor,
      # and patch versions.
      #
      # If we weren't given a version to filter by, then we don't
      # care, and we'll take everything
      in_release_line = if filter_version
                          filter_version.in_same_release_line?(v)
                        else
                          true
                        end

      in_release_line && if use_prerelease_versions && use_build_versions
                           v.prerelease_build?
                         elsif !use_prerelease_versions &&
                             use_build_versions
                           v.release_build?
                         elsif use_prerelease_versions &&
                             !use_build_versions
                           v.prerelease?
                         elsif !use_prerelease_versions &&
                             !use_build_versions
                           v.release?
                         end
    end.max # select the most recent version
  end # if
end

.parse(version_string, format = nil) ⇒ Object

Create a new Format instance given a version string to parse, and an optional format type.

Examples:

Mixlib::Versioning.parse('11.0.0')
Mixlib::Versioning.parse('11.0.0', :semver)
Mixlib::Versioning.parse('11.0.0', 'semver')
Mixlib::Versioning.parse('11.0.0', Mixlib::Versioning::Format::SemVer)

Parameters:

  • version_string (String)

    String representatin of the version to parse

  • format (String, Symbol, Mixlib::Versioning::Format, Array) (defaults to: nil)

    Optional format type to parse the version string as. If this is exluded all version types will be attempted from most specific to most specific with a preference for SemVer formats. If it is an array, only version types in that list will be considered

Returns:

Raises:


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mixlib/versioning.rb', line 57

def self.parse(version_string, format = nil)
  if version_string.is_a?(Mixlib::Versioning::Format)
    version_string
  else
    formats = if format
                [format].flatten.map { |f| Mixlib::Versioning::Format.for(f) }
              else
                DEFAULT_FORMATS
              end
    # Attempt to parse from the most specific formats first.
    parsed_version = nil
    formats.each do |version|
      begin
        break parsed_version = version.new(version_string)
      rescue Mixlib::Versioning::ParseError
        next
      end
    end
    parsed_version
  end
end