Module: Chemlab::CLI::NewLibrary

Defined in:
lib/chemlab/cli/new_library.rb

Overview

New Library generator module

Class Method Summary collapse

Class Method Details

.render_file(source) ⇒ String

Note:

This method will replace ‘new_library` with the library

Render a given file

Parameters:

  • source (File)

    the source file

Returns:

  • (String)

    the destination



56
57
58
59
# File 'lib/chemlab/cli/new_library.rb', line 56

def render_file(source)
  puts "Rendering: #{source}"
  # Dir.mkdir(@tmp_destination)
end

.scaffold(library_name) ⇒ Object

Scaffold a library by name

Parameters:

  • library_name (String)

    the name of the library to scaffold



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chemlab/cli/new_library.rb', line 14

def scaffold(library_name)
  if Dir.exist?(library_name)
    raise %(Cannot create new library `#{library_name}` as the directory "#{library_name}" already exists)
  end

  require 'chemlab/core_ext/string/inflections'

  @library = {
    name: library_name,
    classified_name: library_name.tr('-', '_').classify,
    underscored_name: library_name.underscore
  }

  $stdout.print %(Scaffolding new library in "#{library_name}/"...)

  Dir.mktmpdir(library_name) do |dir|
    # copy the fixture folder into the tmp folder and name the directory #{library_name}
    root_dir = File.join(dir, library_name)
    FileUtils.copy_entry(File.expand_path('./fixtures/new_library', __dir__), root_dir)

    # rename all `new_library` references to #{library_name}
    Dir.glob(File.join(root_dir, '**', 'new_library*')) do |file|
      FileUtils.move(file, file.gsub('new_library', library_name))
    end

    Dir["#{root_dir}/**/*.erb"].each do |template|
      File.open(template[0..-5], 'w') do |file|
        file.puts ERB.new(File.read(template), trim_mode: '%<>').result_with_hash({ library: @library })
        File.delete(template)
      end
    end

    FileUtils.move(File.join(dir, library_name), library_name)
  end

  $stdout.print " Done\n"
end