-
Notifications
You must be signed in to change notification settings - Fork 54
/
methadone
executable file
·131 lines (98 loc) · 4.14 KB
/
methadone
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
require 'fileutils'
require 'optparse'
require 'methadone'
require 'methadone/cli'
include FileUtils
include Methadone::Main
include Methadone::CLILogging
include Methadone::CLI
include Methadone::SH
main do |app_name|
check_and_prepare_basedir!(app_name,options[:force])
using_readme = options[:readme]
gemname = File.basename(app_name)
module_name = gemname.split(/-/).map(&:capitalize).collect{ |segment| segment.split(/_/).map(&:capitalize).join('') }.join('::')
debug "Creating project for gem #{gemname}"
chdir File.dirname(app_name)
require_file = nil
sh! "bundle gem #{gemname} --no-color" do |stdout|
require_file = stdout.split(/\n/).select { |file|
file =~ /\.rb$/ && file !~ /version.rb$/
}.first
end
rm_rf "#{gemname}/spec" # Don't want the default RSpec droppings
rm_rf "#{gemname}/.rspec"
require_file = gemname if require_file.nil?
require_file.gsub!(/^.*lib\//,'')
chdir gemname
template_dirs_in(:full).each { |dir| mkdir_p dir }
rspec = options[:rspec]
["Rakefile", ".gitignore", ].each do |file|
copy_file file, :binding => binding
end
if rspec
template_dirs_in(:rspec).each { |dir| mkdir_p dir }
copy_file "spec/something_spec.rb", :from => :rspec, :binding => binding
else
template_dirs_in(:test_unit).each { |dir| mkdir_p dir }
copy_file "test/unit/test_something.rb", :from => :test_unit, :binding => binding
copy_file "test/integration/test_cli.rb", :from => :test_unit, :binding => binding
end
gemspec = "#{gemname}.gemspec"
gem_variable = File.open(gemspec) { |x| x.read }.match(/(\w+)\.executables/)[1]
license = options[:license]
warn "warning: your app has no license" unless license
license = nil if license == 'NONE'
if license
copy_file "#{options[:license]}_LICENSE.txt", :as => "LICENSE.txt"
else
#Remove the MIT license generated by `bundle gem`
debug "Making sure no LICENSE.txt file exists at the root of the repo"
FileUtils.rm_f "LICENSE.txt"
end
#Ensure the gemspec file mentions the correct license
gemspec_content = File.read(gemspec)
if gemspec_content =~ /(^\s*#{gem_variable}\.license\s*=\s*).*/
gemspec_content.gsub!(/(^\s*#{gem_variable}\.license\s*=\s*).*/,"\\1#{license.to_s.inspect}")
else
gemspec_content.gsub!(/(^\s*#{gem_variable}\.name\s*=\s*.*)/,"\\1\n#{" #{gem_variable}.license".ljust(20)} = #{license.to_s.inspect.upcase}")
end
# RubyGems won't deal with a gemspec in this state and so Bundler won't even
# work at all. This is not helpful, so replace the magic keys its looking for
# with something else
gemspec_content.gsub!(/TODO/,"to-do")
gemspec_content.gsub!(/FIXME/,"fix me")
gemspec_content.gsub!(/^.*\.homepage.*$/,"")
File.open(gemspec,'w') {|f| f.write gemspec_content }
copy_file "README.rdoc", :binding => binding if using_readme
copy_file "bin/executable", :as => gemname, :executable => true, :binding => binding
add_to_file gemspec, [
" #{gem_variable}.add_development_dependency('rdoc')",
" #{gem_variable}.add_dependency('methadone', '~> #{Methadone::VERSION}')",
], :before => /^end\s*$/
ruby_major,ruby_minor,ruby_patch = RUBY_VERSION.split(/\./).map(&:to_i)
if ruby_major >= 2 && ruby_minor >= 2
add_to_file gemspec, [
" #{gem_variable}.add_development_dependency('test-unit')",
], :before => /^end\s*$/
end
if rspec
add_to_file gemspec, [
" #{gem_variable}.add_development_dependency('rspec', '~> 3')",
], :before => /^end\s*$/
end
FileUtils.rm_f "README.md", verbose: true
sh! %q(git add --all .)
end
options[:readme] = true
description "Kick the bash habit by bootstrapping your Ruby command-line apps"
on("--force","Overwrite files if they exist")
on("--[no-]readme","[Do not ]produce a README file")
on("--rspec", "Generate RSpec unit tests instead of Test::Unit")
licenses = %w(mit apache gplv2 gplv3 custom NONE)
on("-l LICENSE","--license",licenses,"Specify the license for your project",'(' + licenses.join('|') + ')')
use_log_level_option
arg :app_name, :required, "Name of your app, which is used for the gem name and executable name"
version Methadone::VERSION, :compact => true
go!