-
Notifications
You must be signed in to change notification settings - Fork 4
/
README
168 lines (130 loc) · 4.41 KB
/
README
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
GN(1)
NAME
gn -- The simplest template generator.
SYNOPSIS
gn <plan>
gn -i <plan-url>
gn -h
DESCRIPTION
<plan>
Opens the blueprints with the default editor, and once saved
it expands the templates.
-i <plan-url>
Installs the given plan from a git repository. It clones the
repository to ~/.gn/<plan>.
-h
Display this help message.
EXAMPLE PLAN
A plan is a directory in ~/.gn/<plan>, with the following
structure:
~/.gn/<plan>/plan.rb
~/.gn/<plan>/plan/<template>.mote
For example, consider a plan for generating a gemspec file.
~/.gn/gemspec/plan.rb
~/.gn/gemspec/plan/gemspec.mote
The plan definition `plan.rb` has this format:
module Plan
class GemSpec
def name
"foo"
end
def version
"0.0.1"
end
def description
"Description of my gem"
end
def authors
["My Name"]
end
def email
end
def homepage
"http://example.com/"
end
def license
"MIT"
end
def destination
"#{name}.gemspec"
end
end
end
The only mandatory method is `destination`. Everything else is
optional and is based on how the template is designed.
Templates are rendered using mote (http://soveran.github.com/mote),
the minimalist template engine. Variable interpolation is
done by using the {{variable}} syntax, as shown in the example
below. Variable names are extracted from the plan context, in this
case an instance of `Plan::GemSpec`.
# encoding: utf-8
Gem::Specification.new do |s|
s.name = "{{name}}"
s.version = "{{version}}"
s.summary = "{{description}}"
s.description = "{{description}}"
s.authors = {{authors.inspect}}
s.email = {{email.inspect}}
s.homepage = "{{homepage}}"
s.files = []
s.license = "{{license}}"
# s.executables.push(<executable>)
# s.add_dependency <dependency>, <version>
end
Refer to the advanced example for information about how to
generate different files and directory structures.
ADVANCED EXAMPLE
It is possible to use gn to generate several files and even
complex directory structures. Consider this plan definition:
$ find .gn/foo
.gn/foo//plan.rb
.gn/foo//plan/foo.mote
.gn/foo//plan/bar/baz.mote
$ cat plan.rb
module Plan
class Foo
def destination
"foo.rb"
end
end
module Bar
class Bar
def destination
"bar/baz.rb"
end
end
end
end
DSL USAGE
Define your plan like this:
require 'gn/dsl'
module Plan
extend Gn::DSL
template "App", "app.rb"
end
Or you can provide additional methods in a block:
require 'gn/dsl'
module Plan
extend Gn::DSL
template "App", "app.rb" do
def name
"My App Name"
end
end
end
EDITING A PLAN
When using a template, gn gives you the ability to edit the plan
before running it. Only the templates that correspond to classes
defined in the Plan module get expanded and written. It means
that if you want to create only the file `foo.rb` in the example
above, all you have to do is delete the Bar module from the plan
definition.
INSTALLATION
$ gem install gn
HISTORY
In software development, there's usually the need to generate
code or directory structures, and many tools have this feature
built in. The idea with gn is to provide a small and simple tool
that generalizes the task of template expansion in a way that's
easy to understand, yet powerful and flexible.