gengen 1.0.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.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +33 -0
- data/Rakefile +10 -0
- data/bin/gengen +2 -0
- data/gengen.gemspec +21 -0
- data/lib/gengen/version.rb +3 -0
- data/lib/gengen.rb +107 -0
- data/spec/fixtures/example/foo.rb +1 -0
- data/spec/fixtures/example/lib/bar.txt +3 -0
- data/spec/fixtures/example/{{{var2}}}_xxx.rb +1 -0
- data/spec/gengen_spec.rb +52 -0
- data/spec/spec_helper.rb +6 -0
- metadata +120 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 jugyo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# GenGen
|
2
|
+
|
3
|
+
A simple generator using github repository.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install gengen
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
From github:
|
12
|
+
|
13
|
+
gengen user/template [dirctory] [foo=bar ...]
|
14
|
+
|
15
|
+
↓
|
16
|
+
|
17
|
+
gengen jugyo/sublime-plugin RubyUtils name=RubyUtils command=test
|
18
|
+
|
19
|
+
From local git repository:
|
20
|
+
|
21
|
+
gengen --local(-l) git_repository_path [dirctory] [foo=bar ...]
|
22
|
+
|
23
|
+
↓
|
24
|
+
|
25
|
+
gengen -l /path/to/sublime-plugin RubyUtils name=RubyUtils command=test
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/gengen
ADDED
data/gengen.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/gengen/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["jugyo"]
|
6
|
+
gem.email = ["jugyo.org@gmail.com"]
|
7
|
+
gem.description = %q{A simple generator using github repository}
|
8
|
+
gem.summary = %q{A simple generator}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "gengen"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Gengen::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
gem.add_development_dependency "rspec", "~> 2.0"
|
20
|
+
gem.add_development_dependency "rr"
|
21
|
+
end
|
data/lib/gengen.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "gengen/version"
|
3
|
+
require "tmpdir"
|
4
|
+
require "fileutils"
|
5
|
+
|
6
|
+
module GenGen
|
7
|
+
REGEXP = /{{{\s*(.*?)\s*}}}/
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def gen(args)
|
11
|
+
args = args.dup
|
12
|
+
options = extract_otpions!(args)
|
13
|
+
|
14
|
+
local = true if args.delete('-l') || args.delete('--local')
|
15
|
+
|
16
|
+
if local
|
17
|
+
git_url = args[0]
|
18
|
+
else
|
19
|
+
github_project = args[0]
|
20
|
+
unless github_project && github_project =~ /^[^\/]+\/[^\/]+$/
|
21
|
+
usage!
|
22
|
+
end
|
23
|
+
github_project += '.gengen' unless github_project =~ /\.gengen$/
|
24
|
+
git_url = "git@github.com:#{github_project}.git"
|
25
|
+
end
|
26
|
+
|
27
|
+
dest_dir = args[1] || File.basename(git_url).sub('.gengen', '').sub('.git', '')
|
28
|
+
if File.exists?(dest_dir)
|
29
|
+
abort "'#{dest_dir}' already exists"
|
30
|
+
end
|
31
|
+
|
32
|
+
temp_dir = fetch(git_url)
|
33
|
+
process(temp_dir, options)
|
34
|
+
|
35
|
+
FileUtils.mv(temp_dir, dest_dir)
|
36
|
+
end
|
37
|
+
|
38
|
+
def usage!
|
39
|
+
abort <<-D
|
40
|
+
Usage:
|
41
|
+
gengen user/template [dirctory] [foo=bar ...]
|
42
|
+
gengen --local(-l) git_repository_path [dirctory] [foo=bar ...]
|
43
|
+
|
44
|
+
Examples:
|
45
|
+
gengen jugyo/sublime-plugin RubyUtils name=RubyUtils command=test
|
46
|
+
gengen -l /path/to/sublime-plugin RubyUtils name=RubyUtils command=test
|
47
|
+
D
|
48
|
+
end
|
49
|
+
|
50
|
+
def extract_otpions!(args)
|
51
|
+
options = {}
|
52
|
+
args.delete_if do |_|
|
53
|
+
if _ =~ /^(\w+)=(\w+)$/
|
54
|
+
options[$1] = $2
|
55
|
+
true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
options
|
59
|
+
end
|
60
|
+
|
61
|
+
def fetch(git_url)
|
62
|
+
temp_dir = Dir.mktmpdir
|
63
|
+
puts "git clone #{git_url} ..."
|
64
|
+
system 'git', 'clone', git_url, temp_dir
|
65
|
+
FileUtils.rm_rf(File.join(temp_dir, '.git'))
|
66
|
+
temp_dir
|
67
|
+
end
|
68
|
+
|
69
|
+
def process(dir, vars)
|
70
|
+
Dir[File.join(dir, '**', '*')].each do |filepath|
|
71
|
+
next unless File.file?(filepath)
|
72
|
+
|
73
|
+
basename = File.basename(filepath)
|
74
|
+
if basename =~ REGEXP
|
75
|
+
old_path = filepath
|
76
|
+
new_path = File.join File.dirname(filepath), replace(basename, vars)
|
77
|
+
puts "renaming... #{old_path.sub(dir + '/', '')} => #{new_path.sub(dir + '/', '')}"
|
78
|
+
FileUtils.mv(old_path, new_path)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
Dir[File.join(dir, '**', '*')].each do |filepath|
|
83
|
+
next unless File.file?(filepath)
|
84
|
+
|
85
|
+
puts "processing... #{filepath.sub(dir + '/', '')}"
|
86
|
+
contents = replace(File.read(filepath), vars)
|
87
|
+
File.open(filepath, 'w') do |file|
|
88
|
+
file.write contents
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
puts 'done!'
|
93
|
+
end
|
94
|
+
|
95
|
+
def replace(text, vars)
|
96
|
+
text.gsub(REGEXP) do |_|
|
97
|
+
var_name = $1
|
98
|
+
unless vars.key?(var_name)
|
99
|
+
print "[#{var_name}]: "
|
100
|
+
value = STDIN.gets.strip
|
101
|
+
vars[var_name] = value.empty? ? var_name : value
|
102
|
+
end
|
103
|
+
vars[var_name]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
foo{{{ var1 }}}foo{{{ var2 }}}bar
|
@@ -0,0 +1 @@
|
|
1
|
+
foo{{{var3}}}bar
|
data/spec/gengen_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tmpdir'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
describe GenGen do
|
6
|
+
describe ".gen" do
|
7
|
+
it 'works with github repository' do
|
8
|
+
mock(GenGen).fetch('git@github.com:foo/bar.gengen.git') { '/tmp/baz' }
|
9
|
+
mock(GenGen).process('/tmp/baz', {'a' => 'b', 'c' => 'd'})
|
10
|
+
mock(FileUtils).mv('/tmp/baz', 'baz')
|
11
|
+
GenGen.gen(%w(foo/bar baz a=b c=d))
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'works with local repository' do
|
15
|
+
mock(GenGen).fetch('/foo/bar') { '/tmp/baz' }
|
16
|
+
mock(GenGen).process('/tmp/baz', {'a' => 'b', 'c' => 'd'})
|
17
|
+
mock(FileUtils).mv('/tmp/baz', 'baz')
|
18
|
+
GenGen.gen(%w(-l /foo/bar baz a=b c=d))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe ".extract_otpions" do
|
23
|
+
it "works" do
|
24
|
+
args = ['foo/bar', 'bar', 'a=b', 'c=d']
|
25
|
+
options = GenGen.extract_otpions!(args)
|
26
|
+
args.should eq(['foo/bar', 'bar'])
|
27
|
+
options.should eq({'a' => 'b', 'c' => 'd'})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".process" do
|
32
|
+
let(:tmpdir) { File.join(Dir.mktmpdir, 'example') }
|
33
|
+
|
34
|
+
before do
|
35
|
+
FileUtils.cp_r(File.expand_path('../fixtures/example', __FILE__), tmpdir)
|
36
|
+
end
|
37
|
+
|
38
|
+
after do
|
39
|
+
FileUtils.rm_rf(tmpdir)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "works" do
|
43
|
+
mock(STDIN).gets { "BAZ\n" }
|
44
|
+
|
45
|
+
GenGen.process(tmpdir, 'var1' => 'FOO', 'var2' => 'BAR')
|
46
|
+
|
47
|
+
File.read(File.join(tmpdir, 'foo.rb')).should eq("fooFOOfooBARbar\n")
|
48
|
+
File.read(File.join(tmpdir, 'lib', 'bar.txt')).should eq("foo{{var1\n}}\nfooBARbar\n")
|
49
|
+
File.read(File.join(tmpdir, 'BAR_xxx.rb')).should eq("fooBAZbar\n")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gengen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- jugyo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rr
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A simple generator using github repository
|
63
|
+
email:
|
64
|
+
- jugyo.org@gmail.com
|
65
|
+
executables:
|
66
|
+
- gengen
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- bin/gengen
|
77
|
+
- gengen.gemspec
|
78
|
+
- lib/gengen.rb
|
79
|
+
- lib/gengen/version.rb
|
80
|
+
- spec/fixtures/example/foo.rb
|
81
|
+
- spec/fixtures/example/lib/bar.txt
|
82
|
+
- spec/fixtures/example/{{{var2}}}_xxx.rb
|
83
|
+
- spec/gengen_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
homepage: ''
|
86
|
+
licenses: []
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
hash: -2094350327006867538
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
hash: -2094350327006867538
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.8.23
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: A simple generator
|
115
|
+
test_files:
|
116
|
+
- spec/fixtures/example/foo.rb
|
117
|
+
- spec/fixtures/example/lib/bar.txt
|
118
|
+
- spec/fixtures/example/{{{var2}}}_xxx.rb
|
119
|
+
- spec/gengen_spec.rb
|
120
|
+
- spec/spec_helper.rb
|