class Bookwatch::Commands::Generate

Attributes

fs[R]
sheller[R]
streams[R]

Public Class Methods

new(fs, sheller, context_dir, streams) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 6
def initialize(fs, sheller, context_dir, streams)
  @fs = fs
  @sheller = sheller
  @context_dir = context_dir
  @streams = streams
end

Public Instance Methods

run(name, special_bookwatch_gem_args={}) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 13
def run(name, special_bookwatch_gem_args={})
  path = context_dir.join(name)
  streams[:out].puts "Generating book at #{path}..."
  if fs.file_exist?(path)
    streams[:err].puts "Cannot generate book: directory already exists"
    1
  elsif install(path, special_bookwatch_gem_args).success?
    streams[:success].puts "Successfully generated book at #{path}"
    0
  else
    1
  end
end

Private Instance Methods

bundle_install(path) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 78
def bundle_install(path)
  Bundler.with_clean_env do
    sheller.run_command(
      "bundle install --binstubs --gemfile=#{path.join('Gemfile')}",
      out: streams[:out], err: streams[:err]
    )
  end
end
context_dir() click to toggle source
# File lib/bookwatch/commands/generate.rb, line 87
def context_dir
  Pathname(@context_dir)
end
init_config(path) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 61
def init_config(path)
  fs.write(
    text: YAML.dump(
      'book_repo' => '',
      'public_host' => '',
    ),
    to: path.join('config.yml')
  )
end
init_gemfile(path, special_bookwatch_gem_args) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 43
      def init_gemfile(path, special_bookwatch_gem_args)
        bookwatch_config = ''

        unless special_bookwatch_gem_args.empty?
          config = special_bookwatch_gem_args.first
          bookwatch_config = ", #{config.first}: \"#{config.last}\""
        end

        fs.write(
          text: <<-GEMFILE,
source "https://rubygems.org"

gem "bookwatch"#{bookwatch_config}
          GEMFILE
          to: path.join('Gemfile')
        )
      end
init_index(path) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 71
def init_index(path)
  fs.write(
    text: '# Empty book',
    to: path.join('master_middleman/source/index.md.erb')
  )
end
install(path, special_bookwatch_gem_args) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 31
def install(path, special_bookwatch_gem_args)
  make_middleman_dir(path)
  init_gemfile(path, special_bookwatch_gem_args)
  init_config(path)
  init_index(path)
  bundle_install(path)
end
make_middleman_dir(path) click to toggle source
# File lib/bookwatch/commands/generate.rb, line 39
def make_middleman_dir(path)
  fs.make_directory(path.join('master_middleman/build'))
end