11# frozen_string_literal: true
22
33require_relative "diff/version"
4+ require_relative "diff/file_tracker"
45require "rails"
56require "thor"
67require "diffy"
@@ -23,12 +24,12 @@ def file(*files, no_cache: false, commit: nil, new_app_options: nil)
2324 files . filter_map { |it | diff_with_header ( it ) } . join ( "\n " )
2425 end
2526
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 )
2728 clear_cache if no_cache
2829 ensure_template_app_exists ( commit , new_app_options )
2930 install_app_dependencies
3031
31- generated_files ( generator_name , *args , skip )
32+ generated_files ( generator_name , *args , skip , only )
3233 . map { |it | diff_with_header ( it ) }
3334 . join ( "\n \n " )
3435 end
@@ -97,29 +98,11 @@ def railsrc_options
9798
9899 def app_name = @app_name ||= File . basename ( Dir . pwd )
99100
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 )
119102 Dir . chdir ( template_app_path ) do
120103 system! ( "bin/rails" , "destroy" , generator_name , *args )
121104 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 ) }
123106 . map { |it | it . delete_prefix ( "#{ template_app_path } /" ) }
124107 end
125108 end
@@ -255,9 +238,16 @@ def file(*files)
255238
256239 desc "generated GENERATOR [args]" , "Compare files that would be created by a Rails generator"
257240 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 : [ ]
258242 def generated ( generator_name , *args )
259243 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 ] )
261251 return if diff . empty?
262252
263253 options [ :fail ] ? abort ( diff ) : puts ( diff )
0 commit comments