penchant/bin/penchant

95 lines
2.4 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'rubygems'
require 'thor'
require 'penchant'
require 'fileutils'
class PenchantCLI < Thor
include Thor::Actions
source_root File.expand_path('../..', __FILE__)
desc "install", "Copy the common scripts to the project"
method_options :dir => 'script'
def install
directory 'template/script', options[:dir]
Dir[File.join(options[:dir], '**/*')].each { |file| File.chmod(0755, file) }
Dir['script/hooks/*'].each do |hook|
FileUtils.ln_sf File.join(Dir.pwd, hook), ".git/hooks/#{File.split(hook).last}"
end
end
desc "update", "Update the installed scripts"
method_options :dir => 'script'
def update
install
end
desc "convert", "Make an existing project Penchant-isized"
method_options :dir => 'script'
def convert
install
FileUtils.mv 'Gemfile', 'Gemfile.penchant'
gemfile(:remote)
end
method_options :deployment => false
method_options :switch_back => false
desc "gemfile ENV", "Switch the gemfile environment, or rebuild the current environment if not given"
def gemfile(env = get_current_env)
if env
if options[:switch_back]
puts "[penchant] Switching back, fallback: #{env}..."
Penchant::Gemfile.switch_back!(env)
else
puts "[penchant] Rebunding for #{env} environment#{options[:deployment] ? ", deployment mode" : ''}..."
Penchant::Gemfile.do_full_env_switch!(env, options[:deployment])
end
end
gemfile = Penchant::Gemfile.new
if !gemfile.has_gemfile?
puts "No Gemfile or Gemfile.penchant, exiting."
exit 1
end
system %{bundle}
end
desc "gemfile-env", "Get the gemfile environment"
def gemfile_env
puts get_current_env
end
desc "bootstrap [DIR = ..]", "Download all referred-to git repos to the specified directory"
def bootstrap(dir = '..')
Penchant::Gemfile.defined_git_repos.each do |repo|
puts "Cloning #{repo} to #{dir}."
repo.clone_to(dir)
end
end
def method_missing(method, *args)
if Penchant::Gemfile.available_environments.include?(method)
gemfile(method, *args)
else
super(method, *args)
end
end
no_tasks do
def get_current_env
gemfile = Penchant::Gemfile.new
out = [ gemfile.environment ]
out << "deployment" if gemfile.deployment?
out.join(' ')
end
end
default_task :gemfile
end
PenchantCLI.start