Refactor DSL and Artifacts.

This commit is contained in:
Markus Reiter 2017-10-04 15:47:53 +02:00
parent 57035b3ba4
commit 2c7ef064e4
9 changed files with 35 additions and 42 deletions

View File

@ -20,7 +20,7 @@ module Hbc
end end
def self.for_cask(cask) def self.for_cask(cask)
cask.artifacts[dsl_key].to_a cask.artifacts[self].to_a
end end
# TODO: this sort of logic would make more sense in dsl.rb, or a # TODO: this sort of logic would make more sense in dsl.rb, or a

View File

@ -11,12 +11,6 @@ module Hbc
dsl_key.to_s.prepend("uninstall_").to_sym dsl_key.to_s.prepend("uninstall_").to_sym
end end
def self.for_cask(cask)
[dsl_key, uninstall_dsl_key].flat_map do |key|
[*cask.artifacts[key]].map { |block| new(cask, key => block) }
end
end
attr_reader :directives attr_reader :directives
def initialize(cask, **directives) def initialize(cask, **directives)

View File

@ -218,7 +218,7 @@ module Hbc
end end
def check_generic_artifacts def check_generic_artifacts
cask.artifacts[:artifact].each do |artifact| cask.artifacts[Hbc::Artifact::Artifact].each do |artifact|
unless artifact.target.absolute? unless artifact.target.absolute?
add_error "target must be absolute path for #{artifact.class.english_name} #{artifact.source}" add_error "target must be absolute path for #{artifact.class.english_name} #{artifact.source}"
end end

View File

@ -20,7 +20,7 @@ module Hbc
unless children.count == 1 && unless children.count == 1 &&
!nested_container.directory? && !nested_container.directory? &&
@cask.artifacts[:nested_container].empty? && Artifact::NestedContainer.for_cask(@cask).none? &&
extract_nested_container(nested_container) extract_nested_container(nested_container)
children.each do |src| children.each do |src|

View File

@ -43,7 +43,7 @@ module Hbc
Artifact::Zap, Artifact::Zap,
].freeze ].freeze
ACTIVATABLE_ARTIFACT_TYPES = (ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key) - [:stage_only]).freeze ACTIVATABLE_ARTIFACT_CLASSES = ORDINARY_ARTIFACT_CLASSES - [Artifact::StageOnly]
ARTIFACT_BLOCK_CLASSES = [ ARTIFACT_BLOCK_CLASSES = [
Artifact::PreflightBlock, Artifact::PreflightBlock,
@ -71,11 +71,12 @@ module Hbc
:version, :version,
:appdir, :appdir,
*ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key), *ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key),
*ACTIVATABLE_ARTIFACT_TYPES, *ACTIVATABLE_ARTIFACT_CLASSES.map(&:dsl_key),
*ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] }, *ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] },
].freeze ].freeze
attr_reader :token, :cask attr_reader :cask, :token
def initialize(cask) def initialize(cask)
@cask = cask @cask = cask
@token = cask.token @token = cask.token
@ -175,7 +176,7 @@ module Hbc
DSL::Container.new(*args).tap do |container| DSL::Container.new(*args).tap do |container|
# TODO: remove this backward-compatibility section after removing nested_container # TODO: remove this backward-compatibility section after removing nested_container
if container&.nested if container&.nested
artifacts[:nested_container] << Artifact::NestedContainer.new(cask, container.nested) artifacts[Artifact::NestedContainer] << Artifact::NestedContainer.new(cask, container.nested)
end end
end end
end end
@ -250,15 +251,13 @@ module Hbc
end end
ORDINARY_ARTIFACT_CLASSES.each do |klass| ORDINARY_ARTIFACT_CLASSES.each do |klass|
type = klass.dsl_key define_method(klass.dsl_key) do |*args|
define_method(type) do |*args|
begin begin
if [*artifacts.keys, type].include?(:stage_only) && (artifacts.keys & ACTIVATABLE_ARTIFACT_TYPES).any? if [*artifacts.keys, klass].include?(Artifact::StageOnly) && (artifacts.keys & ACTIVATABLE_ARTIFACT_CLASSES).any?
raise CaskInvalidError.new(cask, "'stage_only' must be the only activatable artifact.") raise CaskInvalidError.new(cask, "'stage_only' must be the only activatable artifact.")
end end
artifacts[type].add(klass.from_args(cask, *args)) artifacts[klass].add(klass.from_args(cask, *args))
rescue CaskInvalidError rescue CaskInvalidError
raise raise
rescue StandardError => e rescue StandardError => e
@ -270,7 +269,7 @@ module Hbc
ARTIFACT_BLOCK_CLASSES.each do |klass| ARTIFACT_BLOCK_CLASSES.each do |klass|
[klass.dsl_key, klass.uninstall_dsl_key].each do |dsl_key| [klass.dsl_key, klass.uninstall_dsl_key].each do |dsl_key|
define_method(dsl_key) do |&block| define_method(dsl_key) do |&block|
artifacts[dsl_key] << block artifacts[klass] << klass.new(cask, dsl_key => block)
end end
end end
end end

