-
Notifications
You must be signed in to change notification settings - Fork 3
/
copy.rb
executable file
·57 lines (48 loc) · 1.84 KB
/
copy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby
require 'bundler/setup'
require 'rake'
require 'yaml'
# extract options
gem_class_name = ARGV[0]
gem_description = ARGV[1]
if not gem_class_name or not gem_description
abort "Usage: ./template/copy.rb MyNewLib 'A great lib to do stuff'"
end
gem_name = gem_class_name.gsub(/(.)([A-Z])/,'\1_\2').downcase.gsub("/", "-")
# load settings
here = File.dirname(__FILE__)
SETTINGS = YAML.load_file("#{here}/.copy.yml")
# copy files
files_to_copy = Dir["#{here}/**/**"] + ["#{here}/.rubocop.yml"] - ["#{here}/copy.rb", "#{here}/Gemfile.lock"]
files_to_copy.reject! { |f| File.directory?(f) || f.include?("/vendor/") }
files_to_copy.each do |file_to_copy|
# make new dir and file
new_file = file_to_copy
new_file = new_file.
sub(here,'').
sub(%r{^/},'').
sub('GEM_NAME', gem_name.sub("-", "/")).
sub(/\/(.*gemspec)/, "-\\1")
new_file = "#{gem_name}/#{new_file}"
sh "mkdir -p #{File.dirname(new_file)} 2>&1"
# write modified content
content = File.read(file_to_copy)
content.gsub!('AUTHOR_GITHUB', SETTINGS['github'])
content.gsub!('AUTHOR_EMAIL', SETTINGS['email'])
content.gsub!('AUTHOR_HOMEPAGE', SETTINGS['homepage'])
content.gsub!('AUTHOR_NAME', SETTINGS['name'])
content.gsub!('GEM_NAME', gem_name)
content.gsub!('GEM_CLASS_NAME', gem_class_name)
content.gsub!('GEM_DESCRIPTION', gem_description)
File.open(new_file, 'w'){|f| f.write content }
end
Bundler.with_original_env do
# check tests and (fast) generate Gemfile.lock
sh "cd #{gem_name} && (bundle check || bundle) && bundle exec rake"
# commit everything into 'initial'
sh "cd #{gem_name} && git init && git add . && git commit -m 'initial by http://github.com/grosser/project_template'"
if system("which -s pre_commit_rubocop.rb")
sh "cd #{gem_name} && pre_commit_rubocop.rb install"
end
end
puts "#{gem_name} is now ready at ./#{gem_name}"