Class: Steep::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stdin:, stderr:, argv:) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
15
16
# File 'lib/steep/cli.rb', line 11

def initialize(stdout:, stdin:, stderr:, argv:)
  @stdout = stdout
  @stdin = stdin
  @stderr = stderr
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



5
6
7
# File 'lib/steep/cli.rb', line 5

def argv
  @argv
end

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/steep/cli.rb', line 9

def command
  @command
end

#stderrObject (readonly)

Returns the value of attribute stderr.



8
9
10
# File 'lib/steep/cli.rb', line 8

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



7
8
9
# File 'lib/steep/cli.rb', line 7

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



6
7
8
# File 'lib/steep/cli.rb', line 6

def stdout
  @stdout
end

Class Method Details

.available_commandsObject



18
19
20
# File 'lib/steep/cli.rb', line 18

def self.available_commands
  [:init, :check, :validate, :annotations, :version, :project, :watch, :langserver, :vendor]
end

Instance Method Details

#handle_logging_options(opts) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/steep/cli.rb', line 53

def handle_logging_options(opts)
  opts.on("--log-level=[debug,info,warn,error,fatal]") do |level|
    Steep.logger.level = level
  end

  opts.on("--log-output=[PATH]") do |file|
    Steep.log_output = file
  end

  opts.on("--verbose") do
    Steep.logger.level = Logger::DEBUG
  end
end

#process_annotationsObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/steep/cli.rb', line 102

def process_annotations
  Drivers::Annotations.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep annotations [options] [sources]"
      handle_logging_options opts
    end.parse!(argv)

    command.command_line_patterns.push *argv
  end.run
end

#process_checkObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/steep/cli.rb', line 80

def process_check
  Drivers::Check.new(stdout: stdout, stderr: stderr).tap do |check|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep check [options] [sources]"

      opts.on("--steepfile=PATH") {|path| check.steepfile = Pathname(path) }
      opts.on("--dump-all-types") { check.dump_all_types = true }
      handle_logging_options opts
    end.parse!(argv)

    check.command_line_patterns.push *argv
  end.run
end

#process_global_optionsObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/steep/cli.rb', line 22

def process_global_options
  OptionParser.new do |opts|
    opts.on("--version") do
      process_version
      exit 0
    end

    handle_logging_options(opts)
  end.order!(argv)

  true
end

#process_initObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/steep/cli.rb', line 67

def process_init
  Drivers::Init.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep init [options]"

      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      opts.on("--force") { command.force_write = true }

      handle_logging_options opts
    end.parse!(argv)
  end.run()
end

#process_langserverObject



134
135
136
137
138
139
140
141
# File 'lib/steep/cli.rb', line 134

def process_langserver
  Drivers::Langserver.new(stdout: stdout, stderr: stderr, stdin: stdin).tap do |command|
    OptionParser.new do |opts|
      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      handle_logging_options opts
    end.parse!(argv)
  end.run
end

#process_projectObject



113
114
115
116
117
118
119
120
121
# File 'lib/steep/cli.rb', line 113

def process_project
  Drivers::PrintProject.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep project [options]"
      opts.on("--steepfile=PATH") {|path| command.steepfile = Pathname(path) }
      handle_logging_options opts
    end.parse!(argv)
  end.run
end

#process_validateObject



94
95
96
97
98
99
100
# File 'lib/steep/cli.rb', line 94

def process_validate
  Drivers::Validate.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      handle_logging_options opts
    end.parse!(argv)
  end.run
end

#process_vendorObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/steep/cli.rb', line 143

def process_vendor
  Drivers::Vendor.new(stdout: stdout, stderr: stderr, stdin: stdin).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep vendor [options] [dir]"
      handle_logging_options opts

      opts.on("--[no-]clean") do |v|
        command.clean_before = v
      end
    end.parse!(argv)

    command.vendor_dir = Pathname(argv[0] || "vendor/sigs")
  end.run
end

#process_versionObject



158
159
160
161
# File 'lib/steep/cli.rb', line 158

def process_version
  stdout.puts Steep::VERSION
  0
end

#process_watchObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/steep/cli.rb', line 123

def process_watch
  Drivers::Watch.new(stdout: stdout, stderr: stderr).tap do |command|
    OptionParser.new do |opts|
      opts.banner = "Usage: steep watch [options] [dirs]"
      handle_logging_options opts
    end.parse!(argv)

    command.dirs.push *argv
  end.run
end

#runObject



46
47
48
49
50
51
# File 'lib/steep/cli.rb', line 46

def run
  process_global_options or return 1
  setup_command or return 1

  __send__(:"process_#{command}")
end

#setup_commandObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/steep/cli.rb', line 35

def setup_command
  @command = argv.shift&.to_sym
  if CLI.available_commands.include?(@command)
    true
  else
    stderr.puts "Unknown command: #{command}"
    stderr.puts "  available commands: #{CLI.available_commands.join(', ')}"
    false
  end
end