Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b17324e

Browse files
committedAug 19, 2022
Split out config file logic
1 parent d15f400 commit b17324e

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed
 

‎lib/syntax_tree/cli.rb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module SyntaxTree
66
# Syntax Tree ships with the `stree` CLI, which can be used to inspect and
77
# manipulate Ruby code. This module is responsible for powering that CLI.
88
module CLI
9-
CONFIG_FILE = ".streerc"
10-
119
# A utility wrapper around colored strings in the output.
1210
class Color
1311
attr_reader :value, :code
@@ -289,16 +287,41 @@ def parser
289287
end
290288
end
291289

290+
# We allow a minimal configuration file to act as additional command line
291+
# arguments to the CLI. Each line of the config file should be a new
292+
# argument, as in:
293+
#
294+
# --plugins=plugin/single_quote
295+
# --print-width=100
296+
#
297+
# When invoking the CLI, we will read this config file and then parse it if
298+
# it exists in the current working directory.
299+
class ConfigFile
300+
FILENAME = ".streerc"
301+
302+
attr_reader :filepath
303+
304+
def initialize
305+
@filepath = File.join(Dir.pwd, FILENAME)
306+
end
307+
308+
def exists?
309+
File.readable?(filepath)
310+
end
311+
312+
def arguments
313+
exists? ? File.readlines(filepath, chomp: true) : []
314+
end
315+
end
316+
292317
class << self
293318
# Run the CLI over the given array of strings that make up the arguments
294319
# passed to the invocation.
295320
def run(argv)
296321
name, *arguments = argv
297322

298-
config_file = File.join(Dir.pwd, CONFIG_FILE)
299-
if File.readable?(config_file)
300-
arguments.unshift(*File.readlines(config_file, chomp: true))
301-
end
323+
config_file = ConfigFile.new
324+
arguments.unshift(*config_file.arguments)
302325

303326
options = Options.new
304327
options.parse(arguments)

‎lib/syntax_tree/language_server.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,9 @@ def write(value)
119119
output.print("Content-Length: #{response.bytesize}\r\n\r\n#{response}")
120120
output.flush
121121
end
122+
123+
def log(message)
124+
write(method: "window/logMessage", params: { type: 4, message: message })
125+
end
122126
end
123127
end

‎test/cli_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def run_cli(command, *args, contents: :default)
242242
end
243243

244244
def with_config_file(contents)
245-
filepath = File.join(Dir.pwd, SyntaxTree::CLI::CONFIG_FILE)
245+
filepath = File.join(Dir.pwd, SyntaxTree::CLI::ConfigFile::FILENAME)
246246
File.write(filepath, contents)
247247

248248
yield

0 commit comments

Comments
 (0)
Please sign in to comment.