Class: Diogenes::Cli::Init
- Inherits:
-
Object
- Object
- Diogenes::Cli::Init
- Defined in:
- lib/diogenes/cli/init.rb,
sig/generated/diogenes/cli/init.rbs
Constant Summary collapse
- DIOGENES_DIR =
: String
".diogenes"- TEMPLATES_ROOT =
: String
File.("../templates/init", __dir__)
Class Method Summary collapse
-
.run(cwd:, out:, err:, argv: [], **_opts) ⇒ Integer
: (cwd: String, out: IO, err: IO, ?argv: Array, **untyped) -> Integer.
Instance Method Summary collapse
-
#diogenes_dir_exist? ⇒ Boolean
: () -> bool.
-
#initialize(cwd:, out:, err:) ⇒ Init
constructor
: (cwd: String, out: IO, err: IO) -> void.
- #print_summary(created:, skipped:) ⇒ void
-
#run ⇒ Integer
: () -> Integer.
-
#template_files ⇒ Array[String]
: () -> Array.
-
#template_path(relative_path) ⇒ String
: (String) -> String.
Constructor Details
#initialize(cwd:, out:, err:) ⇒ Init
: (cwd: String, out: IO, err: IO) -> void
20 21 22 23 24 |
# File 'lib/diogenes/cli/init.rb', line 20 def initialize(cwd:, out:, err:) @cwd = cwd #: String @out = out #: IO @err = err #: IO end |
Class Method Details
.run(cwd:, out:, err:, argv: [], **_opts) ⇒ Integer
: (cwd: String, out: IO, err: IO, ?argv: Array, **untyped) -> Integer
13 14 15 16 17 |
# File 'lib/diogenes/cli/init.rb', line 13 def self.run(cwd:, out:, err:, argv: [], **_opts) raise Diogenes::UserError, "`init` takes no arguments" unless argv.empty? new(cwd: cwd, out: out, err: err).run end |
Instance Method Details
#diogenes_dir_exist? ⇒ Boolean
: () -> bool
57 58 59 |
# File 'lib/diogenes/cli/init.rb', line 57 def diogenes_dir_exist? Dir.exist?(File.join(@cwd, DIOGENES_DIR)) end |
#print_summary(created:, skipped:) ⇒ void
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/diogenes/cli/init.rb', line 75 def print_summary(created:, skipped:) if created.any? @out.puts "Initialized #{DIOGENES_DIR}/ with #{created.size} file#{"s" unless created.size == 1}." created.each { |path| @out.puts " created #{DIOGENES_DIR}/#{path}" } elsif skipped.any? @out.puts "No files created — all #{skipped.size} template files already exist." end return if skipped.empty? @out.puts "Skipped #{skipped.size} existing file#{"s" unless skipped.size == 1}:" skipped.each { |path| @out.puts " skipped #{DIOGENES_DIR}/#{path}" } end |
#run ⇒ Integer
: () -> Integer
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/diogenes/cli/init.rb', line 27 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 |
#template_files ⇒ Array[String]
: () -> Array
62 63 64 65 66 67 |
# File 'lib/diogenes/cli/init.rb', line 62 def template_files Dir.glob("**/*", base: TEMPLATES_ROOT, flags: File::FNM_DOTMATCH) .reject { |path| path == "." || path.end_with?("/.") } .select { |path| File.file?(template_path(path)) } .sort end |
#template_path(relative_path) ⇒ String
: (String) -> String
70 71 72 |
# File 'lib/diogenes/cli/init.rb', line 70 def template_path(relative_path) File.join(TEMPLATES_ROOT, relative_path) end |