@@ -46,10 +46,8 @@ export class CoberturaAntCodeCoverageEnabler extends cc.CoberturaCodeCoverageEna
4646
4747 tl . debug ( "Reading the build file: " + _this . buildFile ) ;
4848
49- return util . readXmlFileAsJson ( _this . buildFile )
50- . then ( function ( resp ) {
51- return _this . addCodeCoverageData ( resp ) ;
52- } )
49+ let buildContent = util . readXmlFileAsDom ( _this . buildFile ) ;
50+ return _this . addCodeCoverageData ( buildContent )
5351 . thenResolve ( true ) ;
5452 }
5553
@@ -68,25 +66,20 @@ export class CoberturaAntCodeCoverageEnabler extends cc.CoberturaCodeCoverageEna
6866 return ccfilter ;
6967 }
7068
71- protected getClassData ( ) : any {
69+ protected getClassData ( ) : string {
7270 let _this = this ;
73- let fileset = [ ] ;
71+ let classData = "" ;
7472 let classDirs = _this . classDirs ;
7573
7674 if ( str ( classDirs ) . isEmpty ( ) ) {
7775 classDirs = "." ;
7876 }
7977 classDirs . split ( "," ) . forEach ( cdir => {
80- let filter = {
81- $ : {
82- dir : cdir ,
83- includes : _this . includeFilter ,
84- excludes : _this . excludeFilter
85- }
86- } ;
87- fileset . push ( filter ) ;
78+ classData += classData + `
79+ <fileset dir="${ cdir } " includes="${ _this . includeFilter } " excludes="${ _this . excludeFilter } " />
80+ ` ;
8881 } ) ;
89- return fileset ;
82+ return classData ;
9083 }
9184
9285 protected createReportFile ( reportContent : string ) : Q . Promise < void > {
@@ -97,94 +90,70 @@ export class CoberturaAntCodeCoverageEnabler extends cc.CoberturaCodeCoverageEna
9790 return util . writeFile ( reportFile , reportContent ) ;
9891 }
9992
100- protected addCodeCoverageData ( pomJson : any ) : Q . Promise < any [ ] > {
93+ protected addCodeCoverageData ( pomJson : CheerioStatic ) : Q . Promise < any [ ] > {
10194 let _this = this ;
10295
103- if ( ! pomJson || ! pomJson . project ) {
96+ if ( ! pomJson || ! pomJson ( " project" ) ) {
10497 return Q . reject < any > ( tl . loc ( "InvalidBuildFile" ) ) ;
10598 }
10699
107100 let reportPluginData = ccc . coberturaAntReport ( _this . sourceDirs , path . join ( path . dirname ( _this . buildFile ) , _this . reportDir ) ) ;
108101 return Q . all ( [ _this . addCodeCoverageNodes ( pomJson ) , _this . createReportFile ( reportPluginData ) ] ) ;
109102 }
110103
111- protected addCodeCoverageNodes ( buildJsonContent : any ) : Q . Promise < any > {
104+ protected addCodeCoverageNodes ( buildJsonContent : CheerioStatic ) : Q . Promise < any > {
112105 let _this = this ;
113106
114- if ( ! buildJsonContent . project . target ) {
115- tl . debug ( "Build tag is not present" ) ;
107+ if ( ! buildJsonContent ( " project" ) . children ( " target" ) ) {
108+ tl . debug ( "Target tasks are not present" ) ;
116109 return Q . reject ( tl . loc ( "InvalidBuildFile" ) ) ;
117110 }
118111
119- ccc . coberturaAntCoverageEnable ( buildJsonContent . project ) ;
120-
121- if ( ! buildJsonContent . project . target || typeof buildJsonContent . project . target === "string" ) {
122- buildJsonContent . project . target = { } ;
123- }
112+ buildJsonContent ( "project" ) . prepend ( ccc . coberturaAntCoverageEnable ( ) ) ;
113+ buildJsonContent ( "project" ) . children ( "target" ) . each ( function ( i , elem ) {
114+ _this . enableForking ( buildJsonContent , elem ) ;
115+ } ) ;
124116
125- if ( buildJsonContent . project . target instanceof Array ) {
126- buildJsonContent . project . target . forEach ( element => {
127- _this . enableForking ( element ) ;
128- } ) ;
129- } else {
130- _this . enableForking ( buildJsonContent . project . target ) ;
131- }
132- return util . writeJsonAsXmlFile ( _this . buildFile , buildJsonContent ) ;
117+ return util . writeFile ( _this . buildFile , buildJsonContent . xml ( ) ) ;
133118 }
134119
135- protected enableForking ( targetNode : any ) {
120+ protected enableForking ( buildJsonContent : CheerioStatic , targetNode : CheerioElement ) {
136121 let _this = this ;
137- let coberturaNode = ccc . coberturaAntInstrumentedClasses ( path . dirname ( _this . buildFile ) , _this . reportDir ) ;
138- coberturaNode . fileset = _this . getClassData ( ) ;
139122 let testNodes = [ "junit" , "java" , "testng" , "batchtest" ] ;
140-
141- if ( targetNode . javac ) {
142- if ( targetNode . javac instanceof Array ) {
143- targetNode . javac . forEach ( jn => {
144- jn . $ . debug = "true" ;
145- } ) ;
146- }
123+ let buildDir = path . dirname ( _this . buildFile ) ;
124+ let coberturaNode = ccc . coberturaAntProperties ( path . join ( buildDir , _this . reportDir ) , path . dirname ( _this . buildFile ) ) ;
125+ let classData = ccc . coberturaAntInstrumentedClasses ( buildDir , path . join ( buildDir , _this . reportDir ) , _this . getClassData ( ) ) ;
126+
127+ if ( targetNode . children ) {
128+ targetNode . children . forEach ( n => {
129+ if ( n . name && n . name === "javac" ) {
130+ n . attribs [ "debug" ] = "true" ;
131+ }
132+ } ) ;
147133 }
148134
149135 testNodes . forEach ( tn => {
150- if ( ! targetNode [ tn ] ) {
136+ if ( ! targetNode . children ) {
151137 return ;
152138 }
153139
154- let node = targetNode [ tn ] ;
155- _this . enableForkOnTestNodes ( node , true ) ;
156- if ( node instanceof Array ) {
157- node . forEach ( n => {
158- ccc . coberturaAntProperties ( n , _this . reportDir , path . dirname ( _this . buildFile ) ) ;
159- } ) ;
160- } else {
161- ccc . coberturaAntProperties ( node , _this . reportDir , path . dirname ( _this . buildFile ) ) ;
140+ targetNode . children . forEach ( node => {
141+ if ( node . name && node . name === tn ) {
142+ _this . enableForkOnTestNodes ( node ) ;
143+ buildJsonContent ( "project" ) . children ( "target" ) . children ( tn ) . prepend ( coberturaNode ) ;
144+ buildJsonContent ( "project" ) . children ( "target" ) . children ( tn ) . append ( ccc . coberturaAntClasspathRef ( ) ) ;
145+ } ;
146+ } ) ;
147+ if ( buildJsonContent ( "project" ) . children ( "target" ) . children ( tn )
148+ && ( ! buildJsonContent ( "project" ) . children ( "target" ) . children ( "cobertura-instrument" )
149+ || buildJsonContent ( "project" ) . children ( "target" ) . children ( "cobertura-instrument" ) . length === 0 ) ) {
150+ buildJsonContent ( "project" ) . children ( "target" ) . children ( tn ) . before ( classData ) ;
162151 }
163-
164- targetNode [ "cobertura-instrument" ] = coberturaNode ;
165152 } ) ;
166153 }
167154
168- protected enableForkOnTestNodes ( testNode : any , enableForkMode : boolean ) {
169- if ( testNode instanceof Array ) {
170- testNode . forEach ( element => {
171- if ( ! element . $ ) {
172- element . $ = { } ;
173- }
174- if ( enableForkMode ) {
175- element . $ . forkmode = "once" ;
176- }
177- element . $ . fork = "true" ;
178-
179- } ) ;
180- } else {
181- if ( ! testNode . $ ) {
182- testNode . $ = { } ;
183- }
184- if ( enableForkMode ) {
185- testNode . $ . forkmode = "once" ;
186- }
187- testNode . $ . fork = "true" ;
188- }
155+ protected enableForkOnTestNodes ( testNode : CheerioElement ) {
156+ testNode . attribs [ "forkmode" ] = "once" ;
157+ testNode . attribs [ "fork" ] = "true" ;
189158 }
190159}
0 commit comments