tap icon indicating copy to clipboard operation
tap copied to clipboard

Add source option for file in generator

Open thinkerbot opened this issue 15 years ago • 0 comments

module FilePatch
  def file(target, options={})
    options[:source] ||= begin
      source_file = Tempfile.new('generate')
      yield(source_file) if block_given?
      source_file.close
      source_file.path
    end

    source = options[:source]
    target = path(target)

    copy_file = true
    msg = case
    when !File.exists?(target)
      :create
    when FileUtils.cmp(source, target)
      :exists
    when force_file_collision?(target)
      :force
    else
      copy_file = false
      :skip
    end

    log_relative msg, target
    if copy_file && !pretend
      dir = File.dirname(target)
      FileUtils.mkdir_p(dir, :mode => 0755) unless File.exists?(dir) 
      FileUtils.mv(source, target, :force => true)
      FileUtils.chmod(0644, target)
    end

    target
  end
end

thinkerbot avatar Nov 30 '10 18:11 thinkerbot