Class: RBS::Inline::CLI

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

Defined Under Namespace

Classes: PathCalculator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout: STDOUT, stderr: STDERR) ⇒ CLI

Returns a new instance of CLI.



63
64
65
66
67
68
# File 'lib/rbs/inline/cli.rb', line 63

def initialize(stdout: STDOUT, stderr: STDERR) #: void
  @stdout = stdout
  @stderr = stderr
  @logger = Logger.new(stderr)
  logger.level = :ERROR
end

Instance Attribute Details

#loggerObject (readonly)

: Logger



59
60
61
# File 'lib/rbs/inline/cli.rb', line 59

def logger
  @logger
end

#stderrObject (readonly)

: IO



58
59
60
# File 'lib/rbs/inline/cli.rb', line 58

def stderr
  @stderr
end

#stdoutObject (readonly)

: IO



58
59
60
# File 'lib/rbs/inline/cli.rb', line 58

def stdout
  @stdout
end

Instance Method Details

#run(args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbs/inline/cli.rb', line 72

def run(args)
  base_paths = [Pathname("lib"), Pathname("app")]
  output_path = nil #: Pathname?
  opt_in = true

  OptionParser.new do |opts|
    opts.on("--base=BASE", "The path to calculate relative path of files (defaults to #{base_paths.join(File::PATH_SEPARATOR)})") do |str|
      # @type var str: String
      base_paths = str.split(File::PATH_SEPARATOR).map {|path| Pathname(path) }
    end

    opts.on("--output[=DEST]", "Save the generated RBS files under `sig/generated` or DEST if specified (defaults to output to STDOUT)") do
      if _1
        output_path = Pathname(_1)
      else
        output_path = Pathname("sig/generated")
      end
    end

    opts.on("--opt-out", "Generates RBS files by default. Opt-out with `# rbs_inline: disabled` comment") do
      opt_in = false
    end

    opts.on("--opt-in", "Generates RBS files only for files with `# rbs_inline: enabled` comment (default)") do
      opt_in = true
    end

    opts.on("--verbose") do
      logger.level = :DEBUG
    end
  end.parse!(args)

  logger.debug { "base_paths = #{base_paths.join(File::PATH_SEPARATOR)}, output_path = #{output_path}" }

  if output_path
    calculator = PathCalculator.new(Pathname.pwd, base_paths, output_path)
  end

  targets = args.flat_map { Pathname.glob(_1) }.flat_map do |path|
    if path.directory?
      pattern = path + "**/*.rb"
      Pathname.glob(pattern.to_s)
    else
      path
    end
  end

  targets.sort!
  targets.uniq!

  count = 0

  targets.each do |target|
    absolute_path = Pathname.pwd + target

    if output_path && calculator
      output_file_path = calculator.calculate(absolute_path)
      if output_file_path
        output = output_file_path.sub_ext(".rbs")
      else
        raise "Cannot calculate the output file path for #{target} in #{output_path}: calculated = #{output}"
      end

      logger.debug { "Generating #{output} from #{target} ..." }
    else
      logger.debug { "Generating RBS declaration from #{target} ..." }
    end

    logger.debug { "Parsing ruby file #{target}..." }

    if (uses, decls, rbs_decls = Parser.parse(Prism.parse_file(target.to_s), opt_in: opt_in))
      writer = Writer.new()
      writer.header("Generated from #{target.relative? ? target : target.relative_path_from(Pathname.pwd)} with RBS::Inline")
      writer.write(uses, decls, rbs_decls)

      if output
        unless output.parent.directory?
          logger.debug { "Making directory #{output.parent}..." }
          output.parent.mkpath
        end

        if output.file? && output.read == writer.output
          logger.debug { "Skip writing identical RBS file" }
        else
          logger.debug { "Writing RBS file to #{output}..." }
          output.write(writer.output)
          count += 1
        end
      else
        stdout.puts writer.output
        stdout.puts
        count += 1
      end
    else
      logger.debug { "Skipping #{target} because `# rbs_inline: enabled` comment not found" }
    end
  end

  stderr.puts "🎉 Generated #{count} RBS files under #{output_path}"

  0
end