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



62
63
64
65
# File 'lib/sandbox/cli.rb', line 62

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

Instance Attribute Details

#optionsObject (readonly)

The options for this execution.



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

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



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sandbox/cli.rb', line 68

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

  if targets.size < 1
    raise Sandbox::Error.new('no target specified - see `ruby-virtualenv --help` for assistance')
  elsif targets.size > 1
    raise Sandbox::Error.new('multiple targets specified - see `ruby-virtualenv --help` for assistance')
  end

  options[ :target ] = targets[0]

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

#long_helpObject



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
162
163
164
# File 'lib/sandbox/cli.rb', line 124

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



88
89
90
91
92
93
94
95
# File 'lib/sandbox/cli.rb', line 88

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



97
98
99
# File 'lib/sandbox/cli.rb', line 97

def parser
  @parser ||= create_parser
end