Sha256: 0998f2a333e6a74580d460bcfeed272a6188bc950bafb5ca2b185ae2e8a2c0a8

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 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)
      shebang = data.lines.first

      # First line must start with #!
      return unless shebang && shebang.start_with?("#!")

      # Get the parts of the shebang without the #!
      tokens = shebang.sub(/^#!\s*/, '').strip.split(' ')

      # There was nothing after the #!
      return if tokens.empty?

      # Get the name of the interpreter
      script = File.basename(tokens.first)

      # Get next argument if interpreter was /usr/bin/env
      script = tokens[1] if script == 'env'

      # Interpreter was /usr/bin/env with no arguments
      return unless script

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

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

      File.basename(script)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
github-linguist-4.5.4 lib/linguist/shebang.rb
github-linguist-4.5.3 lib/linguist/shebang.rb
github-linguist-4.5.2 lib/linguist/shebang.rb
github-linguist-4.5.1 lib/linguist/shebang.rb
github-linguist-4.4.3 lib/linguist/shebang.rb
github-linguist-4.4.2 lib/linguist/shebang.rb
github-linguist-4.4.1 lib/linguist/shebang.rb
github-linguist-4.4.0 lib/linguist/shebang.rb
github-linguist-4.3.1 lib/linguist/shebang.rb
github-linguist-4.3.0 lib/linguist/shebang.rb
github-linguist-4.2.7 lib/linguist/shebang.rb
github-linguist-4.2.6 lib/linguist/shebang.rb
github-linguist-4.2.5 lib/linguist/shebang.rb