class Epuber::Command::Init

Public Class Methods

new(argv) click to toggle source

@param [CLAide::ARGV] argv

Calls superclass method Epuber::Command::new
# File lib/epuber/command/init.rb, line 19
def initialize(argv)
  @book_name = argv.arguments!.first

  super(argv)
end

Public Instance Methods

run() click to toggle source
Calls superclass method Epuber::Command::run
# File lib/epuber/command/init.rb, line 34
def run
  super

  write_gitignore
  write_bookspec(@book_name)
  write_sublime_project(@book_name)

  create_folder('images')
  create_folder('fonts')
  create_folder('styles')
  write_default_style(@book_name)

  create_folder('text')

  print_good_bye(@book_name)
end
validate!() click to toggle source
Calls superclass method
# File lib/epuber/command/init.rb, line 25
def validate!
  super

  help! 'You must specify identifier-like name of the book as first argument' if @book_name.nil?

  existing = Dir.glob('*.bookspec')
  help! "Can't reinit this folder, #{existing.first} already exists." unless existing.empty?
end

Private Instance Methods

append_new_lines(file_path, string) click to toggle source

@param [String] string text to file @param [String] file_path path to file

@return [void]

# File lib/epuber/command/init.rb, line 135
def append_new_lines(file_path, string)
  unless File.exist?(file_path)
    write(file_path, string)
    return
  end

  existing_content = File.read(file_path)
  string.split("\n").each do |line|
    next if existing_content.include?(line)

    existing_content << "\n#{line}"
  end

  existing_content << "\n"

  File.write(file_path, existing_content)
  UI.info "   #{'update'.ansi.green}  #{file_path}"
end
ask(text) click to toggle source

@param [String] text

@return [String] returned text without new line

# File lib/epuber/command/init.rb, line 167
def ask(text)
  print text
  result = $stdin.gets.chomp

  while result.empty?
    UI.info 'Value cannot be empty, please fill it!'.ansi.red
    print text
    result = $stdin.gets.chomp
  end

  result
end
create_folder(dir_path) click to toggle source

@param [String] dir_path path to dir

@return [nil]

# File lib/epuber/command/init.rb, line 158
def create_folder(dir_path)
  FileUtils.mkdir_p(dir_path)
  UI.info "   #{'create'.ansi.green}  #{dir_path}/"
end
print_good_bye(book_id) click to toggle source
write(file_path, string) click to toggle source

@param [String] string text to file @param [String] file_path path to file

@return [void]

# File lib/epuber/command/init.rb, line 125
def write(file_path, string)
  File.write(file_path, string)
  UI.info "   #{'create'.ansi.green}  #{file_path}"
end
write_bookspec(book_id) click to toggle source

Creates <book-id>.bookspec file from template

@param [String] book_id

@return [void]

# File lib/epuber/command/init.rb, line 66
def write_bookspec(book_id)
  rendered = Epuber::RubyTemplater.from_file(Templates::TEMPLATE_BOOKSPEC)
                                  .with_locals(book_id: book_id)
                                  .render

  write("#{book_id}.bookspec", rendered)
end
write_default_style(book_id) click to toggle source
# File lib/epuber/command/init.rb, line 114
      def write_default_style(book_id)
        write("styles/#{book_id}.styl", <<~STYLUS)
          // This is generated with `epuber init` script.
        STYLUS
      end
write_gitignore() click to toggle source

Creates .gitignore file

@return [void]

# File lib/epuber/command/init.rb, line 101
      def write_gitignore
        append_new_lines('.gitignore', <<~TEXT)
          # This is generated with `epuber init`
          *.epub
          *.mobi
          !.epuber/
          .epuber/build/
          .epuber/release_build/
          .epuber/build_cache/
          .epuber/metadata/
        TEXT
      end
write_sublime_project(book_id) click to toggle source

Creates <book-id>.sublime-project

@param [String] book_id

@return [void]

# File lib/epuber/command/init.rb, line 80
      def write_sublime_project(book_id)
        text = <<~JSON
          {
            "folders": [
              {
                "follow_symlinks": true,
                "path": ".",
                "folder_exclude_patterns": [".epuber"],
                "file_exclude_patterns": ["*.epub"]
              }
            ]
          }
        JSON

        write("#{book_id}.sublime-project", text)
      end