Module: Mutineer::Init
- Defined in:
- lib/mutineer/init.rb
Overview
Writes a starter .mutineer.yml and prints the first run command.
It does not replace an existing file unless --force is set.
Constant Summary collapse
- USAGE =
Usage line for a bad invocation.
"Usage: mutineer init [--rails] [--force]"
Class Method Summary collapse
-
.command_for(dir, rails:) ⇒ String?
The first run command.
-
.run(argv, out: $stdout, err: $stderr, dir: Dir.pwd) ⇒ Integer
Writes the config and prints the first command.
-
.template(rails:) ⇒ String
Starter config.
Class Method Details
.command_for(dir, rails:) ⇒ String?
The first run command. Plain init names lib.
--rails names each existing folder among app/models, app/services, and lib.
A missing folder is left out: mutineer run rejects a path that is not there.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/mutineer/init.rb', line 113 def self.command_for(dir, rails:) return "mutineer run lib" unless rails folders = ["app/models", "app/services", "lib"].select do |folder| File.directory?(File.join(dir, folder)) end return if folders.empty? "mutineer run #{folders.join(" ")}" end |
.run(argv, out: $stdout, err: $stderr, dir: Dir.pwd) ⇒ Integer
Writes the config and prints the first command. 0 is success. 2 is a usage error, a refused overwrite, or a missing directory.
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mutineer/init.rb', line 22 def self.run(argv, out: $stdout, err: $stderr, dir: Dir.pwd) rails = false force = false help = false parser = OptionParser.new do |o| o. = USAGE o.on("--rails") { rails = true } o.on("--force") { force = true } o.on("--help") { help = true } end begin parser.parse!(argv) rescue OptionParser::ParseError => e err.puts "mutineer: #{e.}" return 2 end if help out.puts USAGE return 0 end unless argv.empty? err.puts "mutineer: init takes no source paths" return 2 end path = File.join(dir, CONFIG_FILE) if File.directory?(path) err.puts "mutineer: #{CONFIG_FILE} is a directory" return 2 end if File.exist?(path) && !force err.puts "mutineer: #{CONFIG_FILE} already exists. Pass --force to replace it." return 2 end # The command writes one file. It does not create the project directory. unless File.directory?(dir) err.puts "mutineer: cannot write #{CONFIG_FILE}: no such directory" return 2 end begin File.write(path, template(rails: rails)) rescue SystemCallError => e err.puts "mutineer: cannot write #{CONFIG_FILE}: #{e.}" return 2 end command = command_for(dir, rails: rails) if command out.puts command else err.puts "mutineer: no source folder found. Create app/models or lib, then run mutineer on that folder." end 0 end |
.template(rails:) ⇒ String
Starter config. since stays commented so the first run is a full scan.
--rails also sets rails: true and leaves daemon commented.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mutineer/init.rb', line 82 def self.template(rails:) operators = MutatorRegistry::DEFAULT_NAMES.map { |name| " - #{name}" }.join("\n") head = <<~YAML # Mutineer config. Command-line flags override this file. # These operators are the default set. operators: YAML tail = <<~YAML # since: origin/main # Leave this commented so the first run is a full scan. YAML body = "#{head}#{operators}\n\n#{tail}" return body unless rails extra = <<~YAML rails: true # daemon: true # Leave this commented until the support matrix says the daemon fits. # The matrix is on https://davidteren.github.io/mutineer/rails.html#daemon YAML "#{body.rstrip}\n\n#{extra}" end |