@@ -16,54 +16,52 @@ export class CocoaPodsService implements ICocoaPodsService {
1616 return `${ EOL } end` ;
1717 }
1818
19- public mergePodfileHookContent ( hookName : string , pathToPodfile : string ) : IFuture < void > {
20- return ( ( ) => {
21- if ( ! this . $fs . exists ( pathToPodfile ) ) {
22- throw new Error ( `The Podfile ${ pathToPodfile } does not exist.` ) ;
19+ public mergePodfileHookContent ( hookName : string , pathToPodfile : string ) : void {
20+ if ( ! this . $fs . exists ( pathToPodfile ) ) {
21+ throw new Error ( `The Podfile ${ pathToPodfile } does not exist.` ) ;
22+ }
23+
24+ let podfileContent = this . $fs . readText ( pathToPodfile ) ;
25+ let hookStart = `${ hookName } do` ;
26+
27+ let hookDefinitionRegExp = new RegExp ( `${ hookStart } *(\\|(\\w+)\\|)?` , "g" ) ;
28+ let newFunctionNameIndex = 1 ;
29+ let newFunctions : IRubyFunction [ ] = [ ] ;
30+
31+ let replacedContent = podfileContent . replace ( hookDefinitionRegExp , ( substring : string , firstGroup : string , secondGroup : string , index : number ) : string => {
32+ let newFunctionName = `${ hookName } ${ newFunctionNameIndex ++ } ` ;
33+ let newDefinition = `def ${ newFunctionName } ` ;
34+
35+ let rubyFunction : IRubyFunction = { functionName : newFunctionName } ;
36+ // firstGroup is the block parameter, secondGroup is the block parameter name.
37+ if ( firstGroup && secondGroup ) {
38+ newDefinition = `${ newDefinition } (${ secondGroup } )` ;
39+ rubyFunction . functionParameters = secondGroup ;
2340 }
2441
25- let podfileContent = this . $fs . readText ( pathToPodfile ) ;
26- let hookStart = `${ hookName } do` ;
42+ newFunctions . push ( rubyFunction ) ;
43+ return newDefinition ;
44+ } ) ;
2745
28- let hookDefinitionRegExp = new RegExp ( `${ hookStart } *(\\|(\\w+)\\|)?` , "g" ) ;
29- let newFunctionNameIndex = 1 ;
30- let newFunctions : IRubyFunction [ ] = [ ] ;
46+ if ( newFunctions . length > 1 ) {
47+ // Execute all methods in the hook and pass the parameter to them.
48+ let blokParameterName = "installer" ;
49+ let mergedHookContent = `${ hookStart } |${ blokParameterName } |${ EOL } ` ;
3150
32- let replacedContent = podfileContent . replace ( hookDefinitionRegExp , ( substring : string , firstGroup : string , secondGroup : string , index : number ) : string => {
33- let newFunctionName = `${ hookName } ${ newFunctionNameIndex ++ } ` ;
34- let newDefinition = `def ${ newFunctionName } ` ;
35-
36- let rubyFunction : IRubyFunction = { functionName : newFunctionName } ;
37- // firstGroup is the block parameter, secondGroup is the block parameter name.
38- if ( firstGroup && secondGroup ) {
39- newDefinition = `${ newDefinition } (${ secondGroup } )` ;
40- rubyFunction . functionParameters = secondGroup ;
51+ _ . each ( newFunctions , ( rubyFunction : IRubyFunction ) => {
52+ let functionExecution = rubyFunction . functionName ;
53+ if ( rubyFunction . functionParameters && rubyFunction . functionParameters . length ) {
54+ functionExecution = `${ functionExecution } ${ blokParameterName } ` ;
4155 }
4256
43- newFunctions . push ( rubyFunction ) ;
44- return newDefinition ;
57+ mergedHookContent = `${ mergedHookContent } ${ functionExecution } ${ EOL } ` ;
4558 } ) ;
4659
47- if ( newFunctions . length > 1 ) {
48- // Execute all methods in the hook and pass the parameter to them.
49- let blokParameterName = "installer" ;
50- let mergedHookContent = `${ hookStart } |${ blokParameterName } |${ EOL } ` ;
51-
52- _ . each ( newFunctions , ( rubyFunction : IRubyFunction ) => {
53- let functionExecution = rubyFunction . functionName ;
54- if ( rubyFunction . functionParameters && rubyFunction . functionParameters . length ) {
55- functionExecution = `${ functionExecution } ${ blokParameterName } ` ;
56- }
57-
58- mergedHookContent = `${ mergedHookContent } ${ functionExecution } ${ EOL } ` ;
59- } ) ;
60+ mergedHookContent = `${ mergedHookContent } end` ;
6061
61- mergedHookContent = `${ mergedHookContent } end` ;
62-
63- let newPodfileContent = `${ replacedContent } ${ EOL } ${ mergedHookContent } ` ;
64- this . $fs . writeFile ( pathToPodfile , newPodfileContent ) . wait ( ) ;
65- }
66- } ) . future < void > ( ) ( ) ;
62+ let newPodfileContent = `${ replacedContent } ${ EOL } ${ mergedHookContent } ` ;
63+ this . $fs . writeFile ( pathToPodfile , newPodfileContent ) ;
64+ }
6765 }
6866}
6967
0 commit comments