@@ -76,6 +76,20 @@ class DSL
7676 Artifact ::PostflightBlock ,
7777 ] . freeze
7878
79+ INSTALL_STEP_ARTIFACT_CLASSES = [
80+ Artifact ::PreflightSteps ,
81+ Artifact ::PostflightSteps ,
82+ Artifact ::UninstallPreflightSteps ,
83+ Artifact ::UninstallPostflightSteps ,
84+ ] . freeze
85+
86+ INSTALL_STEP_FLIGHT_BLOCK_CLASSES = T . let ( {
87+ Artifact ::PreflightSteps => [ Artifact ::PreflightBlock , :preflight ] ,
88+ Artifact ::PostflightSteps => [ Artifact ::PostflightBlock , :postflight ] ,
89+ Artifact ::UninstallPreflightSteps => [ Artifact ::PreflightBlock , :uninstall_preflight ] ,
90+ Artifact ::UninstallPostflightSteps => [ Artifact ::PostflightBlock , :uninstall_postflight ] ,
91+ } . freeze , T ::Hash [ T . untyped , T . untyped ] )
92+
7993 DSL_METHODS = T . let ( Set . new ( [
8094 :arch ,
8195 :artifacts ,
@@ -121,6 +135,7 @@ class DSL
121135 *ORDINARY_ARTIFACT_CLASSES . map ( &:dsl_key ) ,
122136 *ACTIVATABLE_ARTIFACT_CLASSES . map ( &:dsl_key ) ,
123137 *ARTIFACT_BLOCK_CLASSES . flat_map { |klass | [ klass . dsl_key , klass . uninstall_dsl_key ] } ,
138+ *INSTALL_STEP_ARTIFACT_CLASSES . map ( &:dsl_key ) ,
124139 ] ) . freeze , T ::Set [ Symbol ] )
125140
126141 include OnSystem ::MacOSAndLinux
@@ -798,11 +813,61 @@ def disable!(date:, because:, replacement: nil, replacement_formula: nil, replac
798813 [ klass . dsl_key , klass . uninstall_dsl_key ] . each do |dsl_key |
799814 define_method ( dsl_key ) do |&block |
800815 T . bind ( self , DSL )
801- artifacts . add ( klass . new ( cask , dsl_key => block ) )
816+ if install_step_artifact_defined? ( dsl_key )
817+ warn_on_install_step_conflict ( dsl_key , T . must ( install_step_artifact_class ( dsl_key ) ) . dsl_key )
818+ else
819+ artifacts . add ( klass . new ( cask , dsl_key => block ) )
820+ end
802821 end
803822 end
804823 end
805824
825+ INSTALL_STEP_ARTIFACT_CLASSES . each do |klass |
826+ define_method ( klass . dsl_key ) do |steps = nil , **kwargs , &block |
827+ T . bind ( self , DSL )
828+ steps = if block
829+ Homebrew ::InstallSteps ::DSL . build ( default_base : :staged_path , default_source_base : :staged_path ,
830+ default_target_base : :staged_path , &block )
831+ else
832+ Homebrew ::InstallSteps ::DSL . normalise_steps ( [ kwargs [ :steps ] || steps ] . flatten . compact )
833+ end
834+ remove_conflicting_flight_blocks ( klass )
835+ artifacts . add ( klass . new ( cask , steps ) )
836+ end
837+ end
838+
839+ sig { params ( dsl_key : Symbol ) . returns ( T ::Boolean ) }
840+ def install_step_artifact_defined? ( dsl_key )
841+ return false unless ( klass = install_step_artifact_class ( dsl_key ) )
842+
843+ artifacts . any? ( klass )
844+ end
845+
846+ sig { params ( dsl_key : Symbol ) . returns ( T . nilable ( T . class_of ( Artifact ::AbstractInstallSteps ) ) ) }
847+ def install_step_artifact_class ( dsl_key )
848+ INSTALL_STEP_FLIGHT_BLOCK_CLASSES . find do |_step_class , ( _block_class , block_dsl_key ) |
849+ block_dsl_key == dsl_key
850+ end &.first
851+ end
852+
853+ sig { params ( klass : T . class_of ( Artifact ::AbstractInstallSteps ) ) . void }
854+ def remove_conflicting_flight_blocks ( klass )
855+ flight_block_class , dsl_key = INSTALL_STEP_FLIGHT_BLOCK_CLASSES . fetch ( klass )
856+ conflicting_flight_blocks = artifacts . select do |artifact |
857+ artifact . is_a? ( flight_block_class ) && T . unsafe ( artifact ) . directives . key? ( dsl_key )
858+ end
859+
860+ conflicting_flight_blocks . each do |artifact |
861+ warn_on_install_step_conflict ( dsl_key , klass . dsl_key )
862+ artifacts . delete ( artifact )
863+ end
864+ end
865+
866+ sig { params ( dsl_key : Symbol , steps_key : Symbol ) . void }
867+ def warn_on_install_step_conflict ( dsl_key , steps_key )
868+ opoo "#{ token } : `#{ dsl_key } ` is ignored because `#{ steps_key } ` is defined!"
869+ end
870+
806871 sig { override . params ( method : Symbol , _args : T . anything ) . returns ( T . noreturn ) }
807872 def method_missing ( method , *_args )
808873 raise NoMethodError , "undefined method '#{ method } ' for Cask '#{ token } '"
0 commit comments