Skip to content

Instantly share code, notes, and snippets.

@phoolish
Forked from edavis10/plugin_to_gem.rb
Created May 21, 2012 09:11
Show Gist options
  • Save phoolish/2761388 to your computer and use it in GitHub Desktop.
Save phoolish/2761388 to your computer and use it in GitHub Desktop.
Script to convert a Rails/Redmine plugin to a RubyGem without Jeweler
#!/usr/bin/env ruby
# Usage:
# ruby plugin_to_gem.rb my_plugin_directory
require 'fileutils'
require 'uri'
require 'erb'
@plugin_dir = ARGV[0]
def gemspec_content
description = 'TODO'
authors = "'Nobody'"
email = "'test@test.com'"
homepage = nil
summary = nil
redmine_init_content = File.read('init.rb')
if redmine_init_content.match(/description (.*$)/)
description = $1.gsub("'",'').gsub('"','')
end
summary = description
if redmine_init_content.match(/name (.*$)/)
summary = $1.gsub("'",'').gsub('"','')
end
if redmine_init_content.match(/author (.*$)/)
authors = $1.gsub("'",'').gsub('"','').split(',')
authors = "'#{authors.join("', '")}'"
end
if redmine_init_content.match(/email (.*$)/)
email = $1.gsub("'",'').gsub('"','').split(',')
email = "'#{email.join("', '")}'"
end
if redmine_init_content.match(/url (.*$)/)
url = $1.gsub("'",'').gsub('"','')
unless (url =~ URI::regexp).nil?
homepage = url
end
end
content = ERB.new <<-EOF
# -*- encoding: utf-8 -*-
require File.join(File.dirname(__FILE__), 'VERSION.rb')
Gem::Specification.new do |s|
s.name = '<%= $plugin_name %>'
s.version = VERSION
s.authors = [<%= authors %>]
s.email = [<%= email %>]
s.description = '<%= description %>'
<% if homepage %>s.homepage = '<%= homepage %>'<% end %>
<% if summary %>s.summary = '<%= summary %>'<% end %>
s.rubyforge_project = "<%= $plugin_name %>" #TODO
s.files = `git ls-files`.split("\\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\\n")
end
EOF
content.result(binding)
end
FileUtils.cd(@plugin_dir, :verbose => true) do |dir|
system('rake clean')
system('git status')
system('git checkout master')
system('git merge origin/master')
system('git checkout -b gem')
redmine_init_content = File.read('init.rb')
if redmine_init_content.match(/Redmine::Plugin.register :(.*) do/)
$plugin_name = $1.strip()
else
$plugin_name = @plugin_dir
end
# Rakefile
File.open('Rakefile','a') do |file|
file.puts('require "bundler/gem_tasks"')
end
# Gemspec
File.open("#{$plugin_name}.gemspec",'a') do |file|
file.puts(gemspec_content)
end
system("git add Rakefile #{$plugin_name}.gemspec")
system('git commit -m "Updated rakefile and added gemspec."')
# VERSION
File.open('VERSION.rb','w') do |version_file|
version = "0.0.1"
if redmine_init_content.match(/version (.*$)/)
version = $1.gsub("'",'').gsub('"','')
end
version_file.puts("VERSION = '#{version}'")
end
system('git add VERSION.rb')
system('git commit -am "Added Version file"')
# Rails GemPlugin init.rb
FileUtils.mkdir_p('rails')
system('git mv init.rb rails/init.rb')
File.open('init.rb','w') do |init_file|
init_file.puts('require File.dirname(__FILE__) + "/rails/init"')
end
system('git add init.rb')
system('git commit -am "Added init file for Rails GemPlugin"')
# Install to test
system('rake install')
# Back to master to allow merging
system('git checkout master')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment