Class: Seedling::ProjectCreator
- Inherits:
-
Object
- Object
- Seedling::ProjectCreator
- Includes:
- Enumerable
- Defined in:
- lib/seedling/project_creator.rb
Constant Summary collapse
- PROTO =
[Pathname.new(__FILE__).dirname..join("..", "templates", "core")]
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#pot ⇒ Object
Returns the value of attribute pot.
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(pot, options = {}) ⇒ ProjectCreator
constructor
A new instance of ProjectCreator.
- #mkdir(dir) ⇒ Object
-
#post_process(file) ⇒ Object
Any file in the prototype directory named /.seed$/ will be treated as an ERB template and parsed in the current binding.
- #proceed ⇒ Object
- #proceed_directories(dirs) ⇒ Object
- #proceed_files(files) ⇒ Object
- #proto ⇒ Object
- #relate(path) ⇒ Object
- #target ⇒ Object
Constructor Details
#initialize(pot, options = {}) ⇒ ProjectCreator
Returns a new instance of ProjectCreator.
24 25 26 27 |
# File 'lib/seedling/project_creator.rb', line 24 def initialize(pot, = {}) @pot, @options = Pathname.new(pot), @interactive = @options.keys.include?(:interactive) ? @options[:interactive] : true end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/seedling/project_creator.rb', line 22 def @options end |
#pot ⇒ Object
Returns the value of attribute pot.
22 23 24 |
# File 'lib/seedling/project_creator.rb', line 22 def pot @pot end |
Instance Method Details
#amend? ⇒ Boolean
137 |
# File 'lib/seedling/project_creator.rb', line 137 def amend?; [:amend] end |
#copy(from, to) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/seedling/project_creator.rb', line 81 def copy(from, to) return unless copy_check(to) puts("copy(%p, %p)" % [from, to]) if @interactive FileUtils.cp(from, to) post_process(to) end |
#copy_check(to) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/seedling/project_creator.rb', line 88 def copy_check(to) exists = File.file?(to) return false if exists and amend? return false if exists and not force? return true end |
#create ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/seedling/project_creator.rb', line 51 def create got_proto? puts("Found proto at: %p, proceeding...\n\n" % proto) if @interactive mkdir(relate('/')) if create_root? proceed end |
#create_root? ⇒ Boolean
40 41 42 43 44 |
# File 'lib/seedling/project_creator.rb', line 40 def create_root? return true unless target.directory? return true if amend? or force? fatal "%p is a directory, choose different project name or use --amend/--force" % target end |
#each ⇒ Object
145 146 147 |
# File 'lib/seedling/project_creator.rb', line 145 def each Dir["#{proto}/**/*"].each{|path| yield(path) } end |
#fatal(message) ⇒ Object
140 141 142 143 |
# File 'lib/seedling/project_creator.rb', line 140 def fatal() warn exit 1 end |
#force? ⇒ Boolean
138 |
# File 'lib/seedling/project_creator.rb', line 138 def force?; [:force] end |
#got_proto? ⇒ Boolean
46 47 48 49 |
# File 'lib/seedling/project_creator.rb', line 46 def got_proto? return true if proto.directory? fatal "Cannot create, %p doesn't exist, use --proto or create the proto directory" % proto end |
#mkdir(dir) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/seedling/project_creator.rb', line 73 def mkdir(dir) exists = File.directory?(dir) return if exists and amend? return if exists and not force? puts("mkdir(%p)" % dir) if @interactive FileUtils.mkdir_p(dir) end |
#post_process(file) ⇒ Object
Any file in the prototype directory named /.seed$/ will be treated as an ERB template and parsed in the current binding. the @options hash should be used for storing values to be used in the templates
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 |
# File 'lib/seedling/project_creator.rb', line 99 def post_process(file) if File.basename(file.to_s).match(/library/) oldfile = file file = file.to_s.sub("library", @options[:lib_name_u]) FileUtils.mv(oldfile, file) end if File.dirname(file.to_s).split("/").last == "library" origdir = File.dirname(file.to_s) dirarr = origdir.split("/") dirarr[dirarr.size-1] = @options[:lib_name_u] new_dir = File.join(dirarr) mkdir(new_dir) oldfile = file file = File.join(new_dir, File.basename(file)) FileUtils.mv(oldfile, file) FileUtils.rmdir(origdir) end if file.to_s.match(/\.seed$/) out_file = Pathname.new(file.to_s.sub(/\.seed$/, '')) # Don't overwrite a file of the same name, unless they --force if copy_check(out_file) template = ::ERB.new(File.read(file)) # This binding has access to any instance variables of # the ProjectCreator instance result = template.result(binding) File.open(file.to_s.sub(/\.seed$/,''), 'w+') do |io| io.puts result end end # Remove the seed file whether we copied or not FileUtils.rm_f(file) end end |
#proceed ⇒ Object
59 60 61 62 63 |
# File 'lib/seedling/project_creator.rb', line 59 def proceed files, directories = partition{|path| File.file?(path) } proceed_directories(directories) proceed_files(files) end |
#proceed_directories(dirs) ⇒ Object
69 70 71 |
# File 'lib/seedling/project_creator.rb', line 69 def proceed_directories(dirs) dirs.each{|dir| mkdir(relate(dir)) } end |
#proceed_files(files) ⇒ Object
65 66 67 |
# File 'lib/seedling/project_creator.rb', line 65 def proceed_files(files) files.each{|file| copy(file, relate(file)) } end |
#proto ⇒ Object
33 34 35 36 37 38 |
# File 'lib/seedling/project_creator.rb', line 33 def proto PROTO.map!{|pr| pr. } proto = [:proto] ||= PROTO.find{|f| f.directory? } layout = [:layout] || "" proto.join(layout). end |
#relate(path) ⇒ Object
133 134 135 |
# File 'lib/seedling/project_creator.rb', line 133 def relate(path) File.join(target, path.to_s.sub(proto.to_s, '')) end |
#target ⇒ Object
29 30 31 |
# File 'lib/seedling/project_creator.rb', line 29 def target pot. end |