2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
module Discourse
|
2020-07-09 14:57:27 -04:00
|
|
|
VERSION_REGEXP ||= /\A\d+\.\d+\.\d+(\.beta\d+)?\z/
|
|
|
|
VERSION_COMPATIBILITY_FILENAME ||= ".discourse-compatibility"
|
2013-08-07 18:47:04 -04:00
|
|
|
# work around reloader
|
|
|
|
unless defined?(::Discourse::VERSION)
|
2013-03-12 23:06:58 -04:00
|
|
|
module VERSION #:nodoc:
|
2024-04-04 06:59:29 -04:00
|
|
|
# Use the `version_bump:*` rake tasks to update this value
|
2024-08-27 10:54:25 -04:00
|
|
|
STRING = "3.4.0.beta2-dev"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2023-07-26 09:48:08 -04:00
|
|
|
PARTS = STRING.split(".")
|
|
|
|
private_constant :PARTS
|
|
|
|
|
|
|
|
MAJOR = PARTS[0].to_i
|
|
|
|
MINOR = PARTS[1].to_i
|
|
|
|
TINY = PARTS[2].to_i
|
2024-07-08 02:03:11 -04:00
|
|
|
PRE = PARTS[3]&.split("-", 2)&.[](0)
|
|
|
|
DEV = PARTS[3]&.split("-", 2)&.[](1)
|
2013-03-12 23:06:58 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2015-04-27 13:06:53 -04:00
|
|
|
|
2020-10-12 12:25:06 -04:00
|
|
|
class InvalidVersionListError < StandardError
|
|
|
|
end
|
|
|
|
|
2015-04-27 13:06:53 -04:00
|
|
|
def self.has_needed_version?(current, needed)
|
2020-06-29 03:52:33 -04:00
|
|
|
Gem::Version.new(current) >= Gem::Version.new(needed)
|
2015-04-27 13:06:53 -04:00
|
|
|
end
|
2020-07-06 17:48:00 -04: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
|
2023-07-25 09:04:39 -04:00
|
|
|
def self.find_compatible_resource(version_list, target_version = ::Discourse::VERSION::STRING)
|
2024-05-27 06:27:13 -04:00
|
|
|
return if version_list.blank?
|
2020-07-06 17:48:00 -04:00
|
|
|
|
2020-10-12 12:25:06 -04:00
|
|
|
begin
|
|
|
|
version_list = YAML.safe_load(version_list)
|
|
|
|
rescue Psych::SyntaxError, Psych::DisallowedClass => e
|
|
|
|
end
|
|
|
|
|
|
|
|
raise InvalidVersionListError unless version_list.is_a?(Hash)
|
|
|
|
|
2023-07-25 09:04:39 -04:00
|
|
|
version_list =
|
|
|
|
version_list
|
|
|
|
.transform_keys do |v|
|
|
|
|
Gem::Requirement.parse(v)
|
|
|
|
rescue Gem::Requirement::BadRequirementError => e
|
|
|
|
raise InvalidVersionListError, "Invalid version specifier: #{v}"
|
|
|
|
end
|
|
|
|
.sort_by do |parsed_requirement, _|
|
|
|
|
operator, version = parsed_requirement
|
|
|
|
[version, operator == "<" ? 0 : 1]
|
|
|
|
end
|
2020-07-06 17:48:00 -04:00
|
|
|
|
2023-07-25 09:04:39 -04:00
|
|
|
parsed_target_version = Gem::Version.new(target_version)
|
|
|
|
|
|
|
|
lowest_matching_entry =
|
|
|
|
version_list.find do |parsed_requirement, target|
|
|
|
|
req_operator, req_version = parsed_requirement
|
|
|
|
req_operator = "<=" if req_operator == "="
|
|
|
|
|
|
|
|
if !%w[<= <].include?(req_operator)
|
|
|
|
raise InvalidVersionListError,
|
|
|
|
"Invalid version specifier operator for '#{req_operator} #{req_version}'. Operator must be one of <= or <"
|
|
|
|
end
|
|
|
|
|
2023-12-06 17:25:00 -05:00
|
|
|
resolved_requirement = Gem::Requirement.new("#{req_operator} #{req_version}")
|
2023-07-25 09:04:39 -04:00
|
|
|
resolved_requirement.satisfied_by?(parsed_target_version)
|
2020-07-06 17:48:00 -04:00
|
|
|
end
|
|
|
|
|
2023-07-25 09:04:39 -04:00
|
|
|
return if lowest_matching_entry.nil?
|
|
|
|
|
|
|
|
checkout_version = lowest_matching_entry[1]
|
2021-04-14 10:32:47 -04:00
|
|
|
|
|
|
|
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 17:48:00 -04: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")
|
2023-08-22 14:30:33 -04:00
|
|
|
|
|
|
|
tree_info =
|
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
"git",
|
|
|
|
"-C",
|
|
|
|
path,
|
|
|
|
"ls-tree",
|
|
|
|
"-l",
|
|
|
|
"HEAD",
|
|
|
|
Discourse::VERSION_COMPATIBILITY_FILENAME,
|
|
|
|
)
|
|
|
|
blob_size = tree_info.split[3].to_i
|
|
|
|
|
|
|
|
if blob_size > Discourse::MAX_METADATA_FILE_SIZE
|
|
|
|
$stderr.puts "#{Discourse::VERSION_COMPATIBILITY_FILENAME} file in #{path} too big"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
compat_resource =
|
|
|
|
Discourse::Utils.execute_command(
|
|
|
|
"git",
|
|
|
|
"-C",
|
|
|
|
path,
|
|
|
|
"show",
|
|
|
|
"HEAD@{upstream}:#{Discourse::VERSION_COMPATIBILITY_FILENAME}",
|
2020-07-06 17:48:00 -04:00
|
|
|
)
|
2023-08-22 14:30:33 -04:00
|
|
|
|
|
|
|
Discourse.find_compatible_resource(compat_resource)
|
2020-10-12 12:25:06 -04:00
|
|
|
rescue InvalidVersionListError => e
|
|
|
|
$stderr.puts "Invalid version list in #{path}"
|
2023-08-22 14:30:33 -04:00
|
|
|
rescue Discourse::Utils::CommandError => e
|
|
|
|
nil
|
2020-07-06 17:48:00 -04:00
|
|
|
end
|
2013-03-12 23:06:58 -04:00
|
|
|
end
|