Class: Bookshop::Commands::Build

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/bookshop/commands/build.rb

Overview

Define build commands for bookshop command line

Constant Summary collapse

BOOK_SOURCE =
'book.html.erb'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuild

Returns a new instance of Build.



18
19
20
# File 'lib/bookshop/commands/build.rb', line 18

def initialize
  @book = []
end

Class Method Details

.build_epubObject



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
# File 'lib/bookshop/commands/build.rb', line 44

def self.build_epub
  clean_builds('epub')
  
  @output = :epub

  FileUtils.cp_r('book/epub/META-INF', 'builds/epub/', :verbose => true)
  FileUtils.mkdir 'builds/epub/OEBPS'
  FileUtils.cp_r('book/epub/mimetype', 'builds/epub/', :verbose => true)
  
  puts "Generating new html from erb"
  generate_file(BOOK_SOURCE, "builds/epub/OEBPS/book.html")
          
  # Generate the cover.html file
  puts "Generating new cover.html from erb"
  generate_file("frontmatter/cover.html.erb", "builds/epub/OEBPS/cover.html")
          
  # Generate the nav.html file
  puts "Generating new toc.html from erb"
  generate_file("frontmatter/toc.html.erb", "builds/epub/OEBPS/toc.html")
  
  # Generate the OPF file
  puts "Generating new content.opf from erb"
  generate_file("epub/OEBPS/content.opf.erb", "builds/epub/OEBPS/content.opf")
         
  # Generate the NCX file
  puts "Generating new toc.ncx from erb"
  generate_file("epub/OEBPS/toc.ncx.erb", "builds/epub/OEBPS/toc.ncx")
          
  FileUtils.cp_r 'book/assets', 'builds/epub/OEBPS/assets/', :verbose => true
  FileUtils.rm %w( builds/epub/OEBPS/assets/css/stylesheet.pdf.css
                   builds/epub/OEBPS/assets/css/stylesheet.html.css
                   builds/epub/OEBPS/assets/css/stylesheet.mobi.css )
  
  puts "Zipping up into epub"
  if RUBY_PLATFORM =~ /linux|darwin|cygwin/
    cmd = system("cd builds/epub/ && zip -X0 'book.epub' mimetype && zip -rDX9 'book.epub' * -x '*.DS_Store' -x mimetype")
  elsif RUBY_PLATFORM =~ /mingw|mswin32/
    cmd = system("cd builds/epub/ & zip.exe -X0 book.epub mimetype & zip.exe -rDX9 book.epub * -x mimetype")
  end
  
  puts "Validating with epubcheck"
  cmd = system("epubcheck builds/epub/book.epub")

end

.build_htmlObject



34
35
36
37
38
39
40
41
42
# File 'lib/bookshop/commands/build.rb', line 34

def self.build_html 
  clean_builds( 'html' )  
  @output = :html
  
  puts "Generating new html from erb"
  generate_file(BOOK_SOURCE, "builds/html/book.html")
          
  FileUtils.cp_r('book/assets/', 'builds/html/', :verbose => true)
end

.build_mobiObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bookshop/commands/build.rb', line 89

def self.build_mobi
  clean_builds('mobi')
  @output = :mobi

  FileUtils.cp_r('book/epub/META-INF', 'builds/mobi/', :verbose => true)
  FileUtils.mkdir 'builds/mobi/OEBPS'
  FileUtils.cp_r('book/epub/mimetype', 'builds/mobi/', :verbose => true)
  
  puts "Generating new html from erb"
  generate_file(BOOK_SOURCE, "builds/mobi/OEBPS/book.html")
        
  # Generate the nav.html file
  puts "Generating new toc.html from erb"
  generate_file("frontmatter/toc.html.erb", "builds/mobi/OEBPS/toc.html")
 
  # Generate the OPF file
  puts "Generating new content.opf from erb"
  generate_file("epub/OEBPS/content.opf.erb","builds/mobi/OEBPS/content.opf")
    
  # Generate the NCX file
  puts "Generating new toc.ncx from erb"
  generate_file("epub/OEBPS/toc.ncx.erb","builds/mobi/OEBPS/toc.ncx")
  
  FileUtils.cp_r 'book/assets', 'builds/mobi/OEBPS/assets/', :verbose => true
  FileUtils.rm %w( builds/mobi/OEBPS/assets/css/stylesheet.pdf.css
                   builds/mobi/OEBPS/assets/css/stylesheet.html.css
                   builds/mobi/OEBPS/assets/css/stylesheet.epub.css )
  
  puts "Zipping up into epub"
  if RUBY_PLATFORM =~ /linux|darwin|cygwin/
   cmd = system("cd builds/mobi/ && zip -X0 'book.epub' mimetype && zip -rDX9 'book.epub' * -x '*.DS_Store' -x mimetype")
  elsif RUBY_PLATFORM =~ /mingw|mswin32/
   cmd = system("cd builds/mobi/ & zip.exe -X0 'book.epub' mimetype & zip.exe -rDX9 'book.epub' * -x mimetype")
  end
  
  # Validate with Epubcheck
  puts "Validating with epubcheck"
  cmd  = system("epubcheck builds/mobi/book.epub")
  
  # Convert Epub to Mobi with Kindlegen
  puts "Generating mobi file with KindleGen"
  cmd = system("kindlegen builds/mobi/book.epub")
  
end

.build_pdfObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bookshop/commands/build.rb', line 134

def self.build_pdf
  # Clean up any old builds
  clean_builds("pdf") 
  @output = :pdf
  # Generate the html from ERB
  puts "Generating new html from erb"
  generate_file(BOOK_SOURCE, 'builds/pdf/book.html')
  
  # Copy over html assets
  FileUtils.cp_r('book/assets/', 'builds/pdf/', :verbose => true)

  # Builds the pdf from builds/html/book.html
  puts "Building new pdf at builds/pdf/book.pdf from new html build"
  cmd = system("prince -v builds/pdf/book.html -o builds/pdf/book.pdf")
  
end

.clean_builds(build_type) ⇒ Object



22
23
24
25
# File 'lib/bookshop/commands/build.rb', line 22

def self.clean_builds(build_type)
  puts "Deleting any old builds"
  FileUtils.rm_r Dir.glob("builds/#{build_type}/*"), :verbose => true
end

.generate_file(src, dest) ⇒ Object



27
28
29
30
31
32
# File 'lib/bookshop/commands/build.rb', line 27

def self.generate_file(src, dest)
  src= import(src)
  File.open(dest, 'a') do |f|
    f << src
  end
end

.import(file) ⇒ Object

Renders <%= import(source.html.erb) %> files with ERB

When a new import() is encountered within source files it is

processed with this method and the result is added to 'erb'


180
181
182
183
184
# File 'lib/bookshop/commands/build.rb', line 180

def self.import(file)
  load_yaml_files
  # Parse the source erb file
  ERB.new(File.read('book/'+file)).result(binding).gsub(/\n$/,'')
end

.load_yaml_filesObject

Load YAML files



171
172
173
174
# File 'lib/bookshop/commands/build.rb', line 171

def self.load_yaml_files
  # Load the book.yml into the Book object
  @book = Book.new(YAML.load_file('config/book.yml'))
end

.source_rootObject

Define source root of application



166
167
168
# File 'lib/bookshop/commands/build.rb', line 166

def self.source_root
  File.dirname(__FILE__)
end