Skip to content

Instantly share code, notes, and snippets.

@roshanca
Last active January 1, 2016 08:59
Show Gist options
  • Save roshanca/8121898 to your computer and use it in GitHub Desktop.
Save roshanca/8121898 to your computer and use it in GitHub Desktop.
My workflow Rakefile
require 'rubygems'
require 'rake/minify'
## -- Misc Configs -- ##
public_dir = "public" # 生成的预览目录
source_dir = "source" # 源码目录
export_dir = "export" # 输出文件目录
themes_dir = "themes"
current_theme = "swsc"
svn_path = "/Users/wuwenjun/Workspace/OpenAccount"
static_folder = "frontend-static"
## -- Rsync Deploy config -- ##
ssh_user = "root"
ssh_port = "22"
ssh_server = "192.168.1.215"
doc_root = "/usr/local/app/apache/htdocs"
rsync_args = ""
rsync_delete = false
## -- Tasks Define -- ##
desc "生成网站"
task :generate do
raise "出错了,请检查当前目录下是否存在 #{source_dir} 文件夹" unless File.directory?(source_dir)
if ENV['production']
puts "### 压缩 JS"
Rake::Task['minify_js'].execute
system "compass compile --css-dir #{export_dir}/assets/stylesheets -e production --force"
else
puts "## build 网站"
system "compass compile --css-dir #{source_dir}/stylesheets"
system "jekyll build"
end
end
desc "在浏览器中预览"
task :preview do
raise "出错了,请检查当前目录下是否存在 #{source_dir} 文件夹" unless File.directory?(source_dir)
puts "## 启动 web 服务,通过浏览器预览"
jekyllPid = Process.spawn("jekyll serve --watch")
compassPid = Process.spawn("compass watch")
trap("INT") {
[jekyllPid, compassPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
}
[jekyllPid, compassPid].each { |pid| Process.wait(pid) }
end
desc "安装主题"
task :install_theme, :theme do |t, args|
# if File.directory?(source_dir)
# abort("rake aborted!") if ask("A theme is already installed, proceeding will overwrite existing files. Are you sure?", ['y', 'n']) == 'n'
# end
if File.directory?("#{source_dir}")
rm_r "#{source_dir}"
end
if File.directory?("sass")
rm_r "sass"
end
theme = args.theme || current_theme
mkdir_p source_dir
cp_r "#{themes_dir}/" + theme + "/source/.", source_dir
cp_r "#{themes_dir}/" + theme + "/sass/", "sass"
puts "已安装 #{theme}"
Rake::Task['mark_theme'].invoke(theme)
end
desc "更新主题"
task :update_theme do
if !File.directory?(source_dir) || !File.directory?("sass")
raise "还未安装任何主题,请先执行 `rake install_theme`"
end
current_theme_dir = "#{themes_dir}/#{current_theme}"
if File.directory?(current_theme_dir)
rm_r "#{current_theme_dir}"
end
mkdir_p "#{current_theme_dir}"
puts "更新 #{current_theme}"
cp_r "sass", "#{current_theme_dir}/sass/"
cp_r "#{source_dir}", "#{current_theme_dir}/source/"
puts "已更新 #{current_theme}"
end
desc "标注当前主题"
task :mark_theme, :theme do |t, args|
rakefile = IO.read(__FILE__)
rakefile.sub!(/current_theme(\s*)=(\s*)(["'])[\w\-\/]*["']/, "current_theme\\1=\\2\\3#{args.theme}\\3")
File.open(__FILE__, 'w') do |f|
f.write rakefile
end
end
# desc "压缩合并 CSS 和 JS"
# task :compress do
# puts "### 压缩 CSS 与图片"
# system "compass compile --css-dir #{export_dir}/assets/stylesheets -e production --force"
# puts "### 压缩 JS"
# Rake::Task['minify_js'].execute
# end
desc "生成输出结构"
task :organize do
puts "### 生成 #{export_dir} 结构"
mkdir_p "#{export_dir}/assets/images"
mkdir_p "#{export_dir}/assets/javascripts"
mkdir_p "#{export_dir}/assets/stylesheets"
mkdir_p "#{export_dir}/views"
puts "### 将资源打包到 #{export_dir}"
cp_r Dir.glob("#{public_dir}/images/*.{png,jpg,gif}"), "#{export_dir}/assets/images"
cp_r Dir.glob("#{public_dir}/images/dp"), "#{export_dir}/assets/images"
cp_r Dir.glob("#{public_dir}/images/help"), "#{export_dir}/assets/images"
cp_r Dir.glob("#{public_dir}/javascripts/*.js"), "#{export_dir}/assets/javascripts"
cp_r Dir.glob("#{public_dir}/stylesheets/*.css"), "#{export_dir}/assets/stylesheets"
cp_r Dir.glob("#{public_dir}/**/*.html"), "#{export_dir}/views"
end
desc "交付物输出"
task :export do
package = "#{export_dir}/package.json"
if File.exist?(package)
new_version = IO.binread(package).scan(/\w+/)[3].to_i + 1
rm_rf "#{export_dir}"
else
mkdir_p "#{export_dir}"
system "touch #{export_dir}/package.json"
new_version = 0
end
Rake::Task['organize'].execute
# Rake::Task['compress'].execute
ENV['production'] = 'true'
Rake::Task['generate'].execute
puts "## 将打包信息写入 package.json"
open(package, 'w') do |json|
json.puts "{"
json.puts " \"theme\": \"#{current_theme}\","
json.puts " \"version\": \"#{new_version}\","
json.puts " \"build\": \"#{Time.now.strftime('%Y-%m-%d %H:%M')}\""
json.puts "}"
end
puts "打包完成!"
end
desc "上传 SVN 目录"
task :upload do
# puts "## 将打包文件上传至本地 SVN"
svn_dir = svn_path + '/' + current_theme + '/' + static_folder
# rm_rf svn_dir unless File.directory? svn_dir
# mkdir_p svn_dir
# puts "\n### Copying #{export_dir} to #{svn_dir}"
# cp_r "#{export_dir}/.", svn_dir
# puts "上传完毕"
system "rsync -avzl --delete #{export_dir} #{svn_dir}"
end
desc "扫描并复制文档到指定目录"
task :copydot, :source, :dest do |t, args|
# puts file.gsub(/#{args.source}/, "#{args.dest}")
FileList["#{args.source}/**/*"].exclude("**/.", "**/..", "**/.DS_Store", "**/._*").each do |file|
cp_r file, file.gsub(/#{args.source}/, "#{args.dest}") unless File.directory?(file)
end
end
desc "清除样式缓存"
task :clean do
rm_rf [".sass-cache/**", "source/stylesheets/screen.css"]
end
desc "部署到开发服务器"
task :deploy do
Rake::Task['generate'].execute unless File.directory? public_dir
realpath = Dir.pwd + "/#{public_dir}/."
server_dir = "#{doc_root}/#{current_theme}"
# system "scp -r #{realpath} #{ssh_user}@#{ssh_server}:#{doc_root}/#{current_theme}"
ok_failed system "rsync -avze 'ssh -p #{ssh_port}' #{rsync_args} #{"--delete" unless rsync_delete == false} #{public_dir}/ #{ssh_user}@#{ssh_server}:#{server_dir}"
end
Rake::Minify.new(:combine_js) do
files = FileList.new("#{source_dir}/javascripts/src/*.*")
output_file = "#{source_dir}/javascripts/ui.js"
puts "开始拼装 #{output_file}"
group(output_file) do
Dir["#{source_dir}/javascripts/lib/*.*"].each do |file|
puts "Combining- #{file}"
add(file, :minify => false)
end
files.each do |filename|
puts "Combining- #{filename}"
add(filename, :minify => false)
end
end
puts "拼装完成"
end
Rake::Minify.new(:minify_js) do
js_dir = "#{export_dir}/assets/javascripts"
dir(js_dir) do
puts "Minify- #{js_dir}/ui.js into #{js_dir}/ui.min.js"
add("#{js_dir}/ui.min.js", "ui.js")
end
end
def ok_failed(condition)
if (condition)
puts "OK"
else
puts "FAILED"
end
end
desc "任务列表"
task :list do
puts "Tasks:\n- #{(Rake::Task.tasks - [Rake::Task[:list]]).join("\n- ")}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment