Ruby指南针配置文件覆盖

时间:2013-09-26 17:11:33

标签: ruby compass-sass

我正在寻找一种通过检查本地文件并包含它来覆盖指南针config.rb变量/常量的方法。 使用这种方法(而不是定义调用罗盘时使用的配置文件的当前选项)意味着我们可以为所有开发人员和构建系统提供一组默认值,并允许开发人员在必要时为自己的本地设置覆盖这些默认值。 不幸的是我根本不知道Ruby,只需检查一个文件并在config.rb中需要它就不会覆盖原始设置。我目前的编码尝试如下。请有人向我解释我在这里做错了什么?

config.rb

# Compass configuration file.

# Require any additional compass plugins here.

# Sass / Compass paths
http_path = "/"
css_dir = "../../web/stylesheets"
sass_dir = "sass"
images_dir = "../../web/images"
javascripts_dir = "javascript"
fonts_dir = "fonts"

# Output style environment can be forced on build using -e
output_style = (environment == :production) ? :compressed : :expanded

# To enable relative paths to assets via compass helper functions. Uncomment:
# relative_assets = true

# Disable the compass cache method - we use our own methods.
asset_cache_buster = :none
line_comments = false
color_output = false

preferred_syntax = :scss

# Define the location of a the compass / sass cache directory.
cache_path = "/tmp/compass-cache"

# Add shared sass path to make it easier to include assets.
add_import_path = "../shared/sass"

# TODO: Check for a local config file - use this to extend/override this config file.
$localConfig = File.join(File.dirname(__FILE__), "config.local.rb")
require $localConfig if File.exist?($localConfig) and File.file?($localConfig)

config.local.rb

# Additional custom Compass Configuration file.

# Require any additional compass plugins here.

line_comments = true

cache_path = "/Users/jwestbrook/Sites/compass-cache"

sass_options = {
    :debug_info => true,
    :sourcemap => true
}
enable_sourcemaps = true 

1 个答案:

答案 0 :(得分:0)

所以我没有Ruby开发人员,但以下内容应该有效......

我们的想法是使用标准的config.rb文件和config.production.rb文件,将所有标准生产设置作为哈希/关联数组。然后,我们将这些散列键引用为config.rb中的罗盘常量。

如果开发人员想要覆盖任何设置,那么他们只需将config.development.rb文件添加到与config.rb和config.production.rb相同的位置,并定义它们的覆盖。

实施例

<强> config.rb

require 'compass/import-once/activate'
# Require any additional compass plugins here.

# Define the paths for config files.
productionSettings = File.join(File.dirname(__FILE__), "config.production.rb")
developmentSettings = File.join(File.dirname(__FILE__), "config.development.rb")

# Include the production config
require productionSettings if File.exist?(productionSettings) and File.file?(productionSettings)

# Set the compass settings to productionSettings $configSettings
compassSettings = $configSettings

# If a development config file exists include it and merge it's $configSettings
# with the current compassSettings
if File.exist?(developmentSettings) and File.file?(developmentSettings)
    require developmentSettings
    compassSettings = compassSettings.merge($configSettings)    
end

# Compass settings. If statements to prevent errors if a key doesn't exist.
# Note that any additional settings you add to production or development 
# will need to be referenced here else compass won't pick them up.

http_path = compassSettings['http_path'] if compassSettings.key?('http_path')
css_dir = compassSettings['css_dir'] if compassSettings.key?('css_dir')
sass_dir = compassSettings['sass_dir'] if compassSettings.key?('sass_dir')
images_dir = compassSettings['images_dir'] if compassSettings.key?('images_dir')
javascripts_dir = compassSettings['javascripts_dir'] if compassSettings.key?('javascripts_dir')
fonts_dir = compassSettings['fonts_dir'] if compassSettings.key?('fonts_dir')

output_style = compassSettings['output_style'] if compassSettings.key?('output_style')

relative_assets = compassSettings['relative_assets'] if compassSettings.key?('relative_assets')

line_comments = compassSettings['line_comments'] if compassSettings.key?('line_comments')
color_output = compassSettings['color_output'] if compassSettings.key?('color_output')

preferred_syntax = compassSettings['preferred_syntax'] if compassSettings.key?('preferred_syntax')
sourcemap = compassSettings['sourcemap'] if compassSettings.key?('sourcemap')

cache_path = compassSettings['cache_path'] if compassSettings.key?('cache_path')

<强> config.production.rb

$configSettings = {
    'http_path' => "/",
    'css_dir' => "css",
    'sass_dir' => "sass",
    'images_dir' => "images",
    'javascripts_dir' => "scripts",
    'fonts_dir' => "fonts",
    'preferred_syntax' => :scss,
    'color_output' => false,
    'output_style' => :compressed,
    'sourcemap' => false,
}

<强> config.development.rb

$configSettings = {
    'cache_path' => '/tmp/sass-cache',
    'output_style' => :expanded,
    'sourcemap' => true,
}