Skip to content

Commit

Permalink
Add RUBOCOP_DAEMON_RUBOCOP_VERSION to rubocop-daemon-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
RaMin0 committed Apr 11, 2021
1 parent c8fcb0a commit 8dad3a6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ $ export RUBOCOP_DAEMON_USE_BUNDLER=true

Now `rubocop-daemon-wrapper` will call the `rubocop-daemon` command with `bundle exec`.

### Use with a specific version of RuboCop

If you have multiple versions of RuboCop installed, you can set `RUBOCOP_DAEMON_RUBOCOP_VERSION` environment variable to the specific version you want `rubocop-daemon` to use:

```console
$ export RUBOCOP_DAEMON_RUBOCOP_VERSION=x.y.z
```

Now `rubocop-daemon-wrapper` will start `rubocop-daemon` with the specified version.

> (Make sure you stop the server if it's already running before setting this environment variable.)
### Use with VS Code

Unfortunately, the [vscode-ruby extension doesn't really allow you to customize the `rubocop` path or binary](https://github.com/rubyide/vscode-ruby/issues/413). (You can change the linter path, but not the formatter.)
Expand Down
10 changes: 8 additions & 2 deletions bin/rubocop-daemon-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ if [ -n "$RUBOCOP_DAEMON_USE_BUNDLER" ]; then
COMMAND_PREFIX="bundle exec"
fi

RUBOCOP_DAEMON_OPTS=""

if [ -n "$RUBOCOP_DAEMON_RUBOCOP_VERSION" ]; then
RUBOCOP_DAEMON_OPTS="--rubocop-version $RUBOCOP_DAEMON_RUBOCOP_VERSION"
fi

if ! command -v rubocop-daemon > /dev/null; then
$COMMAND_PREFIX rubocop $@
exit $?
Expand Down Expand Up @@ -97,7 +103,7 @@ for ARG in $@; do
done

if [ ! -f "$TOKEN_PATH" ]; then
$RUBOCOP_DAEMON start
$RUBOCOP_DAEMON start $RUBOCOP_DAEMON_OPTS
fi

run_rubocop_command() {
Expand All @@ -122,7 +128,7 @@ if ! run_rubocop_command $@; then
rm -rf "$CACHE_DIR"
pkill -f "rubocop-daemon (re)?start"
echo "Starting new rubocop-daemon server..." >&2
$RUBOCOP_DAEMON start
$RUBOCOP_DAEMON start $RUBOCOP_DAEMON_OPTS
if ! run_rubocop_command $@; then
echo "Sorry, something went wrong with rubocop-daemon!" >&2
echo "Please try updating the gem or re-installing the rubocop-daemon-wrapper script."
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/daemon/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run(argv = ARGV)
return if argv.empty?

create_subcommand_instance(argv)
rescue OptionParser::InvalidOption => e
rescue OptionParser::InvalidOption, InvalidRuboCopVersionError => e
warn "error: #{e.message}"
exit 1
rescue UnknownClientCommandError => e
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/daemon/client_command/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def run
end

parser.parse(@argv)
Server.new(@options.fetch(:no_daemon, false)).start(@options.fetch(:port, 0))
Server.new(@options.fetch(:no_daemon, false)).start(@options.fetch(:port, 0), @options.fetch(:rubocop_version, nil))
end
end

Expand All @@ -31,6 +31,7 @@ def parser

p.on('-p', '--port [PORT]') { |v| @options[:port] = v }
p.on('--no-daemon', 'Starts server in foreground with debug information') { @options[:no_daemon] = true }
p.on('--rubocop-version [VERSION]', 'The version of rubocop to use.') { |v| @options[:rubocop_version] = v }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/daemon/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class InvalidTokenError < StandardError; end
class ServerStopRequest < StandardError; end
class UnknownClientCommandError < StandardError; end
class UnknownServerCommandError < StandardError; end
class InvalidRuboCopVersionError < StandardError; end
end
end
16 changes: 14 additions & 2 deletions lib/rubocop/daemon/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def token
self.class.token
end

def start(port)
require 'rubocop'
def start(port, rubocop_version)
require_rubocop(rubocop_version)
start_server(port)
Cache.write_port_and_token_files(port: @server.addr[1], token: token)
Process.daemon(true) unless verbose
Expand All @@ -33,6 +33,18 @@ def start(port)

private

def require_rubocop(version = nil)
begin
rubocop_path = Gem::Dependency.new('rubocop', version).to_spec.full_gem_path
$LOAD_PATH.unshift(File.join(rubocop_path, 'lib'))
rescue Gem::MissingSpecVersionError => e
raise InvalidRuboCopVersionError,
"could not find '#{e.name}' (#{e.requirement}) - "\
"did find: [#{e.specs.map { |s| s.version.version }.join(', ')}]"
end
require 'rubocop'
end

def start_server(port)
@server = TCPServer.open('127.0.0.1', port)
puts "Server listen on port #{@server.addr[1]}" if verbose
Expand Down

0 comments on commit 8dad3a6

Please sign in to comment.