Module: Glim::Commands
- Defined in:
- lib/commands.rb
Class Method Summary collapse
- .build(config) ⇒ Object
- .clean(config) ⇒ Object
- .delete_items(files, dirs, keep: []) ⇒ Object
- .generate(output_dir, number_of_jobs, files) ⇒ Object
- .generate_async(output_dir, files, number_of_jobs) ⇒ Object
- .generate_subset(output_dir, files) ⇒ Object
-
.items_in_directory(dir, skip: []) ⇒ Object
= Private = ===========.
- .profile(config) ⇒ Object
Class Method Details
.build(config) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/commands.rb', line 3 def self.build(config) output_dir = File.(config['destination']) files = config.site.files_and_documents.select { |file| file.write? } symlinks = (config.site.symlinks || []).map { |link| [ File.(File.join(link[:data]['domain'] || '.', link[:name]), output_dir), link[:realpath] ] }.to_h output_paths = files.map { |file| file.output_path(output_dir) } output_paths.concat(symlinks.keys) delete_files, delete_dirs = items_in_directory(output_dir, skip: config['keep_files']) deleted = delete_items(delete_files, delete_dirs, keep: output_paths) created, updated, warnings, errors = *generate(output_dir, config['jobs'] || 7, files) symlinks.each do |dest, path| FileUtils.mkdir_p(File.dirname(dest)) begin File.symlink(path, dest) created << dest rescue Errno::EEXIST if File.readlink(dest) != path File.unlink(dest) File.symlink(path, dest) updated << dest end end end [ [ 'Created', created ], [ 'Deleted', deleted ], [ 'Updated', updated ] ].each do |label, files| unless files.empty? STDERR.puts "==> #{label} #{files.count} #{files.count == 1 ? 'File' : 'Files'}" STDERR.puts files.map { |path| Util.relative_path(path, output_dir) }.sort.join(', ') end end unless warnings.empty? STDERR.puts "==> #{warnings.count} #{warnings.count == 1 ? 'Warnings' : 'Warning'}" warnings.each do || STDERR.puts end end unless errors.empty? STDERR.puts "==> Stopped After #{errors.count} #{errors.count == 1 ? 'Error' : 'Errors'}" errors.each do |arr| arr.each_with_index do |err, i| STDERR.puts err.gsub(/^/, ' '*i) end end end end |
.clean(config) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/commands.rb', line 53 def self.clean(config) files, dirs = items_in_directory(File.(config['destination']), skip: config['keep_files']) if config['dry_run'] if files.empty? STDOUT.puts "No files to delete" else files.each do |file| STDOUT.puts "Delete #{Util.relative_path(file, File.expand_path(config['source']))}" end end else deleted = delete_items(files, dirs) STDOUT.puts "Deleted #{deleted.count} #{deleted.count == 1 ? 'File' : 'Files'}." end end |
.delete_items(files, dirs, keep: []) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/commands.rb', line 138 def self.delete_items(files, dirs, keep: []) res = [] keep_files = Set.new(keep) files.each do |path| unless keep_files.include?(path) begin File.unlink(path) res << path rescue => e $log.error("Error unlinking ‘#{path}’: #{e}\n") end end end dirs.sort.reverse.each do |path| begin Dir.rmdir(path) rescue Errno::ENOTEMPTY => e # Ignore rescue => e $log.error("Error removing directory ‘#{path}’: #{e}\n") end end res end |
.generate(output_dir, number_of_jobs, files) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/commands.rb', line 166 def self.generate(output_dir, number_of_jobs, files) Profiler.run("Creating pages") do if number_of_jobs == 1 generate_subset(output_dir, files) else generate_async(output_dir, files.shuffle, number_of_jobs) end end end |
.generate_async(output_dir, files, number_of_jobs) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/commands.rb', line 176 def self.generate_async(output_dir, files, number_of_jobs) total = files.size slices = number_of_jobs.times.map do |i| first = (total * i / number_of_jobs).ceil last = (total * (i+1) / number_of_jobs).ceil files.shift(last-first) end Glim::Cache.track_updates = true semaphore = Mutex.new created, updated, warnings, errors = [], [], [], [] threads = slices.each_with_index.map do |files_slice, i| pipe_rd, pipe_wr = IO.pipe pid = fork do start = Time.now pipe_rd.close created, updated, warnings, errors = *generate_subset(output_dir, files_slice) pipe_wr << Marshal.dump({ 'cache_updates' => Glim::Cache.updates, 'created' => created, 'updated' => updated, 'warnings' => warnings, 'errors' => errors, 'duration' => Time.now - start, 'id' => i, }) pipe_wr.close end Process.detach(pid) Thread.new do pipe_wr.close res = Marshal.load(pipe_rd) semaphore.synchronize do Glim::Cache.merge!(res['cache_updates']) created += res['created'] updated += res['updated'] warnings += res['warnings'] errors += res['errors'] $log.debug("Wrote #{files_slice.size} pages in #{res['duration']} seconds (thread #{res['id']})") if Profiler.enabled end end end threads.each { |thread| thread.join } [ created, updated, warnings, errors ] end |
.generate_subset(output_dir, files) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/commands.rb', line 227 def self.generate_subset(output_dir, files) created, updated, warnings, errors = [], [], [], [] for file in files do dest = file.output_path(output_dir) file_exists = File.exists?(dest) FileUtils.mkdir_p(File.dirname(dest)) if file.frontmatter? begin if !file_exists || File.read(dest) != file.output (file_exists ? updated : created) << dest File.unlink(dest) if file_exists File.write(dest, file.output) end warnings.concat(file.warnings.map { |warning| "#{file}: #{warning}" }) unless file.warnings.nil? rescue Glim::Error => e errors << [ "Unable to create output for: #{file}", *e. ] break rescue => e errors << [ "Unable to create output for: #{file}", e.to_s, e.backtrace.join("\n") ] break end else unless File.file?(dest) && File.file?(file.path) && File.stat(dest).ino == File.stat(file.path).ino File.unlink(dest) if file_exists File.link(file.path, dest) end end end [ created, updated, warnings, errors ] end |
.items_in_directory(dir, skip: []) ⇒ Object
Private =
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/commands.rb', line 116 def self.items_in_directory(dir, skip: []) files, dirs = [], [] begin Find.find(dir) do |path| next if path == dir Find.prune if skip.include?(File.basename(path)) if File.file?(path) || File.symlink?(path) files << path elsif File.directory?(path) dirs << path else $log.warn("Unknown entry: #{path}") end end rescue Errno::ENOENT end [ files, dirs ] end |
.profile(config) ⇒ Object
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/commands.rb', line 70 def self.profile(config) Profiler.enabled = true site = Profiler.run("Setting up site") do config.site end Profiler.run("Loading cache") do Glim::Cache.load end files = [] Profiler.run("Loading pages") do files.concat(site.files) end Profiler.run("Loading collections") do files.concat(site.documents) end Profiler.run("Generating virtual pages") do files.concat(site.generated_files) end files = files.select { |file| file.frontmatter? } Profiler.run("Expanding liquid tags") do files.each { |file| file.content('post-liquid') } end Profiler.run("Transforming pages") do files.each { |file| file.content('pre-output') } end Profiler.run("Creating final output (layout)") do files.each { |file| file.output } end Profiler.enabled = false end |