Sha256: ddbf217d31e1f82fde3e35f16aff4b29151f42291aac4fc90c83640fab76883a

Contents?: true

Size: 979 Bytes

Versions: 11

Compression:

Stored size: 979 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(0755, 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

11 entries across 11 versions & 2 rubygems

Version Path
aruba-0.14.7 lib/aruba/generators/script_file.rb
aruba-0.14.6 lib/aruba/generators/script_file.rb
aruba-0.14.5 lib/aruba/generators/script_file.rb
aruba-0.14.4 lib/aruba/generators/script_file.rb
aruba-0.14.3 lib/aruba/generators/script_file.rb
aruba-1.0.0.pre.alpha.1 lib/aruba/generators/script_file.rb
aruba-win-fix-0.14.2 lib/aruba/generators/script_file.rb
aruba-0.14.2 lib/aruba/generators/script_file.rb
aruba-0.14.1 lib/aruba/generators/script_file.rb
aruba-0.14.0 lib/aruba/generators/script_file.rb
aruba-0.13.0 lib/aruba/generators/script_file.rb