View File

@ -3,17 +3,17 @@ require "hbc/system_command"
module Hbc module Hbc
class DSL class DSL
class Appcast class Appcast
attr_reader :parameters, :checkpoint attr_reader :uri, :checkpoint, :parameters
def initialize(uri, parameters = {}) def initialize(uri, **parameters)
@parameters = parameters @uri = URI(uri)
@uri = URI(uri) @parameters = parameters
@checkpoint = @parameters[:checkpoint] @checkpoint = parameters[:checkpoint]
end end
def calculate_checkpoint def calculate_checkpoint
curl_executable, *args = curl_args( curl_executable, *args = curl_args(
"--compressed", "--location", "--fail", @uri, "--compressed", "--location", "--fail", uri,
user_agent: :fake user_agent: :fake
) )
result = SystemCommand.run(curl_executable, args: args, print_stderr: false) result = SystemCommand.run(curl_executable, args: args, print_stderr: false)
@ -30,11 +30,11 @@ module Hbc
end end
def to_yaml def to_yaml
[@uri, @parameters].to_yaml [uri, parameters].to_yaml
end end
def to_s def to_s
@uri.to_s uri.to_s
end end
end end
end end

View File

@ -10,14 +10,14 @@ module Hbc
def_delegators :@cask, :token, :version, :caskroom_path, :staged_path, :appdir, :language def_delegators :@cask, :token, :version, :caskroom_path, :staged_path, :appdir, :language
def system_command(executable, options = {}) def system_command(executable, **options)
@command.run!(executable, options) @command.run!(executable, **options)
end end
def method_missing(method, *) def method_missing(method, *)
if method if method
underscored_class = self.class.name.gsub(/([[:lower:]])([[:upper:]][[:lower:]])/, '\1_\2').downcase underscored_class = self.class.name.gsub(/([[:lower:]])([[:upper:]][[:lower:]])/, '\1_\2').downcase
section = underscored_class.downcase.split("::").last section = underscored_class.split("::").last
Utils.method_missing_message(method, @cask.to_s, section) Utils.method_missing_message(method, @cask.to_s, section)
nil nil
else else

View File

@ -4,7 +4,7 @@ module Hbc
index = 0 if index == :first index = 0 if index == :first
index = 1 if index == :second index = 1 if index == :second
index = -1 if index == :last index = -1 if index == :last
@cask.artifacts[:app].to_a.at(index).target.join("Contents", "Info.plist") @cask.artifacts[Artifact::App].to_a.at(index).target.join("Contents", "Info.plist")
end end
def plist_exec(cmd) def plist_exec(cmd)

View File

