rails-diff 0.1.0 → 0.2.0

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: a5049aad3c07aafb46e197ad4a1e57655615d1c0b577ab07939a13cb0a28cce9
4
- data.tar.gz: 589d63e3f324dc0a86b9816586d88517146f9e2dc84f71c4b10c7b414e4de054
3
+ metadata.gz: 1597a0a52bb69e722c40bdd8714a79d6ab42fcef23a844484c91ca70496d7e4b
4
+ data.tar.gz: 92e9120a9d970073725b002ea0ae8a2144bfdc075b30ba02e637ce86fdcebee8
5
5
  SHA512:
6
- metadata.gz: cf848b14e60228991ee6f6091f17a49ad30e69fe3efd1fdbddf9a069826a6ba4af0460ec4e6e9891bd1dfcd5762a6b457e7c68c115972229ca18a64f375ef5ae
7
- data.tar.gz: f72083a3b5329062e0939ad04c7dc3745262d755550197a4e8e5e814a9561705e9c9cee45a9e010a5d323698fe8987240e33a8d1cb7dd8555431ca1fa7d98a44
6
+ metadata.gz: 55149e9e567a6a63d6f0b6af85351d29a26af4c48ca16a8053cf46768cb883bfc16c8a908ab89e8047897194802ded032145b58d185f62772d5e81ca308d5120
7
+ data.tar.gz: 716c754d1e4ff11ba617007aceb4de23fdea5cae49daaeedf794f3c497ad307e02d1827c48944d3fc4621b74427e6dba76c48089d878f36141f17e15f1c7a87b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2025-02-21
4
+
5
+ - Allow comparing a specific commit
6
+ ```sh
7
+ rails-diff file Dockerfile --commit 3e7640
8
+ ```
9
+
10
+ - Allow failing the command when there are diffs
11
+
12
+ ```sh
13
+ rails-diff file Dockerfile --fail-on-diff
14
+ ```
15
+
16
+ - Return no output when there's no diff
17
+
18
+ M## [0.1.1] - 2025-02-21
19
+
20
+ - Fix generator differ
21
+
3
22
  ## [0.1.0] - 2025-02-21
4
23
 
5
24
  - Initial release
