Skip to content

Commit

Permalink
Display instruction defined by more elegantly when the confition is s…
Browse files Browse the repository at this point in the history
…imple
  • Loading branch information
dhower-qc committed Jan 14, 2025
1 parent 5846c50 commit c9abcf5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backends/common_templates/adoc/inst.adoc.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ Operation::
Included in::
<%- if inst.defined_by_condition.flat? -%>
|===
| Extension | Version
<%- inst.defined_by_condition.flat_versions.each do |r| -%>
| *<%= r.name %>* | <%= r.requirement_specs_to_s %>
<%- end -%>
|===
<%- else -%>
<%= inst.defined_by_condition.to_asciidoc %>
<%- end -%>
16 changes: 16 additions & 0 deletions backends/ext_pdf_doc/templates/ext_pdf.adoc.erb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ Requires::
--
<%- end -%>

<<<
== Conventions

Version requirements are specified as conditions using the following operators:

[cols="1,2"]
|===
| Operator | Meaning

| `~> VERSION` | Accepts any version that is _compatible_ with `VERSION`. By default, a version A is compatible with a version B if A's version number is greater than or equal to version B. If that default assumption is ever broken, it will be noted in the list of extension versions.
| `= VERSION` | Accepts only version `VERSION`.
| `>= VERSION` | Accepts any version greater than or equal to `VERSION`.
| `\<= VERSION` | Accepts any version less than or equal to `VERSION`.
| `> VERSION` | Accepts any version greater than `VERSION`.
| `< VERSION` | Accepts any version less than `VERSION`.
|===

<<<
== Extension description
Expand Down
62 changes: 62 additions & 0 deletions lib/arch_obj_models/obj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,66 @@ def is_a_version_requirement(ver)
end
end

# @return [Boolean] True if the condition is a join of N terms over the same operator
#
# A or B or C #=> true
# A and B #=> true
# A or B and C #=> false
def flat?
case @hsh
when String
true
when Hash
@hsh.key?("name") || @hsh[@hsh.keys.first].all? { |child| child.is_a?(String) || (child.is_a?(Hash) && child.key?("name")) }
else
raise "unexpected"
end
end

# @return [:or, :and] The operator for a flat condition
# Only valid if #flat? is true
def flat_op
case @hsh
when String
:or
when Hash
@hsh.key?("name") ? :or : { "allOf" => :and, "anyOf" => :or }[@hsh.keys.first]
else
raise "unexpected"
end
end

# @return [Array<ExtensionRequirement>] The elements of the flat join
# Only valid if #flat? is true
def flat_versions
case @hsh
when String
[ExtensionRequirement.new(@hsh, arch: @arch)]
when Hash
if @hsh.key?("name")
if @hsh.key?("version").nil?
[ExtensionRequirement.new(@hsh["name"], arch: @arch)]
else
[ExtensionRequirement.new(@hsh["name"], @hsh["version"], arch: @arch)]
end
else
@hsh[@hsh.keys.first].map do |r|
if r.is_a?(String)
ExtensionRequirement.new(r, arch: @arch)
else
if r.key?("version").nil?
ExtensionRequirement.new(r["name"], arch: @arch)
else
ExtensionRequirement.new(r["name"], r["version"], arch: @arch)
end
end
end
end
else
raise "unexpected"
end
end

def to_asciidoc(cond = @hsh, indent = 0)
case cond
when String
Expand Down Expand Up @@ -616,6 +676,8 @@ def satisfied_by? = true

def empty? = true

def flat? = false

def to_h = {}
def minimize = {}
end

0 comments on commit c9abcf5

Please sign in to comment.