Class: Diogenes::Cli::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/diogenes/cli/init.rb

Constant Summary collapse

DIOGENES_DIR =

: String

".diogenes"
TEMPLATES_ROOT =

: String

File.expand_path("../templates/init", __dir__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cwd:, out:, err:) ⇒ Init

: (cwd: String, out: IO, err: IO) -> void



18
19
20
21
22
# File 'lib/diogenes/cli/init.rb', line 18

def initialize(cwd:, out:, err:)
  @cwd = cwd #: String
  @out = out #: IO
  @err = err #: IO
end

Class Method Details

.run(cwd:, out:, err:) ⇒ Object

: (cwd: String, out: IO, err: IO) -> Integer



13
14
15
# File 'lib/diogenes/cli/init.rb', line 13

def self.run(cwd:, out:, err:)
  new(cwd: cwd, out: out, err: err).run
end

Instance Method Details

#runObject

: () -> Integer



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/diogenes/cli/init.rb', line 25

def run
  if diogenes_dir_exist?
    @err.puts "Warning: #{DIOGENES_DIR}/ already exists — existing files will not be overwritten."
  end

  @out.puts "Initializing #{DIOGENES_DIR}/"

  created = [] #: Array[String]
  skipped = [] #: Array[String]

  template_files.each do |relative_path|
    destination = File.join(@cwd, DIOGENES_DIR, relative_path)

    if File.exist?(destination)
      skipped << relative_path
      next
    end

    FileUtils.mkdir_p(File.dirname(destination))
    FileUtils.cp(template_path(relative_path), destination)
    created << relative_path
  end

  print_summary(created:, skipped:)
  0
end