Class: RBS::CLI::Diff

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

Instance Method Summary collapse

Constructor Details

#initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr) ⇒ Diff

Returns a new instance of Diff.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/rbs/cli/diff.rb', line 6

def initialize(argv:, library_options:, stdout: $stdout, stderr: $stderr)
  @format = nil
  @stdout = stdout
  @stderr = stderr

  # @type var type_name: String?
  type_name = nil
  library_options = library_options
  before_path = [] #: Array[String]
  after_path = [] #: Array[String]
  detail = false

  opt = OptionParser.new do |o|
    o.banner = "      [Experimental] This command is experimental. API and output compatibility is not guaranteed.\n\n      Usage:\n        rbs diff --format markdown --type-name Foo --before before_sig --after after_sig\n\n      Print diff for rbs environment dir\n\n      Examples:\n\n        # Diff dir1 and dir2 for Foo\n        $ rbs diff --format markdown --type-name Foo --before dir1 --after dir2\n\n        # Confirmation of methods related to Time class added by including stdlib/time\n        $ rbs diff --format diff --type-name Time --after stdlib/time\n    HELP\n    o.on(\"--format NAME\")    { |arg| @format = arg }\n    o.on(\"--type-name NAME\") { |arg| type_name = arg }\n    o.on(\"--before DIR\")     { |arg| before_path << arg }\n    o.on(\"--after DIR\")      { |arg| after_path << arg }\n    o.on(\"--[no-]detail\")    { |arg| detail = arg }\n  end\n  opt.parse!(argv)\n\n  unless @format && type_name && [\"markdown\", \"diff\"].include?(@format)\n    @stderr.puts opt.banner\n    exit 1\n  end\n\n  @diff = RBS::Diff.new(\n    type_name: TypeName.parse(type_name).absolute!,\n    library_options: library_options,\n    after_path: after_path,\n    before_path: before_path,\n    detail: detail,\n  )\nend\n"

Instance Method Details

#runObject



57
58
59
# File 'lib/rbs/cli/diff.rb', line 57

def run
  public_send("run_#{@format}")
end

#run_diffObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/rbs/cli/diff.rb', line 61

def run_diff
  first = true
  io = RBS::CLI::ColoredIO.new(stdout: @stdout)
  @diff.each_diff do |before, after|
    io.puts if !first
    io.puts_red   "- #{before}"
    io.puts_green "+ #{after}"
    first = false
  end
end

#run_markdownObject



72
73
74
75
76
77
78
79
80
# File 'lib/rbs/cli/diff.rb', line 72

def run_markdown
  @stdout.puts "| before | after |"
  @stdout.puts "| --- | --- |"
  @diff.each_diff do |before, after|
    before.gsub!("|", "\\|")
    after.gsub!("|", "\\|")
    @stdout.puts "| `#{before}` | `#{after}` |"
  end
end