Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0b459c9

Browse files
committed
Add support for a simple config file
As syntax_tree gains wider usage, it'll be very helpful to allow projects to commit their particular configs within their repo for use by any tool that uses `stree` instead of having to create wrapper scripts, rely on rake tasks that come with their own overhead, or manually keep settings up to date among all contributors. This set of changes takes inspiration from sorbet to provide users with a simple config file that's also easy to parse and use within the gem. It's a text file that has the exact options you'd pass on the command line, each on their own line. These are parsed and prepended to the arguments array within `CLI.run`, still allowing for other options to passed from the command line. I decided to restrict this only to the command line options and avoid the source files argument, opting to let other tools pass their own source file from the command line, which is preferable for tools like editor integrations that might interact with a single file at a time. If users want to interact with all of their Ruby files at once, the rake tasks are perfect for providing larger, static patterns of files. And since they use `CLI.run` as well, they'll pick up options from a .streerc file, if present. I also opted for only supporting a single .streerc file at the project root. If there's a need for multiple configs or config locations, this can be easily extended to look up through a directory structure or accept an option for a specific config file location (or even a different filename). Those felt out of scope for this initial support.
1 parent cf97c1b commit 0b459c9

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ It is built with only standard library dependencies. It additionally ships with
1919
- [json](#json)
2020
- [match](#match)
2121
- [write](#write)
22+
- [Configuration](#configuration)
2223
- [Library](#library)
2324
- [SyntaxTree.read(filepath)](#syntaxtreereadfilepath)
2425
- [SyntaxTree.parse(source)](#syntaxtreeparsesource)
@@ -231,6 +232,19 @@ To change the print width that you are writing with, specify the `--print-width`
231232
stree write --print-width=100 path/to/file.rb
232233
```
233234

235+
### Configuration
236+
237+
Any of the above CLI commands can also read configuration options from a `.streerc` file in the directory where the commands are executed.
238+
239+
This should be a text file with each argument on a separate line.
240+
241+
```txt
242+
--print-width=100
243+
--plugins=plugin/trailing_comma
244+
```
245+
246+
If this file is present, it will _always_ be used for CLI commands. You can also pass options from the command line as in the examples above. The options in the `.streerc` file are passed to the CLI first, then the arguments from the command line. In the case of exclusive options (e.g. `--print-width`), this means that the command line options override what's in the config file. In the case of options that can take multiple inputs (e.g. `--plugins`), the effect is additive. That is, the plugins passed from the command line will be loaded _in addition to_ the plugins in the config file.
247+
234248
## Library
235249

236250
Syntax Tree can be used as a library to access the syntax tree underlying Ruby source code.

lib/syntax_tree/cli.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module SyntaxTree
44
# Syntax Tree ships with the `stree` CLI, which can be used to inspect and
55
# manipulate Ruby code. This module is responsible for powering that CLI.
66
module CLI
7+
8+
CONFIG_FILE = ".streerc"
9+
710
# A utility wrapper around colored strings in the output.
811
class Color
912
attr_reader :value, :code
@@ -269,6 +272,11 @@ def run(argv)
269272
name, *arguments = argv
270273
print_width = DEFAULT_PRINT_WIDTH
271274

275+
config_file = File.join(Dir.pwd, CONFIG_FILE)
276+
if File.readable?(config_file)
277+
arguments.unshift(*File.readlines(config_file, chomp: true))
278+
end
279+
272280
while arguments.first&.start_with?("--")
273281
case (argument = arguments.shift)
274282
when /^--plugins=(.+)$/

test/cli_test.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,77 @@ def test_language_server
180180
$stdout = prev_stdout
181181
end
182182

183+
def test_config_file
184+
config_file = File.join(Dir.pwd, SyntaxTree::CLI::CONFIG_FILE)
185+
config = <<~TXT
186+
--print-width=100
187+
--plugins=plugin
188+
TXT
189+
File.write(config_file, config)
190+
191+
Dir.mktmpdir do |directory|
192+
Dir.mkdir(File.join(directory, "syntax_tree"))
193+
$:.unshift(directory)
194+
195+
File.write(
196+
File.join(directory, "syntax_tree", "plugin.rb"),
197+
"puts 'Hello, world!'"
198+
)
199+
200+
file = Tempfile.new(%w[test- .rb])
201+
contents = "#{"a" * 40} + #{"b" * 40}\n"
202+
file.write(contents)
203+
204+
result = run_cli("format", file: file)
205+
assert_equal("Hello, world!\n#{contents}", result.stdio)
206+
end
207+
ensure
208+
FileUtils.rm(config_file)
209+
end
210+
211+
def test_print_width_args_with_config_file
212+
config_file = File.join(Dir.pwd, SyntaxTree::CLI::CONFIG_FILE)
213+
File.write(config_file, "--print-width=100")
214+
215+
contents = "#{"a" * 40} + #{"b" * 40}\n"
216+
217+
file = Tempfile.new(%w[test- .rb])
218+
file.write(contents)
219+
result = run_cli("check", file: file)
220+
assert_includes(result.stdio, "match")
221+
222+
file = Tempfile.new(%w[test- .rb])
223+
file.write(contents)
224+
result = run_cli("check", "--print-width=82", file: file)
225+
assert_includes(result.stderr, "expected")
226+
ensure
227+
FileUtils.rm(config_file)
228+
end
229+
230+
def test_plugin_args_with_config_file
231+
config_file = File.join(Dir.pwd, SyntaxTree::CLI::CONFIG_FILE)
232+
File.write(config_file, "--plugins=hello_plugin")
233+
234+
Dir.mktmpdir do |directory|
235+
Dir.mkdir(File.join(directory, "syntax_tree"))
236+
$:.unshift(directory)
237+
238+
File.write(
239+
File.join(directory, "syntax_tree", "hello_plugin.rb"),
240+
"puts 'Hello, world!'"
241+
)
242+
File.write(
243+
File.join(directory, "syntax_tree", "bye_plugin.rb"),
244+
"puts 'Bye, world!'"
245+
)
246+
247+
result = run_cli("format", "--plugins=bye_plugin")
248+
assert_equal("Hello, world!\nBye, world!\ntest\n", result.stdio)
249+
end
250+
ensure
251+
FileUtils.rm(config_file)
252+
end
253+
183254
private
184255

185256
Result = Struct.new(:status, :stdio, :stderr, keyword_init: true)

0 commit comments

Comments
 (0)