module Take class Project class Target # Initializes the target. # # @param [Project] project the project that this target is # a part of. # @param [Hash] hash a hash of the options for this target. If # the any key is not one of the below, it is assumed to be the # target key-pair; there may only be one target key-pair (any # more will cause an ArgumentError). If the key of the key # pair is a symbol, it is considered an "invalid" target, # meaning it _must not_ be a default target. # @option hash [Symbol] :type The type of target it is. This is # normally assumed from the extension. # @option hash [Array] :depends, :depends_on Files that # the target depends on. This is normally set by the value # of the target key pair. # @option hash [Symbol, String] :name The name of the target. # This is normally set by the key of the target key pair. def initialize(hash, project, &block) raise ArgumentError, "Targets require blocks" \ unless block_given? @block = block @project = project @dependencies = [] handle_arguments(hash) guess_type unless @type end def invalid? @target.is_a?(Symbol) end def name if invalid? @target else @target.to_path end end def sub(what, to) @dependencies.map do |dep| if dep.extname == what dep.sub_ext(to) else dep end end end def to_a @dependencies.map(&:to_path) end private attr_reader :project def guess_type if invalid? @type = :invalid else @type = @target.type end end def handle_arguments(hash) [:type, :depends, :depends_on, :name] defined_target = false raise ArgumentError, "Expected Hash, got #{hash.class}" \ unless hash.is_a?(Hash) hash.each do |key, value| case key when :type @type = value when :depends, :depends_on @dependencies.concat(Array(value)) when :name @target = value when String, Symbol raise ArgumentError, "Target already defined! Maybe you" \ " misspelled. Unknown option #{key.inspect}." \ if defined_target @target = key defined_target = true @dependencies.concat(Array(value)) else raise ArgumentError, "Unknown option #{key.inspect}." end end @target = project.file(@target) unless invalid? @dependencies.map! { |dep| project.file(dep) } end end end end