Skip to content

Commit 3b81273

Browse files
authored
Merge pull request #125 from molawson/config-file-support
Add support for a simple config file
2 parents f1ed1e9 + bd85cd5 commit 3b81273

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

README.md

+14
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

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ 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+
CONFIG_FILE = ".streerc"
8+
79
# A utility wrapper around colored strings in the output.
810
class Color
911
attr_reader :value, :code
@@ -269,6 +271,11 @@ def run(argv)
269271
name, *arguments = argv
270272
print_width = DEFAULT_PRINT_WIDTH
271273

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

test/cli_test.rb

+71
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)