Sha256: dca4f9e64453d0ca3e7f913584b4f1d2bc9a61e155ca85661cc27fe400f5dd45

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require "techbook"
require "fileutils"

module Techbook
  class Generator
    attr_accessor :name, :path, :basename, :output

    def self.run(name, output:)
      g = new name, output
      g.generate
    end

    def initialize(name, output)
      @name = name
      @output = output
    end

    def generate
      puts "Generating book as #{@name} in #{@output}...".yellow
      create_dir
      copy_files
    end

    private

    def create_dir
      @basename = sanitize(@name)
      @path = File.join @output, @basename
      FileUtils.mkdir_p File.join(@path, "book", "img")
      FileUtils.mkdir_p File.join(@path, "book", "src")
    end

    def copy_files
      FileUtils.cp File.join(Techbook.root, "templates", "book.asc"), File.join(@path, "#{@basename}.asc")
      FileUtils.cp File.join(Techbook.root, "templates", "book-theme.yml"), File.join(@path, "book-theme.yml")
      FileUtils.cp File.join(Techbook.root, "templates", "gitignore"), File.join(@path, ".gitignore")
      FileUtils.cp File.join(Techbook.root, "templates", "README.md"), File.join(@path, "README.md")

      FileUtils.cp File.join(Techbook.root, "templates", "01-first-chapter.asc"), File.join(@path, "book", "01-first-chapter.asc")

      FileUtils.cp File.join(Techbook.root, "templates", "keep"), File.join(@path, "book", "img", ".keep")
      FileUtils.cp File.join(Techbook.root, "templates", "keep"), File.join(@path, "book", "src", ".keep")
    end

    def sanitize(filename)
      filename.strip do |name|
       name.gsub!(/^.*(\\|\/)/, '')
       name.gsub!(/[^0-9A-Za-z.\-]/, '_')
      end

      filename
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
techbook-0.2.3 lib/techbook/generator.rb
techbook-0.2.2 lib/techbook/generator.rb
techbook-0.2.1 lib/techbook/generator.rb
techbook-0.2.0 lib/techbook/generator.rb