From b13fd43a2e504b0cd0cb859b51fc6c92e8c1597a Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Wed, 2 Sep 2009 19:47:01 -0700 Subject: [PATCH] Require relative to the load path instead of using the more convoluted approach of File.join(File.dirname(__FILE__), ...). Also, don't ever muck with the load paths or rubygems without first trying to require the library without doing so. --- Rakefile | 3 ++- bin/compass | 15 +++++++++++++-- lib/compass.rb | 10 +++++----- lib/compass/app_integration.rb | 2 +- lib/compass/app_integration/merb.rb | 2 +- lib/compass/app_integration/rails.rb | 4 ++-- lib/compass/app_integration/rails/runtime.rb | 2 +- lib/compass/app_integration/stand_alone.rb | 4 ++-- lib/compass/commands/create_project.rb | 3 +-- lib/compass/commands/generate_grid_background.rb | 6 +++--- lib/compass/commands/installer_command.rb | 2 +- lib/compass/commands/project_base.rb | 4 ++-- lib/compass/commands/stamp_pattern.rb | 4 ++-- lib/compass/commands/update_project.rb | 4 ++-- lib/compass/commands/validate_project.rb | 6 +++--- lib/compass/commands/watch_project.rb | 11 ++++++++--- lib/compass/commands/write_configuration.rb | 2 +- lib/compass/configuration.rb | 2 +- lib/compass/dependencies.rb | 14 ++++++-------- lib/compass/exec.rb | 14 ++++++++------ lib/compass/installers.rb | 2 +- lib/compass/sass_extensions.rb | 4 ++-- lib/compass/sass_extensions/functions.rb | 4 ++-- lib/compass/sass_extensions/monkey_patches.rb | 4 ++-- lib/vendor/fssm.rb | 2 -- test/test_helper.rb | 3 +++ 26 files changed, 75 insertions(+), 58 deletions(-) diff --git a/Rakefile b/Rakefile index b53706ce..7fa6ca31 100644 --- a/Rakefile +++ b/Rakefile @@ -12,7 +12,8 @@ end require 'rubygems' require 'rake' -require 'lib/compass' +$:.unshift File.join(File.dirname(__FILE__), 'lib') +require 'compass' # ----- Default: Testing ------ diff --git a/bin/compass b/bin/compass index fb94e65f..b8af02d8 100755 --- a/bin/compass +++ b/bin/compass @@ -1,8 +1,19 @@ #!/usr/bin/env ruby # The compass command line utility -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'compass')) -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'compass', 'exec')) +begin + retried = false + require 'compass' + require 'compass/exec' +rescue LoadError + if retried + raise + else + $: << File.join(File.dirname(__FILE__), '..', 'lib') + retried = true + retry + end +end command = Compass::Exec::Compass.new(ARGV) exit command.run! diff --git a/lib/compass.rb b/lib/compass.rb index 0956ff87..edc23581 100644 --- a/lib/compass.rb +++ b/lib/compass.rb @@ -1,8 +1,8 @@ module Compass end -['dependencies', 'sass_extensions', 'core_ext', 'version', 'errors'].each do |file| - require File.join(File.dirname(__FILE__), 'compass', file) +%w(dependencies sass_extensions core_ext version errors).each do |lib| + require "compass/#{lib}" end module Compass @@ -16,6 +16,6 @@ module Compass module_function :base_directory, :lib_directory end -require File.join(File.dirname(__FILE__), 'compass', 'configuration') -require File.join(File.dirname(__FILE__), 'compass', 'frameworks') -require File.join(File.dirname(__FILE__), 'compass', 'app_integration') +%w(configuration frameworks app_integration).each do |lib| + require "compass/#{lib}" +end diff --git a/lib/compass/app_integration.rb b/lib/compass/app_integration.rb index 9e81105d..8d7ec37b 100644 --- a/lib/compass/app_integration.rb +++ b/lib/compass/app_integration.rb @@ -1,3 +1,3 @@ %w(stand_alone rails merb).each do |lib| - require File.join(File.dirname(__FILE__), 'app_integration', lib) + require "compass/app_integration/#{lib}" end diff --git a/lib/compass/app_integration/merb.rb b/lib/compass/app_integration/merb.rb index bb460dc9..546ac987 100644 --- a/lib/compass/app_integration/merb.rb +++ b/lib/compass/app_integration/merb.rb @@ -1 +1 @@ -require File.join(File.dirname(__FILE__), 'merb', 'runtime') if defined?(Merb::Plugins) +require 'compass/app_integration/merb/runtime' if defined?(Merb::Plugins) diff --git a/lib/compass/app_integration/rails.rb b/lib/compass/app_integration/rails.rb index c095e889..28bef715 100644 --- a/lib/compass/app_integration/rails.rb +++ b/lib/compass/app_integration/rails.rb @@ -1,8 +1,8 @@ %w(configuration_defaults installer).each do |lib| - require File.join(File.dirname(__FILE__), 'rails', lib) + require "compass/app_integration/rails/#{lib}" end -require File.join(File.dirname(__FILE__), 'rails', 'runtime') if defined?(ActionController::Base) +require 'compass/app_integration/rails/runtime' if defined?(ActionController::Base) diff --git a/lib/compass/app_integration/rails/runtime.rb b/lib/compass/app_integration/rails/runtime.rb index 8cc3b061..82a353d1 100644 --- a/lib/compass/app_integration/rails/runtime.rb +++ b/lib/compass/app_integration/rails/runtime.rb @@ -2,7 +2,7 @@ unless defined?(Compass::RAILS_LOADED) Compass::RAILS_LOADED = true %w(action_controller sass_plugin urls).each do |lib| - require File.join(File.dirname(__FILE__), lib) + require "compass/app_integration/rails/#{lib}" end # Wierd that this has to be re-included to pick up sub-modules. Ruby bug? diff --git a/lib/compass/app_integration/stand_alone.rb b/lib/compass/app_integration/stand_alone.rb index f5cc3c21..4e5d0722 100644 --- a/lib/compass/app_integration/stand_alone.rb +++ b/lib/compass/app_integration/stand_alone.rb @@ -1,3 +1,3 @@ -['configuration_defaults', 'installer'].each do |lib| - require File.join(File.dirname(__FILE__), 'stand_alone', lib) +%w(configuration_defaults installer).each do |lib| + require "compass/app_integration/stand_alone/#{lib}" end \ No newline at end of file diff --git a/lib/compass/commands/create_project.rb b/lib/compass/commands/create_project.rb index 3605af1e..45cf2706 100644 --- a/lib/compass/commands/create_project.rb +++ b/lib/compass/commands/create_project.rb @@ -1,6 +1,5 @@ require 'fileutils' -require File.join(File.dirname(__FILE__), 'stamp_pattern') -require File.join(File.dirname(__FILE__), 'update_project') +require 'compass/commands/stamp_pattern' module Compass module Commands diff --git a/lib/compass/commands/generate_grid_background.rb b/lib/compass/commands/generate_grid_background.rb index 0494a420..646c8d0a 100644 --- a/lib/compass/commands/generate_grid_background.rb +++ b/lib/compass/commands/generate_grid_background.rb @@ -1,6 +1,6 @@ -require File.join(File.dirname(__FILE__), 'project_base') -require File.join(File.dirname(__FILE__), 'update_project') -require File.join(File.dirname(__FILE__), '..', 'grid_builder') +require 'compass/commands/project_base' +require 'compass/commands/update_project' +require 'compass/grid_builder' module Compass module Commands diff --git a/lib/compass/commands/installer_command.rb b/lib/compass/commands/installer_command.rb index 7385c7f8..35bc0af3 100644 --- a/lib/compass/commands/installer_command.rb +++ b/lib/compass/commands/installer_command.rb @@ -1,4 +1,4 @@ -require File.join(Compass.lib_directory, 'compass', 'installers') +require 'compass/installers' module Compass module Commands diff --git a/lib/compass/commands/project_base.rb b/lib/compass/commands/project_base.rb index 335c403a..3567682e 100644 --- a/lib/compass/commands/project_base.rb +++ b/lib/compass/commands/project_base.rb @@ -1,7 +1,7 @@ require 'fileutils' require 'pathname' -require File.join(File.dirname(__FILE__), 'base') -require File.join(File.dirname(__FILE__), 'installer_command') +require 'compass/commands/base' +require 'compass/commands/installer_command' module Compass module Commands diff --git a/lib/compass/commands/stamp_pattern.rb b/lib/compass/commands/stamp_pattern.rb index 47f02b84..e091c153 100644 --- a/lib/compass/commands/stamp_pattern.rb +++ b/lib/compass/commands/stamp_pattern.rb @@ -1,6 +1,6 @@ require 'fileutils' -require File.join(File.dirname(__FILE__), 'base') -require File.join(File.dirname(__FILE__), 'update_project') +require 'compass/commands/base' +require 'compass/commands/update_project' module Compass module Commands diff --git a/lib/compass/commands/update_project.rb b/lib/compass/commands/update_project.rb index e00e7a8f..709dd2aa 100644 --- a/lib/compass/commands/update_project.rb +++ b/lib/compass/commands/update_project.rb @@ -1,5 +1,5 @@ -require File.join(File.dirname(__FILE__), 'project_base') -require File.join(Compass.lib_directory, 'compass', 'compiler') +require 'compass/commands/project_base' +require 'compass/compiler' module Compass module Commands diff --git a/lib/compass/commands/validate_project.rb b/lib/compass/commands/validate_project.rb index 13c06da9..cd19ce60 100644 --- a/lib/compass/commands/validate_project.rb +++ b/lib/compass/commands/validate_project.rb @@ -1,5 +1,5 @@ -require File.join(File.dirname(__FILE__), 'project_base') -require File.join(File.dirname(__FILE__), 'update_project') +require 'compass/commands/project_base' +require 'compass/commands/update_project' module Compass module Commands @@ -11,7 +11,7 @@ module Compass end def perform - require File.join(File.dirname(__FILE__), '..', 'validator') + require 'compass/validator' UpdateProject.new(working_path, options).perform Validator.new(project_css_subdirectory).validate() end diff --git a/lib/compass/commands/watch_project.rb b/lib/compass/commands/watch_project.rb index ee0528d8..9841b3a9 100644 --- a/lib/compass/commands/watch_project.rb +++ b/lib/compass/commands/watch_project.rb @@ -1,7 +1,7 @@ require 'fileutils' require 'pathname' -require File.join(File.dirname(__FILE__), 'base') -require File.join(File.dirname(__FILE__), 'update_project') +require 'compass/commands/base' +require 'compass/commands/update_project' module Compass module Commands @@ -19,7 +19,12 @@ module Compass puts ">>> Compass is watching for changes. Press Ctrl-C to Stop." - require File.join(Compass.lib_directory, 'vendor', 'fssm') + begin + require 'fssm' + rescue LoadError + $: << File.join(Compass.lib_directory, 'vendor') + retry + end FSSM.monitor do |monitor| Compass.configuration.sass_load_paths.each do |load_path| diff --git a/lib/compass/commands/write_configuration.rb b/lib/compass/commands/write_configuration.rb index 7d9604aa..2aaefb69 100644 --- a/lib/compass/commands/write_configuration.rb +++ b/lib/compass/commands/write_configuration.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), 'project_base') +require 'compass/commands/project_base' module Compass module Commands diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index 1d8203c9..fdf686eb 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -34,5 +34,5 @@ module Compass end ['adapters', 'comments', 'defaults', 'helpers', 'inheritance', 'serialization', 'data'].each do |lib| - require File.join(File.dirname(__FILE__), 'configuration', lib) + require "compass/configuration/#{lib}" end diff --git a/lib/compass/dependencies.rb b/lib/compass/dependencies.rb index dbc9c43d..5c697b3c 100644 --- a/lib/compass/dependencies.rb +++ b/lib/compass/dependencies.rb @@ -1,8 +1,6 @@ -unless defined?(Sass) - begin - require 'sass' - rescue LoadError - require 'rubygems' - require 'sass' - end -end \ No newline at end of file +begin + require 'sass' +rescue LoadError + require 'rubygems' + require 'sass' +end diff --git a/lib/compass/exec.rb b/lib/compass/exec.rb index f7a7ba90..571c3713 100644 --- a/lib/compass/exec.rb +++ b/lib/compass/exec.rb @@ -1,8 +1,8 @@ -require File.join(Compass.lib_directory, 'compass', 'dependencies') +require 'compass/dependencies' require 'optparse' -require File.join(Compass.lib_directory, 'compass', 'logger') -require File.join(Compass.lib_directory, 'compass', 'errors') -require File.join(Compass.lib_directory, 'compass', 'actions') +require 'compass/logger' +require 'compass/errors' +require 'compass/actions' module Compass module Exec @@ -245,6 +245,8 @@ END end end -Dir.glob(File.join(File.dirname(__FILE__), 'commands', "*.rb")).each do |file| - require file +%w(base generate_grid_background list_frameworks project_base + update_project watch_project create_project installer_command + print_version stamp_pattern validate_project write_configuration).each do |lib| + require "compass/commands/#{lib}" end diff --git a/lib/compass/installers.rb b/lib/compass/installers.rb index 7ae1b400..84598af7 100644 --- a/lib/compass/installers.rb +++ b/lib/compass/installers.rb @@ -1,3 +1,3 @@ %w(manifest template_context base).each do |f| - require File.join(File.dirname(__FILE__), 'installers', f) + require "compass/installers/#{f}" end diff --git a/lib/compass/sass_extensions.rb b/lib/compass/sass_extensions.rb index 986be47f..182717b1 100644 --- a/lib/compass/sass_extensions.rb +++ b/lib/compass/sass_extensions.rb @@ -1,5 +1,5 @@ module Compass::SassExtensions end -require File.join(File.dirname(__FILE__), 'sass_extensions', 'functions') -require File.join(File.dirname(__FILE__), 'sass_extensions', 'monkey_patches') +require 'compass/sass_extensions/functions' +require 'compass/sass_extensions/monkey_patches' diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index 75c8eb2f..a945b59d 100644 --- a/lib/compass/sass_extensions/functions.rb +++ b/lib/compass/sass_extensions/functions.rb @@ -1,8 +1,8 @@ module Compass::SassExtensions::Functions end -['selectors', 'enumerate', 'urls', 'display', 'inline_image'].each do |func| - require File.join(File.dirname(__FILE__), 'functions', func) +%w(selectors enumerate urls display inline_image).each do |func| + require "compass/sass_extensions/functions/#{func}" end module Sass::Script::Functions diff --git a/lib/compass/sass_extensions/monkey_patches.rb b/lib/compass/sass_extensions/monkey_patches.rb index de54a75c..04cc8a72 100644 --- a/lib/compass/sass_extensions/monkey_patches.rb +++ b/lib/compass/sass_extensions/monkey_patches.rb @@ -1,3 +1,3 @@ -['stylesheet_updating'].each do |patch| - require File.join(File.dirname(__FILE__), 'monkey_patches', patch) +%w(stylesheet_updating).each do |patch| + require "compass/sass_extensions/monkey_patches/#{patch}" end \ No newline at end of file diff --git a/lib/vendor/fssm.rb b/lib/vendor/fssm.rb index d04b32aa..245fa5d0 100644 --- a/lib/vendor/fssm.rb +++ b/lib/vendor/fssm.rb @@ -16,7 +16,6 @@ module FSSM end end -$:.unshift(File.dirname(__FILE__)) require 'pathname' require 'fssm/ext' require 'fssm/support' @@ -26,5 +25,4 @@ require 'fssm/monitor' require "fssm/backends/#{FSSM::Support.backend.downcase}" FSSM::Backends::Default = FSSM::Backends.const_get(FSSM::Support.backend) -$:.shift diff --git a/test/test_helper.rb b/test/test_helper.rb index d731494b..d1c32c4a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,8 @@ need_gems = false +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +$:.unshift(lib_dir) unless $:.include?(lib_dir) + # allows testing with edge Haml by creating a test/haml symlink linked_haml = File.dirname(__FILE__) + '/haml'