diff options
author | Scott Chacon <schacon@gmail.com> | 2012-12-11 05:59:35 +0400 |
---|---|---|
committer | Scott Chacon <schacon@gmail.com> | 2012-12-11 06:02:39 +0400 |
commit | f1ddc1653a9ef40086ef934f5c3b877a5e6510bf (patch) | |
tree | 7e2e0aabb6e8e45c3c6169455e31491840823cb1 | |
parent | b101dca1933e7cbe5fa323fa05b21770d79bf3de (diff) |
make book regen more efficient
- save shas of each chapter so we don't reprocess stuff that hasn't
changed
- save directly to the database since we're running on the server now
- remove old remote task
- use logged in user so we have more API calls
-rw-r--r-- | db/migrate/20121211011752_add_chapter_shas.rb | 9 | ||||
-rw-r--r-- | db/schema.rb | 3 | ||||
-rw-r--r-- | lib/tasks/book.rake | 62 |
3 files changed, 51 insertions, 23 deletions
diff --git a/db/migrate/20121211011752_add_chapter_shas.rb b/db/migrate/20121211011752_add_chapter_shas.rb new file mode 100644 index 00000000..0d1344e3 --- /dev/null +++ b/db/migrate/20121211011752_add_chapter_shas.rb @@ -0,0 +1,9 @@ +class AddChapterShas < ActiveRecord::Migration + def up + add_column :chapters, :sha, :string + end + + def down + remove_column :chapters, :string + end +end
\ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 78e53d43..8549b90f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120925124736) do +ActiveRecord::Schema.define(:version => 20121211011752) do create_table "books", :force => true do |t| t.string "code" @@ -27,6 +27,7 @@ ActiveRecord::Schema.define(:version => 20120925124736) do t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.integer "number" + t.string "sha" end add_index "chapters", ["book_id"], :name => "index_chapters_on_book_id" diff --git a/lib/tasks/book.rake b/lib/tasks/book.rake index cc89025e..c43e30f3 100644 --- a/lib/tasks/book.rake +++ b/lib/tasks/book.rake @@ -9,7 +9,7 @@ require 'octokit' CONTENT_SERVER = ENV["CONTENT_SERVER"] || "http://localhost:3000" -def generate_pages(lang, chapter, content) +def generate_pages(lang, chapter, content, sha) toc = {:title => '', :sections => []} markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) @@ -64,20 +64,22 @@ def generate_pages(lang, chapter, content) nav = '<div id="nav"><a href="[[nav-prev]]">prev</a> | <a href="[[nav-next]]">next</a></div>' html += nav - data = { - :lang => lang, - :chapter => chapter, - :section => section, - :chapter_title => chapter_title, - :section_title => section_title, - :content => html, - :token => ENV['UPDATE_TOKEN'] - } - - url = CONTENT_SERVER + "/publish" - result = Nestful.post url, :format => :form, :params => data - - puts "\t\t#{chapter}.#{section} : #{chapter_title} . #{section_title} - #{html.size} (#{result})" + # create book (if needed) + book = Book.where(:code => lang).first_or_create + + # create chapter (if needed) + schapter = book.chapters.where(:number => chapter).first_or_create + schapter.title = chapter_title.to_s + schapter.sha = sha + schapter.save + + # create/update section + csection = schapter.sections.where(:number => section).first_or_create + csection.title = section_title.to_s + csection.html = html.to_s + csection.save + + puts "\t\t#{chapter}.#{section} : #{chapter_title} . #{section_title} - #{html.size}" section += 1 end @@ -86,24 +88,40 @@ end desc "Generate the book html for the sites (Using Octokit gem)" task :remote_genbook => :environment do + @octokit = Octokit::Client.new(:login => ENV['API_USER'], :password => ENV['API_PASS']) repo = 'progit/progit' book = {} blob_content = Hash.new do |blobs, sha| - content = Base64.decode64( Octokit.blob( repo, sha, :encoding => 'base64' ).content ) + content = Base64.decode64( @octokit.blob( repo, sha, :encoding => 'base64' ).content ) blobs[sha] = content.encode( 'utf-8', :undef => :replace ) end - repo_tree = Octokit.tree(repo, "HEAD", :recursive => true) + repo_tree = @octokit.tree(repo, "HEAD", :recursive => true) trees = repo_tree.tree.map {|tree| tree if tree.path =~ /\.markdown$/}.compact trees.each do |tree| #tree = trees.first lang, section, chapter = tree.path.split("/") section_number = $1 if section =~ /^(\d+)/ chapter_number = $1 if chapter =~ /chapter(\d+)\.markdown/ - #book[lang] ||= [] - content = blob_content[tree.sha] - puts "*** #{tree.sha}- #{lang} - #{section} - #{chapter} - #{section_number}:#{chapter_number}" - #book[lang] << {:sha => tree.sha, :section => section, :chapter => chapter, :blob => content} - generate_pages(lang, chapter_number, content) + + skip = false + + if book = Book.where(:code => lang).first + c = book.chapters.where(:number => chapter_number.to_i).first + if c && (c.sha == tree.sha) + skip = true + end + end + + skip = true if chapter_number.to_i == 0 + + puts "*** #{skip} #{tree.sha}- #{lang} - #{section} - #{chapter} - #{section_number}:#{chapter_number}" + + if !skip + #book[lang] ||= [] + content = blob_content[tree.sha] + #book[lang] << {:sha => tree.sha, :section => section, :chapter => chapter, :blob => content} + generate_pages(lang, chapter_number, content, tree.sha) + end end #p book end |