Class: Autoshell::Base

Inherits:
Object
  • Object
show all
Includes:
Environment, Filestuff, Git, Log, Run
Defined in:
lib/autoshell.rb

Overview

The core autoshell class

Instance Attribute Summary collapse

Attributes included from Log

#logger

Attributes included from Git

#branch, #commit_hash_for_checkout

Instance Method Summary collapse

Methods included from Log

#log_output, #log_reset

Methods included from Git

#archive, #branch_from_repo_url, #checkout_version, #clone, #commit_hash, #git, #git?, #switch, #update

Methods included from Environment

#environment?, #node?, #python?, #ruby?, #setup_environment, #setup_node_environment, #setup_python_environment, #setup_ruby_environment

Methods included from Filestuff

#cd, #copy_to, #cp, #dir?, #exist?, #expand, #glob, #ls, #mime, #mkdir, #mkpdir, #move_to, #mv, #read, #read_binary, #read_json, #read_text, #read_yaml, #rm

Methods included from Run

#command?, #run

Constructor Details

#initialize(path = '.', env: {}, logger: nil) ⇒ Base

Create a new shell object with a working directory and environment vars

Parameters:

  • path (String) (defaults to: '.')

    Create a new shell at this path

  • env (Hash) (defaults to: {})

    a customizable set of options

  • logger (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (env:):

  • :env (Hash)

    Environment variables to add to this shell

Options Hash (logger:):

  • :logger (Logger)

    Logger instance to use



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/autoshell.rb', line 64

def initialize(path = '.', env: {}, logger: nil)
  # save the working directory
  self.working_dir = File.expand_path(path.to_s)
  # Load whitelisted environment variables
  self.env = ENV.select { |k, _| ALLOWED_ENV.include? k }
  # Set some defaults
  self.env['SHELL'] = '/bin/bash'
  # Since this is likely to be running within a `bundle exec` context, we
  # expect any original GEM_PATH has been unset, and the ENV is configured
  # such that we only use what is within this bundle, which probably
  # doesn't include bundler itself, although we'll actually need to
  # 'bundle install' in working directories. To keep things sane, just
  # carry over the _ORIGINAL_GEM_PATH so that bundler is available
  if !ENV['_ORIGINAL_GEM_PATH'].nil? &&
     ENV['_ORIGINAL_GEM_PATH'].strip.length > 0 &&
     (env['GEM_PATH'].nil? || env['GEM_PATH'].strip.length == 0)
    self.env['GEM_PATH'] = ENV['_ORIGINAL_GEM_PATH']
  end
  # Update environment variables from option
  self.env.update env
  # did we get a logger to use?
  self.logger = logger if logger
end

Instance Attribute Details

#envHash

Returns environment variables in this shell.

Returns:

  • (Hash)

    environment variables in this shell



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/autoshell.rb', line 49

class Base
  include Autoshell::Run
  include Autoshell::Filestuff
  include Autoshell::Environment
  include Autoshell::Git
  include Autoshell::Log

  attr_accessor :env, :working_dir
  alias_method :to_s, :working_dir

  # Create a new shell object with a working directory and environment vars
  #
  # @param path [String] Create a new shell at this path
  # @option env [Hash] :env Environment variables to add to this shell
  # @option logger [Logger] :logger Logger instance to use
  def initialize(path = '.', env: {}, logger: nil)
    # save the working directory
    self.working_dir = File.expand_path(path.to_s)
    # Load whitelisted environment variables
    self.env = ENV.select { |k, _| ALLOWED_ENV.include? k }
    # Set some defaults
    self.env['SHELL'] = '/bin/bash'
    # Since this is likely to be running within a `bundle exec` context, we
    # expect any original GEM_PATH has been unset, and the ENV is configured
    # such that we only use what is within this bundle, which probably
    # doesn't include bundler itself, although we'll actually need to
    # 'bundle install' in working directories. To keep things sane, just
    # carry over the _ORIGINAL_GEM_PATH so that bundler is available
    if !ENV['_ORIGINAL_GEM_PATH'].nil? &&
       ENV['_ORIGINAL_GEM_PATH'].strip.length > 0 &&
       (env['GEM_PATH'].nil? || env['GEM_PATH'].strip.length == 0)
      self.env['GEM_PATH'] = ENV['_ORIGINAL_GEM_PATH']
    end
    # Update environment variables from option
    self.env.update env
    # did we get a logger to use?
    self.logger = logger if logger
  end
end

#home_dirString

Returns directory this shell was started with.

Returns:

  • (String)

    directory this shell was started with



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/autoshell.rb', line 49

class Base
  include Autoshell::Run
  include Autoshell::Filestuff
  include Autoshell::Environment
  include Autoshell::Git
  include Autoshell::Log

  attr_accessor :env, :working_dir
  alias_method :to_s, :working_dir

  # Create a new shell object with a working directory and environment vars
  #
  # @param path [String] Create a new shell at this path
  # @option env [Hash] :env Environment variables to add to this shell
  # @option logger [Logger] :logger Logger instance to use
  def initialize(path = '.', env: {}, logger: nil)
    # save the working directory
    self.working_dir = File.expand_path(path.to_s)
    # Load whitelisted environment variables
    self.env = ENV.select { |k, _| ALLOWED_ENV.include? k }
    # Set some defaults
    self.env['SHELL'] = '/bin/bash'
    # Since this is likely to be running within a `bundle exec` context, we
    # expect any original GEM_PATH has been unset, and the ENV is configured
    # such that we only use what is within this bundle, which probably
    # doesn't include bundler itself, although we'll actually need to
    # 'bundle install' in working directories. To keep things sane, just
    # carry over the _ORIGINAL_GEM_PATH so that bundler is available
    if !ENV['_ORIGINAL_GEM_PATH'].nil? &&
       ENV['_ORIGINAL_GEM_PATH'].strip.length > 0 &&
       (env['GEM_PATH'].nil? || env['GEM_PATH'].strip.length == 0)
      self.env['GEM_PATH'] = ENV['_ORIGINAL_GEM_PATH']
    end
    # Update environment variables from option
    self.env.update env
    # did we get a logger to use?
    self.logger = logger if logger
  end
end

#working_dirString Also known as: to_s

Returns current working directory of this shell.

Returns:

  • (String)

    current working directory of this shell



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/autoshell.rb', line 49

class Base
  include Autoshell::Run
  include Autoshell::Filestuff
  include Autoshell::Environment
  include Autoshell::Git
  include Autoshell::Log

  attr_accessor :env, :working_dir
  alias_method :to_s, :working_dir

  # Create a new shell object with a working directory and environment vars
  #
  # @param path [String] Create a new shell at this path
  # @option env [Hash] :env Environment variables to add to this shell
  # @option logger [Logger] :logger Logger instance to use
  def initialize(path = '.', env: {}, logger: nil)
    # save the working directory
    self.working_dir = File.expand_path(path.to_s)
    # Load whitelisted environment variables
    self.env = ENV.select { |k, _| ALLOWED_ENV.include? k }
    # Set some defaults
    self.env['SHELL'] = '/bin/bash'
    # Since this is likely to be running within a `bundle exec` context, we
    # expect any original GEM_PATH has been unset, and the ENV is configured
    # such that we only use what is within this bundle, which probably
    # doesn't include bundler itself, although we'll actually need to
    # 'bundle install' in working directories. To keep things sane, just
    # carry over the _ORIGINAL_GEM_PATH so that bundler is available
    if !ENV['_ORIGINAL_GEM_PATH'].nil? &&
       ENV['_ORIGINAL_GEM_PATH'].strip.length > 0 &&
       (env['GEM_PATH'].nil? || env['GEM_PATH'].strip.length == 0)
      self.env['GEM_PATH'] = ENV['_ORIGINAL_GEM_PATH']
    end
    # Update environment variables from option
    self.env.update env
    # did we get a logger to use?
    self.logger = logger if logger
  end
end