epuber 0.3.12 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/epuber.gemspec +13 -15
  4. data/lib/epuber/book.rb +35 -4
  5. data/lib/epuber/book/target.rb +41 -2
  6. data/lib/epuber/command.rb +5 -0
  7. data/lib/epuber/command/build.rb +155 -0
  8. data/lib/epuber/command/compile.rb +5 -131
  9. data/lib/epuber/command/init.rb +19 -7
  10. data/lib/epuber/command/server.rb +1 -1
  11. data/lib/epuber/compiler.rb +56 -2
  12. data/lib/epuber/compiler/compilation_context.rb +27 -1
  13. data/lib/epuber/compiler/file_database.rb +138 -0
  14. data/lib/epuber/compiler/file_finders/abstract.rb +3 -1
  15. data/lib/epuber/compiler/file_resolver.rb +6 -2
  16. data/lib/epuber/compiler/file_stat.rb +76 -0
  17. data/lib/epuber/compiler/file_types/abstract_file.rb +3 -37
  18. data/lib/epuber/compiler/file_types/bade_file.rb +63 -4
  19. data/lib/epuber/compiler/file_types/coffee_script_file.rb +24 -0
  20. data/lib/epuber/compiler/file_types/generated_file.rb +3 -1
  21. data/lib/epuber/compiler/file_types/image_file.rb +6 -4
  22. data/lib/epuber/compiler/file_types/source_file.rb +63 -1
  23. data/lib/epuber/compiler/file_types/static_file.rb +2 -2
  24. data/lib/epuber/compiler/file_types/stylus_file.rb +15 -0
  25. data/lib/epuber/compiler/file_types/xhtml_file.rb +71 -13
  26. data/lib/epuber/compiler/opf_generator.rb +1 -0
  27. data/lib/epuber/compiler/xhtml_processor.rb +94 -17
  28. data/lib/epuber/config.rb +47 -14
  29. data/lib/epuber/plugin.rb +6 -1
  30. data/lib/epuber/server.rb +35 -11
  31. data/lib/epuber/server/handlers.rb +2 -1
  32. data/lib/epuber/server/pages/book.bade +6 -2
  33. data/lib/epuber/server/pages/common.bade +5 -7
  34. data/lib/epuber/user_interface.rb +36 -4
  35. data/lib/epuber/vendor/version.rb +2 -2
  36. data/lib/epuber/version.rb +3 -1
  37. metadata +49 -57
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 187fa111e9fda8094a8780e1090e6284264f63bf
4
- data.tar.gz: b4ac53c6d3320cc7a3a25a235f9c0296c954458c
3
+ metadata.gz: 43b004b5cbf151d828970b67bdad742b4199629e
4
+ data.tar.gz: f763945ec366475789ba83af1945393478beef8f
5
5
  SHA512:
