Class: Bundlebun::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/bundlebun/runner.rb

Overview

Runner is the class that bundlebun uses to run the bundled Bun executable.

See Also:

Constant Summary collapse

BINSTUB_PATH =
'bin/bun'
RELATIVE_DIRECTORY =
'lib/bundlebun/vendor/bun'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = '') ⇒ Runner

Intialize the Bundlebun::Runner with arguments to run the Bun runtime later via #call.

Examples:

String as an argument

Bundlebun::Runner.new('--version') # => `bun --version`

Array of strings as an argument

Bundlebun::Runner.new(['add', 'postcss']) # => `bun add postcss`

Parameters:

  • arguments (String, Array<String>) (defaults to: '')

    Command arguments to pass to Bun

See Also:



112
113
114
# File 'lib/bundlebun/runner.rb', line 112

def initialize(arguments = '')
  @arguments = arguments
end

Class Method Details

.binary_pathString

A full path to the bundled Bun binary we run (includes .exe on Windows).

Returns:

  • (String)


69
70
71
72
73
74
# File 'lib/bundlebun/runner.rb', line 69

def binary_path
  return @binary_path if defined?(@binary_path)

  executable = "bun#{Bundlebun::Platform.windows? ? ".exe" : ""}"
  @binary_path = File.join(full_directory, executable)
end

.binary_path_exist?Boolean

Does the bundled Bun binary exist?

Returns:

  • (Boolean)


79
80
81
# File 'lib/bundlebun/runner.rb', line 79

def binary_path_exist?
  File.exist?(binary_path)
end

.binstub_exist?Boolean

Does the binstub exist?

Returns:

  • (Boolean)


96
97
98
# File 'lib/bundlebun/runner.rb', line 96

def binstub_exist?
  File.exist?(binstub_path)
end

.binstub_or_binary_pathString

Returns the preferred way to run Bun when bundlebun is installed.

If the binstub is installed (see binstub_path), use the full path to binstub. If not, use the full binary path for the bundled executable (binary_path).

Returns:

  • (String)


89
90
91
# File 'lib/bundlebun/runner.rb', line 89

def binstub_or_binary_path
  binstub_exist? ? full_binstub_path : binary_path
end

.binstub_pathString

A relative path to binstub that bundlebun usually generates with installation Rake tasks.

For Windows, the binstub path will return the bun.cmd wrapper.

Returns:

  • (String)


36
37
38
# File 'lib/bundlebun/runner.rb', line 36

def binstub_path
  Bundlebun::Platform.windows? ? "#{BINSTUB_PATH}.cmd" : BINSTUB_PATH
end

.callInteger

Runs the Bun runtime with parameters.

A wrapper for new, call.

Examples:

String as an argument

Bundlebun.call('--version') # => `bun --version`

Array of strings as an argument

Bundlebun.call(['add', 'postcss']) # => `bun add postcss`

Parameters:

  • arguments (String, Array<String>)

    Command arguments to pass to Bun

Returns:

  • (Integer)

    Exit status code (127 if executable not found)

See Also:



27
28
29
# File 'lib/bundlebun/runner.rb', line 27

def call(...)
  new(...).call
end

.full_binstub_pathString

A full path to binstub that bundlebun usually generates with installation Rake tasks.

For Windows, that will use the bun.cmd wrapper.

Returns:

  • (String)


45
46
47
# File 'lib/bundlebun/runner.rb', line 45

def full_binstub_path
  File.expand_path(binstub_path)
end

.full_directoryString

A full directory path to the bundled Bun executable from the root of the gem.

Returns:

  • (String)


59
60
61
62
63
# File 'lib/bundlebun/runner.rb', line 59

def full_directory
  return @full_directory if defined?(@full_directory)

  @full_directory = File.expand_path("../../#{relative_directory}", __dir__)
end

.relative_directoryString

A relative directory path to the bundled Bun executable from the root of the gem.

Returns:

  • (String)


52
53
54
# File 'lib/bundlebun/runner.rb', line 52

def relative_directory
  RELATIVE_DIRECTORY
end

Instance Method Details

#callInteger

Runs the Bun executable with previously specified arguments.

Check other methods of Bundlebun::Runner to see how we determine what to run exactly.

Examples:

b = Bundlebun::Runner.new('--version')
b.call

Returns:

  • (Integer)

    Exit status code (127 if executable not found)

See Also:



127
128
129
130
# File 'lib/bundlebun/runner.rb', line 127

def call
  check_executable!
  exec(command)
end