lib/nanoc/base/source_data/site.rb in nanoc-3.5.0 vs lib/nanoc/base/source_data/site.rb in nanoc-3.6.0

- old
+ new

@@ -275,10 +275,25 @@ @loaded = false @unloading = false end + # @return [Boolean] true if the current working directory is a nanoc site, false otherwise + # + # @api private + def self.cwd_is_nanoc_site? + !self.config_filename_for_cwd.nil? + end + + # @return [String] filename of the nanoc config file in the current working directory, or nil if there is none + # + # @api private + def self.config_filename_for_cwd + filenames = %w( nanoc.yaml config.yaml ) + filenames.find { |f| File.file?(f) } + end + private # Loads this site’s code and executes it. def load_code_snippets @code_snippets_loaded ||= false @@ -303,11 +318,11 @@ @items_loaded ||= false return if @items_loaded @items_loaded = true # Get items - @items = [] + @items = Nanoc::ItemArray.new data_sources.each do |ds| items_in_ds = ds.items items_in_ds.each do |i| i.identifier = File.join(ds.items_root, i.identifier) i.site = self @@ -338,11 +353,17 @@ # Check whether it is supported if dir_or_config_hash != '.' warn 'WARNING: Calling Nanoc::Site.new with a directory that is not the current working directory is not supported. It is recommended to change the directory before calling Nanoc::Site.new. For example, instead of Nanoc::Site.new(\'abc\'), use Dir.chdir(\'abc\') { Nanoc::Site.new(\'.\') }.' end - # Read config from config.yaml in given dir - config_path = File.join(dir_or_config_hash, 'config.yaml') + # Read config from nanoc.yaml/config.yaml in given dir + config_path = Dir.chdir(dir_or_config_hash) do + filename = self.class.config_filename_for_cwd + if filename.nil? + raise Nanoc::Errors::GenericTrivial, 'Could not find nanoc.yaml or config.yaml in the current working directory' + end + File.join(dir_or_config_hash, filename) + end @config = DEFAULT_CONFIG.merge(YAML.load_file(config_path).symbolize_keys_recursively) @config[:data_sources].map! { |ds| ds.symbolize_keys_recursively } else # Use passed config hash @config = DEFAULT_CONFIG.merge(dir_or_config_hash)