rails-diff 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4068f995c11a16cd755643fa61b93a8ffa219d8c806dc78b9dbe5a1978d9e41c
4
- data.tar.gz: 5a46ee3bbfd5182daccd70a46daf471401b48daa0ea3da20cca03ef0d49becba
3
+ metadata.gz: 567e5e325109a4ed6c9daee6dd2c18a8c5bf20de492be9c4399c54347a2091fa
4
+ data.tar.gz: 6ef46901ce7d1445e0c8651323fdad776e9225223c4f2e4c1d6e506c31d70130
5
5
  SHA512:
6
- metadata.gz: eabc76fab91cde4c1f5be6e8ebcfdf5ed6649607532eb67ab0867e4817078cc4eeca6cb270dbd1f89fc26f335fda9a82f0ac826fbe5820af7701be05cb8c48bf
7
- data.tar.gz: 1b8fa68bb499cf107179b38168053c63906daf4af363e29bf664f4ecee4b7552caf36156a9e244e962e5c63bedac8116ef4a8cd6df1f59915730029ddb36423a
6
+ metadata.gz: 1b5259244a5b576f651c75981c807854bb82d71cf3b7af72074a9ec05dacb30db477af39dc2a2ab1a11a2d328da23cbeb1b9deec62a0c190355785299f1622d5
7
+ data.tar.gz: 6b037fd06f237a6a48d2b98e4dcd36609b8a2b3ece0a7bada82b38249b63ca20fdcbdb8ee707ecafefa0de40fd5db8d6c18a97be033f2771a620cfb1750d9c42
data/CHANGELOG.md CHANGED
@@ -1,9 +1,36 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.1] - 2025-02-21
3
+ ## [0.2.1] - 2025-02-22
4
+
5
+ - Add missing version command
6
+ - Consistent error messages
7
+ - Ensure rails path exists and dependencies are installed
8
+
9
+ ## [0.2.0] - 2025-02-21
10
+
11
+ - Allow comparing a specific commit
12
+
13
+ ```sh
14
+ rails-diff file Dockerfile --commit 3e7640
15
+ ```
16
+
17
+ - Allow failing the command when there are diffs
18
+
19
+ ```sh
20
+ rails-diff file Dockerfile --fail-on-diff
21
+ ```
22
+
23
+ - Return no output when there's no diff
24
+
25
+ M## [0.1.1] - 2025-02-21
4
26
 
5
27
  - Fix generator differ
6
28
 
7
29
  ## [0.1.0] - 2025-02-21
8
30
 
9
31
  - Initial release
