Class: Bundlebun::Runner

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

Overview

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

bundlebun provides two ways to run Bun:

  • Runner.call (also available as Runner.exec): Replaces the current Ruby process with Bun. This is the default.

  • Runner.system: Runs Bun as a subprocess and returns control to Ruby. Use this when you need to continue executing Ruby code after Bun finishes.

Examples:

Running Bun (replaces process, never returns)

Bundlebun.('install')
Bundlebun.call('outdated')
Bundlebun.call(['add', 'postcss'])

Running Bun as subprocess (returns to Ruby)

if Bundlebun.system('install')
  puts 'Dependencies installed!'
end

See Also:

Constant Summary collapse

BINSTUB_PATH =

Returns:

  • (String)
'bin/bun'
RELATIVE_DIRECTORY =

Returns:

  • (String)
'lib/bundlebun/vendor/bun'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Runner

Initialize the Bundlebun::Runner with arguments to run the Bun runtime later.

Examples:

String as an argument

Bundlebun::Runner.new('--version')

Array of strings as an argument

Bundlebun::Runner.new(['add', 'postcss'])

Parameters:

  • arguments (String, Array<String>) —

    Command arguments to pass to Bun

See Also:



199
200
201
# File 'lib/bundlebun/runner.rb', line 199

def initialize(arguments)
  @arguments = arguments
end

Class Method Details

.binary_path ⇒ String

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

Returns:

  • (String)


139
140
141
142
143
144
# File 'lib/bundlebun/runner.rb', line 139

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

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

.binary_path_exist? ⇒ Boolean

Does the bundled Bun binary exist?

Returns:

  • (Boolean)


149
150
151
# File 'lib/bundlebun/runner.rb', line 149

def binary_path_exist?
  File.exist?(binary_path)
end

.binstub_exist? ⇒ Boolean

Does the binstub exist?

Returns:

  • (Boolean)


182
183
184
# File 'lib/bundlebun/runner.rb', line 182

def binstub_exist?
  File.exist?(binstub_path)
end

.binstub_or_binary_path ⇒ String

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)


159
160
161
# File 'lib/bundlebun/runner.rb', line 159

def binstub_or_binary_path
  binstub_exist? ? full_binstub_path : binary_path
end

.binstub_path ⇒ String

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)


102
103
104
105
106
# File 'lib/bundlebun/runner.rb', line 102

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

  @binstub_path = (Bundlebun::Platform.windows? ? "#{BINSTUB_PATH}.cmd" : BINSTUB_PATH).freeze
end

.call ⇒ bot

Replaces the current Ruby process with Bun. Alias for .exec. Never returns.

Parameters:

  • arguments (String, Array[String])

Returns:

  • (bot)


67
68
69
# File 'lib/bundlebun/runner.rb', line 67

def call(...)
  exec(...)
end

.exec ⇒ bot

Replaces the current Ruby process with Bun. Never returns.

Parameters:

  • arguments (String, Array[String])

Returns:

  • (bot)


48
49
50
# File 'lib/bundlebun/runner.rb', line 48

def exec(...)
  new(...).exec
end

.full_binstub_path ⇒ String

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

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

Returns:

  • (String)


113
114
115
116
117
# File 'lib/bundlebun/runner.rb', line 113

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

  @full_binstub_path = File.expand_path(binstub_path).freeze
end

.full_directory ⇒ String

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

Returns:

  • (String)


129
130
131
132
133
# File 'lib/bundlebun/runner.rb', line 129

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

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

.make_shareable ⇒ void

This method returns an undefined value.

Memoizes every path as frozen values on the main Ractor. Runs when the file is loaded.



172
173
174
175
176
177
# File 'lib/bundlebun/runner.rb', line 172

def make_shareable
  Bundlebun::Platform.windows?
  binary_path
  full_binstub_path
  nil
end

.relative_directory ⇒ String

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

Returns:

  • (String)


122
123
124
# File 'lib/bundlebun/runner.rb', line 122

def relative_directory
  RELATIVE_DIRECTORY
end

.system ⇒ Boolean?

Runs Bun as a subprocess and returns control to Ruby.

Parameters:

  • arguments (String, Array[String])

Returns:

  • (Boolean, nil)


93
94
95
# File 'lib/bundlebun/runner.rb', line 93

def system(...)
  new(...).system
end

Instance Method Details

#call ⇒ bot

Replaces the current Ruby process with Bun. Alias for #exec. Never returns.

Returns:

  • (bot)


228
229
230
# File 'lib/bundlebun/runner.rb', line 228

def call
  exec
end

#exec ⇒ bot

Replaces the current Ruby process with Bun. Never returns.

Returns:

  • (bot)


217
218
219
220
221
# File 'lib/bundlebun/runner.rb', line 217

def exec
  check_executable!
  instrument('exec.bundlebun')
  Kernel.exec(*command)
end

#system ⇒ Boolean?

Runs Bun as a subprocess and returns control to Ruby.

Returns:

  • (Boolean, nil)


251
252
253
254
255
256
# File 'lib/bundlebun/runner.rb', line 251

def system
  check_executable!
  instrument('system.bundlebun') do
    Kernel.system(*command)
  end
end