Skip to content

Commit

Permalink
Add checksums generation & saving in yaml file.
Browse files Browse the repository at this point in the history
This is for future work to optimize on only regeneration when needed.
  • Loading branch information
sentientmonkey committed Jun 9, 2013
1 parent 370ecc8 commit eeb4558
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ test/tmp
test/version_tmp
tmp
example/_site/*
example/.meta.yml
8 changes: 5 additions & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
* Refactor down runner into renderer & option parser [done]
* Refactor out filters into seperate files [done]
* Set up default filters via final ext [done]
* Added .meta.yml for checksums [done]
* Build DAG
* Add DAG for rebuild & dependancies
Can checksum files, build .meta.yml file, trigger what needs to be rebuild via DAG
yaml -> partials -> template
* Add options for custom filters
* Add publishers for s3/ftp/rsync
* Add config for site/public
* Add DAG for rebuild & dependancies
Can checksum files, build .meta.yaml file, trigger what needs to be rebuild via DAG
yaml -> partials -> template
1 change: 1 addition & 0 deletions lib/flue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "flue/basefile"
require "flue/partial_file"
require "flue/filter_register"
require "flue/metadata"
require "flue/renderer"
require "flue/watcher"
require "flue/middleware"
Expand Down
18 changes: 18 additions & 0 deletions lib/flue/basefile.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "fileutils"
require "digest/sha2"

module Flue
class Basefile
Expand Down Expand Up @@ -50,5 +51,22 @@ def datafile
def content
File.read(filename)
end

def checksum
digest.hexdigest(content)
end

def ==(other)
other.class == self.class &&
other.filename == self.filename &&
other.checksum == self.checksum
end
alias_method :eql?, :==

private

def digest
Digest::SHA256
end
end
end
20 changes: 20 additions & 0 deletions lib/flue/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'yaml/store'

module Flue
class Metadata
attr_reader :store, :basefiles

def initialize
@store = YAML::Store.new(".meta.yml")
end

def update_checksum(basefile)
store.transaction do
checksums = store["checksums"] || {}
checksums[basefile.filename] = basefile.checksum
store["checksums"] = checksums
end
end

end
end
4 changes: 2 additions & 2 deletions lib/flue/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def initialize(app, watcher, renderer)

def call(env)
file = File.basename(env['PATH_INFO'])
basefile = renderer.files.map{|f| Basefile.new(f) }.detect{|f| f.outfile_basename == file }
basefile = renderer.basefiles.detect{|f| f.outfile_basename == file }
if basefile && watcher.changes.include?(basefile.filename)
renderer.render_file(basefile.filename)
renderer.render_file(basefile)
end
app.call(env)
end
Expand Down
20 changes: 16 additions & 4 deletions lib/flue/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ class Renderer
include Flue::Benchmark
include Flue::Logger

attr_reader :metadata

def initialize
@metadata = Metadata.new
end

def files
Dir["site/[^_]*"] - Dir["site/*.yml"]
end

def basefiles
files.map do |file|
Basefile.new(file)
end
end

def render_files
files.each do |file|
render_file file
basefiles.each do |basefile|
render_file basefile
end
end

def render_file(file)
basefile = Basefile.new(file)
def render_file(basefile)
File.open(basefile.outfile_name, "w") do |f|
benchmark "#{basefile.basename} => #{basefile.outfile_name}" do
options = {}
Expand All @@ -28,6 +39,7 @@ def render_file(file)
f.write FilterRegister.run(basefile.exts, basefile.content, options)
end
end
metadata.update_checksum(basefile)
end

end
Expand Down
14 changes: 14 additions & 0 deletions spec/basefile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,19 @@
end
end

it "should have a checksum" do
test_content = "test content"
File.stub :read, test_content do
md_erb.checksum.must_equal "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72"
end
end

it "should be equal if filename & checksums are same" do
test_content = "test content"
File.stub :read, test_content do
md_erb.must_equal md_erb.dup
end
end

end

26 changes: 26 additions & 0 deletions spec/metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "./spec/test_helper.rb"

describe Metadata do
let(:filename){ "filename" }
let(:checksum){ "checksum" }
let(:basefile) do
m = Minitest::Mock.new
m.expect(:filename, filename)
m.expect(:checksum, checksum)
m
end
let(:basefiles){ [basefile] }
let(:metadata){ Metadata.new }
let(:store){ Minitest::Mock.new }

it "must update checksum of files" do
store.expect(:transaction, nil)
#store.expect(:[]=, nil, ["checksums", {filename => checksum}])
#TODO figure how to get block testing to work properly
YAML::Store.stub :new, store do
metadata.update_checksum(basefile)
end
store.verify
end

end
5 changes: 3 additions & 2 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

let(:rendered_file){ 'index.html' }
let(:template_file){ 'index.erb.html' }
let(:basefile){ Basefile.new(template_file) }

let(:middleware) do
Middleware.new(app, watcher, renderer)
Expand All @@ -23,8 +24,8 @@

let(:renderer) do
r = MiniTest::Mock.new
r.expect :files, [template_file]
r.expect :render_file, true, [template_file]
r.expect :basefiles, [basefile]
r.expect :render_file, true, [basefile]
r
end

Expand Down
23 changes: 20 additions & 3 deletions spec/renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def call(filename, writemode)
b
end

let(:metadata){ MiniTest::Mock.new }

let(:filter_result) do
MiniTest::Mock.new
end
Expand All @@ -48,22 +50,37 @@ def call(filename, writemode)
end
end

it "should fetch basefiles" do
Dir.stub :[], DirMock.new do
Basefile.stub :new, basefile do
renderer.basefiles.must_equal [basefile]
end
end
end

it "should render a file" do
metadata.expect(:update_checksum, true, [basefile])
File.stub :open, FileMock.new do
Basefile.stub :new, basefile do
renderer.render_file RENDER_FILE
Metadata.stub :new, metadata do
renderer.render_file basefile
end
end
end
metadata.verify
end

it "should render files" do
metadata.expect(:update_checksum, true, [basefile])
File.stub :open, FileMock.new do
Dir.stub :[], DirMock.new do
Basefile.stub :new, basefile do
renderer.render_files
Metadata.stub :new, metadata do
renderer.render_files
end
end
end
end
metadata.verify
end

end

0 comments on commit eeb4558

Please sign in to comment.