greg 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +116 -10
- data/bin/greg +37 -3
- data/greg.gemspec +1 -1
- data/lib/greg.rb +9 -8
- data/lib/greg/template_installer.rb +16 -1
- data/lib/greg/templates/file_tree_template.rb +0 -1
- data/lib/greg/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43d86623060138705711092c127b214b7603dedc
|
4
|
+
data.tar.gz: 3d34ed47a7b3db7f901993f7173c2065c1465590
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa23c9e295dd50935e69688c2a3429a68e20668b634d613dbe942e4048858ffa72e6f536fb1b4569c3ded8295f0947fbb7712f8737fa50d21f4e739a21d96222
|
7
|
+
data.tar.gz: 0f765367414ccf8afe1ef527647cfb4a4ec796fd140dd246a86bfa86d5d7033b0a001ae65a548458555a461aea80a0612e2d9ed5b939a38149f683ef409c56b6
|
data/README.md
CHANGED
@@ -1,26 +1,132 @@
|
|
1
1
|
# Greg
|
2
2
|
|
3
|
-
|
3
|
+
Simple code generator
|
4
4
|
|
5
|
-
##
|
5
|
+
## Introduction
|
6
|
+
|
7
|
+
Greg is a simple code generator. It generates code based on templates.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
To create an app use the `greg` command as follows:
|
12
|
+
|
13
|
+
$ greg <app name> --template=<template name>
|
14
|
+
|
15
|
+
This will create your app on the current directory.
|
16
|
+
|
17
|
+
For example, to create a Roda application:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
$ pwd
|
21
|
+
/tmp/code
|
22
|
+
$ ls
|
23
|
+
$ greg my_awesome_app --template=roda_app
|
24
|
+
Creating Project:
|
25
|
+
[create] .env
|
26
|
+
[create] routes/
|
27
|
+
[create] routes/main.rb
|
28
|
+
[create] spec/
|
29
|
+
[create] spec/my_awesome_app/
|
30
|
+
[create] spec/spec_helper.rb
|
31
|
+
[create] spec/my_awesome_app_spec.rb
|
32
|
+
[create] config.ru
|
33
|
+
[create] Gemfile
|
34
|
+
[create] public/
|
35
|
+
[create] my_awesome_app.rb
|
36
|
+
[create] views/
|
37
|
+
[create] views/welcome.haml
|
38
|
+
[create] views/layout.haml
|
39
|
+
[create] assets/
|
40
|
+
[create] assets/js/
|
41
|
+
[create] assets/js/some_file.coffee
|
42
|
+
[create] assets/css/
|
43
|
+
[create] assets/css/some_file.scss
|
44
|
+
%
|
45
|
+
$ ls
|
46
|
+
my_awesome_app
|
47
|
+
$ ls my_awesome_app
|
48
|
+
assets config.ru Gemfile my_awesome_app.rb public routes spec views
|
49
|
+
$
|
50
|
+
```
|
51
|
+
|
52
|
+
To get a list of templates you can use the `--list` flag
|
53
|
+
|
54
|
+
$ greg --list
|
55
|
+
|
56
|
+
To install a template (mus be listed under "Available templates" on the `--list`):
|
57
|
+
|
58
|
+
$ greg --install=<template name>
|
59
|
+
|
60
|
+
For example:
|
61
|
+
|
62
|
+
$ greg --install=roda_app
|
63
|
+
|
64
|
+
## Creating you own template
|
65
|
+
|
66
|
+
The first thing you probably want to do is get some boilerplate in place:
|
6
67
|
|
7
|
-
|
68
|
+
$ greg my_template --template=template
|
69
|
+
|
70
|
+
This will create a new directory called `my_template` under `~/.greg_templates/`.
|
71
|
+
Inside, you'll find the `my_template_generator.rb` file. That's where all the logic for the generator goes.
|
72
|
+
|
73
|
+
Now you just need to define a method called `#files`, which should return an array containing all the needed files to be created.
|
74
|
+
|
75
|
+
The easiest way to get started is using `Greg::FileTreeTemplate`, like this:
|
8
76
|
|
9
77
|
```ruby
|
10
|
-
|
78
|
+
class MyTemplateGenerator < Greg::Generator
|
79
|
+
def files
|
80
|
+
[
|
81
|
+
Greg::FileTreeTemplate.new("templates"),
|
82
|
+
]
|
83
|
+
end
|
84
|
+
end
|
11
85
|
```
|
12
86
|
|
13
|
-
And
|
87
|
+
And under `~/.greg_templates/my_template/templates/` put all the template files. This will compy the `templates` subdirectory almost verbatim to the generated application.
|
14
88
|
|
15
|
-
|
89
|
+
Notice I said "almost verbatim" and that's because we have an ace under our sleeves.
|
90
|
+
Normally, we want to name files with some name different than just 'app.rb' or 'my_app.whatever'. In that case, instead of naming the file like that, you can use the `@APP_NAME@` "variable".
|
91
|
+
If you have the following tree:
|
16
92
|
|
17
|
-
|
93
|
+
```
|
94
|
+
~/.greg_templates/foo/templates/
|
95
|
+
├── @APP_NAME@.rb
|
96
|
+
└── spec
|
97
|
+
├── @APP_NAME@_spec.rb
|
98
|
+
├── @TEMPLATE_NAME@_spec.rb
|
99
|
+
└── spec_helper.rb
|
100
|
+
```
|
18
101
|
|
19
|
-
|
102
|
+
When you run
|
20
103
|
|
21
|
-
|
104
|
+
$ greg my_cool_app --template=foo
|
22
105
|
|
23
|
-
|
106
|
+
the resulting tree will be:
|
107
|
+
|
108
|
+
```
|
109
|
+
~/.greg_templates/foo/templates/
|
110
|
+
├── my_cool_app.rb
|
111
|
+
└── spec
|
112
|
+
├── my_cool_app_spec.rb
|
113
|
+
├── foo_spec.rb
|
114
|
+
└── spec_helper.rb
|
115
|
+
```
|
116
|
+
|
117
|
+
You also have some variables to use inside the file contents. The files will be rendered using ERB and you have the following methods available:
|
118
|
+
(assuming the app name is again `my_cool_app`)
|
119
|
+
|
120
|
+
| variable | exported value |
|
121
|
+
| ------------- |:--------------:|
|
122
|
+
| app_name | my_cool_app |
|
123
|
+
| app_clas_name | MyCoolApp |
|
124
|
+
|
125
|
+
## Installation
|
126
|
+
|
127
|
+
You can install Greg by running
|
128
|
+
|
129
|
+
$ gem install greg
|
24
130
|
|
25
131
|
## Contributing
|
26
132
|
|
data/bin/greg
CHANGED
@@ -19,20 +19,54 @@ Options:
|
|
19
19
|
Installing a template
|
20
20
|
greg --install=<template-name> [--path=<template-path>]
|
21
21
|
greg -i <template-name> [--path=<template-path>]
|
22
|
-
EOF
|
23
22
|
|
23
|
+
Listing templates
|
24
|
+
greg --list
|
25
|
+
EOF
|
24
26
|
puts usage
|
25
27
|
exit(0)
|
26
28
|
end
|
27
29
|
|
30
|
+
require "greg"
|
31
|
+
|
32
|
+
if options[:list]
|
33
|
+
require "json"
|
34
|
+
|
35
|
+
installed_templates = `ls #{Greg.templates_dir}`.split("\n")
|
36
|
+
remote_templates = begin
|
37
|
+
JSON.parse(`curl https://api.github.com/orgs/GregTemplates/repos`).map {|i|i["name"]}
|
38
|
+
rescue
|
39
|
+
[]
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
available_templates = remote_templates - ["greg", *installed_templates]
|
44
|
+
|
45
|
+
puts available_templates
|
46
|
+
msg = []
|
47
|
+
msg << "Installed templates:"
|
48
|
+
msg << installed_templates.map { |l| " * #{l}" }
|
49
|
+
msg << ""
|
50
|
+
if available_templates.any?
|
51
|
+
msg << "Available templates:"
|
52
|
+
msg << available_templates.map { |l| " * #{l}" }
|
53
|
+
end
|
54
|
+
puts msg.join("\n")
|
55
|
+
|
56
|
+
exit(0)
|
57
|
+
end
|
58
|
+
|
28
59
|
if options[:i] || options[:install]
|
60
|
+
i = options.delete(:install)
|
61
|
+
i ||= options.delete(:i)
|
62
|
+
options[:template] ||= i
|
29
63
|
require "greg/template_installer"
|
30
|
-
installer = Greg::TemplateInstaller.new(options).install
|
64
|
+
installer = Greg::TemplateInstaller.new(**options).install
|
65
|
+
|
31
66
|
exit(0)
|
32
67
|
end
|
33
68
|
|
34
69
|
begin
|
35
|
-
require "greg"
|
36
70
|
|
37
71
|
templates_dir = options.delete(:templates_dir)
|
38
72
|
Greg.templates_dir = templates_dir if templates_dir
|
data/greg.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Federico Iachetti"]
|
10
10
|
spec.email = ["iachetti.federico@gmail.com"]
|
11
11
|
spec.summary = %q{Simple generator.}
|
12
|
-
spec.description = %q{Simple generator.}
|
12
|
+
spec.description = %q{Simple code generator.}
|
13
13
|
spec.homepage = "https://github.com/iachettifederico/greg"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/greg.rb
CHANGED
@@ -13,6 +13,15 @@ module Greg
|
|
13
13
|
@generator ||= get_generator(**options)
|
14
14
|
end
|
15
15
|
|
16
|
+
def self.templates_dir
|
17
|
+
@templates_dir ||= Pathname("~/.greg_templates").expand_path.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.templates_dir=(new_dir)
|
21
|
+
@templates_dir = Pathname(new_dir).expand_path.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
16
25
|
def self.get_generator(**options) #name:, template_name: , output_directory: ".", force: false)
|
17
26
|
template_name = options[:template_name]
|
18
27
|
generator_name = "#{template_name}_generator"
|
@@ -27,12 +36,4 @@ module Greg
|
|
27
36
|
generator_class.new( **options )
|
28
37
|
end
|
29
38
|
|
30
|
-
def self.templates_dir
|
31
|
-
@templates_dir ||= Pathname("~/.greg_templates").expand_path.to_s
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.templates_dir=(new_dir)
|
35
|
-
@templates_dir = Pathname(new_dir).expand_path.to_s
|
36
|
-
end
|
37
|
-
|
38
39
|
end
|
@@ -1,5 +1,20 @@
|
|
1
1
|
module Greg
|
2
2
|
class TemplateInstaller
|
3
|
-
|
3
|
+
attr_reader :template
|
4
|
+
attr_reader :templates_dir
|
5
|
+
def initialize(template:, templates_dir: Greg.templates_dir)
|
6
|
+
raise "greg is not a template" if template == "greg"
|
7
|
+
@template = template
|
8
|
+
@templates_dir = templates_dir
|
9
|
+
end
|
10
|
+
def install
|
11
|
+
output_dir = "#{templates_dir}/#{template}"
|
12
|
+
`rm -rf #{output_dir}`
|
13
|
+
command = %W[git clone git@github.com:GregTemplates/#{template}.git #{output_dir}]
|
14
|
+
puts command.join " "
|
15
|
+
IO.popen(command) do |out|
|
16
|
+
out.read.each_line {|l| puts l}
|
17
|
+
end
|
18
|
+
end
|
4
19
|
end
|
5
20
|
end
|
data/lib/greg/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: greg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.1.6
|
83
|
-
description: Simple generator.
|
83
|
+
description: Simple code generator.
|
84
84
|
email:
|
85
85
|
- iachetti.federico@gmail.com
|
86
86
|
executables:
|