如何在ruby代码中使用指南针来编译项目? (不使用系统调用)

时间:2012-12-30 06:33:56

标签: ruby compass-sass

如何在我的ruby代码中调用指南针来编译项目而不调用shell命令?

我尝试从Using Compass from Ruby (not shell)调整解决方案,但没有成功。我的项目结构看起来像

assets/scss               (location of uncompiled project files)
assets/css                (location for compiled css)
assets/compass/config.cfg (the compass config file)

我尝试过这样的事情

fixed_options = {
  :project_path => '/path/to/assets,
  :sass_path => 'scss',
  :css_path => 'css'
}
Compass.add_project_configuration '/path/to/assets/compass/config.rb'
Compass.add_configuration fixed_options, 'custom'
Compass.compiler.run

这样可行,但只有在项目根目录irb内运行/path/to/assets时才会这样做。

似乎fixed_options中设置的任何内容都会根据需要覆盖config.rb中的选项(或者它们被合并,或者有两组选项:我有点难以辨别),但是:project_path似乎没有做任何事情,因为罗盘似乎只关心我从irb运行的目录。

注意:我一直在尝试使用Compass.compilerirb的输出来尝试了解正在发生的事情。

1 个答案:

答案 0 :(得分:3)

注意:这可能不是您想要的答案,因为它涉及shell,但它可能是您想要的答案,因为它涉及从 一次 运行命令shell和从那时起对资产进行更改时会重新编译。也许这对你来说已经足够了。

我运行指南针的方法是使用Guard文件。

这是我的Gemfile的相关部分(我正在使用OSX):

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem "sass"  # sassy CSS
  gem "coffee-script" # destroy javascript!
  gem "guard" # file watcher with rules
  gem "guard-coffeescript" # regen coffee
  gem "guard-sass", :require => false # auto generate sass
  gem "rb-fsevent"
  gem "growl" # notifications for OSX
end

Compass配置文件:

# Set this to the root of your project when deployed:
http_path = "/"
css_dir = "public/css"
sass_dir = "views/stylesheets"
images_dir = "public/images"
javascripts_dir = "public/js"
project_type = :stand_alone
output_style = :compact
line_comments = false
preferred_syntax = "scss"

在./Guardfile

# This will generate stuff:

guard 'coffeescript', :input => "coffee", :output => 'app/public/js'

guard 'sass', :input => 'app/views/stylesheets', :output => 'app/public/css', :compass => true, :style => "compressed", :shallow => true

然后我在终端中运行guard start,它将查看文件以进行更改并重新编译更改。我让终端窗口在后台打开,所以我也可以强制重新编译。 YMMV。