Class: RubyLsp::SetupBundler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/setup_bundler.rb

Defined Under Namespace

Modules: ThorPatch Classes: BundleInstallFailure, BundleNotLocked

Constant Summary collapse

FOUR_HOURS =

: Integer

4 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(project_path, **options) ⇒ SetupBundler

: (String project_path, **untyped options) -> void



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_lsp/setup_bundler.rb', line 35

def initialize(project_path, **options)
  @project_path = project_path
  @branch = options[:branch] #: String?
  @launcher = options[:launcher] #: bool?
  patch_thor_to_print_progress_to_stderr! if @launcher

  # Regular bundle paths
  @gemfile = begin
    Bundler.default_gemfile
  rescue Bundler::GemfileNotFound
    nil
  end #: Pathname?
  @lockfile = @gemfile ? Bundler.default_lockfile : nil #: Pathname?

  @gemfile_hash = @gemfile ? Digest::SHA256.hexdigest(@gemfile.read) : nil #: String?
  @lockfile_hash = @lockfile&.exist? ? Digest::SHA256.hexdigest(@lockfile.read) : nil #: String?

  @gemfile_name = @gemfile&.basename&.to_s || "Gemfile" #: String

  # Custom bundle paths
  @custom_dir = Pathname.new(".ruby-lsp").expand_path(@project_path) #: Pathname
  @custom_gemfile = @custom_dir + @gemfile_name #: Pathname
  @custom_lockfile = @custom_dir + (@lockfile&.basename || "Gemfile.lock") #: Pathname
  @lockfile_hash_path = @custom_dir + "main_lockfile_hash" #: Pathname
  @last_updated_path = @custom_dir + "last_updated" #: Pathname
  @error_path = @custom_dir + "install_error" #: Pathname
  @already_composed_path = @custom_dir + "bundle_is_composed" #: Pathname

  dependencies, bundler_version = load_dependencies
  @dependencies = dependencies #: Hash[String, untyped]
  @bundler_version = bundler_version #: Gem::Version?
  @rails_app = rails_app? #: bool
  @retry = false #: bool
  @needs_update_path = @custom_dir + "needs_update" #: Pathname
end

Instance Method Details

#setup!Object

Sets up the composed bundle and returns the ‘BUNDLE_GEMFILE`, `BUNDLE_PATH` and `BUNDLE_APP_CONFIG` that should be used for running the server : -> Hash[String, String]

Raises:



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
# File 'lib/ruby_lsp/setup_bundler.rb', line 74

def setup!
  raise BundleNotLocked if !@launcher && @gemfile&.exist? && !@lockfile&.exist?

  # If the bundle was composed ahead of time using our custom `rubyLsp/composeBundle` request, then we can skip the
  # entire process and just return the composed environment
  if @already_composed_path.exist?
    $stderr.puts("Ruby LSP> Composed bundle was set up ahead of time. Skipping...")
    @already_composed_path.delete

    env = bundler_settings_as_env
    env["BUNDLE_GEMFILE"] = @custom_gemfile.exist? ? @custom_gemfile.to_s : @gemfile.to_s

    if env["BUNDLE_PATH"]
      env["BUNDLE_PATH"] = File.expand_path(env["BUNDLE_PATH"], @project_path)
    end

    env["BUNDLER_VERSION"] = @bundler_version.to_s if @bundler_version
    return env
  end

  # Automatically create and ignore the .ruby-lsp folder for users
  @custom_dir.mkpath unless @custom_dir.exist?
  ignore_file = @custom_dir + ".gitignore"
  ignore_file.write("*") unless ignore_file.exist?

  # Do not set up a composed bundle if LSP dependencies are already in the Gemfile
  if @dependencies["ruby-lsp"] &&
      @dependencies["debug"] &&
      (@rails_app ? @dependencies["ruby-lsp-rails"] : true)
    $stderr.puts(
      "Ruby LSP> Skipping composed bundle setup since LSP dependencies are already in #{@gemfile}",
    )

    return run_bundle_install
  end

  write_custom_gemfile

  unless @gemfile&.exist? && @lockfile&.exist?
    $stderr.puts("Ruby LSP> Skipping lockfile copies because there's no top level bundle")
    return run_bundle_install(@custom_gemfile)
  end

  if @lockfile_hash && @custom_lockfile.exist? && @lockfile_hash_path.exist? &&
      @lockfile_hash_path.read == @lockfile_hash
    $stderr.puts(
      "Ruby LSP> Skipping composed bundle setup since #{@custom_lockfile} already exists and is up to date",
    )
    return run_bundle_install(@custom_gemfile)
  end

  FileUtils.cp(@lockfile.to_s, @custom_lockfile.to_s)
  correct_relative_remote_paths
  @lockfile_hash_path.write(@lockfile_hash)
  run_bundle_install(@custom_gemfile)
end