Class: ProjectCreator
- Inherits:
-
Object
- Object
- ProjectCreator
- Includes:
- Enumerable
- Defined in:
- lib/autumn/tool/project_creator.rb
Constant Summary collapse
- PROTO =
[]
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #amend? ⇒ Boolean
- #copy(from, to) ⇒ Object
- #copy_check(to) ⇒ Object
- #create ⇒ Object
- #create_root? ⇒ Boolean
- #each ⇒ Object
- #fatal(message) ⇒ Object
- #force? ⇒ Boolean
- #got_proto? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ ProjectCreator
constructor
A new instance of ProjectCreator.
- #mkdir(dir) ⇒ Object
-
#post_process(file) ⇒ Object
Think about a useful way to process the generated files it should be possible to substitute some things like the project name in the configuration.
- #proceed ⇒ Object
- #proceed_directories(dirs) ⇒ Object
- #proceed_files(files) ⇒ Object
- #proto ⇒ Object
- #relate(path) ⇒ Object
- #target ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ ProjectCreator
Returns a new instance of ProjectCreator.
12 13 14 |
# File 'lib/autumn/tool/project_creator.rb', line 12 def initialize(name, = {}) @name, @options = name, end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/autumn/tool/project_creator.rb', line 10 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/autumn/tool/project_creator.rb', line 10 def @options end |
Instance Method Details
#amend? ⇒ Boolean
97 |
# File 'lib/autumn/tool/project_creator.rb', line 97 def amend?; [:amend] end |
#copy(from, to) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/autumn/tool/project_creator.rb', line 68 def copy(from, to) return unless copy_check(to) puts "copy(%p, %p)" % [from, to] FileUtils.cp(from, to) post_process(to) end |
#copy_check(to) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/autumn/tool/project_creator.rb', line 75 def copy_check(to) exists = File.file?(to) return if exists and amend? return if exists and not force? return true end |
#create ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/autumn/tool/project_creator.rb', line 38 def create got_proto? puts "Found proto at: %p, proceeding...\n\n" % proto mkdir(relate('/')) if create_root? proceed end |
#create_root? ⇒ Boolean
27 28 29 30 31 |
# File 'lib/autumn/tool/project_creator.rb', line 27 def create_root? return true unless File.directory?(target) return true if amend? or force? fatal "%p is a directory, choose different project name or use --amend/--force" % target end |
#each ⇒ Object
105 106 107 |
# File 'lib/autumn/tool/project_creator.rb', line 105 def each Dir["#{proto}/**/*"].each{|path| yield(path) } end |
#fatal(message) ⇒ Object
100 101 102 103 |
# File 'lib/autumn/tool/project_creator.rb', line 100 def fatal() warn exit 1 end |
#force? ⇒ Boolean
98 |
# File 'lib/autumn/tool/project_creator.rb', line 98 def force?; [:force] end |
#got_proto? ⇒ Boolean
33 34 35 36 |
# File 'lib/autumn/tool/project_creator.rb', line 33 def got_proto? return true if File.directory?(proto) fatal "Cannot create, %p doesn't exist, use --proto or create the proto directory" % proto end |
#mkdir(dir) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/autumn/tool/project_creator.rb', line 60 def mkdir(dir) exists = File.directory?(dir) return if exists and amend? return if exists and not force? puts "mkdir(%p)" % dir FileUtils.mkdir_p(dir) end |
#post_process(file) ⇒ Object
Think about a useful way to process the generated files it should be possible to substitute some things like the project name in the configuration
86 87 88 89 90 91 |
# File 'lib/autumn/tool/project_creator.rb', line 86 def post_process(file) source = File.read(file) File.open(file, 'w+') do |io| io.puts source.gsub('$${project}', @name) end end |
#proceed ⇒ Object
46 47 48 49 50 |
# File 'lib/autumn/tool/project_creator.rb', line 46 def proceed files, directories = partition{|path| File.file?(path) } proceed_directories(directories) proceed_files(files) end |
#proceed_directories(dirs) ⇒ Object
56 57 58 |
# File 'lib/autumn/tool/project_creator.rb', line 56 def proceed_directories(dirs) dirs.each{|dir| mkdir(relate(dir)) } end |
#proceed_files(files) ⇒ Object
52 53 54 |
# File 'lib/autumn/tool/project_creator.rb', line 52 def proceed_files(files) files.each{|file| copy(file, relate(file)) } end |
#proto ⇒ Object
20 21 22 23 24 25 |
# File 'lib/autumn/tool/project_creator.rb', line 20 def proto PROTO.map!{|pr| File.(pr) } proto = [:proto] ||= PROTO.find{|f| File.directory?(f) } layout = [:layout] ||= '/' File.(File.join(proto, layout)) end |
#relate(path) ⇒ Object
93 94 95 |
# File 'lib/autumn/tool/project_creator.rb', line 93 def relate(path) File.join(target, path.to_s.sub(proto, '')) end |
#target ⇒ Object
16 17 18 |
# File 'lib/autumn/tool/project_creator.rb', line 16 def target File.(name) end |