@ -216,12 +216,12 @@ describe Hbc::DSL, :cask do
app "Bar.app" app "Bar.app"
end end
expect(cask.artifacts[:app].map(&:to_s)).to eq(["Foo.app (App)", "Bar.app (App)"]) expect(cask.artifacts[Hbc::Artifact::App].map(&:to_s)).to eq(["Foo.app (App)", "Bar.app (App)"])
end end
it "allow app stanzas to be empty" do it "allow app stanzas to be empty" do
cask = Hbc::Cask.new("cask-with-no-apps") cask = Hbc::Cask.new("cask-with-no-apps")
expect(cask.artifacts[:app]).to be_empty expect(cask.artifacts[Hbc::Artifact::App]).to be_empty
end end
end end
@ -249,7 +249,7 @@ describe Hbc::DSL, :cask do
pkg "Bar.pkg" pkg "Bar.pkg"
end end
expect(cask.artifacts[:pkg].map(&:to_s)).to eq(["Foo.pkg (Pkg)", "Bar.pkg (Pkg)"]) expect(cask.artifacts[Hbc::Artifact::Pkg].map(&:to_s)).to eq(["Foo.pkg (Pkg)", "Bar.pkg (Pkg)"])
end end
end end
@ -501,10 +501,10 @@ describe Hbc::DSL, :cask do
let(:token) { "with-installer-script" } let(:token) { "with-installer-script" }
it "allows installer script to be specified" do it "allows installer script to be specified" do
expect(cask.artifacts[:installer].first.path).to eq(Pathname("/usr/bin/true")) expect(cask.artifacts[Hbc::Artifact::Installer].first.path).to eq(Pathname("/usr/bin/true"))
expect(cask.artifacts[:installer].first.args[:args]).to eq(["--flag"]) expect(cask.artifacts[Hbc::Artifact::Installer].first.args[:args]).to eq(["--flag"])
expect(cask.artifacts[:installer].to_a[1].path).to eq(Pathname("/usr/bin/false")) expect(cask.artifacts[Hbc::Artifact::Installer].to_a[1].path).to eq(Pathname("/usr/bin/false"))
expect(cask.artifacts[:installer].to_a[1].args[:args]).to eq(["--flag"]) expect(cask.artifacts[Hbc::Artifact::Installer].to_a[1].args[:args]).to eq(["--flag"])
end end
end end
@ -512,7 +512,7 @@ describe Hbc::DSL, :cask do
let(:token) { "with-installer-manual" } let(:token) { "with-installer-manual" }
it "allows installer manual to be specified" do it "allows installer manual to be specified" do
installer = cask.artifacts[:installer].first installer = cask.artifacts[Hbc::Artifact::Installer].first
expect(installer).to be_a(Hbc::Artifact::Installer::ManualInstaller) expect(installer).to be_a(Hbc::Artifact::Installer::ManualInstaller)
expect(installer.path).to eq(cask.staged_path.join("Caffeine.app")) expect(installer.path).to eq(cask.staged_path.join("Caffeine.app"))
end end
@ -524,7 +524,7 @@ describe Hbc::DSL, :cask do
let(:token) { "stage-only" } let(:token) { "stage-only" }
it "allows stage_only stanza to be specified" do it "allows stage_only stanza to be specified" do
expect(cask.artifacts[:stage_only]).not_to be_empty expect(cask.artifacts[Hbc::Artifact::StageOnly]).not_to be_empty
end end
end end
@ -550,7 +550,7 @@ describe Hbc::DSL, :cask do
let(:token) { "appdir-interpolation" } let(:token) { "appdir-interpolation" }
it "is allowed" do it "is allowed" do
expect(cask.artifacts[:binary].first.source).to eq(Hbc.appdir/"some/path") expect(cask.artifacts[Hbc::Artifact::Binary].first.source).to eq(Hbc.appdir/"some/path")
end end
end end
@ -563,7 +563,7 @@ describe Hbc::DSL, :cask do
binary "#{appdir}/some/path" binary "#{appdir}/some/path"
end end
expect(cask.artifacts[:binary].first.source).to eq(original_appdir/"some/path") expect(cask.artifacts[Hbc::Artifact::Binary].first.source).to eq(original_appdir/"some/path")
ensure ensure
Hbc.appdir = original_appdir Hbc.appdir = original_appdir
end end