Skip to content

Commit c63c5ba

Browse files
etagwerkerclaude
andcommitted
Add rails-stats command line executable
Add a standalone `rails-stats` executable so the tool can be run against a directory without setting up a Rakefile. It accepts both absolute and relative paths, defaults to the current directory when none is given, and optionally takes a `json` second argument for JSON output. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 1a5f5c7 commit c63c5ba

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# main ([unreleased](https://github.com/fastruby/rails_stats/compare/v2.1.0...main))
22

3+
* [FEATURE: Add a `rails-stats` command line executable that accepts an absolute or relative directory](https://github.com/fastruby/rails_stats/pull/49)
34
* [FEATURE: Support component-based and Packwerk (packs) applications with a per-pack and Core breakdown](https://github.com/fastruby/rails_stats/issues/23)
45
* [BUGFIX: Respect an explicit path argument (e.g. `rake stats[packs/pack1]`) when running inside a booted Rails app](https://github.com/fastruby/rails_stats/issues/23)
56
* [CHORE: Improve the GH Test Workflow](https://github.com/fastruby/rails_stats/pull/35)

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ There were a few things missing to the included `rake stats`
66

77
RailsStats mainly adds the ability to be run from outside the project in question. This can be helpful if the app you are interested in can not be booted for some reason.
88

9+
### Run it as a command line tool
10+
11+
Once the gem is installed you can point the `rails-stats` executable at any
12+
directory, without setting up a `Rakefile`. It accepts both absolute and
13+
relative paths:
14+
15+
```bash
16+
$ rails-stats /path/to/app
17+
$ rails-stats path/to/app
18+
```
19+
20+
When no path is given it defaults to the current directory:
21+
22+
```bash
23+
$ rails-stats
24+
```
25+
26+
You can also request JSON output by passing `json` as a second argument:
27+
28+
```bash
29+
$ rails-stats path/to/app json
30+
```
31+
932
### Run it outside Rails project
1033

1134
You will need a `Rakefile` in the directory where you call `rake` and you will

bin/rails-stats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Standalone CLI for running rails_stats against a directory without having to
5+
# set up a Rakefile. Accepts both absolute and relative paths:
6+
#
7+
# rails-stats /path/to/app
8+
# rails-stats path/to/app
9+
# rails-stats # defaults to the current directory
10+
# rails-stats path/to/app json
11+
#
12+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
13+
14+
require "rails_stats/all"
15+
16+
path = ARGV[0] || "."
17+
format = ARGV[1] || ""
18+
19+
root_directory = File.absolute_path(path)
20+
21+
unless File.directory?(root_directory)
22+
warn "rails-stats: no such directory - #{path}"
23+
exit 1
24+
end
25+
26+
puts "\nDirectory: #{root_directory}\n\n"
27+
RailsStats::CodeStatistics.new(root_directory, format: format).to_s

0 commit comments

Comments
 (0)