Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source option for file in generator #15

Open
thinkerbot opened this issue Nov 30, 2010 · 0 comments
Open

Add source option for file in generator #15

thinkerbot opened this issue Nov 30, 2010 · 0 comments

Comments

@thinkerbot
Copy link
Owner

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant