gn 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Readme.md +27 -21
  2. data/bin/gn +22 -70
  3. data/lib/gn.rb +64 -0
  4. metadata +12 -6
data/Readme.md CHANGED
@@ -10,30 +10,36 @@ ever find.
10
10
  Say you want to generate always a typical `README` file, like this
11
11
  one, you should do the following:
12
12
 
13
- Create a folder `readme` on your current folder and add an `init.rb`
14
- with the following content:
13
+ Create a folder `~/.gn/readme` and add a `plan.rb` file with the
14
+ following content:
15
15
 
16
16
  ```ruby
17
17
  module Plan
18
- module Readme
19
-
18
+ class README
20
19
  def name
21
20
  "CHANGEME"
22
21
  end
23
22
 
23
+ def author
24
+ "Your Name"
25
+ end
26
+
24
27
  def destination
25
- "readme"
28
+ "README"
26
29
  end
27
30
  end
28
31
  end
29
32
  ```
30
33
 
31
- Create a `readme/templates` folder and add the template for your
32
- README files:
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:
33
39
 
34
40
  ```
35
- mkdir readme/templates
36
- touch readme/templates/readme.mote
41
+ mkdir ~/.gn/readme/plan
42
+ touch ~/.gn/readme/plan/readme.mote
37
43
  ```
38
44
 
39
45
  For example, add the following to the template:
@@ -41,14 +47,14 @@ For example, add the following to the template:
41
47
  ```markdown
42
48
  # {{name}}
43
49
 
44
- Copyright (c) 2012 Lucas Florio
50
+ Copyright (c) 2012 {{author}}
45
51
  ```
46
52
 
47
53
  Templates are rendered using [`mote`](http://soveran.github.com/mote),
48
- the minimalist template engine. In `mote` variable interpolation is
54
+ the minimalist template engine. In `mote`, variable interpolation is
49
55
  done by using the `{{variable}}` syntax, as shown in the example
50
56
  above. Variable names are extracted from the current context, in this
51
- case the `Plan::Readme` module.
57
+ case an instance of `Plan::README`.
52
58
 
53
59
  Then run the generator like this:
54
60
 
@@ -56,19 +62,19 @@ Then run the generator like this:
56
62
  gn readme
57
63
  ```
58
64
 
59
- Now your default `$EDITOR` will pop open with a copy of your `init.rb`
60
- file, ready to be updated if necessary. **Note that your original
61
- `init.rb` file won't be modified**.
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.
62
68
 
63
- Close your editor and done! Your `gn` generated `README` file will be
64
- placed in your current directory.
69
+ Close your editor and you are done! Your `gn` generated `README` file
70
+ will be placed in your current directory.
65
71
 
66
72
  ## Other usages
67
73
 
68
- You can use gn to generate different things. In our case, we design it
69
- so we could generate basic crud/scaffolds for
70
- [Cuba](http://soveran.github.com/cuba), but you could use it for
71
- whatever you want.
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.
72
78
 
73
79
  See the folder named examples for some ideas.
74
80
 
data/bin/gn CHANGED
@@ -1,84 +1,36 @@
1
1
  #!/usr/bin/env ruby
2
- require 'mote'
3
- require 'fileutils'
4
- require 'tempfile'
5
2
 
6
- class Gn
7
-
8
- attr_reader :name
9
-
10
- def initialize(name)
11
- @name = name
12
- if location
13
- process
14
- else
15
- puts "Plan named '#{name}' doesn't exist"
16
- end
17
- end
18
-
19
- def process
20
- load_plan
21
- Plan.constants.each do |constant_name|
22
- constant = Plan.const_get(constant_name)
23
- if constant.class == Class
24
- puts "-> Generating #{constant_name}"
25
- generate_from constant_name, constant.new
26
- end
27
- end
28
- end
29
-
30
- def generate_from(constant_name, blueprint)
31
- template = File.join(templates_location,
32
- "#{constant_name.to_s.downcase}.mote")
33
- FileUtils.mkdir_p(File.dirname(blueprint.destination))
34
- File.open(blueprint.destination, "w") do |f|
35
- f.write Mote.parse(File.read(template), blueprint).call
36
- end
37
- end
38
-
39
- def location
40
- @location ||= available_locations.detect do |path|
41
- Dir.exists?(path)
42
- end
43
- end
44
-
45
- def available_locations
46
- [name, "./plans/#{name}", "#{ENV['HOME']}/.plans/#{name}"]
47
- end
48
-
49
- def templates_location
50
- File.join(@location, "templates")
51
- end
52
-
53
- def load_plan
54
- file = Tempfile.new('plan_init.rb')
55
- file.write(File.read(File.join(location, "init.rb")))
56
- file.close
57
- open_with_editor(file)
58
- load file.path
59
- end
60
-
61
- def open_with_editor(file)
62
- editor = ENV['EDITOR'] || "vi"
63
- system("#{editor} #{file.path}")
64
- end
65
-
66
- end
3
+ require_relative "../lib/gn"
67
4
 
68
5
  name = ARGV.first.to_s.strip
69
6
 
70
7
  if name.empty? || name == "-h" || name == "--help"
8
+
71
9
  puts <<-USAGE
72
10
  Usage: gn <name>
73
11
 
74
- Run the first <name> generator that is found in the following locations:
75
-
76
- * <name>/init.rb
77
- * plans/<name>/init.rb
78
- * ~/.plans/<name>/init.rb
12
+ Run the <name> generator found in ~/.gn/<name>.
79
13
 
80
14
  See http://lucasefe.github.com/gn for more information.
81
15
  USAGE
16
+
82
17
  else
83
- Gn.new(name)
18
+ require "fileutils"
19
+
20
+ gn = Gn.new(name)
21
+
22
+ # Load edited plan.
23
+ gn.load!
24
+
25
+ # Process each blueprint.
26
+ gn.blueprints.each do |blueprint|
27
+
28
+ # Create destination directory.
29
+ FileUtils.mkdir_p(File.dirname(blueprint.destination))
30
+
31
+ # Write generated template.
32
+ File.open(blueprint.destination, "w") do |file|
33
+ file.write blueprint.render
34
+ end
35
+ end
84
36
  end
@@ -0,0 +1,64 @@
1
+ require "mote"
2
+ require "tempfile"
3
+
4
+ class Gn
5
+ class Blueprint
6
+ def initialize(parent, constant)
7
+ @parent = parent
8
+ @instance = constant.new
9
+ @name = constant.name
10
+ end
11
+
12
+ def destination
13
+ @instance.destination
14
+ end
15
+
16
+ def file
17
+ File.join(@name.downcase.split("::")) + ".mote"
18
+ end
19
+
20
+ def template
21
+ File.read(@parent.path(file))
22
+ end
23
+
24
+ def render
25
+ Mote.parse(template, @instance).call
26
+ end
27
+ end
28
+
29
+ PLAN_FILE = "plan.rb"
30
+
31
+ attr :name
32
+
33
+ def initialize(name)
34
+ @name = name
35
+ end
36
+
37
+ def path(file)
38
+ "#{ENV["HOME"]}/.gn/#{name}/#{file}"
39
+ end
40
+
41
+ def load!
42
+ file = Tempfile.new(PLAN_FILE)
43
+ file.write(File.read(path(PLAN_FILE)))
44
+ file.close
45
+
46
+ edit(file)
47
+
48
+ load file.path
49
+ end
50
+
51
+ def editor
52
+ ENV["EDITOR"] || "vi"
53
+ end
54
+
55
+ def edit(file)
56
+ system "%s %s" % [editor, file.path]
57
+ end
58
+
59
+ def blueprints
60
+ Plan.constants.map do |constant|
61
+ Blueprint.new(self, Plan.const_get(constant))
62
+ end
63
+ end
64
+ end
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.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-06-02 00:00:00.000000000 Z
14
+ date: 2012-07-22 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: mote
18
- requirement: &70340185824720 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -23,7 +23,12 @@ dependencies:
23
23
  version: '0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70340185824720
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
27
32
  description: Simple Generator for the masses
28
33
  email:
29
34
  - lucasefe@gmail.com
@@ -32,8 +37,9 @@ executables:
32
37
  extensions: []
33
38
  extra_rdoc_files: []
34
39
  files:
35
- - bin/gn
40
+ - lib/gn.rb
36
41
  - lib/gn/dsl.rb
42
+ - bin/gn
37
43
  - LICENSE
38
44
  - Readme.md
39
45
  homepage: http://lucasefe.github.com/gn
@@ -57,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
63
  version: '0'
58
64
  requirements: []
59
65
  rubyforge_project:
60
- rubygems_version: 1.8.11
66
+ rubygems_version: 1.8.23
61
67
  signing_key:
62
68
  specification_version: 3
63
69
  summary: Simple Generator for the masses