6
- metadata.gz: 8b0ff50f01876bbbcbafbc2bfb4dce60bf12b7a71c7a41fc72cbfe1d0ca2391a7301a4ef1ae27008af5975fd6268c24aba72189ae16942c2e362dde9ae8fb2bf
7
- data.tar.gz: 05179e69736eae612b6f68b9bb06cf3064ef81e8c81f4edd8336994379e6ee8adbeca2021358e1380e74a68158c83e51f73b50bbb1ffb1f67bb6f0f061d38661
6
+ metadata.gz: f99f327f37f466b5233d32b324b5ff47b71564c64f9a485fb9e4ae0d9070d4be61c23f1860341c5e7cbff0a16715de5d9592b978c786333d63cae6d7d77f11d2
7
+ data.tar.gz: 95abd127c5d4c38a0fb12deaf728c44893014e36410a119e6b37000e3571fcc78b9268636ae0076fb30d3a6c55d291e7b3175b320fabd63551c07d712c6f924b
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Epuber
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/epuber.svg)](http://badge.fury.io/rb/epuber) [![Build Status](https://travis-ci.org/epuber-io/epuber.svg?branch=master)](https://travis-ci.org/epuber-io/epuber) [![Coverage Status](https://coveralls.io/repos/epuber-io/epuber/badge.svg?branch=master&service=github)](https://coveralls.io/github/epuber-io/epuber?branch=master) [![Inline docs](http://inch-ci.org/github/epuber-io/epuber.svg?branch=master)](http://inch-ci.org/github/epuber-io/epuber)
3
+ [![Gem Version](https://badge.fury.io/rb/epuber.svg)](http://badge.fury.io/rb/epuber) [![Build Status](https://travis-ci.org/epuber-io/epuber.svg?branch=master)](https://travis-ci.org/epuber-io/epuber) [![Coverage Status](https://coveralls.io/repos/epuber-io/epuber/badge.svg?branch=master&service=github)](https://coveralls.io/github/epuber-io/epuber?branch=master) [![Inline docs](http://inch-ci.org/github/epuber-io/epuber.svg?branch=master)](http://inch-ci.org/github/epuber-io/epuber) [![Join the chat at https://gitter.im/epuber-io/epuber](https://badges.gitter.im/epuber-io/epuber.svg)](https://gitter.im/epuber-io/epuber?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
4
 
5
5
  Epuber is simple tool to compile and pack source files into EPUB format. Offers to define multiple "targets" which allows to create multiple versions of the book by running only one command. Eliminates copying code and information, eliminates needs to use _git_ branches to differ ebook content.
6
6
 
data/epuber.gemspec CHANGED
@@ -12,41 +12,39 @@ Gem::Specification.new do |spec|
12
12
  spec.authors = ['Roman Kříž']
13
13
  spec.email = ['samnung@gmail.com']
14
14
  spec.summary = 'Epuber is simple tool to compile and pack source files into EPUB format.'
15
- spec.homepage = 'https://github.com/epuber-io/epuber'
15
+ spec.homepage = Epuber::HOME_URL
16
16
  spec.license = 'MIT'
17
- spec.required_ruby_version = '>= 2.0'
17
+ spec.required_ruby_version = '>= 2.2'
18
18
 
19
19
  spec.files = Dir['bin/**/*'] + Dir['lib/**/*'] + %w(epuber.gemspec Gemfile LICENSE.txt README.md)
20
20
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
22
  spec.require_paths = ['lib']
23
23
 
24
- spec.add_runtime_dependency 'activesupport', '~> 4.2'
24
+ spec.add_runtime_dependency 'activesupport', '~> 5.0'
25
25
  spec.add_runtime_dependency 'nokogiri', '~> 1.6'
26
26
  spec.add_runtime_dependency 'mime-types', '~> 3.0'
27
- spec.add_runtime_dependency 'claide', '~> 0.8'
27
+ spec.add_runtime_dependency 'claide', '~> 1.0'
28
28
  spec.add_runtime_dependency 'listen', '~> 3.0'
29
- spec.add_runtime_dependency 'os', '~> 0.9'
29
+ spec.add_runtime_dependency 'os', '~> 1.0'
30
30
 
31
- spec.add_runtime_dependency 'epuber-stylus', '~> 1.0'
32
- spec.add_runtime_dependency 'coffee-script', '~> 2.4'
33
-
34
- spec.add_runtime_dependency 'sinatra', '~> 1.4'
31
+ spec.add_runtime_dependency 'sinatra', '~> 2.0'
35
32
  spec.add_runtime_dependency 'sinatra-websocket', '~> 0.3'
36
- spec.add_runtime_dependency 'sinatra-contrib', '~> 1.4'
33
+ spec.add_runtime_dependency 'sinatra-contrib', '~> 2.0'
37
34
  spec.add_runtime_dependency 'thin', '~> 1.6'
38
35
 
39
36
  spec.add_runtime_dependency 'rmagick', '~> 2.14'
40
-
41
37
  spec.add_runtime_dependency 'rubyzip', '~> 1.0'
42
38
 
43
- spec.add_runtime_dependency 'epubcheck', '~> 3.0', '>= 3.0.1'
39
+ spec.add_runtime_dependency 'epubcheck', '~> 3.0'
44
40
 
45
- spec.add_runtime_dependency 'bade', '>= 0.1.3', '< 1.0.0'
41
+ spec.add_runtime_dependency 'epuber-stylus', '~> 1.1.1'
42
+ spec.add_runtime_dependency 'coffee-script', '~> 2.4'
43
+ spec.add_runtime_dependency 'bade', '~> 0.2'
46
44
 
47
- spec.add_development_dependency 'bundler', '~> 1'
45
+ spec.add_development_dependency 'bundler', '~> 1.15'
48
46
  spec.add_development_dependency 'rspec', '~> 3.2'
49
47
  spec.add_development_dependency 'rubocop', '~> 0.29'
50
- spec.add_development_dependency 'rake', '~> 11'
48
+ spec.add_development_dependency 'rake', '~> 12.2'
51
49
  spec.add_development_dependency 'fakefs', '~> 0.6'
52
50
  end
data/lib/epuber/book.rb CHANGED
@@ -78,7 +78,7 @@ module Epuber
78
78
  #
79
79
  # @return [Array<Target>]
80
80
  #
81
- def targets
81
+ def all_targets
82
82
  if @default_target.sub_targets.length == 0
83
83
  [@default_target]
84
84
  else
@@ -96,6 +96,8 @@ module Epuber
96
96
 
97
97
  # Defines new target
98
98
  #
99
+ # @param [String, Symbol] name
100
+ #
99
101
  # @return [Target] result target
100
102
  #
101
103
  def target(name)
@@ -105,6 +107,21 @@ module Epuber
105
107
  end
106
108
  end
107
109
 
110
+ # Defines several new targets with same configuration
111
+ #
112
+ # @param [Array<String, Symbol>] names
113
+ #
114
+ # @return [Array<Target>] result target
115
+ #
116
+ def targets(*names, &block)
117
+ if names.empty?
118
+ UI.warning('Book#targets to get all targets is deprecated, use #all_targets instead', location: caller_locations.first)
119
+ return all_targets
120
+ end
121
+
122
+ names.map { |name| target(name, &block) }
123
+ end
124
+
108
125
 
109
126
  #-------------- TOC --------------------------------------
110
127
 
@@ -197,13 +214,13 @@ module Epuber
197
214
  attribute :version,
198
215
  inherited: true,
199
216
  types: [Version],
200
- auto_convert: { [String, Fixnum, Float] => Version }
217
+ auto_convert: { [String, Integer, Float] => Version }
201
218
 
202
219
  # @return [String] build version of book
203
220
  #
204
221
  attribute :build_version,
205
222
  types: [Version],
206
- auto_convert: { [String, Fixnum, Float] => Version }
223
+ auto_convert: { [String, Integer, Float] => Version }
207
224
 
208
225
  # @return [String] base name for output epub file
209
226
  #
@@ -242,6 +259,20 @@ module Epuber
242
259
  @default_target.add_default_styles(*file_paths)
243
260
  end
244
261
 
262
+ # Add default script to default target, default scripts will be automatically added to xhtml document
263
+ #
264
+ def add_default_script(*file_paths)
265
+ @default_target.add_default_script(*file_paths)
266
+ end
267
+
268
+ # Add default scripts to default target, default scripts will be automatically added to xhtml document
269
+ #
270
+ def add_default_scripts(*file_paths)
271
+ @default_target.add_default_scripts(*file_paths)
272
+ end
273
+
274
+ # Method to add plugin, that should be used while building book
275
+ #
245
276
  def use(path)
246
277
  @default_target.use(path)
247
278
  end
@@ -260,7 +291,7 @@ module Epuber
260
291
  def target_named(target_name)
261
292
  return target_name if target_name.is_a?(Epuber::Book::Target)
262
293
 
263
- targets.find do |target|
294
+ all_targets.find do |target|
264
295
  target.name == target_name || target.name.to_s == target_name.to_s
265
296
  end
266
297
  end
@@ -24,6 +24,7 @@ module Epuber
24
24
  @root_toc = TocItem.new
25
25
 
26
26
  @default_styles = []
27
+ @default_scripts = []
27
28
  @plugins = []
28
29
  end
29
30
 
@@ -35,6 +36,9 @@ module Epuber
35
36
  @default_styles.freeze
36
37
  @default_styles.each(&:freeze)
37
38
 
39
+ @default_scripts.freeze
40
+ @default_scripts.each(&:freeze)
41
+
38
42
  @plugins.freeze
39
43
  @plugins.each(&:freeze)
40
44
 
@@ -96,7 +100,7 @@ module Epuber
96
100
  #
97
101
  def files
98
102
  # parent files plus our files
99
- all_files = ((parent && parent.files) || []) + @files + @default_styles
103
+ all_files = ((parent && parent.files) || []) + @files + @default_styles + @default_scripts
100
104
 
101
105
  unless @attributes_values[:cover_image].nil?
102
106
  all_files << @attributes_values[:cover_image]
@@ -118,6 +122,12 @@ module Epuber
118
122
  ((parent && parent.default_styles) || []) + @default_styles
119
123
  end
120
124
 
125
+ # @return [Array<Epuber::Book::FileRequest>]
126
+ #
127
+ def default_scripts
128
+ ((parent && parent.default_scripts) || []) + @default_scripts
129
+ end
130
+
121
131
  # @return [Array<String>]
122
132
  #
123
133
  def plugins
@@ -134,7 +144,7 @@ module Epuber
134
144
  required: true,
135
145
  inherited: true,
136
146
  types: [Version],
137
- auto_convert: { [String, Fixnum, Float] => Version },
147
+ auto_convert: { [String, Integer, Float] => Version },
138
148
  default_value: 3.0
139
149
 
140
150
  # @return [String] isbn of epub
@@ -262,6 +272,35 @@ module Epuber
262
272
  end
263
273
  end
264
274
 
275
+ # @param file_paths [Array<String>]
276
+ #
277
+ # @return [void]
278
+ #
279
+ def add_default_script(*file_paths)
280
+ file_paths.map do |file_path|
281
+ file_obj = add_file(file_path, group: :script)
282
+ file_obj.only_one = true
283
+
284
+ @default_scripts << file_obj unless @default_scripts.include?(file_obj)
285
+ end
286
+ end
287
+
288
+ # Add default scripts to target, default scripts will be automatically added to xhtml document
289
+ #
290
+ # Only difference with #add_default_script is it adds multiple files with one pattern
291
+ # @param file_paths [Array<String>]
292
+ #
293
+ # @return [void]
294
+ #
295
+ def add_default_scripts(*file_paths)
296
+ file_paths.map do |file_path|
297
+ file_obj = add_file(file_path, group: :script)
298
+ file_obj.only_one = false
299
+
300
+ @default_scripts << file_obj unless @default_scripts.include?(file_obj)
301
+ end
302
+ end
303
+
265
304
  # @yield [toc_item, target]
266
305
  # @yieldparam toc_item [TocItem] root toc item
267
306
  # @yieldparam target [self] current target
@@ -12,6 +12,7 @@ module Epuber
12
12
  end
13
13
 
14
14
  class Command < CLAide::Command
15
+ require_relative 'command/build'
15
16
  require_relative 'command/compile'
16
17
  require_relative 'command/init'
17
18
  require_relative 'command/server'
@@ -46,8 +47,12 @@ module Epuber
46
47
  UI.current_command = self
47
48
  end
48
49
 
50
+ attr_reader :debug_steps_times
51
+
49
52
  protected
50
53
 
54
+ attr_writer :debug_steps_times
55
+
51
56
  # @return [Epuber::Book::Book]
52
57
  #
53
58
  def book
@@ -0,0 +1,155 @@
1
+ # encoding: utf-8
2
+
3
+ require 'fileutils'
4
+ require 'os'
5
+
6
+ require_relative '../command'
7
+
8
+
9
+ module Epuber
10
+ class Command
11
+ class Build < Command
12
+ require_relative 'compile'
13
+
14
+ self.summary = 'Build targets and produces output files.'
15
+ self.arguments = [
16
+ CLAide::Argument.new('TARGETS', false, true),
17
+ ]
18
+
19
+ def self.options
20
+ [
21
+ ['--check', 'Performs additional validation on sources + checks result epub with epubcheck.'],
22
+ ['--write', 'Performs additional transformations which writes to source files.'],
23
+ ['--release', 'Create release version of the book, no caching, everything creates from scratch.'],
24
+ ['--no-cache', 'Turns off incremental build, can resolve some bugs but build takes much longer.'],
25
+ ['--debug-steps-times', 'Shows times of each step'],
26
+ ].concat(super)
27
+ end
28
+
29
+ # @param argv [CLAide::ARGV]
30
+ #
31
+ def initialize(argv)
32
+ @targets_names = argv.arguments!
33
+ @should_check = argv.flag?('check', false)
34
+ @should_write = argv.flag?('write', false)
35
+ @release_version = argv.flag?('release', false)
36
+ @use_cache = argv.flag?('cache', true)
37
+
38
+ self.debug_steps_times = argv.flag?('debug-steps-times', false)
39
+
40
+ super(argv)
41
+ end
42
+
43
+ def validate!
44
+ super
45
+ verify_one_bookspec_exists!
46
+ verify_all_targets_exists!
47
+ end
48
+
49
+ def run
50
+ super
51
+
52
+ puts "compiling book `#{Config.instance.pretty_path_from_project(book.file_path)}`"
53
+
54
+ if @release_version
55
+ # Remove all previous versions of compiled files
56
+ targets.each do |target|
57
+ build_path = Epuber::Config.instance.release_build_path(target)
58
+
59
+ if ::File.directory?(build_path)
60
+ FileUtils.remove_dir(build_path, true)
61
+ end
62
+ end
63
+
64
+ # Build all targets to always clean directory
65
+ targets.each do |target|
66
+ compiler = Epuber::Compiler.new(book, target)
67
+ build_path = Epuber::Config.instance.release_build_path(target)
68
+ compiler.compile(build_path, check: true, write: @should_write,
69
+ release: true, verbose: verbose?,
70
+ use_cache: false)
71
+
72
+ archive_name = compiler.epub_name
73
+
74
+ if ::File.exists?(archive_name)
75
+ FileUtils.remove_file(archive_name)
76
+ end
77
+
78
+ archive_path = compiler.archive(archive_name)
79
+
80
+ system(%(epubcheck "#{archive_path}"))
81
+
82
+ convert_epub_to_mobi(archive_path, ::File.basename(archive_path, '.epub') + '.mobi') if target.create_mobi
83
+ end
84
+ else
85
+ targets.each do |target|
86
+ compiler = Epuber::Compiler.new(book, target)
87
+ build_path = Epuber::Config.instance.build_path(target)
88
+ compiler.compile(build_path, check: @should_check, write: @should_write, verbose: verbose?, use_cache: @use_cache)
89
+ archive_path = compiler.archive(configuration_suffix: 'debug')
90
+
91
+ system(%(epubcheck "#{archive_path}")) if @should_check
92
+
93
+ convert_epub_to_mobi(archive_path, ::File.basename(archive_path, '.epub') + '.mobi') if target.create_mobi
94
+ end
95
+ end
96
+
97
+ write_lockfile
98
+ end
99
+
100
+ private
101
+
102
+ # @return [Array<Epuber::Book::Target>]
103
+ #
104
+ def targets
105
+ @targets ||= (
106
+ targets = @targets_names
107
+
108
+ # when the list of targets is nil pick all available targets
109
+ if targets.empty?
110
+ book.all_targets
111
+ else
112
+ targets.map { |target_name| book.target_named(target_name) }
113
+ end
114
+ )
115
+ end
116
+
117
+ def verify_all_targets_exists!
118
+ index = targets.index(&:nil?)
119
+ help! "Not found target `#{@targets_names[index]}' in bookspec `#{book.file_path}'" unless index.nil?
120
+ end
121
+
122
+ def validate_dir(path)
123
+ if ::File.directory?(path)
124
+ path
125
+ end
126
+ end
127
+
128
+ def find_calibre_app
129
+ locations = `mdfind "kMDItemCFBundleIdentifier == net.kovidgoyal.calibre"`.split("\n")
130
+ UI.error!("Can't find location of calibre.app to convert EPUB to MOBI.") if locations.empty?
131
+
132
+ selected = locations.first
133
+ UI.warning("Found multiple calibre.app, using at location #{selected}") if locations.count > 1
134
+
135
+ selected
136
+ end
137
+
138
+ def find_calibre_convert_file
139
+ if OS.osx?
140
+ ::File.join(find_calibre_app, 'Contents/MacOS/ebook-convert')
141
+ elsif OS.linux?
142
+ script_path = '/usr/bin/ebook-convert'
143
+ UI.error!("Can't find ebook-convert in folder /usr/bin to convert EPUB to MOBI.") unless ::File.executable?(script_path)
144
+ script_path
145
+ else
146
+ UI.error!('Unsupported OS to convert EPUB to MOBI.')
147
+ end
148
+ end
149
+
150
+ def convert_epub_to_mobi(epub_path, mobi_path)
151
+ system(find_calibre_convert_file, epub_path, mobi_path)
152
+ end
153
+ end
154
+ end
155
+ end
@@ -1,143 +1,17 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'fileutils'
4
- require 'os'
5
-
6
- require_relative '../command'
7
-
3
+ require_relative 'build'
8
4
 
9
5
  module Epuber
10
6
  class Command
11
- class Compile < Command
12
- self.summary = 'Compile targets into multiple EPUB files.'
13
- self.arguments = [
14
- CLAide::Argument.new('TARGETS', false, true),
15
- ]
7
+ class Compile < Build
8
+ self.summary = '[Deprecated] Compile targets into multiple EPUB files. Use `build` instead.'
9
+ Command.inherited(self)
16
10
 
17
- def self.options
18
- [
19
- ['--check', 'Performs additional validation on sources + checks result epub with epubcheck.'],
20
- ['--write', 'Performs additional transformations which writes to source files.'],
21
- ['--release', 'Create release version of the book, no caching, everything creates from scratch.'],
22
- ].concat(super)
23
- end
24
-
25
- # @param argv [CLAide::ARGV]
26
- #
27
11
  def initialize(argv)
28
- @targets_names = argv.arguments!
29
- @should_check = argv.flag?('check', false)
30
- @should_write = argv.flag?('write', false)
31
- @release_version = argv.flag?('release', false)
32
-
33
- super(argv)
34
- end
12
+ UI.warning('Compile command is now deprecated, please use `build` command instead.')
35
13
 
36
- def validate!
37
14
  super
38
- verify_one_bookspec_exists!
39
- verify_all_targets_exists!
40
- end
41
-
42
- def run
43
- super
44
-
45
- puts "compiling book `#{Config.instance.pretty_path_from_project(book.file_path)}`"
46
-
47
- if @release_version
48
- # Remove all previous versions of compiled files
49
- targets.each do |target|
50
- build_path = Epuber::Config.instance.release_build_path(target)
51
-
52
- if ::File.directory?(build_path)
53
- FileUtils.remove_dir(build_path, true)
54
- end
55
- end
56
-
57
- # Build all targets to always clean directory
58
- targets.each do |target|
59
- compiler = Epuber::Compiler.new(book, target)
60
- compiler.compile(Epuber::Config.instance.release_build_path(target), check: true, write: @should_write, release: true, verbose: verbose?)
61
-
62
- archive_name = compiler.epub_name
63
-
64
- if ::File.exists?(archive_name)
65
- FileUtils.remove_file(archive_name)
66
- end
67
-
68
- archive_path = compiler.archive(archive_name)
69
-
70
- system(%(epubcheck "#{archive_path}"))
71
-
72
- convert_epub_to_mobi(archive_path, ::File.basename(archive_path, '.epub') + '.mobi') if target.create_mobi
73
- end
74
- else
75
- targets.each do |target|
76
- compiler = Epuber::Compiler.new(book, target)
77
- compiler.compile(Epuber::Config.instance.build_path(target), check: @should_check, write: @should_write, verbose: verbose?)
78
- archive_path = compiler.archive(configuration_suffix: 'debug')
79
-
80
- system(%(epubcheck "#{archive_path}")) if @should_check
81
-
82
- convert_epub_to_mobi(archive_path, ::File.basename(archive_path, '.epub') + '.mobi') if target.create_mobi
83
- end
84
- end
85
-
86
- write_lockfile
87
- end
88
-
89
- private
90
-
91
- # @return [Array<Epuber::Book::Target>]
92
- #
93
- def targets
94
- @targets ||= (
95
- targets = @targets_names
96
-
97
- # when the list of targets is nil pick all available targets
98
- if targets.empty?
99
- book.targets
100
- else
101
- targets.map { |target_name| book.target_named(target_name) }
102
- end
103
- )
104
- end
105
-
106
- def verify_all_targets_exists!
107
- index = targets.index(&:nil?)
108
- help! "Not found target `#{@targets_names[index]}' in bookspec `#{book.file_path}'" unless index.nil?
109
- end
110
-
111
- def validate_dir(path)
112
- if ::File.directory?(path)
113
- path
114
- end
115
- end
116
-
117
- def find_calibre_app
118
- locations = `mdfind "kMDItemCFBundleIdentifier == net.kovidgoyal.calibre"`.split("\n")
119
- UI.error!("Can't find location of calibre.app to convert EPUB to MOBI.") if locations.empty?
120
-
121
- selected = locations.first
122
- UI.warn("Using calibre.app at location #{selected}") if locations.count > 1
123
-
124
- selected
125
- end
126
-
127
- def find_calibre_convert_file
128
- if OS.osx?
129
- ::File.join(find_calibre_app, 'Contents/MacOS/ebook-convert')
130
- elsif OS.linux?
131
- script_path = '/usr/bin/ebook-convert'
132
- UI.error!("Can't find ebook-convert in folder /usr/bin to convert EPUB to MOBI.") unless ::File.executable?(script_path)
133
- script_path
134
- else
135
- UI.error!('Unsupported OS to convert EPUB to MOBI.')
136
- end
137
- end
138
-
139
- def convert_epub_to_mobi(epub_path, mobi_path)
140
- system(find_calibre_convert_file, epub_path, mobi_path)
141
15
  end
142
16
  end
143
17
  end