Class: Hashira::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/hashira/cli.rb,
lib/hashira/cli/flag.rb,
lib/hashira/cli/flags.rb,
lib/hashira/cli/options.rb

Defined Under Namespace

Modules: FailOn, Format, Needs, PackageBy, Skip, Top, Usage Classes: Arguments, CommandLine, Flag, Options, Run

Constant Summary collapse

MISUSE =
2
BROKEN =
70
FLAGS =
[
  Flag.new(
    name: "--format", arg: "FORMAT", parse: Format, mode: :parsed,
    text: ["text (default), json, dot, or mermaid"]
  ),
  Flag.new(name: "--json", mode: :json, text: ["shorthand for --format json"]),
  Flag.new(
    name: "--compact", field: :compact,
    text: ["emit JSON on one line, without the indentation", "meant for reading"]
  ),
  Flag.new(
    name: "--fail-on", arg: "KINDS", field: :fail_on, parse: FailOn, mode: :fail_on,
    text: [
      "exit 1 if findings exist; comma-separated",
      "kinds: cycles, sdp, mixed_audience, wide_edge,",
      "roll_call, complexity, duplication, smells (all",
      "of them), or one smell kind such as feature_envy"
    ]
  ),
  Flag.new(
    name: "--skip", arg: "ANALYZERS", field: :skip, parse: Skip,
    text: ["drop an analyzer; comma-separated: coupling,", "complexity, duplication, smells"]
  ),
  Flag.new(
    name: "--top", arg: "N", field: :top, parse: Top,
    text: [
      "show at most N rows in each table and N",
      "findings (default: 25 packages and findings,",
      "10 methods and files)"
    ]
  ),
  Flag.new(
    name: "--package-by", arg: "WHAT", field: :packaging, parse: PackageBy,
    text: [
      "group coupling by: auto, folder, or namespace",
      "(top-level constant). Default auto: namespace for",
      "Rails apps (config/application.rb in or beside the",
      "analyzed directory), folder otherwise"
    ]
  ),
  Flag.new(
    name: "--ratchet", mode: :ratchet,
    text: ["fail when edges or findings appear that the", "baseline lacks"]
  ),
  Flag.new(name: "--update-baseline", mode: :update, text: ["record the current edges and findings"]),
  Flag.new(
    name: "--baseline", arg: "PATH", field: :baseline, default: "hashira_baseline.json",
    text: ["baseline file (default: hashira_baseline.json)"]
  ),
  Flag.new(name: "-h, --help", mode: :help, text: ["print this help"]),
  Flag.new(name: "--version", mode: :version, text: ["print the version"])
].freeze
PAGE =
{ directories: [], baseline: "", fail_on: [], skip: [], packaging: :auto, top: nil, compact: nil }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CLI

Returns a new instance of CLI.



29
30
31
32
33
34
# File 'lib/hashira/cli.rb', line 29

def initialize(options)
  @started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @options = options
  @notices = Hashira::Report::Notices.new
  @pipeline = options.pipeline
end

Class Method Details

.crash(error) ⇒ Object



24
25
26
27
# File 'lib/hashira/cli.rb', line 24

def self.crash(error)
  Hashira::Report::Notices.new.crashed(error)
  BROKEN
end

.dispatch(options) ⇒ Object



15
# File 'lib/hashira/cli.rb', line 15

def self.dispatch(options) = usage?(options) ? Usage.public_send(options.mode) : new(options).run

.failure(error) ⇒ Object



19
20
21
22
# File 'lib/hashira/cli.rb', line 19

def self.failure(error)
  Kernel.warn("hashira: #{error.message}")
  MISUSE
end

.run(argv) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/hashira/cli.rb', line 7

def self.run(argv)
  dispatch(Options.parse(argv))
rescue Hashira::Error => error
  failure(error)
rescue StandardError => error
  crash(error)
end

.usage?(options) ⇒ Boolean

Returns:

  • (Boolean)


17
# File 'lib/hashira/cli.rb', line 17

def self.usage?(options) = Usage::PAGES.include?(options.mode)

Instance Method Details

#runObject



36
37
38
# File 'lib/hashira/cli.rb', line 36

def run
  Run.new(@pipeline, @options).status.tap { @notices.finished(@pipeline.project.files.size, elapsed) }
end