gn 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README +143 -0
  2. data/bin/gn +30 -9
  3. data/lib/gn.rb +1 -1
  4. metadata +19 -3
  5. data/Readme.md +0 -88
data/README ADDED
@@ -0,0 +1,143 @@
1
+ GN(1)
2
+
3
+ NAME
4
+ gn -- The simplest template generator.
5
+
6
+ SYNOPSIS
7
+ gn <plan>
8
+ gn -i <plan-url>
9
+ gn -h
10
+
11
+ DESCRIPTION
12
+ <plan>
13
+ Opens the blueprints with the default editor, and once saved
14
+ it expands the templates.
15
+
16
+ -i <plan-url>
17
+ Installs the given plan from a git repository. It clones the
18
+ repository to ~/.gn/<plan>.
19
+
20
+ -h
21
+ Display this help message.
22
+
23
+ EXAMPLE PLAN
24
+ A plan is a directory in ~/.gn/<plan>, with the following
25
+ structure:
26
+
27
+ ~/.gn/<plan>/plan.rb
28
+ ~/.gn/<plan>/plan/<template>.mote
29
+
30
+ For example, consider a plan for generating a gemspec file.
31
+
32
+ ~/.gn/gemspec/plan.rb
33
+ ~/.gn/gemspec/plan/gemspec.mote
34
+
35
+ The plan definition `plan.rb` has this format:
36
+
37
+ module Plan
38
+ class GemSpec
39
+ def name
40
+ "foo"
41
+ end
42
+
43
+ def version
44
+ "0.0.1"
45
+ end
46
+
47
+ def description
48
+ "Description of my gem"
49
+ end
50
+
51
+ def authors
52
+ ["My Name"]
53
+ end
54
+
55
+ def email
56
+ ["my_name@example.com"]
57
+ end
58
+
59
+ def homepage
60
+ "http://example.com/"
61
+ end
62
+
63
+ def license
64
+ "MIT"
65
+ end
66
+
67
+ def destination
68
+ "#{name}.gemspec"
69
+ end
70
+ end
71
+ end
72
+
73
+ The only mandatory method is `destination`. Everything else is
74
+ optional and is based on how the template is designed.
75
+
76
+ Templates are rendered using mote (http://soveran.github.com/mote),
77
+ the minimalist template engine. Variable interpolation is
78
+ done by using the {{variable}} syntax, as shown in the example
79
+ below. Variable names are extracted from the plan context, in this
80
+ case an instance of `Plan::GemSpec`.
81
+
82
+ # encoding: utf-8
83
+
84
+ Gem::Specification.new do |s|
85
+ s.name = "{{name}}"
86
+ s.version = "{{version}}"
87
+ s.summary = "{{description}}"
88
+ s.description = "{{description}}"
89
+ s.authors = {{authors.inspect}}
90
+ s.email = {{email.inspect}}
91
+ s.homepage = "{{homepage}}"
92
+ s.files = []
93
+ s.license = "{{license}}"
94
+ # s.executables.push(<executable>)
95
+ # s.add_dependency <dependency>, <version>
96
+ end
97
+
98
+ Refer to de advanced example for information about how to
99
+ generate different files and directory structures.
100
+
101
+ ADVANCED EXAMPLE
102
+ It is possible to use gn to generate several files and even
103
+ complex directory structures. Consider this plan definition:
104
+
105
+ $ find .gn/foo
106
+ .gn/foo//plan.rb
107
+ .gn/foo//plan/foo.mote
108
+ .gn/foo//plan/bar/baz.mote
109
+
110
+ $ cat plan.rb
111
+ module Plan
112
+ class Foo
113
+ def destination
114
+ "foo.rb"
115
+ end
116
+ end
117
+
118
+ module Bar
119
+ class Bar
120
+ def destination
121
+ "bar/baz.rb"
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ EDITING A PLAN
128
+ When using a template, gn gives you the ability to edit the plan
129
+ before running it. Only the templates that correspond to classes
130
+ defined in the Plan module get expanded and written. It means
131
+ that if you want to create only the file `foo.rb` in the example
132
+ above, all you have to do is delete the Bar module from the plan
133
+ definition.
134
+
135
+ INSTALLATION
136
+ $ gem install gn
137
+
138
+ HISTORY
139
+ In software development, there's usually the need to generate
140
+ code or directory structures, and many tools have this feature
141
+ built in. The idea with gn is to provide a small and simple tool
142
+ that generalizes the task of template expansion in a way that's
143
+ easy to understand, yet powerful and flexible.
data/bin/gn CHANGED
@@ -2,21 +2,42 @@
2
2
 
3
3
  require_relative "../lib/gn"
4
4
 
5
- name = ARGV.first.to_s.strip
5
+ require "clap"
6
+ require "fileutils"
6
7
 
7
- if name.empty? || name == "-h" || name == "--help"
8
+ module CLI
9
+ README = File.expand_path("../README", File.dirname(__FILE__))
10
+ GN_HOME = File.join(ENV["HOME"], ".gn")
8
11
 
9
- puts <<-USAGE
10
- Usage: gn <name>
12
+ # Display the README message and exit.
13
+ def self.help
14
+ exec "${PAGER:-less} #{README}"
15
+ end
16
+
17
+ def self.install(template)
18
+
19
+ # Create directory for templates if it doesn't exist.
20
+ FileUtils.mkdir_p(GN_HOME)
21
+
22
+ # Clone the template repository.
23
+ Dir.chdir(GN_HOME) do
24
+ exec "git clone %s" % template
25
+ end
26
+ end
27
+ end
11
28
 
12
- Run the <name> generator found in ~/.gn/<name>.
29
+ # Display the README if there are no arguments.
30
+ CLI.help if ARGV.empty?
13
31
 
14
- See http://lucasefe.github.com/gn for more information.
15
- USAGE
32
+ rest = Clap.run ARGV,
33
+ "-h" => CLI.method(:help),
34
+ "--help" => CLI.method(:help)
16
35
 
17
- else
18
- require "fileutils"
36
+ rest = Clap.run rest,
37
+ "-i" => CLI.method(:install),
38
+ "--install" => CLI.method(:install)
19
39
 
40
+ rest.each do |name|
20
41
  gn = Gn.new(name)
21
42
 
22
43
  # Load edited plan.
data/lib/gn.rb CHANGED
@@ -39,7 +39,7 @@ class Gn
39
39
  end
40
40
 
41
41
  def load!
42
- file = Tempfile.new(PLAN_FILE)
42
+ file = Tempfile.new([PLAN_FILE, ".rb"])
43
43
  file.write(File.read(path(PLAN_FILE)))
44
44
  file.close
45
45
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-07-22 00:00:00.000000000 Z
14
+ date: 2012-07-23 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: mote
@@ -29,6 +29,22 @@ dependencies:
29
29
  - - ! '>='
30
30
  - !ruby/object:Gem::Version
31
31
  version: '0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: clap
34
+ requirement: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
32
48
  description: Simple Generator for the masses
33
49
  email:
34
50
  - lucasefe@gmail.com
@@ -41,7 +57,7 @@ files:
41
57
  - lib/gn/dsl.rb
42
58
  - bin/gn
43
59
  - LICENSE
44
- - Readme.md
60
+ - README
45
61
  homepage: http://lucasefe.github.com/gn
46
62
  licenses:
47
63
  - MIT
data/Readme.md DELETED
@@ -1,88 +0,0 @@
1
- # gn
2
-
3
- ## Description
4
-
5
- gn is the simplest file/folder/structure/whatever generator you could
6
- ever find.
7
-
8
- ## Usage
9
-
10
- Say you want to generate always a typical `README` file, like this
11
- one, you should do the following:
12
-
13
- Create a folder `~/.gn/readme` and add a `plan.rb` file with the
14
- following content:
15
-
16
- ```ruby
17
- module Plan
18
- class README
19
- def name
20
- "CHANGEME"
21
- end
22
-
23
- def author
24
- "Your Name"
25
- end
26
-
27
- def destination
28
- "README"
29
- end
30
- end
31
- end
32
- ```
33
-
34
- The only mandatory method is `destination`. Everything else is
35
- optional and is based on how the template is designed.
36
-
37
- Create a `~/.gn/readme/plan` folder and add the template for your
38
- README file:
39
-
40
- ```
41
- mkdir ~/.gn/readme/plan
42
- touch ~/.gn/readme/plan/readme.mote
43
- ```
44
-
45
- For example, add the following to the template:
46
-
47
- ```markdown
48
- # {{name}}
49
-
50
- Copyright (c) 2012 {{author}}
51
- ```
52
-
53
- Templates are rendered using [`mote`](http://soveran.github.com/mote),
54
- the minimalist template engine. In `mote`, variable interpolation is
55
- done by using the `{{variable}}` syntax, as shown in the example
56
- above. Variable names are extracted from the current context, in this
57
- case an instance of `Plan::README`.
58
-
59
- Then run the generator like this:
60
-
61
- ```
62
- gn readme
63
- ```
64
-
65
- Now your default `$EDITOR` will pop open with a copy of your `plan.rb`
66
- file, ready to be updated if necessary. Note that the original
67
- `plan.rb` file won't be modified.
68
-
69
- Close your editor and you are done! Your `gn` generated `README` file
70
- will be placed in your current directory.
71
-
72
- ## Other usages
73
-
74
- You can use gn to generate different things. In our case, we
75
- designed it so we could generate basic crud/scaffolds for
76
- [Cuba](http://soveran.github.com/cuba), but you can use it for
77
- anything you want.
78
-
79
- See the folder named examples for some ideas.
80
-
81
- ## Installation
82
-
83
- ```
84
- gem install gn
85
- ```
86
-
87
- Given that `gn` is still in development process, you should add
88
- `--pre` to your `gem install`.