Skip to content

Commit

Permalink
Implement theme installer v 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
plusjade committed Feb 1, 2012
1 parent 683d1be commit 53a801b
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion Rakefile
@@ -1,14 +1,43 @@
require "rubygems"
require 'rake'
require 'yaml'

SOURCE = "."
CONFIG = {
'themes' => File.join(SOURCE, "_includes", "themes"),
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md"
'post_ext' => "md",
'theme_package_version' => "0.1.0"
}

# Path configuration helper
module JB
class Path
SOURCE = "."
Paths = {
:layouts => "_layouts",
:themes => "_includes/themes",
:theme_assets => "assets/themes",
:theme_packages => "_theme_packages",
:posts => "_posts"
}

def self.base
SOURCE
end

# build a path relative to configured path settings.
def self.build(path, opts = {})
opts[:root] ||= SOURCE
path = "#{opts[:root]}/#{Paths[path.to_sym]}/#{opts[:node]}".split("/")
path.compact!
File.__send__ :join, path
end

end #Path
end #JB

# Usage: rake post title="A Title"
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
Expand Down Expand Up @@ -92,6 +121,76 @@ task :preview do
system "jekyll --auto --server"
end # task :preview

namespace :theme do

# 0.1.0 version of theme install will be simple 1:1 file matching.
desc "Install theme"
task :install do
name = ENV["name"].to_s.downcase
packaged_theme_path = JB::Path.build(:theme_packages, :node => name)

abort("rake aborted: name cannot be blank") if name.empty?
abort("rake aborted: '#{packaged_theme_path}' directory not found.") unless FileTest.directory?(packaged_theme_path)

# Get relative paths to packaged theme files
packaged_theme_files = []
FileUtils.cd(packaged_theme_path) { packaged_theme_files += Dir["**/*.*"] }
# Don't install metadata files.
packaged_theme_files.delete_if { |f| f =~ /^(manifest|readme|packager)/i }

# Mirror each file into the framework making sure to prompt if already exists.
packaged_theme_files.each do |filename|
file_install_path = File.join(JB::Path.base, filename)
if File.exist? file_install_path
next if ask("#{file_install_path} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
else
mkdir_p File.dirname(file_install_path)
cp_r File.join(packaged_theme_path, filename), file_install_path
end
end

puts "=> #{name} theme has been installed!"
puts "=> ---"
if ask("=> Want to switch themes now?", ['y', 'n']) == 'y'
system("rake switch_theme name='#{name}'")
end
end

# The 0.1.0 version of theme packaging will be simple 1:1 file matching.
# The theme repo will outline it's files in the same structure
# it expects to be included into the Jekyll structure.
desc "Package theme"
task :package do
name = ENV["name"].to_s.downcase
theme_path = JB::Path.build(:themes, :node => name)
asset_path = JB::Path.build(:theme_assets, :node => name)

abort("rake aborted: name cannot be blank") if name.empty?
abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path)
abort("rake aborted: '#{asset_path}' directory not found.") unless FileTest.directory?(asset_path)

## Mirror theme's template directory (_includes)
packaged_theme_path = JB::Path.build(:themes, :root => JB::Path.build(:theme_packages, :node => name))
mkdir_p packaged_theme_path
cp_r theme_path, packaged_theme_path

## Mirror theme's asset directory
packaged_theme_assets_path = JB::Path.build(:theme_assets, :root => JB::Path.build(:theme_packages, :node => name))
mkdir_p packaged_theme_assets_path
cp_r asset_path, packaged_theme_assets_path

## Log packager version
packager = {"packager" => {"version" => CONFIG["theme_package_version"].to_s } }
open(JB::Path.build(:theme_packages, :node => "#{name}/packager.yml"), "w") do |page|
page.puts packager.to_yaml
end

puts "=> '#{name}' theme is packaged and available at: #{JB::Path.build(:theme_packages, :node => name)}"
end

end # end namespace :theme


def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
Expand Down

0 comments on commit 53a801b

Please sign in to comment.