Skip to content

Instantly share code, notes, and snippets.

@dingzhihu
Created September 28, 2012 08:29
Show Gist options
  • Save dingzhihu/3798643 to your computer and use it in GitHub Desktop.
Save dingzhihu/3798643 to your computer and use it in GitHub Desktop.
create gem automatically
#!/usr/bin/env ruby -w
#Usage: ./gem-create.rb your_gem_name
def dump(file_name, content, executable = false)
content ||= ''
lines = content.split('\n')
File.open(file_name, 'w') do |f|
lines.each do |line|
f.puts line
end
end
File.chmod(0755, file_name) if executable
end
def create_gem(gem_name)
fail 'please specify your gem name' unless gem_name
fail "#{gem_name} already exists in current dir" if File.exists?(gem_name)
Dir.mkdir(gem_name)
Dir.chdir(gem_name)
Dir.mkdir('bin')
Dir.mkdir('spec')
lib_dir = 'lib'
Dir.mkdir(lib_dir)
Dir.chdir(lib_dir)
Dir.mkdir(gem_name)
gem_file = "#{gem_name}.rb"
gem_file_content = %q{#!/usr/bin/env ruby}
dump(gem_file, gem_file_content)
Dir.chdir('..')
gem_file = 'Gemfile'
gem_file_content = %q{source 'https://rubygems.org\ngemspec}
dump(gem_file, gem_file_content)
rake_file = 'Rakefile'
rake_file_content = %q{#!/usr/bin/env rake\nrequire "bundler/gem_tasks"}
dump(rake_file, rake_file_content)
gem_spec_file = "#{gem_name}.gemspec"
system "touch #{gem_spec_file}"
system "git init;git add .; git commit -am 'first commit'"
end
create_gem(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment