新手问题 求助重构脚本

Miao · 2013年09月25日 · 最后由 sevk 回复于 2013年09月30日 · 2346 次阅读

这个脚本是用来搭建开发环境的,监测目录下文件变动,然后相应地将".coffee"和".slim"编译成 html 和 js 文件。代码写得比较糟糕,求助坛子里的大虾指导

require 'fssm'

FILE_PATH = '/home/miao/tmp'

def compile_coffee(path)
    puts "coffee -c #{path}"
    `coffee -c #{path}`
end

def compile_slim(path)
    puts "slimrb -p #{path.sub(/\.slim$/,'.html')}"
    `slimrb -p #{path} #{path.sub(/\.slim$/,'.html')}`
end

coffee_monitor = FSSM::Monitor.new
slim_monitor = FSSM::Monitor.new


coffee_monitor.path FILE_PATH,'**/*.coffee' do
    update {|base, relative| compile_coffee "#{base}/#{relative}"}
    delete {|base, relative| system "rm #{base}/#{relative}".sub(/\.coffee$/,'.js')}
    create {|base, relative| compile_coffee "#{base}/#{relative}"}
end

slim_monitor.path FILE_PATH,'**/*.slim' do
    update {|base, relative| compile_slim "#{base}/#{relative}"}
    delete {|base, relative| system "rm #{base}/#{relative}".sub(/\.slim$/,'.html')}
    create {|base, relative| compile_slim "#{base}/#{relative}"}
end

slim_thread = Thread.new {slim_monitor.run}
coffee_thread = Thread.new {coffee_monitor.run}

slim_thread.join
coffee_thread.join 
  1. 推荐 2 格缩进
  2. 组合文件名还有种办法是 File.join base, relative, 替换扩展名可以抽出个小方法
scss --watch input-dir:output-dir
coffee -w -c input-dir/*.coffee -o output-dir

https://github.com/muan/scribble/blob/master/Rakefile

desc "compile and run the site"
task :default do
  pids = [
    spawn("jekyll server -w"),
    spawn("scss --watch _assets:stylesheets"),
    spawn("coffee -b -w -o javascripts -c _assets/*.coffee")
  ]

  trap "INT" do
    Process.kill "INT", *pids
    exit 1
  end

  loop do
    sleep 1
  end
end

刚好用 node 写了一个自用,带了一个 livereload 功能 https://github.com/hhuai/easyweb

学习了 :)

需要 登录 后方可回复, 如果你还没有账号请 注册新账号