class Epuber::Compiler

Constants

EPUB_CONTENT_FOLDER

Attributes

compilation_context[R]

@return [CompilationContext]

file_resolver[R]

@return [Epuber::Compiler::FileResolver]

Public Class Methods

globals_catcher() click to toggle source

@return [Epuber::GlobalsContext]

# File lib/epuber/compiler.rb, line 34
def self.globals_catcher
  @globals_catcher ||= Epuber::GlobalsContext.new
end
new(book, target) click to toggle source

@param book [Epuber::Book::Book] @param target [Epuber::Book::Target]

# File lib/epuber/compiler.rb, line 49
def initialize(book, target)
  @book = book
  @target = target
  @compilation_context = CompilationContext.new(book, target)
end

Public Instance Methods

archive(path = nil, configuration_suffix: nil) click to toggle source

Archives current target files to epub

@param path [String] path to created archive

@return [String] path

# File lib/epuber/compiler.rb, line 112
def archive(path = nil, configuration_suffix: nil)
  path ||= epub_name(configuration_suffix)

  epub_path = File.expand_path(path)

  Dir.chdir(@file_resolver.destination_path) do
    new_paths = @file_resolver.package_files.map(&:pkg_destination_path)

    if ::File.exists?(epub_path)
      Zip::File.open(epub_path, true) do |zip_file|
        old_paths = zip_file.instance_eval { @entry_set.entries.map(&:name) }
        diff = old_paths - new_paths
        diff.each do |file_to_remove|
          puts "DEBUG: removing file from result EPUB: #{file_to_remove}" if compilation_context.verbose?
          zip_file.remove(file_to_remove)
        end
      end
    end

    run_command(%(zip -q0X "#{epub_path}" mimetype))
    run_command(%(zip -qXr9D "#{epub_path}" "#{new_paths.join('" "')}" --exclude \\*.DS_Store))
  end

  path
end
compile(build_folder, check: false, write: false, release: false, verbose: false, use_cache: true) click to toggle source

Compile target to build folder

@param build_folder [String] path to folder, where will be stored all compiled files @param [Bool] check should run non-release checkers @param [Bool] write should perform transformations of source files and write them back @param [Bool] release this is release build

@return [void]

# File lib/epuber/compiler.rb, line 64
def compile(build_folder, check: false, write: false, release: false, verbose: false, use_cache: true)
  @file_resolver = FileResolver.new(Config.instance.project_path, build_folder)
  compilation_context.file_resolver = @file_resolver
  compilation_context.should_check = check
  compilation_context.should_write = write
  compilation_context.release_build = release
  compilation_context.verbose = verbose
  compilation_context.use_cache = use_cache

  self.class.globals_catcher.catch do
    @build_folder = build_folder

    FileUtils.mkdir_p(build_folder)

    puts "  handling target #{@target.name.inspect} in build dir `#{Config.instance.pretty_path_from_project(build_folder)}`"

    file_resolver.add_file(FileTypes::SourceFile.new(Config.instance.pretty_path_from_project(@book.file_path).to_s))
    compilation_context.plugins

    parse_toc_item(@target.root_toc)
    parse_target_file_requests

    process_all_target_files
    generate_other_files

    # build folder cleanup
    remove_unnecessary_files
    remove_empty_folders

    source_paths = file_resolver.files.select { |a| a.is_a?(FileTypes::SourceFile) }.map { |a| a.source_path }
    compilation_context.source_file_database.cleanup(source_paths)
    compilation_context.source_file_database.update_all_metadata
    compilation_context.source_file_database.save_to_file

    compilation_context.target_file_database.cleanup(source_paths)
    compilation_context.target_file_database.update_all_metadata
    compilation_context.target_file_database.save_to_file
  end
ensure
  self.class.globals_catcher.clear_all
end
epub_name(configuration_suffix = nil) click to toggle source

Creates name of epub file for current book and current target

@return [String] name of result epub file

# File lib/epuber/compiler.rb, line 142
def epub_name(configuration_suffix = nil)
  epub_name = if !@book.output_base_name.nil?
                @book.output_base_name
              elsif @book.from_file?
                ::File.basename(@book.file_path, ::File.extname(@book.file_path))
              else
                @book.title
              end

  epub_name += @book.build_version.to_s unless @book.build_version.nil?
  epub_name += "-#{@target.name}" if @target != @book.default_target
  epub_name += "-#{configuration_suffix}" unless configuration_suffix.nil?
  epub_name + '.epub'
end

Private Instance Methods

generate_other_files() click to toggle source

@return nil

