lib/nanoc3/base/item_rep.rb in nanoc3-3.1.0b1 vs lib/nanoc3/base/item_rep.rb in nanoc3-3.1.0b2

- old
+ new

@@ -190,10 +190,15 @@ snapshot_name ||= :pre else snapshot_name ||= :last end + # Check presence of snapshot + if @content[snapshot_name].nil? + warn "WARNING: The “#{self.item.identifier}” item (rep “#{self.name}”) does not have the requested snapshot named #{snapshot_name.inspect}.\n\n* Make sure that you are requesting the correct snapshot.\n* It is not possible to request the compiled content of a binary item representation; if this item is marked as binary even though you believe it should be textual, you may need to add the extension of this item to the site configuration’s `text_extensions` array.".make_compatible_with_env + end + # Get content @content[snapshot_name] end # @deprecated Use {Nanoc3::ItemRep#compiled_content} instead. @@ -331,10 +336,13 @@ # Write File.open(self.raw_path, 'w') { |io| io.write(@content[:last]) } @written = true + # Generate diff + generate_diff + # Check if file was modified @modified = File.read(self.raw_path) != @old_content end end @@ -344,24 +352,22 @@ # # @return [String, nil] The difference between the old and new compiled # content in `diff(1)` format, or nil if there is no previous compiled # content def diff - # Check if content can be diffed # TODO allow binary diffs - return nil if self.binary? - # Check if old content exists - if @old_content.nil? or self.raw_path.nil? + if self.binary? nil else - diff_strings(@old_content, @content[:last]) + @diff_thread.join if @diff_thread + @diff end end def inspect - "<#{self.class}:0x#{self.object_id.to_s(16)} name=#{self.name} binary=#{self.binary?} item.identifier=#{self.item.identifier}>" + "<#{self.class}:0x#{self.object_id.to_s(16)} name=#{self.name} binary=#{self.binary?} raw_path=#{self.raw_path} item.identifier=#{self.item.identifier}>" end private def filter_named(name) @@ -388,10 +394,22 @@ # Done [ filter, filter_name, filter_args ] end + def generate_diff + if @old_content.nil? or self.raw_path.nil? + @diff = nil + else + @diff_thread = Thread.new do + @diff = diff_strings(@old_content, @content[:last]) + sleep 2 + @diff_thread = nil + end + end + end + def diff_strings(a, b) # TODO Rewrite this string-diffing method in pure Ruby. It should not # use the "diff" executable, because this will most likely not work on # operating systems without it, such as Windows. @@ -404,12 +422,14 @@ # Write files old_file.write(a) new_file.write(b) # Diff - stdin, stdout, stderr = Open3.popen3('diff', '-u', old_file.path, new_file.path) - result = stdout.read - result == '' ? nil : result + cmd = [ 'diff', '-u', old_file.path, new_file.path ] + Open3.popen3(*cmd) do |stdin, stdout, stderr| + result = stdout.read + return (result == '' ? nil : result) + end end end rescue Errno::ENOENT warn 'Failed to run `diff`, so no diff with the previously compiled ' \ 'content will be available.'