Skip to content

Commit

Permalink
Standardize path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Aug 9, 2023
1 parent 8d2eb60 commit dfd300a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def on_const(node)
schema_file = model[:schema_file]
content = +""
if schema_file
content << "[Schema](file://#{schema_file})\n\n"
content << "[Schema](#{URI::Generic.build(scheme: "file", path: schema_file)})\n\n"
end
content << model[:columns].map { |name, type| "**#{name}**: #{type}\n" }.join("\n")
contents = RubyLsp::Interface::MarkupContent.new(kind: "markdown", value: content)
Expand Down
27 changes: 10 additions & 17 deletions lib/ruby_lsp/ruby_lsp_rails/rails_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,23 @@ class ServerAddressUnknown < StandardError; end
SERVER_NOT_RUNNING_MESSAGE = "Rails server is not running. " \
"To get Rails features in the editor, boot the Rails server"

sig { returns(String) }
sig { returns(Pathname) }
attr_reader :root

sig { void }
def initialize
project_root = Pathname.new(ENV["BUNDLE_GEMFILE"]).dirname
project_root = T.let(Bundler.with_unbundled_env { Bundler.default_gemfile }.dirname, Pathname)
dummy_path = project_root.join("test", "dummy")

if project_root.basename.to_s == ".ruby-lsp"
project_root = project_root.join("../")
end

dummy_path = File.join(project_root, "test", "dummy")
@root = T.let(Dir.exist?(dummy_path) ? dummy_path : project_root.to_s, String)
app_uri_path = "#{@root}/tmp/app_uri.txt"

if File.exist?(app_uri_path)
url = File.read(app_uri_path).chomp
@root = T.let(dummy_path.exist? ? dummy_path : project_root, Pathname)
app_uri_path = @root.join("tmp", "app_uri.txt")

scheme, rest = url.split("://")
uri, port = T.must(rest).split(":")
if app_uri_path.exist?
url = URI(app_uri_path.read.chomp)

@ssl = T.let(scheme == "https", T::Boolean)
@uri = T.let(T.must(uri), T.nilable(String))
@port = T.let(T.must(port).to_i, Integer)
@ssl = T.let(url.scheme == "https", T::Boolean)
@uri = T.let(T.must(url.path), T.nilable(String))
@port = T.let(T.must(url.port).to_i, Integer)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_lsp_rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Railtie < ::Rails::Railtie
if defined?(::Rails::Server)
ssl_enable, host, port = ::Rails::Server::Options.new.parse!(ARGV).values_at(:SSLEnable, :Host, :Port)
app_uri = "#{ssl_enable ? "https" : "http"}://#{host}:#{port}"
app_uri_path = "#{::Rails.root}/tmp/app_uri.txt"
File.write(app_uri_path, app_uri)
app_uri_path = ::Rails.root.join("tmp", "app_uri.txt")
app_uri_path.write(app_uri)

at_exit do
# The app_uri.txt file should only exist when the server is running. The extension uses its presence to
Expand Down
2 changes: 1 addition & 1 deletion test/ruby_lsp_rails/rails_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RailsClientTest < ActiveSupport::TestCase
project_root = Pathname.new(previous_bundle_gemfile).dirname

ENV["BUNDLE_GEMFILE"] = "#{project_root}/.ruby-lsp/Gemfile"
assert_equal("#{project_root}/test/dummy", RailsClient.new.root)
assert_equal("#{project_root}/test/dummy", RailsClient.new.root.to_s)
ensure
ENV["BUNDLE_GEMFILE"] = previous_bundle_gemfile
end
Expand Down

0 comments on commit dfd300a

Please sign in to comment.