|
1 | 1 | require "rubygems" |
2 | 2 | require 'rake' |
| 3 | +require 'yaml' |
3 | 4 |
|
4 | 5 | SOURCE = "." |
5 | 6 | CONFIG = { |
6 | 7 | 'themes' => File.join(SOURCE, "_includes", "themes"), |
7 | 8 | 'layouts' => File.join(SOURCE, "_layouts"), |
8 | 9 | 'posts' => File.join(SOURCE, "_posts"), |
9 | | - 'post_ext' => "md" |
| 10 | + 'post_ext' => "md", |
| 11 | + 'theme_package_version' => "0.1.0" |
10 | 12 | } |
11 | 13 |
|
| 14 | +# Path configuration helper |
| 15 | +module JB |
| 16 | + class Path |
| 17 | + SOURCE = "." |
| 18 | + Paths = { |
| 19 | + :layouts => "_layouts", |
| 20 | + :themes => "_includes/themes", |
| 21 | + :theme_assets => "assets/themes", |
| 22 | + :theme_packages => "_theme_packages", |
| 23 | + :posts => "_posts" |
| 24 | + } |
| 25 | + |
| 26 | + def self.base |
| 27 | + SOURCE |
| 28 | + end |
| 29 | + |
| 30 | + # build a path relative to configured path settings. |
| 31 | + def self.build(path, opts = {}) |
| 32 | + opts[:root] ||= SOURCE |
| 33 | + path = "#{opts[:root]}/#{Paths[path.to_sym]}/#{opts[:node]}".split("/") |
| 34 | + path.compact! |
| 35 | + File.__send__ :join, path |
| 36 | + end |
| 37 | + |
| 38 | + end #Path |
| 39 | +end #JB |
| 40 | + |
12 | 41 | # Usage: rake post title="A Title" |
13 | 42 | desc "Begin a new post in #{CONFIG['posts']}" |
14 | 43 | task :post do |
@@ -92,6 +121,76 @@ task :preview do |
92 | 121 | system "jekyll --auto --server" |
93 | 122 | end # task :preview |
94 | 123 |
|
| 124 | +namespace :theme do |
| 125 | + |
| 126 | + # 0.1.0 version of theme install will be simple 1:1 file matching. |
| 127 | + desc "Install theme" |
| 128 | + task :install do |
| 129 | + name = ENV["name"].to_s.downcase |
| 130 | + packaged_theme_path = JB::Path.build(:theme_packages, :node => name) |
| 131 | + |
| 132 | + abort("rake aborted: name cannot be blank") if name.empty? |
| 133 | + abort("rake aborted: '#{packaged_theme_path}' directory not found.") unless FileTest.directory?(packaged_theme_path) |
| 134 | + |
| 135 | + # Get relative paths to packaged theme files |
| 136 | + packaged_theme_files = [] |
| 137 | + FileUtils.cd(packaged_theme_path) { packaged_theme_files += Dir["**/*.*"] } |
| 138 | + # Don't install metadata files. |
| 139 | + packaged_theme_files.delete_if { |f| f =~ /^(manifest|readme|packager)/i } |
| 140 | + |
| 141 | + # Mirror each file into the framework making sure to prompt if already exists. |
| 142 | + packaged_theme_files.each do |filename| |
| 143 | + file_install_path = File.join(JB::Path.base, filename) |
| 144 | + if File.exist? file_install_path |
| 145 | + next if ask("#{file_install_path} already exists. Do you want to overwrite?", ['y', 'n']) == 'n' |
| 146 | + else |
| 147 | + mkdir_p File.dirname(file_install_path) |
| 148 | + cp_r File.join(packaged_theme_path, filename), file_install_path |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + puts "=> #{name} theme has been installed!" |
| 153 | + puts "=> ---" |
| 154 | + if ask("=> Want to switch themes now?", ['y', 'n']) == 'y' |
| 155 | + system("rake switch_theme name='#{name}'") |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + # The 0.1.0 version of theme packaging will be simple 1:1 file matching. |
| 160 | + # The theme repo will outline it's files in the same structure |
| 161 | + # it expects to be included into the Jekyll structure. |
| 162 | + desc "Package theme" |
| 163 | + task :package do |
| 164 | + name = ENV["name"].to_s.downcase |
| 165 | + theme_path = JB::Path.build(:themes, :node => name) |
| 166 | + asset_path = JB::Path.build(:theme_assets, :node => name) |
| 167 | + |
| 168 | + abort("rake aborted: name cannot be blank") if name.empty? |
| 169 | + abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path) |
| 170 | + abort("rake aborted: '#{asset_path}' directory not found.") unless FileTest.directory?(asset_path) |
| 171 | + |
| 172 | + ## Mirror theme's template directory (_includes) |
| 173 | + packaged_theme_path = JB::Path.build(:themes, :root => JB::Path.build(:theme_packages, :node => name)) |
| 174 | + mkdir_p packaged_theme_path |
| 175 | + cp_r theme_path, packaged_theme_path |
| 176 | + |
| 177 | + ## Mirror theme's asset directory |
| 178 | + packaged_theme_assets_path = JB::Path.build(:theme_assets, :root => JB::Path.build(:theme_packages, :node => name)) |
| 179 | + mkdir_p packaged_theme_assets_path |
| 180 | + cp_r asset_path, packaged_theme_assets_path |
| 181 | + |
| 182 | + ## Log packager version |
| 183 | + packager = {"packager" => {"version" => CONFIG["theme_package_version"].to_s } } |
| 184 | + open(JB::Path.build(:theme_packages, :node => "#{name}/packager.yml"), "w") do |page| |
| 185 | + page.puts packager.to_yaml |
| 186 | + end |
| 187 | + |
| 188 | + puts "=> '#{name}' theme is packaged and available at: #{JB::Path.build(:theme_packages, :node => name)}" |
| 189 | + end |
| 190 | + |
| 191 | +end # end namespace :theme |
| 192 | + |
| 193 | + |
95 | 194 | def ask(message, valid_options) |
96 | 195 | if valid_options |
97 | 196 | answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer) |
|
0 commit comments