Class: Clamshell::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/clamshell/environment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ Environment

Returns a new instance of Environment.



15
16
17
18
19
20
21
22
23
# File 'lib/clamshell/environment.rb', line 15

def initialize(shell)
  @shell = case shell
    when "csh", "tcsh" then TcshAdapter
    when "zsh", "bash" then BashAdapter
    else
      raise "Unsupported shell"
    end
  @stmts = []
end

Instance Attribute Details

#shellObject (readonly)

Returns the value of attribute shell.



4
5
6
# File 'lib/clamshell/environment.rb', line 4

def shell
  @shell
end

Class Method Details

.setup(shell = nil, &block) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/clamshell/environment.rb', line 6

def self.setup(shell = nil, &block)
  $SHELL = shell ||= Clamshell.settings[:shell]
  raise "No shell specified" unless shell

  e = new(shell)
  e.instance_eval(&block)
  return e
end

Instance Method Details

#cmd(stmt) ⇒ Object



57
58
59
# File 'lib/clamshell/environment.rb', line 57

def cmd(stmt)
  @stmts << stmt
end

#env_alias(name, val) ⇒ Object



53
54
55
# File 'lib/clamshell/environment.rb', line 53

def env_alias(name, val)
  @stmts << @shell.env_alias(name, val)
end

#env_var(name, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/clamshell/environment.rb', line 26

def env_var(name, *args)
  case
  when args[0].class == String    # direct assignment
    @stmts << @shell.env_var(name, args[0])

  else                            # appending or prepending to existing variable

    # In csh, concatting to an undefined environment variable
    # results in an error. This is a safe guard that sets the
    # environment variable to an empty string before appending/prepending.
    unless ENV[name]
      ENV[name] = %q{""}
      @stmts << @shell.env_var(name, ENV[name])
    end

    delimiter = args[0][:delimiter] || ""
    if args[0][:prepend]
      val = args[0][:prepend] + delimiter + "${#{name}}"
    elsif args[0][:append]
      val = "${#{name}}" + delimiter + args[0][:append]
    else
      raise DslError, "Must specify prepend or append"
    end
    @stmts << @shell.env_var(name, val)
  end
end

#include_file(file) ⇒ Object



61
62
63
# File 'lib/clamshell/environment.rb', line 61

def include_file(file)
  instance_eval File.read(file), file
end

#to_sObject



65
66
67
# File 'lib/clamshell/environment.rb', line 65

def to_s
  @stmts.join("\n")
end