Class: Sandbox::CLI

Inherits:
Object
  • Object
show all
Extended by:
Output
Includes:
Output
Defined in:
lib/sandbox/cli.rb

Constant Summary collapse

DEFAULTS =
{
  :gems => []
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Output

tell, tell_unless_quiet, tell_unless_really_quiet, tell_when_really_verbose, tell_when_verbose

Constructor Details

#initializeCLI

setup of a new CLI instance



59
60
61
62
# File 'lib/sandbox/cli.rb', line 59

def initialize
  @options = DEFAULTS.dup
  @parser = nil
end

Instance Attribute Details

#optionsObject (readonly)

The options for this execution.



56
57
58
# File 'lib/sandbox/cli.rb', line 56

def options
  @options
end

Class Method Details

.execute(args = ARGV) ⇒ Object

invokes sandbox via command-line ARGV as the options



17
18
19
20
21
22
# File 'lib/sandbox/cli.rb', line 17

def execute(args = ARGV)
  verify_environment!
  parse(args).execute!
rescue Exception => error
  handle_error(error)
end

.handle_error(error) ⇒ Object

pretty error handling



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sandbox/cli.rb', line 38

def handle_error(error)
  case error
    when Sandbox::Error
      tell_unless_really_quiet(error.message)
    when StandardError #, Timeout::Error
      tell_unless_really_quiet("Error: #{error.message}")
      tell_when_really_verbose(error.backtrace.collect { |bt| "    #{bt}" }.join("\n")) if error.backtrace
    when Interrupt
      tell_unless_really_quiet("Interrupted")
  else
    raise error
  end
end

.parse(args) ⇒ Object

returns a new CLI instance which has parsed the given arguments. will load the command to execute based upon the arguements. if an error occurs, it will print out simple error message and exit.



27
28
29
30
31
# File 'lib/sandbox/cli.rb', line 27

def parse(args)
  cli = new
  cli.parse_args!(args)
  cli
end

.verify_environment!Object

Raises:



33
34
35
# File 'lib/sandbox/cli.rb', line 33

def verify_environment!
  raise LoadedSandboxError if ENV['SANDBOX']
end

Instance Method Details

#create_parserObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sandbox/cli.rb', line 98

def create_parser
  OptionParser.new do |o|
    o.set_summary_indent('  ')
    o.program_name = 'ruby-virtualenv TARGET'
    o.define_head "Create virtual ruby/rubygems environments."
    o.separator ""

    o.separator "ARGUMENTS:"
    o.separator "  TARGET      Target path to new virtualenv. Must not exist beforehand."
    o.separator ""

    o.separator "OPTIONS"
    o.on('-g', '--gems gem1,gem2', Array, 'Gems to install after virtualenv is created.') { |gems| @options[:gems] = gems }
    o.on('-n', '--no-gems', 'Do not install any gems after virtualenv is created.') { @options[:gems] = [] }
    o.on('-q', '--quiet', 'Show less output. (multiple allowed)') { |f| Sandbox.decrease_verbosity }
    o.on('-v', '--verbose', 'Show more output. (multiple allowed)') { |f| Sandbox.increase_verbosity }
    o.on_tail('-h', '--help', 'Show this help message and exit.') { tell_unless_really_quiet(o); exit }
    o.on_tail('-H', '--long-help', 'Show the full description about the program.') { tell_unless_really_quiet(long_help); exit }
    o.on_tail('-V', '--version', 'Display the program version and exit.') { tell_unless_really_quiet(Sandbox::VERSION); exit }
    o.separator ""
  end
end

#execute!Object

perform the sandbox creation



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sandbox/cli.rb', line 65

def execute!
  targets = options.delete(:args)

  if targets.size < 1
    raise Sandbox::Error.new('No target specified. See `ruby-virtualenv -h` for assistance.')
  elsif targets.size > 1
    raise Sandbox::Error.new('Multiple targets specified. See `ruby-virtualenv -h` for assistance.')
  end

  options[:target] = targets[0]

  Sandbox::Installer.new(options).populate
end

#long_helpObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/sandbox/cli.rb', line 121

def long_help
  <<-HELP
Sandbox is a utility to create sandboxed Ruby/Rubygems environments.

It is meant to address the following issues:

  1. Conflicts with unspecified gem dependency versions.
  2. Applications can have their own gem repositories.
  3. Permissions for installing your own gems.
  4. Ability to try gems out without installing into your global repository.
  5. A Simple way to enable this.

Running from your own gem repositories is fairly straight-forward, but 
managing the necessary environment is a pain.  This utility will create a new
environment which may be activated by the script `bin/activate` in
your sandbox directory.

Run the script with the following to enable your new environment:

  $ source bin/activate

When you want to leave the environment:

  $ deactivate

NOTES:

  1. It creates an environment that has its own installation directory for Gems.
  2. It doesn't share gems with other sandbox environments.
  3. It (optionally) doesn't use the globally installed gems either.
  4. It will use a local to the sandbox .gemrc file

WARNINGS:

  Activating your sandbox environment will change your HOME directory
  temporarily to the sandbox directory. Other environment variables are set to
  enable this funtionality, so if you may experience odd behavior. Everything
  should be reset when you deactivate the virtualenv.

  HELP
end

#parse_args!(args) ⇒ Object

processes args to:

  • load global option for the application

  • determine command name to lookup in CommandManager

  • load command and have it process any add’t options

  • catches exceptions for unknown switches or commands



85
86
87
88
89
90
91
92
# File 'lib/sandbox/cli.rb', line 85

def parse_args!(args)
  options[:original_args] = args.dup
  parser.parse!(args)
rescue OptionParser::ParseError => ex
  raise_parse_error(ex.reason, ex.args)
else
  options[:args] = args
end

#parserObject



94
95
96
# File 'lib/sandbox/cli.rb', line 94

def parser
  @parser ||= create_parser
end