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
  [:check, :validate, :annotations, :scaffold, :interface]
end

Instance Method Details

#process_annotationsObject



104
105
106
107
# File 'lib/steep/cli.rb', line 104

def process_annotations
  source_paths = argv.map {|file| Pathname(file) }
  Drivers::Annotations.new(source_paths: source_paths, stdout: stdout, stderr: stderr).run
end

#process_checkObject



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
76
77
78
79
# File 'lib/steep/cli.rb', line 44

def process_check
  signature_dirs = []
  verbose = false
  no_builtin = false
  dump_all_types = false
  fallback_any_is_error = false
  strict = false

  OptionParser.new do |opts|
    opts.on("-I [PATH]") {|path| signature_dirs << Pathname(path) }
    opts.on("--no-builtin") { no_builtin = true }
    opts.on("--verbose") { verbose = true }
    opts.on("--dump-all-types") { dump_all_types = true }
    opts.on("--strict") { strict = true }
    opts.on("--fallback-any-is-error") { fallback_any_is_error = true }
  end.parse!(argv)

  if signature_dirs.empty?
    signature_dirs << Pathname("sig")
  end

  unless no_builtin
    signature_dirs.unshift Pathname(__dir__).join("../../stdlib").realpath
  end

  source_paths = argv.map {|path| Pathname(path) }
  if source_paths.empty?
    source_paths << Pathname(".")
  end

  Drivers::Check.new(source_paths: source_paths, signature_dirs: signature_dirs, stdout: stdout, stderr: stderr).tap do |check|
    check.verbose = verbose
    check.dump_all_types = dump_all_types
    check.fallback_any_is_error = fallback_any_is_error || strict
  end.run
end

#process_interfaceObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/steep/cli.rb', line 114

def process_interface
  signature_dirs = []
  no_builtin = false

  OptionParser.new do |opts|
    opts.on("-I [PATH]") {|path| signature_dirs << Pathname(path) }
    opts.on("--no-builtin") { no_builtin = true }
  end

  unless no_builtin
    signature_dirs.unshift Pathname(__dir__).join("../../stdlib").realpath
  end

  Drivers::PrintInterface.new(type_name: argv.first, signature_dirs: signature_dirs, stdout: stdout, stderr: stderr).run
end

#process_scaffoldObject



109
110
111
112
# File 'lib/steep/cli.rb', line 109

def process_scaffold
  source_paths = argv.map {|file| Pathname(file) }
  Drivers::Scaffold.new(source_paths: source_paths, stdout: stdout, stderr: stderr).run
end

#process_validateObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/steep/cli.rb', line 81

def process_validate
  verbose = false
  no_builtin = false

  OptionParser.new do |opts|
    opts.on("--verbose") { verbose = true }
    opts.on("--no-builtin") { no_builtin = true }
  end.parse!(argv)

  signature_dirs = argv.map {|path| Pathname(path) }
  if signature_dirs.empty?
    signature_dirs << Pathname(".")
  end

  unless no_builtin
    signature_dirs.unshift Pathname(__dir__).join("../../stdlib").realpath
  end

  Drivers::Validate.new(signature_dirs: signature_dirs, stdout: stdout, stderr: stderr).tap do |validate|
    validate.verbose = verbose
  end.run
end

#runObject



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

def run
  setup_global_options or return 1
  setup_command or return 1

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

#setup_commandObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/steep/cli.rb', line 26

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

#setup_global_optionsObject



22
23
24
# File 'lib/steep/cli.rb', line 22

def setup_global_options
  true
end