middleman-template/Rakefile

275 lines
7 KiB
Ruby
Raw Permalink Normal View History

2022-07-13 18:55:59 +02:00
# encoding: UTF-8
# vim: set noet sw=2 ts=2 sts=2:
require 'pathname'
require 'font-awesome-sass'
require 'set'
require "mini_magick"
MiniMagick.configure do |config|
config.cli = :graphicsmagick
config.timeout = 5
end
class Pathname
def each_parent top_dir = nil, &exe
top_dir =
case top_dir
when nil then Pahname.new '/'
when Pathname then top_dir.expand_path
else Pathname.new( top_dir.to_s).expand_path
end
each_parent_till_dir top_dir, &exe
end
def each_parent_till_dir top_dir, &exe
return to_enum( self, __method__) unless block_given?
d = self
until top_dir == d
yield d
d = d.dirname
end
end
def parents top_dir = nil
each_parent( top_dir).to_a
end
def parents_till_dir top_dir, &exe
each_parent_till_dir( top_dir).to_a
end
end
def bexec *a
sh *%w[bundle exec], *a.map(&:to_s)
end
def middleman *a
bexec :middleman, *a
end
def opal_c *a
bexec %w[opal -c], *a
end
def coffee_c *a
sh *%w[coffee -mc], *a.map(&:to_s)
end
def deploy env
ENV['TARGET'] = env.to_s
middleman :deploy
end
def flatten_args_ *as, &e
as.each do |a|
case a
when Array then flatten_args_ *a, &e
else yield a.to_s
end
end
end
def flatten_args *as, &e
if block_given?
flatten_args_ *as, &e
else
r = []
flatten_args_ *as, &r.method( :push)
r
end
end
def font_sfnt2woff t, cache_dir
STDERR.puts "Build font > #{t.name} < #{t.source}"
f = Pathname.new( t.source).basename
symdest = Pathname.new( t.source).relative_path_from cache_dir
symlink = cache_dir + f
cacdest = cache_dir.join( Pathname.new( t.name).basename)
symlink.unlink if begin symlink.lstat; rescue Errno::ENOENT; false; end
symlink.make_symlink symdest
pid = Process.spawn 'sfnt2woff-zopfli', f.to_s, chdir: cache_dir.to_s
_, status = Process.wait2 pid
# sfnt2woff-zopfli will not return anything else than exit-status 0
raise "sfnt2woff-zopfli failed!" unless 0 == status.exitstatus
raise "sfnt2woff-zopfli failed!" unless cacdest.exist?
cacdest.rename t.name
end
def font_webify t, cache_dir
STDERR.puts "Build font: #{t.name}"
f = Pathname.new( t.source).basename
symdest = Pathname.new( t.source).relative_path_from cache_dir
symlink = cache_dir + f
cacdest = cache_dir.join( Pathname.new( t.name).basename)
symlink.unlink if begin symlink.lstat; rescue Errno::ENOENT; false; end
symlink.make_symlink symdest
pid = Process.spawn 'webify', f.to_s, chdir: cache_dir.to_s
_, status = Process.wait2 pid
# webify will not return anything else than exit-status 0
raise "Webify failed!" unless 0 == status.exitstatus
raise "Webify failed!" unless cacdest.exist?
cacdest.rename t.name
end
def tmp_fork dest, *exec_args, &e
unless exec_args.empty?
args = flatten_args exec_args
return tmp_fork( dest) { Kernel.exec *args }
end
tmp = Pathname.new "#{dest}.tmp"
child = fork do
STDOUT.reopen tmp.open( 'w')
yield
end
Process.waitpid child
if 0 == $?.exitstatus
tmp.rename dest
true
else
false
end
end
asset_source_dir = Pathname.new( 'assets') + 'src'
asset_build_dir = Pathname.new( 'assets') + 'build' + 'assets'
cache_dir = Pathname.new '.cache'
font_build_dir = asset_build_dir + 'fonts'
fa_fonts_dir = Pathname.new FontAwesome::Sass::fonts_path
fa_fonts =
fa_fonts_dir.find.select( &:file?).map do |f|
font_build_dir + f.relative_path_from( fa_fonts_dir)
end
all_js = [] # asset_build_dir.join 'scripts', 'all.js'
#all_css = asset_build_dir.join 'styles', 'baroque.css'
directory asset_build_dir
#directory all_js.dirname
script_srcs = []
font_metadata_srcs = []
asset_source_dir.find do |f|
case f.basename.to_s
when /^\./ then Find.prune
when /\.opal$/ then script_srcs.push f
when /\.coffee$/ then script_srcs.push f
when /\.js$/ then script_srcs.push f
when 'METADATA.json' then font_metadata_srcs.push f
end
end
#file all_js => [asset_source_dir.join( 'all.coffee'),
# all_js.dirname, *script_srcs] do |t|
# STDERR.puts "COFFEE >#{t.name} <#{t.source}"
# coffee_c '-o', asset_build_dir+'scripts', t.source
#end
fa_fonts.each do |font|
directory *fa_fonts.map {|f| f.dirname }.uniq
file font => [fa_fonts_dir + font.relative_path_from( font_build_dir), font.dirname] do |t|
cp t.source, t.name
end
end
#directory all_css.dirname => asset_build_dir
#file all_css => [asset_source_dir + 'styles' + 'baroque.css', asset_source_dir+'styles'] do
# STDERR.puts "SCSS >#{t.name} <#{t.source}"
# tmp_fork t.name, %w[bundle exec scss], all_css.dirname, t.source
#end
fonts_source_dir = asset_source_dir + 'fonts'
fonts_build_dir = asset_build_dir + 'fonts'
fonts_cache_dir = cache_dir + 'fonts'
all_fonts = []
fonts_source_dir.find do |f|
case f.basename.to_s
when /^\./ then Find.prune
when /^(.*)\.(ttf|otf)$/ then
#file fonts_build_dir + f.basename => [f, fonts_build_dir] do |t|
# cp t.source, t.name
#end
#%w[svg woff eot].each do |ext|
%w[woff].each do |ext|
d = fonts_build_dir + "#$1.#{ext}"
all_fonts.push d
file d => [f, fonts_cache_dir, fonts_build_dir] do |t|
font_sfnt2woff t, fonts_cache_dir
end
end
d = fonts_build_dir + f.basename
all_fonts.push d
file d => f do |t|
cp t.source, t.name
end
#when /^(.*)\.otf$/ then from_otf += %w[svg woff eot].map {|ext| fonts_build_dir + "#$1.#{ext}" }
end
end
directory fonts_cache_dir => cache_dir
directory fonts_build_dir => asset_build_dir
directory asset_build_dir+'styles' => asset_build_dir
fonts_css = asset_build_dir+'styles'+'fonts.css'
task fonts_css => font_metadata_srcs+[fonts_css.dirname, cache_dir] do |t|
STDERR.puts "Generate #{t.name}"
require 'json'
require 'active_support/all'
cache_file = cache_dir+File.basename( t.name)
File.open cache_file, 'w' do |d|
font_metadata_srcs.each do |f|
STDERR.puts " <= #{f}"
JSON.parse( File.read( f))['fonts'].each do |font|
font = font.symbolize_keys
font[:ttf] = "/assets/fonts/#{font[:filename]}"
font[:woff] = "/assets/fonts/#{font[:filename].gsub /\.[ot]tf\Z/, '.woff'}"
d.puts sprintf( <<EOF, font).gsub(/^\s*(.*?)\s*$/, '\1').gsub(/\n/m,'')
@font-face {
font-family: "%<name>s";
font-weight: %<weight>i;
font-style: %<style>s;
src:
local("%<fullName>s"),
local("%<postScriptName>s"),
url("%<woff>s") format("woff"),
url("%<ttf>s") format("truetype");
}
EOF
end
end
end
cp cache_file.to_s, t.name.to_s
end
task all_fonts: all_fonts
task fontawesome: fa_fonts
task :all_js #: all_js
task fonts_css: fonts_css
task assets: [:all_js, :fontawesome, :all_fonts, :fonts_css]
task :build do
middleman :build, '--environment', 'production'
end
task default: :build
task :run do
middleman :server, '--port', 9296, '--environment', 'development'
#middleman :server, '--bind-address', '127.0.0.1', '--port', 9290, '--environment', 'development'
#bexec :puma, '--bind', 'tcp://127.0.0.1:9290', '--tag', :denkn
#bexec :rackup, '--port', 9290
end
task :clean do
rm_r 'assets/build' rescue Errno::ENOENT
rm_r 'build' rescue Errno::ENOENT
rm_r '.cache' rescue Errno::ENOENT
rm_r '.sass-cache' rescue Errno::ENOENT
end
namespace :deploy do
task :production do
deploy :production
end
task :test do
deploy :test
end
end