Skip to content

Commit

Permalink
Merge pull request #53 from lxxxvi/allow-writing-to-STDOUT
Browse files Browse the repository at this point in the history
Allow writing to stdout
  • Loading branch information
lxxxvi authored Oct 25, 2024
2 parents d1f35ad + 9c8845b commit b9c407e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 15 deletions.
59 changes: 48 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

Finally it orders the configurations **alphabetically** within these groups.

## Example
## Main example

### Input `.rubocop.yml`:

Expand Down Expand Up @@ -84,28 +84,65 @@ gem install ruboclean
## Command synopsis

```shell
ruboclean [path] [--silent] [--preserve-comments] [--preserve-paths] [--verify]
ruboclean [path] \
[--output=/path/to/file.yml] \
[--silent] \
[--preserve-comments] \
[--preserve-paths] \
[--verify]
```

### Parameters

| Parameter | Description |
|:---------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------|
| `path` | Can be a directory that contains a `.rubocop.yml`, or a path to a `.rubocop.yml` directly. Defaults to the current working directory. |
| `--output=[/path/to/output.yml]` | Output path, where the result is written to. Can be either absolute or relative of the current working directory |
| `--silent` | Suppress any output displayed on the screen when executing the command. |
| `--preserve-comments` | Preserves **preceding** comments for each top-level entry in the configuration. Inline comments are **not** preserved. |
| `--preserve-paths` | Skips the path cleanup that are applied against `Include:` and `Exclude:` configuration. |
| `--verify` | Executes in dry-run mode. Exits with 1 if changes are needed, otherwise 0. |
#### `path`

### Examples
Can be a directory that contains a `.rubocop.yml`, or a path to a `.rubocop.yml` directly.
Defaults to the current working directory.

##### Examples

```shell
ruboclean # uses `.rubocop.yml` of current working directory
ruboclean /path/to/dir # uses `.rubocop.yml` of /path/to/dir
ruboclean /path/to/dir/.rubocop.yml
```

#### `--output=/path/to/file.yml`

Output path where the result is written to.
Can be absolute or relative to the current working directory.
`--output=STDOUT` prints it to STDOUT and not to a file.

##### Examples

```shell
ruboclean --output=/absolute/path.yml
ruboclean --output=relative/path.yml # relative to current working directory
ruboclean --output=STDOUT # does not write anything to a file
```

#### `--silent`

Suppress any log output displayed on the screen when executing the command.
It still prints to STDOUT if used in combination with `--output=STDOUT`.

#### `--preserve-comments`

Preserves **preceding** comments for each top-level entry in the configuration.
Inline comments are **not** preserved.

See main example above for explanation.

#### `--preserve-paths`

Skips the path cleanup that are applied against `Include:` and `Exclude:` configurations.

See main example above for explanation.

#### `--verify`

Executes in dry-run mode. Exits with `1` if changes are needed, otherwise `0`.

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
1 change: 1 addition & 0 deletions lib/ruboclean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "ruboclean/grouper"
require "ruboclean/path_cleanup"
require "ruboclean/runner"
require "ruboclean/stream_writer"
require "ruboclean/to_yaml_converter"
require "ruboclean/version"

Expand Down
8 changes: 5 additions & 3 deletions lib/ruboclean/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run!
load_file.then(&method(:order))
.then(&method(:cleanup_paths))
.then(&method(:convert_to_yaml))
.then(&method(:write_file!))
.then(&method(:write_stream!))
.then(&method(:changed?))
end

Expand Down Expand Up @@ -62,12 +62,14 @@ def convert_to_yaml(configuration_hash)
ToYamlConverter.new(configuration_hash, cli_arguments.preserve_comments?, source_yaml).to_yaml
end

def write_file!(target_yaml)
def write_stream!(target_yaml)
target_yaml.tap do |content|
target_file_pathname.write(content) unless verify?
StreamWriter.new(target_file_pathname, content).write! unless verify?
end
end

# TODO: Find a better place to compute source_file_pathname and target_file_pathname
# Preferrably it should happen early in the lifecycle, as it includes (and raises) argument validations
def source_file_pathname
@source_file_pathname ||= find_source_file_pathname
end
Expand Down
27 changes: 27 additions & 0 deletions lib/ruboclean/stream_writer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Ruboclean
# Orders the items within the groups alphabetically
class StreamWriter
def initialize(target_file_pathname, content)
@target_file_pathname = target_file_pathname
@content = content
end

def write!
if stdout?
puts content
else
target_file_pathname.write(content)
end
end

private

attr_reader :target_file_pathname, :content

def stdout?
target_file_pathname.basename.to_s == "STDOUT"
end
end
end
12 changes: 11 additions & 1 deletion test/ruboclean/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_run_with_output_argument_absolute_path
end

def test_run_with_output_argument_relative_path # rubocop:disable Metrics/MethodLength
# we need to read it here, because we're leaving the working directory soon
# we need to read it here, because we're leaving the working directory (see `Dir.chdir` below)
expected_target_file_content = fixture_file_path("00_expected_output.yml").read

using_fixture_files("00_input.yml") do |fixture_path|
Expand All @@ -124,6 +124,16 @@ def test_run_with_output_argument_relative_path # rubocop:disable Metrics/Method
end
end

def test_run_with_output_argument_stdout
using_fixture_files("00_input.yml") do |fixture_path|
arguments = [fixture_path, "--output=STDOUT"]

assert_output(fixture_file_path("00_expected_output.yml").read) do
Ruboclean::Runner.new(arguments).run!
end
end
end

def test_run_with_output_argument_overrides_a_file
using_fixture_files("00_input.yml") do |fixture_path|
output_pathname = Pathname.new(fixture_path).dirname.join("custom_output_path.yml")
Expand Down
24 changes: 24 additions & 0 deletions test/ruboclean/stream_writer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "test_helper"

module Ruboclean
class StreamWriterTest < BaseTest
def test_writes_to_path
Tempfile.create do |tmpfile|
target_file_pathname = Pathname.new(tmpfile)
Ruboclean::StreamWriter.new(target_file_pathname, "magnificent file output").write!

assert_equal "magnificent file output", target_file_pathname.read
end
end

def test_writes_to_stdout
target_file_pathname = Pathname.new("STDOUT")

assert_output("magnificent STDOUT output\n") do
Ruboclean::StreamWriter.new(target_file_pathname, "magnificent STDOUT output").write!
end
end
end
end

0 comments on commit b9c407e

Please sign in to comment.