Module: Rip::Env

Extended by:
Env, Help
Included in:
Env
Defined in:
lib/rip/env.rb

Constant Summary collapse

PRIVATE_ENV =
/^(rip-|active)/i

Instance Method Summary collapse

Methods included from Help

show_help

Instance Method Details

#activeObject



116
117
118
119
# File 'lib/rip/env.rb', line 116

def active
  active = File.readlink(active_dir)
  active.split('/').last
end

#active_dirObject



164
165
166
# File 'lib/rip/env.rb', line 164

def active_dir
  File.join(Rip.dir, 'active')
end

#call(meth, *args) ⇒ Object

for being lazy about what we have vs what we want. enables javascript-style method calling where the number of arguments doesn’t need to match the arity



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rip/env.rb', line 150

def call(meth, *args)
  arity = method(meth).arity.abs

  if arity == args.size
    send(meth, *args)
  elsif arity == 0
    send(meth)
  elsif args.empty?
    send(meth, '')
  else
    send(meth, *args[0, arity])
  end
end

#commandsObject



7
8
9
# File 'lib/rip/env.rb', line 7

def commands
  instance_methods - %w( call active_dir commands ui validate_ripenv )
end

#copy(new) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rip/env.rb', line 122

def copy(new)
  if new.strip.empty?
    return "must give a ripenv to copy to"
  end

  dest = File.join(Rip.dir, new)
  src  = Rip::Env.active_dir
  env  = Rip::Env.active

  if File.exists?(dest)
    return "#{new} exists"
  end

  FileUtils.cp_r src, dest

  if File.exists? ripfile = File.join(dest, "#{env}.ripenv")
    FileUtils.cp ripfile, File.join(dest, "#{new}.ripenv")
    FileUtils.rm ripfile
  end

  use new
  "cloned #{env} to #{new}"
end

#create(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rip/env.rb', line 12

def create(env)
  dir = File.join(Rip.dir, env)

  if env.strip.empty?
    return "must give a ripenv to create"
  end

  if env.strip =~ PRIVATE_ENV
    return "invalid environment name"
  end

  if File.exists? dir
    "#{env} exists"
  else
    FileUtils.mkdir_p File.join(dir, 'bin')
    FileUtils.mkdir_p File.join(dir, 'lib')

    use env
    "created #{env}"
  end
end

#delete(env) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rip/env.rb', line 54

def delete(env)
  if active == env
    return "can't delete active environment"
  end

  if env.strip.empty?
    return "must give a ripenv to delete"
  end

  if env.strip =~ PRIVATE_ENV
    return "invalid environment name"
  end

  if File.exists?(target = File.join(Rip.dir, env))
    FileUtils.rm_rf target
    "deleted #{env}"
  else
    "can't find #{env}"
  end
end

#list(options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rip/env.rb', line 76

def list(options = {})
  # check if we got passed an env. kinda ghetto.
  if options.is_a? String
    target_env = options
    options = {}
  end

  envs = Dir.glob(File.join(Rip.dir, '*')).map do |env|
    env.split('/').last
  end

  envs.reject! { |env| env =~ PRIVATE_ENV }

  if envs.empty?
    "none. make one with `rip env create <env>`"
  elsif target_env
    if error = validate_ripenv(target_env)
      return error
    end

    manager = PackageManager.new(target_env)
    output  = [ target_env, "" ]
    output += manager.packages
    output.join("\n")
  else
    output  = [ "all installed ripenvs", "" ]
    output += envs.map do |env|
      if options[:p]
        packages = PackageManager.new(env).packages
        packages = packages.size > 3 ? packages[0, 3] + ['...'] : packages
        "#{env} - #{packages.join(', ')}"
      else
        env
      end
    end
    output.join("\n")
  end
end

#uiObject



168
169
170
# File 'lib/rip/env.rb', line 168

def ui
  Rip.ui
end

#use(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rip/env.rb', line 35

def use(env)
  if env.strip.empty?
    return "must give a ripenv to use"
  end

  if error = validate_ripenv(env)
    return error
  end

  begin
    FileUtils.rm active_dir
  rescue Errno::ENOENT
  end
  FileUtils.ln_s(File.join(Rip.dir, env), active_dir)

  "using #{env}"
end

#validate_ripenv(env) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/rip/env.rb', line 172

def validate_ripenv(env)
  if env.strip =~ PRIVATE_ENV
    return "invalid environment name"
  end

  if !File.exists?(File.join(Rip.dir, env))
    return "#{env} doesn't exist"
  end
end