Class: Crate::Main
- Inherits:
-
Object
- Object
- Crate::Main
- Defined in:
- lib/crate/main.rb
Overview
The Crate::Main class contains all the functionality needed by the crate
command line application. Much of this code is derived from the Webby::Main class
Instance Attribute Summary collapse
-
#data ⇒ Object
Directory where the template Crate project is located.
-
#options ⇒ Object
behavior options.
-
#project ⇒ Object
Directory where the Crate project will be created.
Class Method Summary collapse
-
.run(args) ⇒ Object
Create a new instance of Crate and run with the command line args.
Instance Method Summary collapse
-
#abort(msg) ⇒ Object
log a fatal message and abort.
-
#cp(file) ⇒ Object
Copy a file from the Crate prototype location to the project location.
-
#create_project ⇒ Object
Create a new Crate project.
-
#creating(msg) ⇒ Object
log a creating message.
-
#default_options ⇒ Object
default options.
-
#initialize ⇒ Main
constructor
Create a new Crate object.
-
#mkdir(dir) ⇒ Object
Make a directory in the specified directory under the project directory and display a message on the screen indicating that the directory is being created.
-
#option_parser ⇒ Object
The option parser for Crate.
-
#parse(argv) ⇒ Object
Parse the command line arguments.
-
#project_files ⇒ Object
Iterate over all the files in the Crate project template directory and store them in a hash.
Constructor Details
#initialize ⇒ Main
Create a new Crate object
44 45 46 47 |
# File 'lib/crate/main.rb', line 44 def initialize @log = Logging::Logger[self] @options = self. end |
Instance Attribute Details
#data ⇒ Object
Directory where the template Crate project is located
18 19 20 |
# File 'lib/crate/main.rb', line 18 def data @data end |
#options ⇒ Object
behavior options
21 22 23 |
# File 'lib/crate/main.rb', line 21 def @options end |
#project ⇒ Object
Directory where the Crate project will be created
15 16 17 |
# File 'lib/crate/main.rb', line 15 def project @project end |
Class Method Details
.run(args) ⇒ Object
Create a new instance of Crate and run with the command line args.
26 27 28 29 30 |
# File 'lib/crate/main.rb', line 26 def self.run( args ) m = self.new m.parse( args ) m.create_project end |
Instance Method Details
#abort(msg) ⇒ Object
log a fatal message and abort
146 147 148 149 |
# File 'lib/crate/main.rb', line 146 def abort( msg ) @log.fatal msg exit 1 end |
#cp(file) ⇒ Object
Copy a file from the Crate prototype location to the project location. Display a message that the file is being created.
129 130 131 132 133 134 |
# File 'lib/crate/main.rb', line 129 def cp( file ) src = ::File.join( data, file ) dest = ::File.join( project, file ) creating dest FileUtils.cp( src, dest ) end |
#create_project ⇒ Object
Create a new Crate project
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/crate/main.rb', line 97 def create_project unless .force abort "'#{project}' already exists" if File.exist?( project ) end # copy over files from the master project data diretory in crate files = project_files files.keys.sort.each do |dir| mkdir dir files[dir].sort.each do |file| cp file end end end |
#creating(msg) ⇒ Object
log a creating message
139 140 141 |
# File 'lib/crate/main.rb', line 139 def creating( msg ) @log.info "creating #{msg}" end |
#default_options ⇒ Object
default options
35 36 37 38 39 |
# File 'lib/crate/main.rb', line 35 def o = OpenStruct.new o.force = false return o end |
#mkdir(dir) ⇒ Object
Make a directory in the specified directory under the project directory and display a message on the screen indicating that the directory is being created.
117 118 119 120 121 122 123 |
# File 'lib/crate/main.rb', line 117 def mkdir( dir ) dir = dir.empty? ? project : ::File.join( project, dir ) unless File.directory?( dir ) creating dir FileUtils.mkdir_p dir end end |
#option_parser ⇒ Object
The option parser for Crate
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/crate/main.rb', line 52 def option_parser OptionParser.new do |op| op. << " project" op.on("-f", "--force", "force the overwriting of existing files") do self..force = true end op.separator "" op.separator "common options:" op.on_tail( "-h", "--help", "show this message") do puts op exit 0 end op.on_tail( "--version", "show version" ) do puts "Crate #{::Crate::VERSION}" exit 0 end end end |
#parse(argv) ⇒ Object
Parse the command line arguments
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/crate/main.rb', line 76 def parse( argv ) self.data = ::Crate.data_path opts = option_parser begin opts.parse!( argv ) self.project = argv.shift if project.nil? puts opts exit 1 end rescue ::OptionParser::ParseError => pe puts "#{opts.program_name}: #{pe}" puts "Try `#{opts.program_name} --help` for more information" exit 1 end end |
#project_files ⇒ Object
Iterate over all the files in the Crate project template directory and store them in a hash
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/crate/main.rb', line 155 def project_files keep = %r/.rake$|Rakefile$|.patch$|.c$/ strip_path = %r/\A#{data}?/o paths = Hash.new { |h,k| h[k] = [] } Find.find( data ) do |path| next unless keep =~ path if File.directory?( path ) then paths[ path.sub( strip_path, '' ) ] next end dir = ::File.dirname( path ).sub( strip_path, '' ) paths[dir] << path.sub( strip_path, '' ) end return paths end |