penchant/bin/penchant

148 lines
3.7 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__)
SCRIPT_DIR = 'script'
CLONE_DIR = '..'
desc "install", "Copy the common scripts to the project"
method_options :dir => SCRIPT_DIR
def install
directory 'template/script', options[:dir]
Dir[File.join(options[:dir], '**/*')].each { |file| File.chmod(0755, file) }
if File.directory?('.git')
Penchant::Hooks.install!
else
puts "No git repository detected here. Skipping git hook installation..."
end
if !File.file?('Gemfile') && !File.file?('Gemfile.penchant')
FileUtils.touch('Gemfile.penchant')
prepend_to_file 'Gemfile.penchant', <<-RB
source :rubygems
RB
install_gemfile_penchant
end
end
desc "update", "Update the installed scripts"
method_options :dir => SCRIPT_DIR
def update
install
end
desc "convert", "Make an existing project Penchant-isized"
method_options :dir => SCRIPT_DIR
def convert
install
FileUtils.mv 'Gemfile', 'Gemfile.penchant'
install_gemfile_penchant
end
method_options :deployment => false
method_options :switch_back => false
method_options :no_auto_update => false
method_options :local => false
desc "gemfile ENV", "Switch the gemfile environment, or rebuild the current environment if not given"
def gemfile(env = get_current_env)
check_git_hooks!
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
command = %{bundle}
command << " --local" if options[:local]
system command
# it's asking for bundle update, we know what we're doing
if $?.exitstatus == 6 and !options[:no_auto_update]
command = %{bundle update}
command << " --local" if options[:local]
system command
end
end
desc "gemfile-env", "Get the gemfile environment"
def gemfile_env
puts get_current_env
end
desc "bootstrap [DIR = #{CLONE_DIR}]", "Download all referred-to git repos to the specified directory"
def bootstrap(dir = CLONE_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
def check_git_hooks!
if !Penchant::Hooks.installed?
puts "[penchant] git hooks not installed. Run script/install-git-hooks."
puts
end
end
def install_gemfile_penchant
prepend_to_file 'Gemfile.penchant', <<-RB
# ensure git hooks are always installed
ensure_git_hooks!
# everything in the :local env is assumed to be a sibling directory of this one
defaults_for env(:local), :path => '../%s'
# reference a github repository with gem 'my-gem', :github => 'username'
property :github, :git => 'git://github.com/$1/%s.git'
RB
gemfile(:remote)
end
end
default_task :gemfile
end
PenchantCLI.start