class Epuber::Command::Init

Public Class Methods

new(argv) click to toggle source

@param argv [CLAide::ARGV]

Calls superclass method
# File lib/epuber/command/init.rb, line 20
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 33
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
# File lib/epuber/command/init.rb, line 26
def validate!
  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 file_path [String] path to file

@return [void]

# File lib/epuber/command/init.rb, line 134
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)
  puts "   #{'update'.ansi.green}  #{file_path}"
end
ask(text) click to toggle source

@param text [String]

@return [String] returned text without new line

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

  while result.empty?
    puts '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 157
def create_folder(dir_path)
  FileUtils.mkdir_p(dir_path)
  puts "   #{'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 file_path [String] path to file

@return [void]

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

Creates <book-id>.bookspec file from template

@param book_id [String]

@return [void]

# File lib/epuber/command/init.rb, line 65
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 112
      def write_default_style(book_id)
        write("styles/#{book_id}.styl", <<~END
          // This is generated with `epuber init` script.
        END
        )
      end
write_gitignore() click to toggle source

Creates .gitignore file

@return [void]

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

Creates <book-id>.sublime-project

@param book_id [String]

@return [void]

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

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