forked from lucasefe/gn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgn
More file actions
executable file
·68 lines (54 loc) · 1.38 KB
/
Copy pathgn
File metadata and controls
executable file
·68 lines (54 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
require 'mote'
require 'fileutils'
require 'tempfile'
class Gn
attr_reader :name
def initialize(name)
@name = name
run
end
def run
load_plan
Plan.constants.each do |constant|
blueprint = Plan.const_get(constant).new
template = File.join(templates_location, constant.to_s.downcase)
FileUtils.mkdir_p(File.dirname(blueprint.destination))
File.open(blueprint.destination, "w") do |f|
f.write Mote.parse(File.read(template), blueprint).call
end
end
end
def location
@location ||= [name, "./plans/#{name}", "#{ENV['HOME']}.plans/#{name}"].detect do |path|
Dir.exists?(path)
end
end
def templates_location
File.join(@location, "templates")
end
def load_plan
file = Tempfile.new('plan_init')
file.write(File.read(File.join(location, "init.rb")))
file.close
open_with_editor(file)
load file.path
end
def open_with_editor(file)
editor = ENV['EDITOR'] || "vi"
system("#{editor} #{file.path}")
end
end
name = ARGV.first.to_s.strip
if name.empty? || name == "-h" || name == "--help"
puts <<-USAGE
Usage: gn <name>
Run the first <name> generator that is found in the following locations:
* <name>/init.rb
* plans/<name>/init.rb
* ~/.plans/<name>/init.rb
See http://lucasefe.github.com/gn for more information.
USAGE
else
Gn.new(name)
end