mygen 0.3.20 → 0.3.21
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 +25 -2
- data/lib/mygen.rb +1 -0
- data/lib/mygen/files.rb +60 -0
- data/lib/mygen/generator.rb +5 -95
- data/lib/mygen/templates.rb +43 -0
- data/lib/mygen/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48da32a4632ba0c9655317c4b1999c52aed507e9
|
4
|
+
data.tar.gz: 2d31418e01b4e5223debf5a5c4b75279874331c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e29d9ef5466168b6174335e69d84db88db61706dcc3c9a8cfd19b21bff815abc2a1f528be866f8fb4fb98c78125163c4168d4ab41a376e2d91e50fa7c4de3bfb
|
7
|
+
data.tar.gz: 41a829d0efdd1a365cc4a5da2b12301bb1692e4f5f6f5b0a74025df5401901b201fc890dafc6f70e67b977e61b3b1fd2d67d91f866c82a10165cd62d72facb84
|
data/README.md
CHANGED
@@ -20,14 +20,37 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
Mygen will help you create repetitious code and folder structures. It works
|
24
|
+
through plugins, that you create through mygen. Get a full list of options and
|
25
|
+
commands with
|
26
|
+
|
23
27
|
```
|
24
28
|
myg --help
|
25
29
|
```
|
26
30
|
|
31
|
+
### Making a new plugin
|
32
|
+
|
33
|
+
Create your first plugin like this
|
34
|
+
|
35
|
+
```
|
36
|
+
myg g myg my_plugin
|
37
|
+
```
|
38
|
+
|
39
|
+
This will create a plugin called ``` my_plugin ```. You will need to run the
|
40
|
+
bundle command to install dependencies.
|
41
|
+
|
42
|
+
In order for mygen to find the plugin, you can make a symlink into
|
43
|
+
|
44
|
+
``` ~./.mygen/plugins/my_plugin ```
|
45
|
+
|
46
|
+
or you can commit your plugin to github and clone it into you plugins folder.
|
47
|
+
|
48
|
+
This will enable mygen to autodetect new plugins.
|
49
|
+
|
27
50
|
|
28
|
-
|
51
|
+
#### Substitution of filenames
|
29
52
|
|
30
|
-
Files named with
|
53
|
+
Files named with ``` name ``` will be renamed to match the attribute ``` name ```
|
31
54
|
|
32
55
|
## Development
|
33
56
|
|
data/lib/mygen.rb
CHANGED
data/lib/mygen/files.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
module Mygen
|
3
|
+
module Files
|
4
|
+
def template_files(path = template_source_dir)
|
5
|
+
Dir.glob(File.join(path, "**/*")).select { |f| File.file? f }
|
6
|
+
end
|
7
|
+
|
8
|
+
def template_dirs(path = template_source_dir)
|
9
|
+
Dir.glob(File.join(path, "**/*")).select { |f| File.directory? f }
|
10
|
+
end
|
11
|
+
|
12
|
+
def internal_template_files
|
13
|
+
template_files(internal_template_source_dir)
|
14
|
+
end
|
15
|
+
|
16
|
+
def internal_template_source_dir
|
17
|
+
File.join(Mygen.root, "templates", generator_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def file_destination(file, bindings)
|
21
|
+
# subtract template_source_dir from path, add dest_dir and substitute filenames
|
22
|
+
new_file = file.gsub(template_source_dir, '')
|
23
|
+
replaced_filename(new_file, bindings)
|
24
|
+
end
|
25
|
+
|
26
|
+
def move_file_in_place(src, dest)
|
27
|
+
sf = File.absolute_path(src)
|
28
|
+
df = File.absolute_path(dest)
|
29
|
+
file_exist = File.exist?(df)
|
30
|
+
fileutils.mv(sf, df) if sf != df && !file_exist
|
31
|
+
|
32
|
+
if file_exist && sf != df
|
33
|
+
Dir.glob(File.join(sf, "/*")).each do |f|
|
34
|
+
next if f == sf
|
35
|
+
file = f.sub(sf, '')
|
36
|
+
new_name = File.join(df, file)
|
37
|
+
next if directory_exists? new_name
|
38
|
+
fileutils.mv(f, File.join(df, file))
|
39
|
+
end
|
40
|
+
fileutils.rm_rf(sf)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def directory_exists?(path)
|
45
|
+
File.directory?(path) && File.exist?(path)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def replaced_filename(file, bindings)
|
51
|
+
elements = File.basename(file).split('.')
|
52
|
+
elements.pop if elements.last == 'erb'
|
53
|
+
dirname = File.dirname(file)
|
54
|
+
f = elements.map do |element|
|
55
|
+
element.start_with?('__') ? eval( element.sub(/^__/, ''), bindings) : element
|
56
|
+
end.join('.')
|
57
|
+
File.join(dirname, f)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/mygen/generator.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
+
require 'mygen/files'
|
1
2
|
require 'mygen/naming'
|
3
|
+
require 'mygen/templates'
|
2
4
|
# require 'byebug'
|
3
5
|
|
4
6
|
module Mygen
|
5
7
|
class Generator
|
6
8
|
include Mygen::Naming
|
9
|
+
include Mygen::Files
|
10
|
+
include Mygen::Templates
|
11
|
+
|
7
12
|
attr_accessor :name, :dest_dir, :dry_run, :template_source_dir, :options
|
8
13
|
|
9
14
|
def initialize
|
@@ -26,105 +31,10 @@ module Mygen
|
|
26
31
|
dry_run ? FileUtils::DryRun : FileUtils::Verbose
|
27
32
|
end
|
28
33
|
|
29
|
-
def template_files(path = template_source_dir)
|
30
|
-
Dir.glob(File.join(path, "**/*")).select { |f| File.file? f }
|
31
|
-
end
|
32
|
-
|
33
|
-
def template_dirs(path = template_source_dir)
|
34
|
-
Dir.glob(File.join(path, "**/*")).select { |f| File.directory? f }
|
35
|
-
end
|
36
|
-
|
37
|
-
def internal_template_files
|
38
|
-
template_files(internal_template_source_dir)
|
39
|
-
end
|
40
|
-
|
41
|
-
def internal_template_source_dir
|
42
|
-
File.join(Mygen.root, "templates", generator_name)
|
43
|
-
end
|
44
|
-
|
45
|
-
def make_template_tree_in_current_dir
|
46
|
-
fileutils.mkdir_p(dest_dir) unless File.exist?(dest_dir)
|
47
|
-
files = Dir.glob(File.join(template_source_dir, "/*"))
|
48
|
-
fileutils.cp_r(files, dest_dir)
|
49
|
-
end
|
50
|
-
|
51
|
-
def make_template_tree(internal = false)
|
52
|
-
@template_source_dir = internal_template_source_dir if internal
|
53
|
-
fileutils.rm_rf(dest_dir) if File.exist?(dest_dir)
|
54
|
-
fileutils.cp_r(template_source_dir, dest_dir)
|
55
|
-
end
|
56
|
-
|
57
|
-
# rename directories that should be filtered, from __name
|
58
|
-
# files should be from the destination, so no dirs needs to be filtered
|
59
|
-
# and only files need to be processed.
|
60
|
-
#
|
61
|
-
def parse_templates(bindings)
|
62
|
-
template_dirs(File.join(dest_dir)).each do |dir|
|
63
|
-
dest = file_destination(dir, bindings)
|
64
|
-
parent_dir = File.expand_path("..", dest)
|
65
|
-
fileutils.mkdir_p(parent_dir) if parent_dirs_dont_exist?(dest)
|
66
|
-
move_file_in_place(dir, dest)
|
67
|
-
end
|
68
|
-
# Filter files with erb
|
69
|
-
template_files(File.join(dest_dir)).each do |file|
|
70
|
-
dest = file_destination(file, bindings)
|
71
|
-
# This is where you parse the erb files and fill in the contens
|
72
|
-
if file.end_with? 'erb'
|
73
|
-
parse(file, bindings)
|
74
|
-
end
|
75
|
-
move_file_in_place(file, dest)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
34
|
def parent_dirs_dont_exist?(path)
|
80
35
|
parent_dir = File.expand_path("..", path)
|
81
36
|
return false if path.include?('__')
|
82
37
|
return !File.exist?(path)
|
83
38
|
end
|
84
|
-
|
85
|
-
def parse(file, bindings)
|
86
|
-
erb = ERB.new(File.read(file))
|
87
|
-
result = erb.result bindings
|
88
|
-
File.open(file, "w") { |f| f.write(result) }
|
89
|
-
end
|
90
|
-
|
91
|
-
def move_file_in_place(src, dest)
|
92
|
-
sf = File.absolute_path(src)
|
93
|
-
df = File.absolute_path(dest)
|
94
|
-
file_exist = File.exist?(df)
|
95
|
-
fileutils.mv(sf, df) if sf != df && !file_exist
|
96
|
-
|
97
|
-
if file_exist && sf != df
|
98
|
-
Dir.glob(File.join(sf, "/*")).each do |f|
|
99
|
-
next if f == sf
|
100
|
-
file = f.sub(sf, '')
|
101
|
-
fileutils.mv(f, File.join(df, file))
|
102
|
-
end
|
103
|
-
fileutils.rm_rf(sf)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def parse_and_place_file(file, dest, bindings)
|
108
|
-
erb = ERB.new(File.read(file))
|
109
|
-
erb.result bindings
|
110
|
-
end
|
111
|
-
|
112
|
-
def file_destination(file, bindings)
|
113
|
-
# subtract template_source_dir from path, add dest_dir and substitute filenames
|
114
|
-
new_file = file.gsub(template_source_dir, '')
|
115
|
-
replaced_filename(new_file, bindings)
|
116
|
-
end
|
117
|
-
|
118
|
-
private
|
119
|
-
|
120
|
-
def replaced_filename(file, bindings)
|
121
|
-
elements = File.basename(file).split('.')
|
122
|
-
elements.pop if elements.last == 'erb'
|
123
|
-
dirname = File.dirname(file)
|
124
|
-
f = elements.map do |element|
|
125
|
-
element.start_with?('__') ? eval( element.sub(/^__/, ''), bindings) : element
|
126
|
-
end.join('.')
|
127
|
-
File.join(dirname, f)
|
128
|
-
end
|
129
39
|
end
|
130
40
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Mygen
|
2
|
+
module Templates
|
3
|
+
def make_template_tree_in_current_dir
|
4
|
+
fileutils.mkdir_p(dest_dir) unless File.exist?(dest_dir)
|
5
|
+
files = Dir.glob(File.join(template_source_dir, "/*"))
|
6
|
+
fileutils.cp_r(files, dest_dir)
|
7
|
+
end
|
8
|
+
|
9
|
+
def make_template_tree(internal = false)
|
10
|
+
@template_source_dir = internal_template_source_dir if internal
|
11
|
+
fileutils.rm_rf(dest_dir) if File.exist?(dest_dir)
|
12
|
+
fileutils.cp_r(template_source_dir, dest_dir)
|
13
|
+
end
|
14
|
+
|
15
|
+
# rename directories that should be filtered, from __name
|
16
|
+
# files should be from the destination, so no dirs needs to be filtered
|
17
|
+
# and only files need to be processed.
|
18
|
+
#
|
19
|
+
def parse_templates(bindings)
|
20
|
+
template_dirs(File.join(dest_dir)).each do |dir|
|
21
|
+
dest = file_destination(dir, bindings)
|
22
|
+
parent_dir = File.expand_path("..", dest)
|
23
|
+
fileutils.mkdir_p(parent_dir) if parent_dirs_dont_exist?(dest)
|
24
|
+
move_file_in_place(dir, dest)
|
25
|
+
end
|
26
|
+
# Filter files with erb
|
27
|
+
template_files(File.join(dest_dir)).each do |file|
|
28
|
+
dest = file_destination(file, bindings)
|
29
|
+
# This is where you parse the erb files and fill in the contens
|
30
|
+
if file.end_with? 'erb'
|
31
|
+
parse(file, bindings)
|
32
|
+
end
|
33
|
+
move_file_in_place(file, dest)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def parse(file, bindings)
|
38
|
+
erb = ERB.new(File.read(file))
|
39
|
+
result = erb.result bindings
|
40
|
+
File.open(file, "w") { |f| f.write(result) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/mygen/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mygen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.21
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kristian Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -114,10 +114,12 @@ files:
|
|
114
114
|
- bin/myg
|
115
115
|
- bin/setup
|
116
116
|
- lib/mygen.rb
|
117
|
+
- lib/mygen/files.rb
|
117
118
|
- lib/mygen/generator.rb
|
118
119
|
- lib/mygen/naming.rb
|
119
120
|
- lib/mygen/plugins.rb
|
120
121
|
- lib/mygen/plugins/mygen_generator.rb
|
122
|
+
- lib/mygen/templates.rb
|
121
123
|
- lib/mygen/version.rb
|
122
124
|
- mygen.gemspec
|
123
125
|
- templates/mygen_generator/Gemfile
|
@@ -151,3 +153,4 @@ signing_key:
|
|
151
153
|
specification_version: 4
|
152
154
|
summary: Code generator for generating project structures, for various languages
|
153
155
|
test_files: []
|
156
|
+
has_rdoc:
|