32
+
33
+ [0.2.1]: https://github.com/matheusrich/rails-diff/releases/tag/v0.2.1
34
+ [0.2.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.2.0
35
+ [0.1.1]: https://github.com/matheusrich/rails-diff/releases/tag/v0.1.1
36
+ [0.1.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.1.0
data/README.md CHANGED
@@ -37,6 +37,12 @@ rails-diff file Dockerfile Gemfile
37
37
 
38
38
  # Force regenerate Rails app by clearing cache
39
39
  rails-diff file Dockerfile --clear-cache
40
+
41
+ # Fail if there are differences (useful for CI)
42
+ rails-diff file Dockerfile --fail-on-diff
43
+
44
+ # Compare a specific commit
45
+ rails-diff file Dockerfile --commit 7df1b8
40
46
  ```
41
47
 
42
48
  ### Compare generator files
@@ -53,8 +59,25 @@ rails-diff generated scaffold Post --clear-cache
53
59
 
54
60
  # Skip specific files or directories during the diff
55
61
  rails-diff generated scaffold Post --skip app/views app/helpers
62
+
63
+ # Fail if there are differences (useful for CI)
64
+ rails-diff generated scaffold Post --fail-on-diff
65
+
66
+ # Compare a specific commit
67
+ rails-diff generated authentication --commit 7df1b8
56
68
  ```
57
69
 
70
+ ### Options
71
+
72
+ #### --fail-on-diff
73
+
74
+ If this option is specified, the command will exit with a non-zero status code if there are any differences between your files and the generated ones. This can be particularly useful when using the gem in Continuous Integration (CI) environments.
75
+
76
+ #### --commit <commit_hash>
77
+
78
+ Specify the commit hash you want to compare against. If not provided, the latest
79
+ commit on main will be used by default.
80
+
58
81
  ## How it works
59
82
 
60
83
  When you run the diff, it will:
data/Rakefile CHANGED
@@ -6,3 +6,4 @@ require "rspec/core/rake_task"
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
8
  task default: :spec
9
+ task build: :spec
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rails
4
4
  module Diff
5
- VERSION = "0.1.1"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
data/lib/rails/diff.rb CHANGED
@@ -14,41 +14,54 @@ module Rails
14
14
  CACHE_DIR = File.expand_path("~/.rails-diff/cache")
15
15
 
16
16
  class << self
17
- def file(*files, no_cache: false)
17
+ def file(*files, no_cache: false, commit: nil)
18
18
  clear_cache if no_cache
19
- ensure_template_app_exists
19
+ ensure_template_app_exists(commit)
20
20
 
21
- files.map { |file| diff_with_header(file) }.join("\n")
21
+ files.filter_map { |it| diff_with_header(it) }.join("\n")
22
22
  end
23
23
 
24
- def generated(generator_name, *args, no_cache: false, skip: [])
24
+ def generated(generator_name, *args, no_cache: false, skip: [], commit: nil)
25
25
  clear_cache if no_cache
26
- ensure_template_app_exists
26
+ ensure_template_app_exists(commit)
27
27
  install_app_dependencies
28
28
 
29
29
  generated_files(generator_name, *args, skip)
30
- .map { |it| diff_generated_file(it) }
30
+ .map { |it| diff_with_header(it) }
31
31
  .join("\n\n")
32
32
  end
33
33
 
34
34
  private
35
35
 
36
- def app_name
37
- @app_name ||= File.basename(Dir.pwd)
36
+ def clear_cache
37
+ puts "Clearing cache"
38
+ FileUtils.rm_rf(CACHE_DIR)
39
+ end
40
+
41
+ def ensure_template_app_exists(commit)
42
+ FileUtils.mkdir_p(CACHE_DIR)
43
+ @commit = commit || latest_commit
44
+ return if cached_app?
45
+
46
+ FileUtils.rm_rf(template_app_path)
47
+ create_new_rails_app
38
48
  end
39
49
 
40
50
  def template_app_path
41
- @template_app_path ||= File.join(CACHE_DIR, app_name)
51
+ @template_app_path ||= File.join(CACHE_DIR, commit, app_name)
42
52
  end
43
53
 
44
54
  def rails_path
45
- @rails_path ||= File.join(CACHE_DIR, "rails")
55
+ @rails_path ||= begin
56
+ File.join(CACHE_DIR, "rails").tap do |path|
57
+ unless File.exist?(path)
58
+ system("git clone --depth 1 #{RAILS_REPO} #{path} >/dev/null 2>&1")
59
+ end
60
+ end
61
+ end
46
62
  end
47
63
 
48
- def clear_cache
49
- puts "Clearing cache..."
50
- FileUtils.rm_rf(CACHE_DIR)
51
- end
64
+ def app_name = @app_name ||= File.basename(Dir.pwd)
52
65
 
53
66
  def list_files(dir, skip = [])
54
67
  Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).reject do |it|
@@ -74,31 +87,22 @@ module Rails
74
87
  system("bin/rails destroy #{command} >/dev/null 2>&1")
75
88
  puts "Running generator: rails generate #{command}"
76
89
  track_new_files(skip) { system("bin/rails generate #{command} > /dev/null 2>&1") }
90
+ .map { |it| it.delete_prefix("#{template_app_path}/") }
77
91
  end
78
92
  end
79
93
 
80
94
  def diff_with_header(file)
81
- header = "#{file} diff:"
82
- [
83
- header,
84
- "=" * header.size,
85
- diff_file(file)
86
- ].join("\n")
87
- end
95
+ diff = diff_file(file)
96
+ return if diff.empty?
88
97
 
89
- def diff_generated_file(file)
90
- relative_path = file.delete_prefix("#{template_app_path}/")
91
- [
92
- "#{relative_path} diff:",
93
- "=" * (10 + relative_path.length),
94
- diff_file(relative_path)
95
- ].join("\n")
98
+ header = "#{file} diff:"
99
+ [header, "=" * header.size, diff].join("\n")
96
100
  end
97
101
 
98
102
  def install_app_dependencies
99
103
  Dir.chdir(template_app_path) do
100
104
  unless system("bundle check >/dev/null 2>&1")
101
- puts "Installing application dependencies..."
105
+ puts "Installing application dependencies"
102
106
  system("bundle install >/dev/null 2>&1")
103
107
  end
104
108
  end
@@ -112,19 +116,11 @@ module Rails
112
116
  return "#{file} not found in your repository" unless File.exist?(repo_file)
113
117
 
114
118
  Diffy::Diff.new(
115
- File.read(rails_file),
116
- File.read(repo_file),
117
- context: 2
118
- ).to_s(:color)
119
- end
120
-
121
- def ensure_template_app_exists
122
- FileUtils.mkdir_p(CACHE_DIR)
123
-
124
- return if cached_app?
125
-
126
- FileUtils.rm_rf(template_app_path)
127
- create_new_rails_app
119
+ rails_file,
120
+ repo_file,
121
+ context: 2,
122
+ source: 'files'
123
+ ).to_s(:color).chomp
128
124
  end
