attentive/bin/attentive
2012-03-13 13:43:23 -04:00

124 lines
2.7 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default)
rescue Bundler::GemfileNotFound
$: << File.expand_path('../../lib', __FILE__)
end
require 'thor'
require 'attentive'
begin
load 'presentation.rb'
rescue LoadError => e
end
class Attentive::CLI < Thor
include Thor::Actions
default_task :start
desc "start", "Start a Rack server for previewing the presentation"
method_options [ :port, '-p' ] => 9393
def start
if Attentive.has_presentation?
Attentive::Server.start(options)
else
raise Attentive::NoPresentationError
end
end
def self.source_root
File.expand_path('../../skel', __FILE__)
end
desc "create", "Create a new skeleton presentation"
def create(name)
self.destination_root = File.join(Dir.pwd, name)
Dir[File.join(self.class.source_root, '**/*')].each do |file|
if File.file?(file)
filename = file.gsub(self.class.source_root + '/', '')
template filename, filename
end
end
end
desc "static", "Create a static copy of the site in _site"
def static
urls = [ '/' ]
Dir['assets/**/*'].each do |file|
if File.file?(file)
parts = file.split('/')[2..-1]
if !parts.empty?
file = parts.join('/')
%w{js css}.each do |root|
file.gsub!(%r{([^/]+\.#{root}).*$}, '\1')
end
urls << File.join('', 'assets', file)
end
end
end
target_dir = "_site"
FileUtils.rm_rf target_dir
FileUtils.mkdir_p target_dir
Attentive.middleware.replace([])
urls.each do |url|
response = Attentive::Server.call(Rack::MockRequest.env_for(url))
target = "#{target_dir}#{url}"
target += "index.html" if target[-1..-1] == '/'
puts "Writing #{target}..."
FileUtils.mkdir_p(File.dirname(target))
File.open(target, 'wb') { |fh| response.last.each { |part| fh.print part } }
end
FileUtils.rm_rf '.sass-cache' if File.directory?('.sass-cache')
end
desc "gh-pages", "Commit the static site to the associated GitHub pages account"
def gh_pages
static
target = "/tmp/attentive-#{Time.now.to_i}"
system %{cp -Rpv _site #{target}}
system %{git checkout gh-pages}
if $?.exitstatus == 1
puts "Creating gh-pages branch..."
system %{git add . && git commit -a -m "pre-gh pages creation"}
system %{git symbolic-ref HEAD refs/heads/gh-pages}
system %{rm .git/index}
system %{git clean -fdx}
system %{git checkout gh-pages}
end
system %{rm -Rf *}
system %{cp -Rpv #{target}/* .}
system %{git add .}
system %{git add *}
system %{git commit -a -m "Update published site"}
system %{git checkout master}
end
end
Attentive::CLI.start