Class: Bundlebun::EnvPath

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

Overview

EnvPath exists to help prepend the bun binary path to the system PATH, in order to make it easier for other gems and utilities to "see" the existing bun executable without the need to monkey-patch tools that already support Bun. This approach only works when the bundlebun gem is already loaded, which is not always the case, unfortunately.

Class Method Summary collapse

Class Method Details

.pathString

Returns the current system PATH.

Returns:

  • (String)

    The system PATH



15
16
17
# File 'lib/bundlebun/env_path.rb', line 15

def path
  ENV['PATH']
end

.path=(new_path) ⇒ Object

Sets the system PATH to a new value (not prepends the value).

Parameters:

  • new_path (String)

    The new system PATH



22
23
24
# File 'lib/bundlebun/env_path.rb', line 22

def path=(new_path)
  ENV['PATH'] = new_path
end

.prepend(new_path) ⇒ String

Prepends a new path to the system PATH. Makes sure to use different separators for different platforms.

Parameters:

  • new_path (String)

    The new path to prepend

Returns:

  • (String)

    The new system PATH



31
32
33
34
35
36
37
38
39
# File 'lib/bundlebun/env_path.rb', line 31

def prepend(new_path)
  return if new_path.nil? || new_path.empty?

  path_to_check = Bundlebun::Platform.windows? ? path.downcase : path
  check_path = Bundlebun::Platform.windows? ? new_path.downcase : new_path

  self.path = "#{new_path}#{separator}#{path}" unless path_to_check.start_with?(check_path)
  path
end

.separatorString

The PATH separator for the current platform (: or ;)

Returns:

  • (String)

    The separator character



44
45
46
47
48
# File 'lib/bundlebun/env_path.rb', line 44

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

  @separator = Bundlebun::Platform.windows? ? ';' : ':'
end