129
125
 
130
126
  def cached_app?
@@ -149,42 +145,68 @@ module Rails
149
145
  end
150
146
 
151
147
  def create_new_rails_app
152
- unless File.exist?(rails_path)
153
- system("git clone --depth 1 #{RAILS_REPO} #{rails_path} >/dev/null 2>&1")
154
- end
155
-
156
148
  Dir.chdir(rails_path) do
157
- commit = `git rev-parse HEAD`.strip
158
- puts "Using Rails edge (commit #{commit[0..6]})"
149
+ checkout_rails
150
+ generate_app
151
+ end
152
+ end
159
153
 
154
+ def generate_app
155
+ Dir.chdir("railties") do
160
156
  unless system("bundle check >/dev/null 2>&1")
161
- puts "Installing Rails dependencies..."
157
+ puts "Installing Rails dependencies"
162
158
  system("bundle install >/dev/null 2>&1")
163
159
  end
164
160
 
165
- Dir.chdir("railties") do
166
- puts "Generating new Rails application..."
167
- system("bundle exec rails new #{template_app_path} --main --skip-bundle --force --skip-test --skip-system-test --quiet")
168
- end
161
+ puts "Generating new Rails application"
162
+ system("bundle exec rails new #{template_app_path} --main --skip-bundle --force --skip-test --skip-system-test --quiet")
163
+ end
164
+ end
165
+
166
+ def checkout_rails
167
+ puts "Checking out Rails (at commit #{commit[0..6]})"
168
+ system("git checkout #{commit} >/dev/null 2>&1")
169
+ end
170
+
171
+ def commit = @commit
172
+
173
+ def latest_commit
174
+ Dir.chdir(rails_path) do
175
+ `git rev-parse origin/main`.strip
169
176
  end
170
177
  end
171
178
  end
172
179
 
173
180
  class CLI < Thor
174
181
  class_option :no_cache, type: :boolean, desc: "Clear cache before running", aliases: ["--clear-cache"]
182
+ class_option :fail_on_diff, type: :boolean, desc: "Fail if there are differences"
183
+ class_option :commit, type: :string, desc: "Compare against a specific commit"
184
+
175
185
  def self.exit_on_failure? = true
176
186
 
177
187
  desc "file FILE [FILE ...]", "Compare one or more files from your repository with Rails' generated version"
178
188
  def file(*files)
179
189
  abort "Please provide at least one file to compare" if files.empty?
180
190
 
181
- puts Rails::Diff.file(*files, no_cache: options[:no_cache])
191
+ diff = Rails::Diff.file(*files, no_cache: options[:no_cache], commit: options[:commit])
192
+ return if diff.empty?
193
+
194
+ options[:fail] ? abort(diff) : puts(diff)
182
195
  end
183
196
 
184
197
  desc "generated GENERATOR [args]", "Compare files that would be created by a Rails generator"
185
198
  option :skip, type: :array, desc: "Skip specific files or directories", aliases: ["-s"], default: []
186
199
  def generated(generator_name, *args)
187
- puts Rails::Diff.generated(generator_name, *args, no_cache: options[:no_cache], skip: options[:skip])
200
+ diff = Rails::Diff.generated(generator_name, *args, no_cache: options[:no_cache], skip: options[:skip], commit: options[:commit])
201
+ return if diff.empty?
202
+
203
+ options[:fail] ? abort(diff) : puts(diff)
204
+ end
205
+
206
+ map %w[--version -v] => :__version
207
+ desc "--version, -v", "print the version"
208
+ def __version
209
+ puts VERSION
188
210
  end
189
211
  end
190
212
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matheus Richard
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-21 00:00:00.000000000 Z
10
+ date: 2025-02-22 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails