Module: SinatraToTheMoon::Generator

Defined in:
lib/sinatra_to_the_moon/generator.rb,
sig/sinatra_to_the_moon.rbs

Class Method Summary collapse

Class Method Details

.ensure_destination_available!(target, force:) ⇒ Object



34
35
36
37
38
39
# File 'lib/sinatra_to_the_moon/generator.rb', line 34

def ensure_destination_available!(target, force:)
  return unless File.exist?(target)
  return if force

  raise DestinationExistsError, "Destination already exists: #{target}. Pass --force to overwrite generated files"
end

.generate(name:, profile: "web", destination: Dir.pwd, force: false, dry_run: false) ⇒ Array[String]

Parameters:

  • name: (String)
  • profile: (String) (defaults to: "web")
  • destination: (String) (defaults to: Dir.pwd)
  • force: (Boolean) (defaults to: false)
  • dry_run: (Boolean) (defaults to: false)

Returns:

  • (Array[String])


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sinatra_to_the_moon/generator.rb', line 13

def generate(name:, profile: "web", destination: Dir.pwd, force: false, dry_run: false)
  Project.validate_name!(name)
  Project.validate_profile!(profile)

  target = File.expand_path(name, destination)
  ensure_destination_available!(target, force: force)

  locals = { app_name: name, constant_name: Project.constant_name(name), profile: profile }
  files = Templates.files_for(profile)

  files.each do |relative_path, template|
    path = File.join(target, relative_path)
    next if dry_run

    FileUtils.mkdir_p(File.dirname(path))
    File.write(path, Templates.render(template, locals))
  end

  files.keys.map { |path| File.join(name, path) }
end