Module: TRMNLP::TransformBackend::Which

Defined in:
lib/trmnlp/transform_backend/which.rb

Overview

Resolves a command to the interpreter actually present on PATH, so one transform runs unchanged on platforms that name an interpreter differently — most pressingly Windows, which ships no python3.

Class Method Summary collapse

Class Method Details

.locate(cmd, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', '')) ⇒ Object

Windows marks executables by extension, enumerated in PATHEXT (.EXE/.BAT/...); POSIX leaves PATHEXT unset and relies on the exec bit.



19
20
21
22
23
24
25
26
# File 'lib/trmnlp/transform_backend/which.rb', line 19

def locate(cmd, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', ''))
  suffixes = pathext.split(File::PATH_SEPARATOR)
  suffixes = [''] if suffixes.empty?
  path.split(File::PATH_SEPARATOR)
      .product(suffixes)
      .map { |dir, suffix| File.join(dir, "#{cmd}#{suffix}") }
      .find { |candidate| File.file?(candidate) && File.executable?(candidate) }
end

.resolve(candidates, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', '')) ⇒ Object

Falls back to the first candidate when none resolve, leaving the caller to surface the usual "interpreter not available" ENOENT.



13
14
15
# File 'lib/trmnlp/transform_backend/which.rb', line 13

def resolve(candidates, path: ENV.fetch('PATH', ''), pathext: ENV.fetch('PATHEXT', ''))
  candidates.find { |cmd| locate(cmd, path: path, pathext: pathext) } || candidates.first
end