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'])

Parameters:

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

    Command arguments to pass to Bun

See Also:



178
179
180
# File 'lib/bundlebun/runner.rb', line 178

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)


134
135
136
137
138
139
# File 'lib/bundlebun/runner.rb', line 134

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?

Returns:

  • (Boolean)


144
145
146
# File 'lib/bundlebun/runner.rb', line 144

def binary_path_exist?
  File.exist?(binary_path)
end

.binstub_exist?Boolean

Does the binstub exist?

Returns:

  • (Boolean)


161
162
163
# File 'lib/bundlebun/runner.rb', line 161

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)


154
155
156
# File 'lib/bundlebun/runner.rb', line 154

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)


101
102
103
# File 'lib/bundlebun/runner.rb', line 101

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

Parameters:

  • arguments (String, Array<String>)

    Command arguments to pass to Bun

See Also:



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

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)

Parameters:

  • arguments (String, Array<String>)

    Command arguments to pass to Bun

See Also:



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

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.

Returns:

  • (String)


110
111
112
# File 'lib/bundlebun/runner.rb', line 110

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)


124
125
126
127
128
# File 'lib/bundlebun/runner.rb', line 124

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)


117
118
119
# File 'lib/bundlebun/runner.rb', line 117

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

Parameters:

  • arguments (String, Array<String>)

    Command arguments to pass to Bun

Returns:

  • (Boolean, nil)

    true if Bun exited successfully (status 0), false if it exited with an error, nil if execution failed

See Also:



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

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:



207
208
209
# File 'lib/bundlebun/runner.rb', line 207

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:



196
197
198
199
200
# File 'lib/bundlebun/runner.rb', line 196

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

Returns:

  • (Boolean, nil)

    true if Bun exited successfully (status 0), false if it exited with an error, nil if execution failed

See Also:



230
231
232
233
234
235
# File 'lib/bundlebun/runner.rb', line 230

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