lib/itamae/resource/remote_file.rb in itamae-1.1.19 vs lib/itamae/resource/remote_file.rb in itamae-1.1.20

- old
+ new

@@ -1,16 +1,51 @@ require 'itamae' module Itamae module Resource class RemoteFile < File - define_attribute :source, type: String, required: true + SourceNotFoundError = Class.new(StandardError) - def pre_action - attributes.content_file = - ::File.expand_path(attributes.source, ::File.dirname(@recipe.path)) + define_attribute :source, type: [String, Symbol], default: :auto - super + private + + def content_file + source_file + end + + def source_file + @source_file ||= find_source_file + end + + def find_source_file + if attributes.source == :auto + dirs = attributes.path.split(::File::SEPARATOR) + dirs.shift if dirs.first == "" + + searched_paths = [] + dirs.size.times do |i| + path = ::File.join(@recipe.dir, source_file_dir, "#{dirs[i..-1].join("/")}#{source_file_ext}") + if ::File.exist?(path) + Logger.debug "#{path} is used as a source file." + return path + else + searched_paths << path + end + end + + raise SourceNotFoundError, "source file is not found (searched paths: #{searched_paths.join(', ')})" + else + ::File.expand_path(attributes.source, @recipe.dir) + end + end + + def source_file_dir + "files" + end + + def source_file_ext + "" end end end end