Sha256: eea13c7587b5c71d72932a0ca55bd65711eb311098c6847ff0b7001b9b2844d7

Contents?: true

Size: 980 Bytes

Versions: 17

Compression:

Stored size: 980 Bytes

Contents

# Aruba
module Aruba
  # Generate script files on command line
  class ScriptFile
    private

    attr_reader :path, :content, :interpreter

    public

    def initialize(opts = {})
      @path        = opts[:path]
      @content     = opts[:content]
      @interpreter = opts[:interpreter]
    end

    def call
      Aruba.platform.write_file(path, "#{header}#{content}")
      Aruba.platform.chmod(0o755, path, {})
    end

    private

    def header
      if script_starts_with_shebang?
        ''
      elsif interpreter_is_absolute_path?
        format("#!%s\n", interpreter)
      elsif interpreter_is_just_the_name_of_shell?
        format("#!/usr/bin/env %s\n", interpreter)
      end
    end

    def interpreter_is_absolute_path?
      Aruba.platform.absolute_path? interpreter
    end

    def interpreter_is_just_the_name_of_shell?
      interpreter =~ /^[-_a-zA-Z.]+$/
    end

    def script_starts_with_shebang?
      content.start_with? '#!'
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
aruba-1.0.4 lib/aruba/generators/script_file.rb
aruba-1.0.3 lib/aruba/generators/script_file.rb
aruba-1.0.2 lib/aruba/generators/script_file.rb
aruba-1.0.1 lib/aruba/generators/script_file.rb
aruba-1.0.0 lib/aruba/generators/script_file.rb
aruba-0.14.14 lib/aruba/generators/script_file.rb
aruba-0.14.13 lib/aruba/generators/script_file.rb
aruba-1.0.0.pre.alpha.5 lib/aruba/generators/script_file.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/aruba-0.14.12/lib/aruba/generators/script_file.rb
aruba-0.14.12 lib/aruba/generators/script_file.rb
aruba-0.14.11 lib/aruba/generators/script_file.rb
aruba-0.14.10 lib/aruba/generators/script_file.rb
aruba-1.0.0.pre.alpha.4 lib/aruba/generators/script_file.rb
aruba-1.0.0.pre.alpha.3 lib/aruba/generators/script_file.rb
aruba-0.14.9 lib/aruba/generators/script_file.rb
aruba-0.14.8 lib/aruba/generators/script_file.rb
aruba-1.0.0.pre.alpha.2 lib/aruba/generators/script_file.rb