Class: Shakapacker::Utils::Misc

Inherits:
Object
  • Object
show all
Extended by:
FileUtils
Defined in:
lib/shakapacker/utils/misc.rb

Class Method Summary collapse

Class Method Details

.js_binstub_executable(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/shakapacker/utils/misc.rb', line 45

def self.js_binstub_executable(path)
  return nil unless File.file?(path)

  shebang = File.open(path, "rb") { |f| f.gets }.to_s.chomp
  return nil unless shebang.start_with?("#!")

  shebang_tokens = begin
    Shellwords.split(shebang.delete_prefix("#!"))
  rescue ArgumentError
    return nil
  end
  executable = shebang_tokens.first.to_s

  if File.basename(executable) == "env"
    shebang_tokens = shebang_tokens.drop(1)
    while (env_token = shebang_tokens.first)
      if env_token.start_with?("-")
        env_flag = shebang_tokens.shift
        shebang_tokens.shift if ENV_FLAGS_WITH_ARGUMENTS.include?(env_flag)
      elsif env_token.match?(/\A[A-Za-z_][A-Za-z0-9_]*=/)
        shebang_tokens.shift
      else
        break
      end
    end
    executable = shebang_tokens.first.to_s
  end

  # Preserve direct interpreter paths so stale absolute Node shebangs fail with actionable binstub guidance.
  NODE_BINSTUB_EXECUTABLES.include?(File.basename(executable)) ? executable : nil
end

.object_to_boolean(value) ⇒ Object



32
33
34
# File 'lib/shakapacker/utils/misc.rb', line 32

def self.object_to_boolean(value)
  [true, "true", "yes", 1, "1", "t"].include?(value.instance_of?(String) ? value.downcase : value)
end

.sh_in_dir(dir, *shell_commands) ⇒ Object

Executes a string or an array of strings in a shell in the given directory. NOTE: this does NOT unbundle. The caller's Bundler environment (BUNDLE_GEMFILE, RUBYOPT, ...) is inherited, so a bundle command run here against a different directory's Gemfile still resolves the caller's Gemfile. Wrap the call in Bundler.with_unbundled_env when that matters.



41
42
43
# File 'lib/shakapacker/utils/misc.rb', line 41

def self.sh_in_dir(dir, *shell_commands)
  shell_commands.flatten.each { |shell_command| sh %(cd '#{dir}' && #{shell_command.strip}) }
end

.uncommitted_changes?(message_handler) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shakapacker/utils/misc.rb', line 17

def self.uncommitted_changes?(message_handler)
  return false if ENV["COVERAGE"] == "true"

  status = `git status --porcelain`
  return false if $CHILD_STATUS.success? && status.empty?

  error = if $CHILD_STATUS.success?
    "You have uncommitted code. Please commit or stash your changes before continuing"
          else
            "You do not have Git installed. Please install Git, and commit your changes before continuing"
  end
  message_handler.add_error(error)
  true
end