1
1
# frozen_string_literal: true
2
2
3
3
require_relative "diff/version"
4
+ require_relative "diff/file_tracker"
4
5
require "rails"
5
6
require "thor"
6
7
require "diffy"
@@ -23,12 +24,12 @@ def file(*files, no_cache: false, commit: nil, new_app_options: nil)
23
24
files . filter_map { |it | diff_with_header ( it ) } . join ( "\n " )
24
25
end
25
26
26
- def generated ( generator_name , *args , no_cache : false , skip : [ ] , commit : nil , new_app_options : nil )
27
+ def generated ( generator_name , *args , no_cache : false , skip : [ ] , only : [ ] , commit : nil , new_app_options : nil )
27
28
clear_cache if no_cache
28
29
ensure_template_app_exists ( commit , new_app_options )
29
30
install_app_dependencies
30
31
31
- generated_files ( generator_name , *args , skip )
32
+ generated_files ( generator_name , *args , skip , only )
32
33
. map { |it | diff_with_header ( it ) }
33
34
. join ( "\n \n " )
34
35
end
@@ -97,29 +98,11 @@ def railsrc_options
97
98
98
99
def app_name = @app_name ||= File . basename ( Dir . pwd )
99
100
100
- def list_files ( dir , skip = [ ] )
101
- Dir . glob ( "#{ dir } /**/*" , File ::FNM_DOTMATCH ) . reject do |it |
102
- File . directory? ( it ) ||
103
- it . start_with? ( "#{ dir } /.git" ) ||
104
- it . start_with? ( "#{ dir } /tmp" ) ||
105
- it . start_with? ( "#{ dir } /log" ) ||
106
- it . start_with? ( "#{ dir } /test" ) ||
107
- skip . any? { |s | it . start_with? ( "#{ dir } /#{ s } " ) }
108
- end
109
- end
110
-
111
- def track_new_files ( skip )
112
- files_before = list_files ( template_app_path )
113
- yield
114
- files_after = list_files ( template_app_path , skip )
115
- files_after - files_before
116
- end
117
-
118
- def generated_files ( generator_name , *args , skip )
101
+ def generated_files ( generator_name , *args , skip , only )
119
102
Dir . chdir ( template_app_path ) do
120
103
system! ( "bin/rails" , "destroy" , generator_name , *args )
121
104
info "Running generator: rails generate #{ generator_name } #{ args . join ( ' ' ) } "
122
- track_new_files ( skip ) { system! ( "bin/rails" , "generate" , generator_name , *args ) }
105
+ FileTracker . new . track_new_files ( template_app_path , skip , only ) { system! ( "bin/rails" , "generate" , generator_name , *args ) }
123
106
. map { |it | it . delete_prefix ( "#{ template_app_path } /" ) }
124
107
end
125
108
end
@@ -255,9 +238,16 @@ def file(*files)
255
238
256
239
desc "generated GENERATOR [args]" , "Compare files that would be created by a Rails generator"
257
240
option :skip , type : :array , desc : "Skip specific files or directories" , aliases : [ "-s" ] , default : [ ]
241
+ option :only , type : :array , desc : "Only include specific files or directories" , aliases : [ "-o" ] , default : [ ]
258
242
def generated ( generator_name , *args )
259
243
ENV [ "DEBUG" ] = "true" if options [ :debug ]
260
- diff = Rails ::Diff . generated ( generator_name , *args , no_cache : options [ :no_cache ] , skip : options [ :skip ] , commit : options [ :commit ] , new_app_options : options [ :new_app_options ] )
244
+ diff = Rails ::Diff . generated ( generator_name ,
245
+ *args ,
246
+ no_cache : options [ :no_cache ] ,
247
+ skip : options [ :skip ] ,
248
+ only : options [ :only ] ,
249
+ commit : options [ :commit ] ,
250
+ new_app_options : options [ :new_app_options ] )
261
251
return if diff . empty?
262
252
263
253
options [ :fail ] ? abort ( diff ) : puts ( diff )
0 commit comments