Skip to content

Add source option for file in generator #15

@thinkerbot

Description

@thinkerbot
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions