rails-diff 0.3.0 → 0.4.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: 93b1be272240b33b7c9357f98d876b1816fab44a032f7f655cc3b3c65a6bc925
4
- data.tar.gz: 8201af2847c96ac8bda63200fffcc9a45179f571f72fd695260422e383fee89e
3
+ metadata.gz: 65e5efcc214d48f2c3d2aac80dfb88c7506b3bd559a74e2c77f31fd1388736f0
4
+ data.tar.gz: cd3123a08c990c068401869e18e6a8c3ad0fb54867640b42a655357d73b23a01
5
5
  SHA512:
6
- metadata.gz: 38ced646ec7b54baaed0a072be43cc2a78e7e15e6658363747611de84e403df14f1cfbebbec7d67732ee0c15e0d0fc5d1ac2bf8dcecafd1060ab15f4fab2fd51
7
- data.tar.gz: 7e17923b6b90f24e2a8542e291862121d7f5cccd0587bf96fd28601b32d2c83234152bb98cd769a512af86076da057eaf97cdbd8fb399b7665f9123eabfc614e
6
+ metadata.gz: 1177590a5fa948253de27d6169f567cd75a50dc9f840e4145b45ddc55e7e3f884c8595b0ed2a8a01911290be9eaeab34eaee670212cddd56f412b7ab1a4564ea
7
+ data.tar.gz: 67c3ff2090a3f81c1ff1d22e1dad2ca549b45e80588021eec5c128d0f8bede276cb8da2ffcd5719fc3a312e0be52fde112ccd37cf6a97398c6725e22dc3c2375
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2025-03-05
4
+
5
+ - Respect `~/.railsrc` file when generating new rails apps (PR #4). Thanks [@marcoroth](https://github.com/marcoroth) 🎉
6
+ - Use array version of `system` to avoid command injection.
7
+ - Update cache keys to be shorter.
8
+ - Improve log messages.
9
+
3
10
  ## [0.3.0] - 2025-02-23
4
11
 
5
12
  - Allow passing options to generate the new application
@@ -38,6 +45,7 @@ M## [0.1.1] - 2025-02-21
38
45
 
39
46
  - Initial release
40
47
 
48
+ [0.4.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.4.0
41
49
  [0.3.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.3.0
42
50
  [0.2.1]: https://github.com/matheusrich/rails-diff/releases/tag/v0.2.1
43
51
  [0.2.0]: https://github.com/matheusrich/rails-diff/releases/tag/v0.2.0
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rails
4
4
  module Diff
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
data/lib/rails/diff.rb CHANGED
@@ -5,13 +5,15 @@ require "rails"
5
5
  require "thor"
6
6
  require "diffy"
7
7
  require "fileutils"
8
+ require "open3"
8
9
 
9
10
  module Rails
10
11
  module Diff
11
12
  class Error < StandardError; end
12
13
 
13
14
  RAILS_REPO = "https://github.com/rails/rails.git"
14
- CACHE_DIR = File.expand_path("~/.rails-diff/cache")
15
+ CACHE_DIR = File.expand_path("#{ENV["HOME"]}/.rails-diff/cache")
16
+ RAILSRC_PATH = "#{ENV["HOME"]}/.railsrc"
15
17
 
16
18
  class << self
17
19
  def file(*files, no_cache: false, commit: nil, new_app_options: nil)
@@ -33,8 +35,23 @@ module Rails
33
35
 
34
36
  private
35
37
 
38
+ def system!(*cmd)
39
+ _, stderr, status = Open3.capture3(*cmd)
40
+
41
+ unless status.success?
42
+ puts "\e[1;31mCommand failed:\e[0m #{cmd.join(' ')}"
43
+ abort stderr
44
+ end
45
+
46
+ true
47
+ end
48
+
49
+ def info(message)
50
+ puts "\e[1;34minfo:\e[0m #{message}"
51
+ end
52
+
36
53
  def clear_cache
37
- puts "Clearing cache"
54
+ info "Clearing cache"
38
55
  FileUtils.rm_rf(CACHE_DIR)
39
56
  end
40
57
 
@@ -48,19 +65,26 @@ module Rails
48
65
  end
49
66
 
50
67
  def template_app_path
51
- @template_app_path ||= File.join(CACHE_DIR, commit, new_app_options_hash, app_name)
68
+ @template_app_path ||= File.join(CACHE_DIR, "rails-#{commit.first(10)}", rails_new_options_hash, app_name)
52
69
  end
53
70
 
54
71
  def rails_path
55
72
  @rails_path ||= begin
56
73
  File.join(CACHE_DIR, "rails").tap do |path|
57
74
  unless File.exist?(path)
58
- system("git clone --depth 1 #{RAILS_REPO} #{path} >/dev/null 2>&1")
75
+ info "Cloning Rails repository"
76
+ system!("git", "clone", "--depth", "1", RAILS_REPO, path)
59
77
  end
60
78
  end
61
79
  end
62
80
  end
63
81
 
82
+ def railsrc_options
83
+ return @railsrc_options if defined?(@railsrc_options)
84
+
85
+ @railsrc_options = File.read(RAILSRC_PATH).lines if File.exist?(RAILSRC_PATH)
86
+ end
87
+
64
88
  def app_name = @app_name ||= File.basename(Dir.pwd)
65
89
 
66
90
  def list_files(dir, skip = [])
@@ -82,11 +106,10 @@ module Rails
82
106
  end
83
107
 
84
108
  def generated_files(generator_name, *args, skip)
85
- command = "#{generator_name} #{args.join(' ')}"
86
109
  Dir.chdir(template_app_path) do
87
- system("bin/rails destroy #{command} >/dev/null 2>&1")
88
- puts "Running generator: rails generate #{command}"
89
- track_new_files(skip) { system("bin/rails generate #{command} > /dev/null 2>&1") }
110
+ system!("bin/rails", "destroy", generator_name, *args)
111
+ info "Running generator: rails generate #{generator_name} #{args.join(' ')}"
112
+ track_new_files(skip) { system!("bin/rails", "generate", generator_name, *args) }
90
113
  .map { |it| it.delete_prefix("#{template_app_path}/") }
91
114
  end
92
115
  end
@@ -101,9 +124,9 @@ module Rails
101
124
 
102
125
  def install_app_dependencies
103
126
  Dir.chdir(template_app_path) do
104
- unless system("bundle check >/dev/null 2>&1")
105
- puts "Installing application dependencies"
106
- system("bundle install >/dev/null 2>&1")
127
+ unless system!("bundle check")
128
+ info "Installing application dependencies"
129
+ system!("bundle install")
107
130
  end
108
131
  end
109
132
  end
@@ -112,8 +135,8 @@ module Rails
112
135
  rails_file = File.join(template_app_path, file)
113
136
  repo_file = File.join(Dir.pwd, file)
114
137
 
115
- return "#{file} not found in the Rails template" unless File.exist?(rails_file)
116
- return "#{file} not found in your repository" unless File.exist?(repo_file)
138
+ return "File not found in the Rails template" unless File.exist?(rails_file)
139
+ return "File not found in your repository" unless File.exist?(repo_file)
117
140
 
118
141
  Diffy::Diff.new(
119
142
  rails_file,
@@ -131,7 +154,7 @@ module Rails
131
154
  return true unless File.exist?(rails_path)
132
155
 
133
156
  Dir.chdir(rails_path) do
134
- system("git fetch origin main >/dev/null 2>&1")
157
+ system!("git fetch origin main")
135
158
  current = `git rev-parse HEAD`.strip
136
159
  latest = `git rev-parse origin/main`.strip
137
160
 
@@ -152,21 +175,24 @@ module Rails
152
175
  end
153
176
 
154
177
  def generate_app
155
- FileUtils.rm_rf(template_app_path)
156
178
  Dir.chdir("railties") do
157
- unless system("bundle check >/dev/null 2>&1")
158
- puts "Installing Rails dependencies"
159
- system("bundle install >/dev/null 2>&1")
179
+ unless system!("bundle check")
180
+ info "Installing Rails dependencies"
181
+ system!("bundle install")
160
182
  end
161
183
 
162
- puts "Generating new Rails application"
163
- system("bundle exec rails new #{template_app_path} --main --skip-bundle --force --skip-test --skip-system-test --quiet #{new_app_options}")
184
+ if railsrc_options
185
+ info "Using default options from #{RAILSRC_PATH}:\n > #{railsrc_options.join(' ')}"
186
+ end
187
+
188
+ info "Generating new Rails application\n > #{rails_new_command.join(' ')}"
189
+ system!(*rails_new_command)
164
190
  end
165
191
  end
166
192
 
167
193
  def checkout_rails
168
- puts "Checking out Rails (at commit #{commit[0..6]})"
169
- system("git checkout #{commit} >/dev/null 2>&1")
194
+ info "Checking out Rails (at commit #{commit[0..6]})"
195
+ system!("git", "checkout", commit)
170
196
  end
171
197
 
172
198
  def commit = @commit
@@ -179,9 +205,22 @@ module Rails
179
205
  end
180
206
  end
181
207
 
182
- def new_app_options_hash
183
- Digest::SHA256.hexdigest(new_app_options.to_s)
184
- end
208
+ def rails_new_command = @rails_new_command ||= [
209
+ "bundle",
210
+ "exec",
211
+ "rails",
212
+ "new",
213
+ template_app_path,
214
+ "--main",
215
+ "--skip-bundle",
216
+ "--force",
217
+ "--quiet",
218
+ *rails_new_options
219
+ ]
220
+
221
+ def rails_new_options = @rails_new_options ||= [*new_app_options, *railsrc_options].compact
222
+
223
+ def rails_new_options_hash = Digest::MD5.hexdigest(rails_new_options.join(" "))
185
224
  end
186
225
 
187
226
  class CLI < Thor
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matheus Richard
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-23 00:00:00.000000000 Z
10
+ date: 2025-03-06 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -62,6 +62,7 @@ extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
64
  - ".rspec"
65
+ - ".ruby-version"
65
66
  - CHANGELOG.md
66
67
  - LICENSE.txt
67
68
  - README.md
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubygems_version: 3.6.5
95
+ rubygems_version: 3.6.2
95
96
  specification_version: 4
96
97
  summary: Compare Rails-generated files with the ones in your repository
97
98
  test_files: []