Class: Nanoc::Core::ItemRepWriter

Inherits:
Object
  • Object
show all
Includes:
Assertions::Mixin, ContractsSupport
Defined in:
lib/nanoc/core/item_rep_writer.rb

Constant Summary collapse

TMP_TEXT_ITEMS_DIR =
'text_items'

Instance Method Summary collapse

Methods included from Assertions::Mixin

#assert

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Instance Method Details

#smart_cp(from, to) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nanoc/core/item_rep_writer.rb', line 100

def smart_cp(from, to)
  # Try clonefile
  if defined?(Clonefile)
    FileUtils.rm_f(to)
    begin
      res = Clonefile.always(from, to)
      return if res
    rescue Clonefile::UnsupportedPlatform,
           Errno::ENOTSUP,
           Errno::EXDEV,
           Errno::EINVAL
    end
  end

  # Try with hardlink
  begin
    FileUtils.ln(from, to, force: true)
    return
  rescue Errno::EXDEV, Errno::EACCES
  end

  # Fall back to old-school copy
  FileUtils.cp(from, to)
end

#temp_filenameObject



96
97
98
# File 'lib/nanoc/core/item_rep_writer.rb', line 96

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

#write(item_rep, compiled_content_repo, snapshot_name, written_paths) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nanoc/core/item_rep_writer.rb', line 19

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

#write_all(item_rep, compiled_content_repo) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/nanoc/core/item_rep_writer.rb', line 11

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

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

#write_single(item_rep, compiled_content_repo, snapshot_name, raw_path, written_paths) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nanoc/core/item_rep_writer.rb', line 31

def write_single(
  item_rep,
  compiled_content_repo,
  snapshot_name,
  raw_path,
  written_paths
)
  assert Nanoc::Core::Assertions::PathIsAbsolute.new(path: raw_path)

  # 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::Core::NotificationCenter.post(
    :rep_write_started, item_rep, raw_path
  )

  content = compiled_content_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)

  # Notify ready for diff generation
  if !is_created && is_modified && !content.binary?
    Nanoc::Core::NotificationCenter.post(
      :rep_ready_for_diff,
      raw_path,
      File.read(raw_path, encoding: 'UTF-8'),
      content.string,
    )
  end

  # Write
  if is_modified
    smart_cp(temp_path, raw_path)
  end

  item_rep.modified = is_modified

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