Class: Bones::App::Command
- Inherits:
-
Object
- Object
- Bones::App::Command
- Includes:
- Colors
- Defined in:
- lib/bones/app/command.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary
- DEFAULT_SKELETON =
:stopdoc:
'default'
Constants included from Colors
Instance Attribute Summary (collapse)
-
- (Object) config
readonly
Returns the value of attribute config.
-
- (Object) stderr
readonly
Returns the value of attribute stderr.
-
- (Object) stdout
readonly
:startdoc:.
Class Method Summary (collapse)
- + (Object) inherited(other)
-
+ (Object) standard_options
Returns a hash of the standard options that can be used for individual commadns.
Instance Method Summary (collapse)
-
- (Object) in_directory(dir)
Run a block of code in the given directory.
-
- (Command) initialize(opts = {})
constructor
A new instance of Command.
-
- (Object) mrbones_dir
Returns the '.mrbones' resource directory in the user's home directory.
-
- (Object) name
The project name from the command line.
-
- (Object) output_dir
The output directory where files will be written.
- - (Object) parse(args)
-
- (Object) repository
A git or svn repository URL from the command line.
- - (Object) run(args)
-
- (Object) skeleton_dir
The directory where the project skeleton is located.
- - (Object) standard_options
-
- (Boolean) verbose?
Returns true if the user has requested verbose messages.
Methods included from Colors
Constructor Details
- (Command) initialize(opts = {})
A new instance of Command
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bones/app/command.rb', line 14 def initialize( opts = {} ) @stdout = opts[:stdout] || $stdout @stderr = opts[:stderr] || $stderr @config = { :skeleton_dir => File.join(mrbones_dir, DEFAULT_SKELETON), :verbose => false, :name => nil, :output_dir => nil, } @config[:skeleton_dir] = ::Bones.path(DEFAULT_SKELETON) unless test(?d, skeleton_dir) end |
Instance Attribute Details
- (Object) config (readonly)
Returns the value of attribute config
12 13 14 |
# File 'lib/bones/app/command.rb', line 12 def config @config end |
- (Object) stderr (readonly)
Returns the value of attribute stderr
11 12 13 |
# File 'lib/bones/app/command.rb', line 11 def stderr @stderr end |
- (Object) stdout (readonly)
:startdoc:
10 11 12 |
# File 'lib/bones/app/command.rb', line 10 def stdout @stdout end |
Class Method Details
+ (Object) inherited(other)
208 209 210 |
# File 'lib/bones/app/command.rb', line 208 def self.inherited( other ) other.extend ClassMethods end |
+ (Object) standard_options
Returns a hash of the standard options that can be used for individual commadns.
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 165 166 |
# File 'lib/bones/app/command.rb', line 137 def self. @standard_options ||= { :verbose => ['-v', '--verbose', 'Enable verbose output.', lambda { |_| config[:verbose] = true }], :directory => ['-d', '--directory DIRECTORY', String, 'Project directory to create.', '(defaults to project_name)', lambda { |value| config[:output_dir] = value }], :skeleton => ['-s', '--skeleton NAME', String, 'Project skeleton to use.', lambda { |value| path = File.join(mrbones_dir, value) if test(?e, value) config[:skeleton_dir] = value elsif test(?e, path) config[:skeleton_dir] = path else raise ArgumentError, "Unknown skeleton '#{value}'." end }], :repository => ['-r', '--repository URL', String, 'svn or git repository path.', lambda { |value| config[:repository] = value }], :colorize => ['-c', '--color', '--no-color', 'Colorize output', lambda { |value| Bones.config.colorize = value }] } end |
Instance Method Details
- (Object) in_directory(dir)
Run a block of code in the given directory.
74 75 76 77 78 79 80 |
# File 'lib/bones/app/command.rb', line 74 def in_directory( dir ) pwd = File.(FileUtils.pwd) FileUtils.cd dir yield ensure FileUtils.cd pwd end |
- (Object) mrbones_dir
Returns the '.mrbones' resource directory in the user's home directory.
65 66 67 68 69 70 |
# File 'lib/bones/app/command.rb', line 65 def mrbones_dir return @mrbones_dir if defined? @mrbones_dir path = File.join(::Bones::HOME, '.mrbones') @mrbones_dir = File.(path) end |
- (Object) name
The project name from the command line.
45 46 47 |
# File 'lib/bones/app/command.rb', line 45 def name @config[:name] end |
- (Object) output_dir
The output directory where files will be written.
33 34 35 |
# File 'lib/bones/app/command.rb', line 33 def output_dir @config[:output_dir] end |
- (Object) parse(args)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/bones/app/command.rb', line 90 def parse( args ) opts = OptionParser.new opts. = 'NAME' opts.separator " bones v#{::Bones.version}" opts.separator '' if self.class.synopsis opts.separator 'SYNOPSIS' self.class.synopsis.split("\n").each { |line| opts.separator " #{line.strip}" } opts.separator '' end if self.class.description opts.separator 'DESCRIPTION' self.class.description.split("\n").each { |line| opts.separator " #{line.strip}" } opts.separator '' end if self.class. and not self.class..empty? opts.separator 'PARAMETERS' self.class..each { |option| case option when Array option << method(option.pop) if option.last =~ %r/^__/ opts.on(*option) when String opts.separator(" #{option.strip}") else opts.separator('') end } opts.separator '' end opts.separator ' Common Options:' opts.on_tail( '-h', '--help', 'show this message' ) { stdout.puts opts exit } opts.on_tail '' opts.parse! args return opts end |
- (Object) repository
A git or svn repository URL from the command line.
51 52 53 54 55 |
# File 'lib/bones/app/command.rb', line 51 def repository return @config[:repository] if @config.has_key? :repository return IO.read(skeleton_dir).strip if skeleton_dir and test(?f, skeleton_dir) nil end |
- (Object) run(args)
27 28 29 |
# File 'lib/bones/app/command.rb', line 27 def run( args ) raise NotImplementedError end |
- (Object) skeleton_dir
The directory where the project skeleton is located.
39 40 41 |
# File 'lib/bones/app/command.rb', line 39 def skeleton_dir @config[:skeleton_dir] end |
- (Object) standard_options
84 85 86 |
# File 'lib/bones/app/command.rb', line 84 def Command. end |
- (Boolean) verbose?
Returns true if the user has requested verbose messages.
59 60 61 |
# File 'lib/bones/app/command.rb', line 59 def verbose? @config[:verbose] end |