Sha256: e75b2fd3887a49c8afdf624317617ffd0d1acc5797d04fdf0b67f42e6c693876

Contents?: true

Size: 1.79 KB

Versions: 25

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Nanoc::Int
  # @api private
  class ItemRepWriter
    TMP_TEXT_ITEMS_DIR = 'text_items'

    def write_all(item_rep, snapshot_repo)
      written_paths = Set.new

      item_rep.snapshot_defs.map(&:name).each do |snapshot_name|
        write(item_rep, snapshot_repo, snapshot_name, written_paths)
      end
    end

    def write(item_rep, snapshot_repo, snapshot_name, written_paths)
      item_rep.raw_paths.fetch(snapshot_name, []).each do |raw_path|
        write_single(item_rep, snapshot_repo, snapshot_name, raw_path, written_paths)
      end
    end

    def write_single(item_rep, snapshot_repo, snapshot_name, raw_path, written_paths)
      # Don’t write twice
      # TODO: test written_paths behavior
      return if written_paths.include?(raw_path)
      written_paths << raw_path

      # Create parent directory
      FileUtils.mkdir_p(File.dirname(raw_path))

      # Check if file will be created
      is_created = !File.file?(raw_path)

      # Notify
      Nanoc::Int::NotificationCenter.post(
        :will_write_rep, item_rep, raw_path
      )

      content = snapshot_repo.get(item_rep, snapshot_name)
      if content.binary?
        temp_path = content.filename
      else
        temp_path = temp_filename
        File.write(temp_path, content.string)
      end

      # Check whether content was modified
      is_modified = is_created || !FileUtils.identical?(raw_path, temp_path)

      # Write
      FileUtils.cp(temp_path, raw_path) if is_modified

      item_rep.modified = is_modified

      # Notify
      Nanoc::Int::NotificationCenter.post(
        :rep_written, item_rep, content.binary?, raw_path, is_created, is_modified
      )
    end

    def temp_filename
      Nanoc::Int::TempFilenameFactory.instance.create(TMP_TEXT_ITEMS_DIR)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
nanoc-4.8.19 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.18 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.17 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.16 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.15 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.14 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.13 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.12 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.11 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.10 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.9 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.8 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.7 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.6 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.5 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.4 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.3 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.2 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.1 lib/nanoc/base/services/item_rep_writer.rb
nanoc-4.8.0 lib/nanoc/base/services/item_rep_writer.rb