2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
module Discourse
|
2020-07-09 13:57:27 -05:00
|
|
|
VERSION_REGEXP ||= /\A\d+\.\d+\.\d+(\.beta\d+)?\z/
|
|
|
|
VERSION_COMPATIBILITY_FILENAME ||= ".discourse-compatibility"
|
2020-07-06 16:48:00 -05:00
|
|
|
|
2013-08-07 17:47:04 -05:00
|
|
|
# work around reloader
|
|
|
|
unless defined? ::Discourse::VERSION
|
2013-03-12 22:06:58 -05:00
|
|
|
module VERSION #:nodoc:
|
2018-01-03 15:54:55 -06:00
|
|
|
MAJOR = 2
|
2021-05-18 15:38:17 -05:00
|
|
|
MINOR = 8
|
2014-08-26 14:24:07 -05:00
|
|
|
TINY = 0
|
2021-06-08 10:27:50 -05:00
|
|
|
PRE = 'beta2'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-03-12 22:06:58 -05:00
|
|
|
STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2015-04-27 12:06:53 -05:00
|
|
|
|
2020-10-12 11:25:06 -05:00
|
|
|
class InvalidVersionListError < StandardError; end
|
|
|
|
|
2015-04-27 12:06:53 -05:00
|
|
|
def self.has_needed_version?(current, needed)
|
2020-06-29 02:52:33 -05:00
|
|
|
Gem::Version.new(current) >= Gem::Version.new(needed)
|
2015-04-27 12:06:53 -05:00
|
|
|
end
|
2020-07-06 16:48:00 -05:00
|
|
|
|
|
|
|
# lookup an external resource (theme/plugin)'s best compatible version
|
|
|
|
# compatible resource files are YAML, in the format:
|
|
|
|
# `discourse_version: plugin/theme git reference.` For example:
|
|
|
|
# 2.5.0.beta6: c4a6c17
|
|
|
|
# 2.5.0.beta4: d1d2d3f
|
|
|
|
# 2.5.0.beta2: bbffee
|
|
|
|
# 2.4.4.beta6: some-other-branch-ref
|
|
|
|
# 2.4.2.beta1: v1-tag
|
2020-07-09 13:57:27 -05:00
|
|
|
def self.find_compatible_resource(version_list, version = ::Discourse::VERSION::STRING)
|
2020-07-06 16:48:00 -05:00
|
|
|
return unless version_list
|
|
|
|
|
2020-10-12 11:25:06 -05:00
|
|
|
begin
|
|
|
|
version_list = YAML.safe_load(version_list)
|
|
|
|
rescue Psych::SyntaxError, Psych::DisallowedClass => e
|
|
|
|
end
|
|
|
|
|
|
|
|
raise InvalidVersionListError unless version_list.is_a?(Hash)
|
|
|
|
|
|
|
|
version_list = version_list.sort_by { |v, pin| Gem::Version.new(v) }.reverse
|
2020-07-06 16:48:00 -05:00
|
|
|
|
|
|
|
# If plugin compat version is listed as less than current Discourse version, take the version/hash listed before.
|
|
|
|
checkout_version = nil
|
|
|
|
version_list.each do |core_compat, target|
|
2020-07-09 13:57:27 -05:00
|
|
|
if Gem::Version.new(core_compat) == Gem::Version.new(version) # Exact version match - return it
|
2020-07-06 16:48:00 -05:00
|
|
|
checkout_version = target
|
|
|
|
break
|
2020-07-09 13:57:27 -05:00
|
|
|
elsif Gem::Version.new(core_compat) < Gem::Version.new(version) # Core is on a higher version than listed, use a later version
|
2020-07-06 16:48:00 -05:00
|
|
|
break
|
|
|
|
end
|
|
|
|
checkout_version = target
|
|
|
|
end
|
|
|
|
|
2021-04-14 09:32:47 -05:00
|
|
|
return if checkout_version.nil?
|
|
|
|
|
|
|
|
begin
|
|
|
|
Discourse::Utils.execute_command "git", "check-ref-format", "--allow-onelevel", checkout_version
|
|
|
|
rescue RuntimeError
|
|
|
|
raise InvalidVersionListError, "Invalid ref name: #{checkout_version}"
|
|
|
|
end
|
|
|
|
|
2020-07-06 16:48:00 -05:00
|
|
|
checkout_version
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find a compatible resource from a git repo
|
|
|
|
def self.find_compatible_git_resource(path)
|
|
|
|
return unless File.directory?("#{path}/.git")
|
|
|
|
compat_resource, std_error, s = Open3.capture3("git -C '#{path}' show HEAD@{upstream}:#{Discourse::VERSION_COMPATIBILITY_FILENAME}")
|
|
|
|
Discourse.find_compatible_resource(compat_resource) if s.success?
|
2020-10-12 11:25:06 -05:00
|
|
|
rescue InvalidVersionListError => e
|
|
|
|
$stderr.puts "Invalid version list in #{path}"
|
2020-07-06 16:48:00 -05:00
|
|
|
end
|
2013-03-12 22:06:58 -05:00
|
|
|
end
|