Class: Bones::App::Command

Inherits:
Object show all
Includes:
Colors
Defined in:
lib/bones/app/command.rb

Direct Known Subclasses

Create, Freeze, Info, Plugins, Unfreeze

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_SKELETON =

:stopdoc:

'default'

Constants included from Colors

Colors::COLORS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

#colorize, #colorize?

Constructor Details

#initialize(opts = {}) ⇒ Command

Returns 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

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/bones/app/command.rb', line 12

def config
  @config
end

#stderrObject (readonly)

Returns the value of attribute stderr.



11
12
13
# File 'lib/bones/app/command.rb', line 11

def stderr
  @stderr
end

#stdoutObject (readonly)

:startdoc:



10
11
12
# File 'lib/bones/app/command.rb', line 10

def stdout
  @stdout
end

Class Method Details

.inherited(other) ⇒ Object



208
209
210
# File 'lib/bones/app/command.rb', line 208

def self.inherited( other )
  other.extend ClassMethods
end

.standard_optionsObject

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
  @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

#in_directory(dir) ⇒ Object

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.expand_path(FileUtils.pwd)
  FileUtils.cd dir
  yield
ensure
  FileUtils.cd pwd
end

#mrbones_dirObject

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.expand_path(path)
end

#nameObject

The project name from the command line.



45
46
47
# File 'lib/bones/app/command.rb', line 45

def name
  @config[:name]
end

#output_dirObject

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

#parse(args) ⇒ Object



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.banner = '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.options and not self.class.options.empty?
    opts.separator 'PARAMETERS'
    self.class.options.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

#repositoryObject

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

#run(args) ⇒ Object

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/bones/app/command.rb', line 27

def run( args )
  raise NotImplementedError
end

#skeleton_dirObject

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

#standard_optionsObject



84
85
86
# File 'lib/bones/app/command.rb', line 84

def standard_options
  Command.standard_options
end

#verbose?Boolean

Returns true if the user has requested verbose messages.

Returns:

  • (Boolean)


59
60
61
# File 'lib/bones/app/command.rb', line 59

def verbose?
  @config[:verbose]
end