Class: Epuber::Command::Compile

Inherits:
Epuber::Command show all
Defined in:
lib/epuber/command/compile.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Epuber::Command

run

Constructor Details

#initialize(argv) ⇒ Compile

Returns a new instance of Compile.

Parameters:

  • argv (CLAide::ARGV)


27
28
29
30
31
32
33
34
# File 'lib/epuber/command/compile.rb', line 27

def initialize(argv)
  @targets_names = argv.arguments!
  @should_check = argv.flag?('check', false)
  @should_write = argv.flag?('write', false)
  @release_version = argv.flag?('release', false)

  super(argv)
end

Class Method Details

.optionsObject



17
18
19
20
21
22
23
# File 'lib/epuber/command/compile.rb', line 17

def self.options
  [
    ['--check',   'Performs additional validation on sources + checks result epub with epubcheck.'],
    ['--write',   'Performs additional transformations which writes to source files.'],
    ['--release', 'Create release version of the book, no caching, everything creates from scratch.'],
  ].concat(super)
end

Instance Method Details

#runObject



42
43
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
# File 'lib/epuber/command/compile.rb', line 42

def run
  super

  puts "compiling book `#{Config.instance.pretty_path_from_project(book.file_path)}`"

  if @release_version
    # Remove all previous versions of compiled files
    targets.each do |target|
      build_path = Epuber::Config.instance.release_build_path(target)

      if ::File.directory?(build_path)
        FileUtils.remove_dir(build_path, true)
      end
    end

    # Build all targets to always clean directory
    targets.each do |target|
      compiler = Epuber::Compiler.new(book, target)
      compiler.compile(Epuber::Config.instance.release_build_path(target), check: true, write: @should_write, release: true, verbose: verbose?)

      archive_name = compiler.epub_name

      if ::File.exists?(archive_name)
        FileUtils.remove_file(archive_name)
      end

      archive_path = compiler.archive(archive_name)

      system(%(epubcheck "#{archive_path}"))

      convert_epub_to_mobi(archive_path, ::File.basename(archive_path, '.epub') + '.mobi') if target.create_mobi
    end
  else
    targets.each do |target|
      compiler = Epuber::Compiler.new(book, target)
      compiler.compile(Epuber::Config.instance.build_path(target), check: @should_check, write: @should_write, verbose: verbose?)
      archive_path = compiler.archive(configuration_suffix: 'debug')

      system(%(epubcheck "#{archive_path}")) if @should_check

      convert_epub_to_mobi(archive_path, ::File.basename(archive_path, '.epub') + '.mobi') if target.create_mobi
    end
  end
end

#validate!Object



36
37
38
39
40
# File 'lib/epuber/command/compile.rb', line 36

def validate!
  super
  verify_one_bookspec_exists!
  verify_all_targets_exists!
end