Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created May 19, 2010 03:01
Show Gist options
  • Save snusnu/405899 to your computer and use it in GitHub Desktop.
Save snusnu/405899 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -Ku
# encoding: utf-8
require 'fileutils'
require 'pathname'
require 'addressable/uri'
require 'ruby-github' # TODO: replace ruby-github with something better maintained
RUBY_VERSIONS = %w[ 1.8.6 1.8.7 1.9.1 ]
ADAPTERS = %w[ in_memory yaml sqlite mysql postgres ]
class Dm < Thor
desc 'sync', 'Sync with the DM repositories'
method_options :scheme => 'git'
def sync
create_path
sync_repositories
end
desc 'spec', 'Run specs for DM gems'
def spec
total = installable.size
padding = total.to_s.length
installable.each_with_index do |repository, index|
repository_name = repository.name
puts "h2. %s\n\n" % repository_name
puts '|| %s |' % ADAPTERS.join(' | ')
FileUtils.cd(project_directory(repository_name)) do
RUBY_VERSIONS.each do |ruby_version|
print '| %s ' % ruby_version
# cleanup compiled DO gems
clean_repository('do')
system <<-CMD
rake local_gemfile >& /dev/null
ADAPTER="#{ADAPTERS.join(',')}" BUNDLE_GEMFILE="Gemfile.local" rvm #{ruby_version} -S bundle install --without=quality --relock >& /dev/null
CMD
ADAPTERS.each do |adapter|
system <<-CMD
ADAPTER="#{adapter}" BUNDLE_GEMFILE="Gemfile.local" rvm #{ruby_version} -S bundle exec rake spec >& /dev/null
CMD
print '| %s ' % [ $?.success? ? 'pass' : 'fail' ]
end
print "|\n"
end
end
end
end
private
def path
@path ||= Pathname('dm').expand_path
end
def repositories
@repositories ||= GitHub::API.user('datamapper').repositories.reject do |repository|
%w[ dm-more ].include?(repository.name)
end
end
def installable
@installable ||= repositories.select do |repository|
project_directory(repository.name).join('Gemfile').file?
end
end
def scheme
@scheme ||= options[:scheme]
end
def create_path
path.mkpath
end
def sync_repositories
total = repositories.size
padding = total.to_s.length
repositories.each_with_index do |repository, index|
puts '[%0*d/%d] Syncing %s' % [ padding, index + 1, total, repository.name ]
uri = repository_uri(repository)
command, path = git_command(uri)
FileUtils.cd(path) { system(command) }
end
end
def repository_uri(repository)
uri = Addressable::URI.parse(repository.url)
uri.scheme = scheme
uri
end
def git_command(uri)
project_directory = project_directory(uri.basename)
if project_directory.directory?
[ 'git checkout master >& /dev/null; git pull --rebase >& /dev/null', project_directory ]
else
[ "git clone #{uri}.git >& /dev/null;", path ]
end
end
def project_directory(name)
path.join(name)
end
def clean_repository(project_name)
FileUtils.cd(project_directory(project_name)) { system 'git clean -dfX --quiet' }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment