1+ require 'yaml'
2+
13module Fastlane
24 module Actions
35 class ApplyPatchAction < Action
46 def self . run ( params )
7+ if params [ :patch ]
8+ # raises
9+ patch = YAML . load_file params [ :patch ]
10+
11+ # If the :patch option is present, load these params from the
12+ # specified file. Action args override.
13+ %w{ regexp text mode global } . each do |option |
14+ value = patch [ option ]
15+ next if value . nil?
16+
17+ case option . to_sym
18+ when :regexp
19+ params [ :regexp ] = /#{ value } /
20+ when :mode
21+ params [ :mode ] = value . to_sym
22+ else
23+ params [ option . to_sym ] = value
24+ end
25+ end
26+ end
27+
28+ UI . user_error! "Must specify :regexp and :text either in a patch or via arguments" if
29+ params [ :regexp ] . nil? || params [ :text ] . nil?
30+
531 helper = Fastlane ::Helper ::PatchHelper
632 modified_contents = File . open ( params [ :file ] , "r" ) do |f |
733 contents = f . read
@@ -43,11 +69,11 @@ def self.available_options
4369 type : String ) ,
4470 FastlaneCore ::ConfigItem . new ( key : :regexp ,
4571 description : "A regular expression to match" ,
46- optional : false ,
72+ optional : true ,
4773 type : Regexp ) ,
4874 FastlaneCore ::ConfigItem . new ( key : :text ,
4975 description : "Text to append to the match" ,
50- optional : false ,
76+ optional : true ,
5177 type : String ) ,
5278 FastlaneCore ::ConfigItem . new ( key : :global ,
5379 description : "If true, patch all occurrences of the pattern" ,
@@ -63,7 +89,11 @@ def self.available_options
6389 description : ":append, :prepend or :replace" ,
6490 optional : true ,
6591 default_value : :append ,
66- type : Symbol )
92+ type : Symbol ) ,
93+ FastlaneCore ::ConfigItem . new ( key : :patch ,
94+ description : "A YAML file specifying patch data" ,
95+ optional : true ,
96+ type : String )
6797 ]
6898 end
6999
0 commit comments