Skip to content

Instantly share code, notes, and snippets.

@arika
Last active March 18, 2020 04:13
Show Gist options
  • Save arika/4f45e6d69914ce29a8571b9d8b86e5b7 to your computer and use it in GitHub Desktop.
Save arika/4f45e6d69914ce29a8571b9d8b86e5b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# rubocop-daemon-wrapper.rb
require 'rubygems'
if $0 == __FILE__
bin_dir = Gem::Specification.find_by_name('rubocop-daemon')&.bin_dir
abort 'rubocop-daemon.gem not found' unless bin_dir
ENV['RUBYOPT'] = "-r #{File.expand_path(__FILE__).sub(/\.rb\z/, '')}"
exec(File.join(bin_dir, '../bin/rubocop-daemon-wrapper'), *ARGV)
end
def activate_rubocop_gems(lockfile_path)
in_gem = in_spec = false
File.foreach(lockfile_path) do |line|
case line
when /^Gem/
in_gem = true
when /^\S/
break if in_gem
when /^ specs:/
in_spec = in_gem
when /^ \S/
break if in_spec
when /^ (?<name>rubocop\S*) \((?<version>[.\d]+)\)$/
m = Regexp.last_match
gem m[:name], "=#{m[:version]}"
end
end
end
dir = Dir.pwd
while dir.start_with?(__dir__)
begin
activate_rubocop_gems("#{dir}/Gemfile.lock")
break
rescue SystemCallError
dir = File.dirname(dir)
end
end
#!/bin/sh
# rubocop-spring-wrapper.sh
exec spring rubocop "$@"
function! s:set_ale_ruby_rubocop_executable()
let l:project = bundler#project()
if empty(l:project) || !l:project.has('rubocop')
return
endif
let l:project_root = l:project['root']
let l:project_rubocop_daemon_wrapper = l:project_root.'/../rubocop-daemon-wrapper.rb'
let l:project_rubocop_spring_wrapper = l:project_root.'/../rubocop-spring-wrapper.sh'
let l:rubocop_binstub = l:project_root.'/bin/rubocop'
if executable(l:project_rubocop_daemon_wrapper)
unlet $RUBOCOP_DAEMON_USE_BUNDLER
let b:ale_ruby_rubocop_executable = l:project_rubocop_daemon_wrapper
elseif l:project.has('rubocop-daemon')
let $RUBOCOP_DAEMON_USE_BUNDLER = '1'
let b:ale_ruby_rubocop_executable = l:project.paths()['rubocop-daemon'].'/bin/rubocop-daemon-wrapper'
elseif executable(l:project_rubocop_spring_wrapper)
let b:ale_ruby_rubocop_executable = l:project_rubocop_spring_wrapper
elseif l:project.has('spring-commands-rubocop') && executable(l:rubocop_binstub)
let b:ale_ruby_rubocop_executable = l:rubocop_binstub
else
let b:ale_ruby_rubocop_executable = 'bundle'
endif
endfunction
augroup my_ale_ruby_rubocop_setting
au!
au FileType ruby :call s:set_ale_ruby_rubocop_executable()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment