Class: ProjectCreator
- Includes:
- Enumerable
- Defined in:
- lib/ramaze/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/ramaze/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/ramaze/tool/project_creator.rb', line 10 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/ramaze/tool/project_creator.rb', line 10 def @options end |
Instance Method Details
#amend? ⇒ Boolean
98 |
# File 'lib/ramaze/tool/project_creator.rb', line 98 def amend?; [:amend] end |
#copy(from, to) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/ramaze/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/ramaze/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/ramaze/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/ramaze/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
106 107 108 |
# File 'lib/ramaze/tool/project_creator.rb', line 106 def each Dir["#{proto}/**/*"].each{|path| yield(path) } end |
#fatal(message) ⇒ Object
101 102 103 104 |
# File 'lib/ramaze/tool/project_creator.rb', line 101 def fatal() warn exit 1 end |
#force? ⇒ Boolean
99 |
# File 'lib/ramaze/tool/project_creator.rb', line 99 def force?; [:force] end |
#got_proto? ⇒ Boolean
33 34 35 36 |
# File 'lib/ramaze/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/ramaze/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 92 |
# File 'lib/ramaze/tool/project_creator.rb', line 86 def post_process(file) return source = File.read(file) File.open(file, 'w+') do |io| io.write(source.gsub('$project', "'#@name'")) end end |
#proceed ⇒ Object
46 47 48 49 50 |
# File 'lib/ramaze/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/ramaze/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/ramaze/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/ramaze/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
94 95 96 |
# File 'lib/ramaze/tool/project_creator.rb', line 94 def relate(path) File.join(target, path.to_s.sub(proto, '')) end |
#target ⇒ Object
16 17 18 |
# File 'lib/ramaze/tool/project_creator.rb', line 16 def target File.(name) end |