2013-09-26 42 views
2

我正在尋找一種方法來通過檢查本地文件幷包括它來覆蓋指南針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 
+0

您是否曾經爲此找到過解決方案? – ojrask

+0

對不起@ojrask。我終於找到了一個解決方案,並與開發人員一起工作。但是,代碼與項目密切相關,否則將會很難分開。不過,我會嘗試整理一個簡單的例子來向你展示我們所採取的方向。 – jwestbrook

回答

0

所以我沒有Ruby開發者,但下面應該工作...

的想法是,我們有標準的config.rb文件和一個config.production.rb文件,所有的標準生產設置都是散列/關聯數組。然後,我們將這些散列鍵作爲config.rb中的指南針常量。

如果開發人員想要覆蓋任何設置,那麼他們只需在config.rb和config.production.rb的相同位置添加一個config.development.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, 
}