# File lib/epuber/compiler.rb, line 187
def generate_other_files
  # generate nav file (nav.xhtml or nav.ncx)
  nav_file = FileTypes::NavFile.new(@target.epub_version)
  @file_resolver.add_file(nav_file)
  process_file(nav_file)

  # generate .opf file
  opf_file = FileTypes::OPFFile.new
  @file_resolver.add_file(opf_file)
  process_file(opf_file)

  # generate mimetype file
  mimetype_file = FileTypes::MimeTypeFile.new
  @file_resolver.add_file(mimetype_file)
  process_file(mimetype_file)

  # generate META-INF files
  container_xml = FileTypes::ContainerXMLFile.new
  @file_resolver.add_file(container_xml)
  process_file(container_xml)

  if @target.epub_version >= 2.0 && @target.epub_version < 3.0
    ibooks_options = FileTypes::IBooksDisplayOptionsFile.new
    @file_resolver.add_file(ibooks_options)
    process_file(ibooks_options)
  end
end
parse_target_file_requests() click to toggle source

@return nil

# File lib/epuber/compiler.rb, line 217
def parse_target_file_requests
  @target.files.each do |file_request|
    @file_resolver.add_file_from_request(file_request)
  end
end
parse_toc_item(toc_item) click to toggle source

@param toc_item [Epuber::Book::TocItem]

# File lib/epuber/compiler.rb, line 281
def parse_toc_item(toc_item)
  unless toc_item.file_request.nil?
    file = @file_resolver.add_file_from_request(toc_item.file_request, :spine)
    file.toc_item = toc_item if file.toc_item.nil?
  end

  # process recursively other files
  toc_item.sub_items.each do |child|
    parse_toc_item(child)
  end
end
process_all_target_files() click to toggle source

@return nil

# File lib/epuber/compiler.rb, line 270
def process_all_target_files
  @file_resolver.manifest_files.each_with_index do |file, idx|
    UI.print_processing_file(file, idx, @file_resolver.manifest_files.count)
    process_file(file)
  end

  UI.processing_files_done
end
process_file(file) click to toggle source

@param [Epuber::Compiler::FileTypes::AbstractFile] file

# File lib/epuber/compiler.rb, line 225
def process_file(file)
  file.compilation_context = compilation_context

  resolve_dependencies(file) if file.is_a?(FileTypes::SourceFile)
  file.process(compilation_context)

  file.compilation_context = nil
end
remove_empty_folders() click to toggle source

@return nil

# File lib/epuber/compiler.rb, line 162
def remove_empty_folders
  Dir.chdir(@file_resolver.destination_path) do
    Dir.glob('**/*')
      .select { |d| File.directory?(d) }
      .select { |d| (Dir.entries(d) - %w(. ..)).empty? }
      .each do |d|
      puts "DEBUG: removing empty folder `#{d}`" if compilation_context.verbose?
      Dir.rmdir(d)
    end
  end
end
remove_unnecessary_files() click to toggle source

@return nil

# File lib/epuber/compiler.rb, line 176
def remove_unnecessary_files
  unnecessary_paths = @file_resolver.unneeded_files_in_destination.map { |path| File.join(@file_resolver.destination_path, path) }
  unnecessary_paths.each do |path|
    puts "DEBUG: removing unnecessary file: `#{Config.instance.pretty_path_from_project(path)}`" if compilation_context.verbose?

    File.delete(path)
  end
end
resolve_dependencies(file) click to toggle source

@param [FileTypes::SourceFile] file

# File lib/epuber/compiler.rb, line 236
def resolve_dependencies(file)
  deps = file.find_dependencies

  # compute better paths for FileDatabase
  dirname = File.dirname(file.source_path)
  paths = deps.map { |relative| Config.instance.pretty_path_from_project(File.expand_path(relative, dirname)) }.uniq

  # add missing files to file_resolver
  paths.each do |path|
    next if file_resolver.file_with_source_path(path)
    file_resolver.add_file(FileTypes::SourceFile.new(path))
  end

  # add .bookspec file
  paths += [Config.instance.pretty_path_from_project(@book.file_path).to_s]

  # add all activated plugin files
  paths += compilation_context.plugins.map do |plugin|
    plugin.files.map { |p_file| p_file.source_path }
  end.flatten

  # add dependencies to databases
  source_db = compilation_context.source_file_database
  source_db.update_metadata(file.source_path) unless source_db.file_stat_for(file.source_path)
  source_db.add_dependency(paths, to: file.source_path)

  # add dependencies to databases
  target_db = compilation_context.target_file_database
  target_db.update_metadata(file.source_path) unless target_db.file_stat_for(file.source_path)
  target_db.add_dependency(paths, to: file.source_path)
end
run_command(cmd) click to toggle source

@param cmd [String]

@return [void]

@raise if the return value is not 0

# File lib/epuber/compiler.rb, line 299
def run_command(cmd)
  system(cmd)

  $stdout.flush
  $stderr.flush

  code = $CHILD_STATUS
  raise 'wrong return value' if code != 0
end