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.

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 =
'bin/bun'
RELATIVE_DIRECTORY =
'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'])

See Also:



176
177
178
# File 'lib/bundlebun/runner.rb', line 176

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).



132
133
134
135
136
137
# File 'lib/bundlebun/runner.rb', line 132

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

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

.binary_path_exist?Boolean

Does the bundled Bun binary exist?



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

def binary_path_exist?
  File.exist?(binary_path)
end

.binstub_exist?Boolean

Does the binstub exist?



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

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).



152
153
154
# File 'lib/bundlebun/runner.rb', line 152

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.



99
100
101
# File 'lib/bundlebun/runner.rb', line 99

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

.callvoid

This method returns an undefined value.

Replaces the current Ruby process with Bun. Alias for exec. Also available via the ‘.()` shorthand syntax.

Examples:

Basic usage

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

Using the .() shorthand

Bundlebun.('install')

See Also:



64
65
66
# File 'lib/bundlebun/runner.rb', line 64

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

.execvoid

This method returns an undefined value.

Replaces the current Ruby process with Bun.

When ActiveSupport::Notifications is available, this method publishes an exec.bundlebun event before replacing the process. The payload contains ‘{ command: arguments }` where arguments is what was passed to the method.

Examples:

In a binstub (bin/bun)

#!/usr/bin/env ruby
require 'bundlebun'
Bundlebun.exec(ARGV)

See Also:



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

def exec(...)
  new(...).exec
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.



108
109
110
# File 'lib/bundlebun/runner.rb', line 108

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.



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

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.



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

def relative_directory
  RELATIVE_DIRECTORY
end

.systemBoolean?

Runs Bun as a subprocess and returns control to Ruby.

Unlike call and exec, this method does not replace the current process. Use this when you need to run Bun and then continue executing Ruby code.

When ActiveSupport::Notifications is available, this method publishes a system.bundlebun event with timing information. The payload contains ‘{ command: arguments }` where arguments is what was passed to the method.

Examples:

Run install and check result

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

See Also:



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

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

Instance Method Details

#callvoid

This method returns an undefined value.

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

See Also:



205
206
207
# File 'lib/bundlebun/runner.rb', line 205

def call
  exec
end

#execvoid

This method returns an undefined value.

Replaces the current Ruby process with Bun. This is the default behavior.

When ActiveSupport::Notifications is available, this method publishes an exec.bundlebun event before replacing the process. The payload contains ‘{ command: arguments }` where arguments is what was passed to the runner.

Examples:

runner = Bundlebun::Runner.new(ARGV)
runner.exec  # Ruby process ends here, Bun takes over

See Also:



194
195
196
197
198
# File 'lib/bundlebun/runner.rb', line 194

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

#systemBoolean?

Runs Bun as a subprocess and returns control to Ruby.

Unlike #call and #exec, this method does not replace the current process. Use this when you need to run Bun and then continue executing Ruby code.

When ActiveSupport::Notifications is available, this method publishes a system.bundlebun event with timing information. The payload contains ‘{ command: arguments }` where arguments is what was passed to the runner.

Examples:

runner = Bundlebun::Runner.new('install')
if runner.system
  puts 'Dependencies installed!'
end

See Also:



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

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