Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
218 lines (191 sloc) 5.99 KB
#!/usr/bin/env ruby
ROOT = File.expand_path('.')
TEMP = File.join(ROOT, 'temp')
DEPLOY = File.join(ROOT, 'bakery')
WIDGETS = File.join(ROOT, "widgets")
SKINS = File.join(ROOT, "skins")
ENGINE = File.join(ROOT, "engine")
EDITOR = File.join(ROOT, "editor")
SYSTEM = File.join(ROOT, "system")
REDBUG = File.join(ROOT, "redbug")
FRONT = File.join(ROOT, "front")
OSX = PLATFORM =~ /darwin/
WIN = PLATFORM =~ /win32/
NIX = !(OSX || WIN)
require "rubygems"
begin
require 'hpdev'
rescue LoadError
begin
$: << "/Users/darwin/code/hpdev/lib"
require 'hpdev'
rescue LoadError
raise 'Please install hpdev: sudo gem install hashpage-hpdev --source http://gems.github.com'
end
end
include HPDev
begin
require 'term/ansicolor'
include Term::ANSIColor
rescue LoadError
raise 'Run "gem install term-ansicolor"'
end
# http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
if WIN then
begin
require 'win32console'
include Win32::Console::ANSI
rescue LoadError
raise 'Run "gem install win32console" to use terminal colors on Windows'
end
end
#############################################################################
def sanitize_path(path)
path = path.gsub('/', '\\') if WIN
path = path.gsub('sudo ', '') if WIN
path
end
def system(path, *params)
path = sanitize_path(path)
puts yellow(">> #{path} #{params.join(' ')}")
Kernel.system(path, *params)
end
def sys(cmd, verbose = true)
puts yellow("> #{cmd}") if verbose
`#{sanitize_path(cmd)}`
end
def die(s)
puts red(s)
exit(1)
end
def relativize_path(path)
return path unless path[0..ROOT.size-1] == ROOT
path[ROOT.size+1..-1]
end
#############################################################################
def reset_bake_counters()
$bake_counters = {
:widget => {
:authors => 0,
:total => 0
},
:skin => {
:authors => 0,
:total => 0
},
:engine => {
:total => 0
},
:editor => {
:total => 0
},
:system => {
:total => 0
},
:redbug => {
:total => 0
},
:front => {
:total => 0
}
}
end
def print_bake_counters()
puts "Processed #{blue("#{$bake_counters[:skin][:total]} skins")} / #{$bake_counters[:skin][:authors]} authors"
puts " #{blue("#{$bake_counters[:widget][:total]} widgets")} / #{$bake_counters[:widget][:authors]} authors"
end
def bake(type, path, dest, url, mode)
die "DEPLOY dir is too short (paranoia)" if dest.size<10
sys("rm -rf \"#{dest}\"")
return unless File.exists? path
sys("hpdev bake #{type.to_s} --mode=\"#{mode}\" --url=\"#{url}\" \"#{relativize_path(path)}\" \"#{relativize_path(dest)}\" 1>&2")
die("Baking error ...") if $?.exitstatus!=0
$bake_counters[type][:total] += 1
end
def bake_author(type, path, dest, url, mode)
$bake_counters[type][:authors] += 1
Dir.glob(File.join(path, "*")) do |path|
next unless File.directory? path
name = File.basename(path)
bake(type, path, File.join(dest, name), url+"/"+name, mode)
end
end
def bake_all(type, path, dest, url, mode)
die "DEPLOY dir is too short (paranoia)" if dest.size<10
sys("rm -rf \"#{dest}\"")
return unless File.exists? path
Dir.glob(File.join(path, "*")) do |author_path|
next unless File.directory? author_path
name = File.basename(author_path)
bake_author(type, author_path, File.join(dest, name), url+name, mode)
end
end
def pull(dir)
puts "cd " + blue(dir)
Dir.chdir(dir) do
sys("git pull 1>&2")
end
end
def pull_all(root)
Dir.glob(File.join(root, "**/.git")) do |dir|
next if dir == File.join(root, ".git")
pull(dir[0..-6])
end
end
def url(mode, server, path = nil)
path = "" unless path
case mode
when :production
return "http://#{server}.hashpage.com/#{path}"
when :simulation
return "http://#{server}.hashpage.local/#{path}"
when :development
return "http://localhost:9876/#{server}/#{path}"
end
"unknown url mode"
end
def is_clean?
!!sys("git status", false).match(/working directory clean/)
end
def print_dirty(root)
Dir.glob(File.join(root, "**/.git")) do |dir|
next if dir == File.join(root, ".git")
dir_path = dir[0..-6]
Dir.chdir(dir_path) do
puts blue(dir_path) unless is_clean?
end
end
end
#############################################################################
#----------------------------------------------------------------------------
desc "start dev server"
task :start do
sys("hpdev 1>&2")
end
#----------------------------------------------------------------------------
desc "bake production files (local)"
task :bake do
mode = ENV["mode"] || "production"
what = (ENV["only"] || "widgets,skins,engine,editor,system,redbug,front").split(",")
reset_bake_counters()
bake_all(:widget, WIDGETS, File.join(DEPLOY, "widgets"), url(mode.to_sym, "widgets"), mode) if what.include? "widgets"
bake_all(:skin, SKINS, File.join(DEPLOY, "skins"), url(mode.to_sym, "skins"), mode) if what.include? "skins"
bake(:engine, ENGINE, File.join(DEPLOY, "code", "hashpage", "engine"), url(mode.to_sym, "code", "engine"), mode) if what.include? "engine"
bake(:editor, EDITOR, File.join(DEPLOY, "code", "hashpage", "editor"), url(mode.to_sym, "code", "editor"), mode) if what.include? "editor"
bake(:system, SYSTEM, File.join(DEPLOY, "code", "hashpage", "system"), url(mode.to_sym, "code", "system"), mode) if what.include? "system"
bake(:redbug, REDBUG, File.join(DEPLOY, "code", "hashpage", "redbug"), url(mode.to_sym, "code", "redbug"), mode) if what.include? "redbug"
bake(:front, FRONT, File.join(DEPLOY, "code", "hashpage", "front"), url(mode.to_sym, "code", "front"), mode) if what.include? "front"
print_bake_counters()
end
#----------------------------------------------------------------------------
desc "pull all submodules"
task :pull do
pull_all(ROOT)
end
#----------------------------------------------------------------------------
desc "print dirty submodules"
task :dirty do
print_dirty(ROOT)
end
#----------------------------------------------------------------------------
task :default => :start