Created
February 21, 2023 12:54
-
-
Save tomoasleep/51632fd1dc974d479d306f6ead391850 to your computer and use it in GitHub Desktop.
YARD の `@example` tag の内容を収集して Ruby コードとして出力してくれる君 (TypeProf とセットで使うと便利かも)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "yard" | |
require "stringio" | |
require "fileutils" | |
Example = Struct.new(:path, :name, :code, :file, :lineno, keyword_init: true) | |
def generate(*args) | |
::YARD.parse(*args) | |
code_objects = ::YARD::Registry.all.select do |code_object| | |
code_object.tag("example") | |
end | |
examples = code_objects.flat_map do |code_object| | |
code_object.tags("example").map do |example| | |
file, lineno = code_object.files.first || [nil, nil] | |
Example.new(path: code_object.path, name: example.name, code: example.text, file: file, lineno: lineno) | |
end | |
end | |
code = examples.reduce(StringIO.new) do |stringio, example| | |
stringio << <<~RUBY | |
# #{example.path} #{example.name} (#{example.file}:#{example.lineno}) | |
#{example.code} | |
RUBY | |
end | |
FileUtils.mkdir_p("runner") | |
File.write("runner/runner_from_yard.rb", code.string) | |
end | |
generate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment