|
| 1 | +require "webrick" |
| 2 | + |
| 3 | +module RbsRails |
| 4 | + class CLI |
| 5 | + class Server |
| 6 | + class Handler |
| 7 | + attr_reader :config #: CLI::Configuration |
| 8 | + |
| 9 | + # @rbs config: CLI::Configuration |
| 10 | + def initialize(config) #: void |
| 11 | + @config = config |
| 12 | + end |
| 13 | + |
| 14 | + # @rbs env: Rack::env |
| 15 | + def call(env) #: Rack::response |
| 16 | + case env["REQUEST_METHOD"] |
| 17 | + when "POST" |
| 18 | + request = JSON.parse(env["rack.input"].read) |
| 19 | + do_POST(request["path"]) |
| 20 | + else |
| 21 | + [405, { "Content-Type" => "text/plain" }, ["Method Not Allowed"]] |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + # @rbs path: String? |
| 26 | + def do_POST(path) #: Rack::response |
| 27 | + path = regulate_path(path) |
| 28 | + if path.nil? |
| 29 | + [404, { "Content-Type" => "text/plain" }, ["Not Found"]] |
| 30 | + elsif path.extname != ".rb" |
| 31 | + [403, { "Content-Type" => "text/plain" }, ["Forbidden"]] |
| 32 | + else |
| 33 | + class_name = classify(path) |
| 34 | + if class_name.nil? || class_name.constantize.nil? |
| 35 | + [404, { "Content-Type" => "text/plain" }, ["Not Found"]] |
| 36 | + else |
| 37 | + generate_signature(class_name) |
| 38 | + [201, { "Content-Type" => "text/plain" }, ["Created"]] |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + # @rbs path: String? |
| 46 | + def regulate_path(path) #: Pathname? |
| 47 | + return nil unless path |
| 48 | + |
| 49 | + pathname = Rails.root.join(path[1..]).realpath |
| 50 | + return nil unless pathname.to_s.start_with?(Rails.root.to_s + File::SEPARATOR) |
| 51 | + |
| 52 | + pathname.relative_path_from(Rails.root) |
| 53 | + rescue Errno::ENOENT |
| 54 | + nil |
| 55 | + end |
| 56 | + |
| 57 | + # @rbs path: Pathname |
| 58 | + def classify(path) #: String? |
| 59 | + # if the specified file is placed under autoload_paths... |
| 60 | + Rails.application.config.autoload_paths.each do |autoload_path| |
| 61 | + next unless path.to_s.start_with?(autoload_path.to_s + File::SEPARATOR) |
| 62 | + |
| 63 | + relative_path = path.relative_path_from(autoload_path) |
| 64 | + return relative_path.to_s.chomp(".rb").classify.constantize |
| 65 | + end |
| 66 | + |
| 67 | + # The file will be placed under app/*/ directories |
| 68 | + relative_path = path.to_s.sub(%r{^app/(.*?)/}, "") |
| 69 | + relative_path.chomp(".rb").classify |
| 70 | + rescue NameError |
| 71 | + nil |
| 72 | + end |
| 73 | + |
| 74 | + # @rbs path: String |
| 75 | + # @rbs class_name: String |
| 76 | + def generate_signature(class_name) #: void |
| 77 | + klass = class_name.constantize |
| 78 | + return unless klass < ::ActiveRecord::Base |
| 79 | + return if config.ignored_model?(klass) |
| 80 | + return unless ::RbsRails::ActiveRecord.generatable?(klass) |
| 81 | + |
| 82 | + original_path, _line = Object.const_source_location(klass.name) rescue nil |
| 83 | + |
| 84 | + rbs_relative_path = if original_path && Pathname.new(original_path).fnmatch?("#{Rails.root}/**") |
| 85 | + Pathname.new(original_path) |
| 86 | + .relative_path_from(Rails.root) |
| 87 | + .sub_ext('.rbs') |
| 88 | + else |
| 89 | + "app/models/#{klass.name.underscore}.rbs" |
| 90 | + end |
| 91 | + |
| 92 | + path = config.signature_root_dir / rbs_relative_path |
| 93 | + path.dirname.mkpath |
| 94 | + |
| 95 | + # TODO: We need to resolve the dependencies problem. |
| 96 | + sig = RbsRails::ActiveRecord.class_to_rbs(klass, dependencies: []) |
| 97 | + path.write sig |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
0 commit comments