Skip to content

Commit

Permalink
Added retreat gem
Browse files Browse the repository at this point in the history
  • Loading branch information
mowat27 committed Sep 19, 2011
1 parent 315bb23 commit 2f26086
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
54 changes: 54 additions & 0 deletions retreat_gem/bin/retreat
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env ruby

require 'coderetreat/retreat'

include CodeRetreat

def usage
result = <<EOS
usage: #{__FILE__} action [command...]
----
Actions:
info : Prints information message
install : Downloads and installs starting point repo to ~/.retreat/retreat
update : Pulls latest langauge starting points from github source repo
start language [directory] : Starts a new iteration using 'language' in 'directory'
directory defaults to '.' if not specified
EOS

result
end

def fail_with_error(message)
STDERR.puts message
exit 9
end

def fail_with_usage_error(message)
STDERR.puts message
STDERR.puts usage
exit 9
end

fail_with_usage_error "Error: No args passed" if ARGV.empty?
action = ARGV.shift

begin
case
when action == "info"
Actions::Info.run!
when action == "install"
Actions::Install.run!
when action == "update"
Actions::Update.run!
when action == "start"
Actions::Start.run!(ARGV)
else
raise UsageError.new("Error: unknown action '#{action}'")
end
rescue UsageError => ex
fail_with_usage_error "Error: #{ex}"
rescue EnvError => ex
fail_with_error "Error: #{ex}"
end

150 changes: 150 additions & 0 deletions retreat_gem/lib/coderetreat/retreat.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,150 @@
require 'etc'
require 'fileutils'

include FileUtils

module CodeRetreat
class UsageError < Exception; end
class EnvError < Exception; end

class Path
# Defines a delegator method to File
def self.def_file_op(name)
file_op = name.to_sym
define_method(file_op) do |*args|
File.send(file_op, @path, *args)
end
end

def initialize(*elements)
relative_path = File.join(*elements)
@path = File.expand_path(relative_path)
end

def_file_op :exists?
def_file_op :directory?
def_file_op :file?
def_file_op :writable?
def_file_op :basename

def dirname
Path.new(File.dirname(@path))
end

def join(*elements)
Path.new(@path, *elements)
end

def to_s
@path.clone
end

def copy_to(target)
cp_r @path, target.to_s
end

def ls
Dir["#{@path}/*"].map{|listing_item| Path.new(listing_item)}
end
end

module Actions
class Action
def initialize
@home = Path.new(Etc.getpwuid.dir, '.retreat')
@local_repo = Path.new(Etc.getpwuid.dir, '.retreat', 'retreat')
@source_repo = "https://github.com/coreyhaines/coderetreat.git"
@starting_points = @local_repo.join("starting_points")
end
end

class Start < Action
def self.run!(args)
new(args).run!
end

def initialize(args)
super()
@language = args[0]
@source = @starting_points.join(@language)
@target = args[1] ? Path.new(args[1]) : Path.new('.')
end

def run!
validate_environment!
@source.copy_to(@target)
puts "Created #{@language} iteration starting point at #{@target}"
end

def validate_environment!
raise EnvError.new("No sources found. Try running 'retreat install' first") unless @source.directory?
raise EnvError.new("Cannot create #{@target}") unless @target.dirname.writable?
end
end

class Install < Action
def self.run!
new.run!
end

def run!
validate_environment!
puts "Pulling coderetreat repo into #{@local_repo}..."
%x{git clone #{@source_repo} #{@local_repo}}
puts "\nInstallation completed! Here is some information..."
Info.run!
puts "\nRun 'retreat start <language> [location]' to start a new iteration"
end

def validate_environment!
if @local_repo.exists?
raise EnvError.new("retreat has already been installed. Run 'retreat update' to get the latest starting points.")
end
end
end

class Update < Action
def self.run!
new.run!
end

def run!
validate_environment!
puts "Updating sources at #{@local_repo}"
%x{git pull #{@local_repo}}
end

def validate_environment!
raise EnvError.new("retreat is not installed. Run 'retreat install' to get started.") unless @local_repo.exists?
end
end

class Info < Action
def self.run!
new.run!
end

def run!
result =<<EOS
#{message}
EOS
puts result
end

def message
result =<<EOS
Source Repo: #{@source_repo}
Local Repo: #{@local_repo}
Langauges Avilable: #{languages.join(",")}
EOS

result
end

def languages
@starting_points.ls.map{|path| path.basename}
end
end
end
end
18 changes: 18 additions & 0 deletions retreat_gem/retreat.gemspec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rubygems'

SPEC = Gem::Specification.new do |s|
s.name = "retreat"
s.version = '0.1.0'
s.author = "Adrian Mowat"
s.homepage = "https://github.com/coreyhaines/coderetreat"
s.summary = "Command line utilities for coderetreat"
s.description = <<EOS
retreat allows you to easily start new coderetreat iterations in different languges. Starting point "skeletons" are held in a git repo and copied as needed.
EOS
s.has_rdoc = false

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end

0 comments on commit 2f26086

Please sign in to comment.