|
3 | 3 | module Bundlebun |
4 | 4 | # {Runner} is the class that bundlebun uses to run the bundled Bun executable. |
5 | 5 | # |
| 6 | + # bundlebun provides two ways to run Bun: |
| 7 | + # |
| 8 | + # - {.call} (also available as {.exec}): Replaces the current Ruby process with Bun. This is the default. |
| 9 | + # |
| 10 | + # - {.system}: Runs Bun as a subprocess and returns control to Ruby. |
| 11 | + # Use this when you need to continue executing Ruby code after Bun finishes. |
| 12 | + # |
6 | 13 | # @see Bundlebun |
| 14 | + # |
| 15 | + # @example Running Bun (replaces process, never returns) |
| 16 | + # Bundlebun.('install') |
| 17 | + # Bundlebun.call('outdated') |
| 18 | + # Bundlebun.call(['add', 'postcss']) |
| 19 | + # |
| 20 | + # @example Running Bun as subprocess (returns to Ruby) |
| 21 | + # if Bundlebun.system('install') |
| 22 | + # puts 'Dependencies installed!' |
| 23 | + # end |
7 | 24 | class Runner |
8 | 25 | BINSTUB_PATH = 'bin/bun' |
9 | 26 | RELATIVE_DIRECTORY = 'lib/bundlebun/vendor/bun' |
10 | 27 |
|
11 | 28 | class << self |
12 | | - # Runs the Bun runtime with parameters. |
| 29 | + # Replaces the current Ruby process with Bun. |
| 30 | + # |
| 31 | + # @param arguments [String, Array<String>] Command arguments to pass to Bun |
| 32 | + # @return [void] This method never returns |
13 | 33 | # |
14 | | - # A wrapper for {Bundlebun::Runner.new}, {Bundlebun::Runner.call}. |
| 34 | + # @example In a binstub (bin/bun) |
| 35 | + # #!/usr/bin/env ruby |
| 36 | + # require 'bundlebun' |
| 37 | + # Bundlebun.exec(ARGV) |
| 38 | + # |
| 39 | + # @see .call |
| 40 | + # @see .system |
| 41 | + def exec(...) |
| 42 | + new(...).exec |
| 43 | + end |
| 44 | + |
| 45 | + # Replaces the current Ruby process with Bun. Alias for {.exec}. |
| 46 | + # Also available via the +.()+ shorthand syntax. |
15 | 47 | # |
16 | 48 | # @param arguments [String, Array<String>] Command arguments to pass to Bun |
17 | | - # @return [Integer] Exit status code (`127` if executable not found) |
| 49 | + # @return [void] This method never returns |
18 | 50 | # |
19 | | - # @example String as an argument |
20 | | - # Bundlebun.call('--version') # => `bun --version` |
| 51 | + # @example Basic usage |
| 52 | + # Bundlebun.call('outdated') |
| 53 | + # Bundlebun.call(['add', 'postcss']) |
21 | 54 | # |
22 | | - # @example Array of strings as an argument |
23 | | - # Bundlebun.call(['add', 'postcss']) # => `bun add postcss` |
| 55 | + # @example Using the .() shorthand |
| 56 | + # Bundlebun.('install') |
24 | 57 | # |
25 | | - # @see Bundlebun::Runner.new |
26 | | - # @see Bundlebun::Runner#call |
| 58 | + # @see .exec |
| 59 | + # @see .system |
27 | 60 | def call(...) |
28 | | - new(...).call |
| 61 | + exec(...) |
| 62 | + end |
| 63 | + |
| 64 | + # Runs Bun as a subprocess and returns control to Ruby. |
| 65 | + # |
| 66 | + # Unlike {.call} and {.exec}, this method does not replace the current process. |
| 67 | + # Use this when you need to run Bun and then continue executing Ruby code. |
| 68 | + # |
| 69 | + # @param arguments [String, Array<String>] Command arguments to pass to Bun |
| 70 | + # @return [Boolean, nil] +true+ if Bun exited successfully (status 0), |
| 71 | + # +false+ if it exited with an error, +nil+ if execution failed |
| 72 | + # |
| 73 | + # @example Run install and check result |
| 74 | + # if Bundlebun.system('install') |
| 75 | + # puts 'Dependencies installed!' |
| 76 | + # else |
| 77 | + # puts 'Installation failed' |
| 78 | + # end |
| 79 | + # |
| 80 | + # @see .call |
| 81 | + # @see .exec |
| 82 | + def system(...) |
| 83 | + new(...).system |
29 | 84 | end |
30 | 85 |
|
31 | 86 | # A relative path to binstub that bundlebun usually generates with installation Rake tasks. |
@@ -98,35 +153,64 @@ def binstub_exist? |
98 | 153 | end |
99 | 154 | end |
100 | 155 |
|
101 | | - # Intialize the {Runner} with arguments to run the Bun runtime later via #call. |
| 156 | + # Initialize the {Runner} with arguments to run the Bun runtime later. |
102 | 157 | # |
103 | 158 | # @param arguments [String, Array<String>] Command arguments to pass to Bun |
104 | 159 | # |
105 | 160 | # @example String as an argument |
106 | | - # Bundlebun::Runner.new('--version') # => `bun --version` |
| 161 | + # Bundlebun::Runner.new('--version') |
107 | 162 | # |
108 | 163 | # @example Array of strings as an argument |
109 | | - # Bundlebun::Runner.new(['add', 'postcss']) # => `bun add postcss` |
| 164 | + # Bundlebun::Runner.new(['add', 'postcss']) |
110 | 165 | # |
111 | | - # @see Bundlebun::Runner#call |
| 166 | + # @see #system |
| 167 | + # @see #exec |
112 | 168 | def initialize(arguments = '') |
113 | 169 | @arguments = arguments |
114 | 170 | end |
115 | 171 |
|
116 | | - # Runs the Bun executable with previously specified arguments. |
117 | | - # |
118 | | - # Check other methods of {Bundlebun::Runner} to see how we determine what to run exactly. |
| 172 | + # Replaces the current Ruby process with Bun. |
| 173 | + # This is the default behavior. |
119 | 174 | # |
120 | | - # @return [Integer] Exit status code (`127` if executable not found) |
| 175 | + # @return [void] This method never returns |
121 | 176 | # |
122 | 177 | # @example |
123 | | - # b = Bundlebun::Runner.new('--version') |
124 | | - # b.call |
| 178 | + # runner = Bundlebun::Runner.new(ARGV) |
| 179 | + # runner.exec # Ruby process ends here, Bun takes over |
| 180 | + # |
| 181 | + # @see #system |
| 182 | + def exec |
| 183 | + check_executable! |
| 184 | + Kernel.exec(command) |
| 185 | + end |
| 186 | + |
| 187 | + # Replaces the current Ruby process with Bun. Alias for {#exec}. |
| 188 | + # |
| 189 | + # @return [void] This method never returns |
125 | 190 | # |
126 | | - # @see Bundlebun::Runner |
| 191 | + # @see #exec |
127 | 192 | def call |
| 193 | + exec |
| 194 | + end |
| 195 | + |
| 196 | + # Runs Bun as a subprocess and returns control to Ruby. |
| 197 | + # |
| 198 | + # Unlike {#call} and {#exec}, this method does not replace the current process. |
| 199 | + # Use this when you need to run Bun and then continue executing Ruby code. |
| 200 | + # |
| 201 | + # @return [Boolean, nil] +true+ if Bun exited successfully (status 0), |
| 202 | + # +false+ if it exited with an error, +nil+ if execution failed |
| 203 | + # |
| 204 | + # @example |
| 205 | + # runner = Bundlebun::Runner.new('install') |
| 206 | + # if runner.system |
| 207 | + # puts 'Dependencies installed!' |
| 208 | + # end |
| 209 | + # |
| 210 | + # @see #exec |
| 211 | + def system |
128 | 212 | check_executable! |
129 | | - exec(command) |
| 213 | + Kernel.system(command) |
130 | 214 | end |
131 | 215 |
|
132 | 216 | private |
|
0 commit comments