Sha256: 7620a5c0538bfdaf6544f62454912ba483bf4646367da4fe40bd1c44bae1d5c9

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

module Linguist
  class Shebang
    # Public: Use shebang to detect language of the blob.
    #
    # blob               - An object that quacks like a blob.
    #
    # Examples
    #
    #   Shebang.call(FileBlob.new("path/to/file"))
    #
    # Returns an Array with one Language if the blob has a shebang with a valid
    # interpreter, or empty if there is no shebang.
    def self.call(blob, _ = nil)
      Language.find_by_interpreter interpreter(blob.data)
    end

    # Public: Get the interpreter from the shebang
    #
    # Returns a String or nil
    def self.interpreter(data)
      lines = data.lines
      return unless match = /^#! ?(.+)$/.match(lines.first)

      tokens = match[1].split(' ')
      script = tokens.first.split('/').last

      script = tokens[1] if script == 'env'

      # If script has an invalid shebang, we might get here
      return unless script

      # "python2.6" -> "python2"
      script.sub! $1, '' if script =~ /(\.\d+)$/

      # Check for multiline shebang hacks that call `exec`
      if script == 'sh' &&
        lines.first(5).any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
        script = $1
      end

      File.basename(script)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
github-linguist-4.2.3 lib/linguist/shebang.rb
github-linguist-4.2.2 lib/linguist/shebang.rb