25
+
26
+ [0.2.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.2.0
27
+ [0.1.1]: https://github.com/matheusrich/rails-diff/releases/tag/v0.1.1
28
+ [0.1.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.1.0
data/README.md CHANGED
@@ -30,13 +30,19 @@ $ gem install rails-diff
30
30
 
31
31
  ```bash
32
32
  # Compare a single file
33
- rails-diff file config/routes.rb
33
+ rails-diff file Dockerfile
34
34
 
35
35
  # Compare multiple files
36
- rails-diff file config/routes.rb config/application.rb
36
+ rails-diff file Dockerfile Gemfile
37
37
 
38
38
  # Force regenerate Rails app by clearing cache
39
- rails-diff file config/routes.rb --clear-cache
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 release: :spec
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rails
4
4
  module Diff
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
data/lib/rails/diff.rb CHANGED
@@ -14,37 +14,35 @@ 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
- generated_files(skip)
30
- .map { |it| diff_generated_file(it) }
29
+ generated_files(generator_name, *args, skip)
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)
38
- end
39
-
40
36
  def template_app_path
41
- @template_app_path ||= File.join(CACHE_DIR, app_name)
37
+ @template_app_path ||= File.join(CACHE_DIR, commit, app_name)
42
38
  end
43
39
 
44
40
  def rails_path
45
41
  @rails_path ||= File.join(CACHE_DIR, "rails")
46
42
  end
47
43
 
44
+ def app_name = @app_name ||= File.basename(Dir.pwd)
45
+
48
46
  def clear_cache
49
47
  puts "Clearing cache..."
50
48
  FileUtils.rm_rf(CACHE_DIR)
@@ -68,31 +66,22 @@ module Rails
68
66
  files_after - files_before
69
67
  end
70
68
 
71
- def generated_files(skip)
69
+ def generated_files(generator_name, *args, skip)
72
70
  command = "#{generator_name} #{args.join(' ')}"
73
71
  Dir.chdir(template_app_path) do
74
72
  system("bin/rails destroy #{command} >/dev/null 2>&1")
75
73
  puts "Running generator: rails generate #{command}"
76
74
  track_new_files(skip) { system("bin/rails generate #{command} > /dev/null 2>&1") }
75
+ .map { |it| it.delete_prefix("#{template_app_path}/") }
77
76
  end
78
77
  end
79
78
 
80
79
  def diff_with_header(file)
81
- header = "#{file} diff:"
82
- [
83
- header,
84
- "=" * header.size,
85
- diff_file(file)
86
- ].join("\n")
87
- end
80
+ diff = diff_file(file)
81
+ return if diff.empty?
88
82
 
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")
83
+ header = "#{file} diff:"
84
+ [header, "=" * header.size, diff].join("\n")
96
85
  end
97
86
 
98
87
  def install_app_dependencies
@@ -112,15 +101,16 @@ module Rails
112
101
  return "#{file} not found in your repository" unless File.exist?(repo_file)
113
102
 
114
103
  Diffy::Diff.new(
115
- File.read(rails_file),
116
- File.read(repo_file),
117
- context: 2
118
- ).to_s(:color)
104
+ rails_file,
105
+ repo_file,
106
+ context: 2,
107
+ source: 'files'
108
+ ).to_s(:color).chomp
119
109
  end
120
110
 
121
- def ensure_template_app_exists
111
+ def ensure_template_app_exists(commit)
122
112
  FileUtils.mkdir_p(CACHE_DIR)
123
-
113
+ @commit = commit || latest_commit
124
114
  return if cached_app?
125
115
 
126
116
  FileUtils.rm_rf(template_app_path)
@@ -154,37 +144,56 @@ module Rails
154
144
  end
155
145
 
156
146
  Dir.chdir(rails_path) do
157
- commit = `git rev-parse HEAD`.strip
158
- puts "Using Rails edge (commit #{commit[0..6]})"
147
+ checkout_rails
148
+ generate_app
149
+ end
150
+ end
159
151
 
160
- unless system("bundle check >/dev/null 2>&1")
161
- puts "Installing Rails dependencies..."
162
- system("bundle install >/dev/null 2>&1")
163
- end
152
+ def generate_app
153
+ Dir.chdir("railties") do
154
+ puts "Generating new Rails application..."
155
+ system("bundle exec rails new #{template_app_path} --main --skip-bundle --force --skip-test --skip-system-test --quiet")
156
+ end
157
+ end
164
158
 
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
159
+ def checkout_rails
160
+ puts "Checking out Rails (at commit #{commit[0..6]})"
161
+ system("git checkout #{commit} >/dev/null 2>&1")
162
+ end
163
+
164
+ def commit = @commit
165
+
166
+ def latest_commit
167
+ Dir.chdir(rails_path) do
168
+ `git rev-parse origin/main`.strip
169
169
  end
170
170
  end
171
171
  end
172
172
 
173
173
  class CLI < Thor
174
174
  class_option :no_cache, type: :boolean, desc: "Clear cache before running", aliases: ["--clear-cache"]
175
+ class_option :fail_on_diff, type: :boolean, desc: "Fail if there are differences"
176
+ class_option :commit, type: :string, desc: "Compare against a specific commit"
177
+
175
178
  def self.exit_on_failure? = true
176
179
 
177
180
  desc "file FILE [FILE ...]", "Compare one or more files from your repository with Rails' generated version"
178
181
  def file(*files)
179
182
  abort "Please provide at least one file to compare" if files.empty?
180
183
 
181
- puts Rails::Diff.file(*files, no_cache: options[:no_cache])
184
+ diff = Rails::Diff.file(*files, no_cache: options[:no_cache], commit: options[:commit])
185
+ return if diff.empty?
186
+
187
+ options[:fail] ? abort(diff) : puts(diff)
182
188
  end
183
189
 
184
190
  desc "generated GENERATOR [args]", "Compare files that would be created by a Rails generator"
185
191
  option :skip, type: :array, desc: "Skip specific files or directories", aliases: ["-s"], default: []
186
192
  def generated(generator_name, *args)
187
- puts Rails::Diff.generated(generator_name, *args, no_cache: options[:no_cache], skip: options[:skip])
193
+ diff = Rails::Diff.generated(generator_name, *args, no_cache: options[:no_cache], skip: options[:skip], commit: options[:commit])
194
+ return if diff.empty?
195
+
196
+ options[:fail] ? abort(diff) : puts(diff)
188
197
  end
189
198
  end
190
199
  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.0
4
+ version: 0.2.0
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
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
- rubygems_version: 3.6.2
94
+ rubygems_version: 3.6.5
95
95
  specification_version: 4
96
96
  summary: Compare Rails-generated files with the ones in your repository
97
97
  test_files: []