Class: Seedling::Bin::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/seedling/bin.rb

Overview

This class contains the command methods {{{

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Cmd

Returns a new instance of Cmd.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/seedling/bin.rb', line 15

def initialize(args = nil)
  args ||= ARGV
  raise "arguments must be an array!" unless args.respond_to?(:detect)
  @ourargs = args.dup
  @command = args.detect { |arg| arg.match(/^(?:--?)?(?:plant|create|h(?:elp)?|v(?:ersion)?|console)/) }
  if command.nil?
    @command = ""
  else
    args.delete(@command)
  end
  ARGV.replace(args)
end

Instance Attribute Details

#commandObject

include Helpers



13
14
15
# File 'lib/seedling/bin.rb', line 13

def command
  @command
end

Class Method Details

.run(args = nil) ⇒ Object

{{{ #run is called when we’re interactive ($0 == __FILE__)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/seedling/bin.rb', line 29

def self.run(args = nil)
  cmd = new(args)
  case cmd.command
  when /^(?:--?)?(?:plant|create)$/
    require "seedling/project_creator"
    cmd.plant(cmd.command)
  when /^(?:--?)?console$/
    require "irb"
    require "irb/completion"
    cmd.include_seedling
    IRB.start
    puts "Seedling session has ended."
  when /^(?:--?)?h(elp)?$/
    puts cmd.usage
  when /^(?:--?)?v(ersion)?$/
    cmd.include_seedling
    puts Seedling::VERSION
    exit
  when /^$/
    raise "Must supply a valid command\n\n" + cmd.usage
  else
    raise "invalid arguments #{args.join(" ")}\n\n" + cmd.usage
  end
end

Instance Method Details

#al_rootObject

}}}



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/seedling/bin.rb', line 91

def al_root
  require "pathname"
  dir = nil
  if ARGV.size == 1
    dir = Pathname.new(ARGV.shift)
  elsif ARGV.size > 1
    $stderr.puts "Unknown options given #{ARGV.join(" ")}"
    puts usage
    exit 1
  end
  if dir.nil? or not dir.directory?
    dir = Pathname.new(ENV["PWD"]).expand_path
    $stderr.puts "Path to seedling tree not given or invalid, using #{dir}"
  end
  Object.const_set("AL_ROOT", dir.expand_path.to_s)
  Dir.chdir(AL_ROOT)
end

#include_seedlingObject

}}}



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/seedling/bin.rb', line 54

def include_seedling # {{{
  begin
    $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '/../lib')
    require 'seedling'
  rescue LoadError
    $LOAD_PATH.shift

    begin
      require 'rubygems'
    rescue LoadError
    end
    require 'seedling'
  end
end

#plant(command) ⇒ Object

{{{



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/seedling/bin.rb', line 135

def plant(command) # {{{
  plant_options(o = {}).parse!(ARGV)
  if ARGV.size > 1
    raise "Invalid options given: #{ARGV.join(" ")}\n\n" + usage
  end
  project_root = ARGV.shift
  if project_root.nil?
    raise "Must supply a valid directory to install your project, you gave none.\n\n" + usage
  end
  o[:lib_name] ||= Pathname.new(project_root).basename.to_s.classify
  o[:lib_name_u] ||= o[:lib_name].underscore
  opts = plant_defaults(o)
  # need to titleize this
  include_seedling
  Seedling::ProjectCreator.new(project_root, opts).create
end

#plant_options(opts = {}) ⇒ Object

Methods for commands {{{



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/seedling/bin.rb', line 110

def plant_options(opts = {})
  @plant_opts ||= OptionParser.new do |o|
    o.banner = "Planting Options"
    o.on("-sSUMMARY", "--summary SUMMARY", "Short description of this project") { |yn| opts[:summary] = yn }
    o.on("-dDESCRIPTION", "--description DESCRIPTION", "Longer description (Default: summary)") { |des| opts[:description] = des }
    o.on("-lLIBNAME", "--libname LIBNAME", "Library name (Default: path specifcation)") { |libname| opts[:lib_name] = libname }
    o.on("-gDOCGENERATOR", "--doc-generator DOCGENERATOR", "Preferred documentation generator (Default: yard)") { |docgenerator| opts[:doc_generator] = docgenerator }
    o.on("-tTESTSUITE", "--test-suite TESTSUITE", "Preferred test suite (Default: bacon)") { |testsuite| opts[:test_suite] = testsuite }
    o.on("-vVER", "--version VER", "Initial version number (Default: 0.0.1)") { |ver| opts[:version] = ver }
    o.on("-rRUBYFORGE", "--rubyforge RUBYFORGE", "Rubyforge project name") { |rubyforge| opts[:rubyforge_project] = rubyforge }

    o.separator ""
    o.separator "Author Options"
    o.on("-aAUTHOR", "--author AUTHOR", "Author's Name") { |yn| opts[:author_name] = yn }
    o.on("-eEMAIL", "--email EMAIL", "Author's Email") { |yn| opts[:author_email] = yn }
    o.on("-uURL", "--url URL", "Project URL/homepage") { |url| opts[:project_url] = url }

    o.separator ""
    o.separator "Directory Creation Options"
    o.on("-f", "--force", "Force creation if dir already exists") { |yn| opts[:force] = true }
    o.on("-A", "--amend", "Update a tree") { |yn| opts[:amend] = true }
    o.on("-q", "--quiet", "Don't show output of tree creation") { |yn| opts[:interactive] = false }
  end
end

#usageObject

}}}



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/seedling/bin.rb', line 69

def usage # {{{
  txt = [
    "\n  Usage:", 
    "seedling <plant|create|console> PROJECT [options]\n",
    "Commands:\n",
    " plant   - Creates a new prototype Seedling application in a directory named PROJECT in",
    "           the current directory.  seedling create foo would make ./foo containing a",
    "           seedling prototype.\n",
    " create  - Synonymous with plant.\n",
    " console - Starts an irb console with seedling (and irb completion) loaded.",
    "           ARGV is passed on to IRB.\n\n"
  ].join("\n\t")

  txt <<  "* All commands take PROJECT as the directory the seedling lives in.\n\n"
  txt << plant_options.to_s << "\n"
  #if is_windows?
    #txt << %x{ruby #{rackup_path} --help}.split("\n").reject { |line| line.match(/^Usage:/) }.join("\n\t")
  #else
    #txt << %x{#{rackup_path} --help}.split("\n").reject { |line| line.match(/^Usage:/) }.join("\n\t")
  #end
end