Skip to content

Commit

Permalink
cask/artifact/exec_script: add DSL to create exec scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cho-m committed Oct 4, 2024
1 parent 3994768 commit 7316d96
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions Library/Homebrew/cask/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "cask/artifact/binary"
require "cask/artifact/colorpicker"
require "cask/artifact/dictionary"
require "cask/artifact/exec_script"
require "cask/artifact/font"
require "cask/artifact/input_method"
require "cask/artifact/installer"
Expand Down
67 changes: 67 additions & 0 deletions Library/Homebrew/cask/artifact/exec_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true

require "cask/artifact/binary"

module Cask
module Artifact
# Artifact corresponding to the `exec_script` stanza.
class ExecScript < Binary
DEFAULT_ARGS = ["$@"].freeze

def self.from_args(cask, source, **args)
new(cask, source, **args)

Check warning on line 13 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L13

Added line #L13 was not covered by tests
end

sig {
params(

Check warning on line 17 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L17

Added line #L17 was not covered by tests
cask: Cask,
source: T.any(String, Pathname),
# File name for installed script. The default is the basename of `source`
target: T.nilable(String),
# File name for wrapper script. The default is `#{target}.wrapper.sh`
wrapper: T.nilable(String),
# Arguments added to exec script for source command
args: T.nilable(T::Array[T.any(String, Pathname)]),
# Set to `false` to disable adding double quotes around each argument in `args`
quote_args: T::Boolean,
# The shell for shebang. The default is `/bin/bash`
shell: String,
# A location to send standard error. Nothing by default. Can be set to `/dev/null` or a file
stderr: T.nilable(T.any(String, Pathname)),
# Directory to change to before running exec command
chdir: T.nilable(T.any(String, Pathname)),
).void
}
def initialize(cask, source, target: nil, wrapper: nil, args: nil,
quote_args: true, shell: "/bin/bash", stderr: nil, chdir: nil)
raise CaskInvalidError, "`wrapper` must be a file name instead of a path" if wrapper&.include?("/")
raise CaskInvalidError, "`target` must be a file name instead of a path" if target&.include?("/")

@command = cask.staged_path.join(source)
@wrapper = cask.staged_path.join(wrapper || "#{target || @command.basename}.wrapper.sh")
@args = args || DEFAULT_ARGS
@quote_args = quote_args
@shell = shell
@stderr = stderr
@chdir = chdir

Check warning on line 47 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L41-L47

Added lines #L41 - L47 were not covered by tests

super(cask, @wrapper, target: target || @command.basename)

Check warning on line 49 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L49

Added line #L49 was not covered by tests
end

def install_phase(**options)
require "utils/shell"

Check warning on line 53 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L53

Added line #L53 was not covered by tests

chdir = "cd #{::Utils::Shell.sh_quote(@chdir.to_s)} && " if @chdir
args = (@quote ? @args.map { |arg| "\"#{arg}\"" } : @args).join(" ")
stderr = " 2>#{::Utils::Shell.sh_quote(@stderr.to_s)}" if @stderr

@wrapper.write <<~EOS

Check warning on line 59 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L59

Added line #L59 was not covered by tests
#!#{@shell}
#{chdir}exec "#{@command}" #{args}#{stderr}
EOS
super

Check warning on line 63 in Library/Homebrew/cask/artifact/exec_script.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/artifact/exec_script.rb#L63

Added line #L63 was not covered by tests
end
end
end
end
5 changes: 5 additions & 0 deletions Library/Homebrew/cask/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ def binarydir
@binarydir ||= HOMEBREW_PREFIX/"bin"
end

sig { returns(Pathname) }
def exec_scriptdir
binarydir

Check warning on line 152 in Library/Homebrew/cask/config.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/config.rb#L152

Added line #L152 was not covered by tests
end

sig { returns(Pathname) }
def manpagedir
@manpagedir ||= HOMEBREW_PREFIX/"share/man"
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DSL
Artifact::Binary,
Artifact::Colorpicker,
Artifact::Dictionary,
Artifact::ExecScript,
Artifact::Font,
Artifact::InputMethod,
Artifact::InternetPlugin,
Expand Down

0 comments on commit 7316d96

Please sign in to comment.