3
3
import path = require( "path" ) ;
4
4
import shelljs = require( "shelljs" ) ;
5
5
import semver = require( "semver" ) ;
6
+ import Future = require( "fibers/future" ) ;
6
7
import constants = require( "./../constants" ) ;
8
+ let xmlmerge = require ( "xmlmerge-js" ) ;
7
9
8
10
export class PluginsService implements IPluginsService {
9
11
private static INSTALL_COMMAND_NAME = "install" ;
@@ -38,7 +40,7 @@ export class PluginsService implements IPluginsService {
38
40
return ( ( ) => {
39
41
this . executeNpmCommand ( PluginsService . UNINSTALL_COMMAND_NAME , pluginName ) . wait ( ) ;
40
42
let showMessage = true ;
41
- let action = ( modulesDestinationPath : string , platform : string ) => {
43
+ let action = ( modulesDestinationPath : string , platform : string , platformData : IPlatformData ) => {
42
44
shelljs . rm ( "-rf" , path . join ( modulesDestinationPath , pluginName ) ) ;
43
45
this . $logger . out ( `Successfully removed plugin ${ pluginName } for ${ platform } platform` ) ;
44
46
showMessage = false ;
@@ -53,7 +55,7 @@ export class PluginsService implements IPluginsService {
53
55
54
56
public prepare ( pluginData : IPluginData ) : IFuture < void > {
55
57
return ( ( ) => {
56
- let action = ( pluginDestinationPath : string , platform : string ) => {
58
+ let action = ( pluginDestinationPath : string , platform : string , platformData : IPlatformData ) => {
57
59
let skipExecution = false ;
58
60
// Process .js files
59
61
let installedFrameworkVersion = this . getInstalledFrameworkVersion ( platform ) . wait ( ) ;
@@ -66,13 +68,20 @@ export class PluginsService implements IPluginsService {
66
68
if ( ! skipExecution ) {
67
69
this . $fs . ensureDirectoryExists ( pluginDestinationPath ) . wait ( ) ;
68
70
shelljs . cp ( "-R" , pluginData . fullPath , pluginDestinationPath ) ;
69
-
70
- // TODO: Merge xmls - check if android.manifest or info.plist files exist and merge them
71
+
71
72
let pluginPlatformsFolderPath = path . join ( pluginDestinationPath , pluginData . name , "platforms" ) ;
72
- if ( this . $fs . exists ( pluginPlatformsFolderPath ) . wait ( ) ) {
73
- shelljs . rm ( "-rf" , pluginPlatformsFolderPath ) ;
73
+ let pluginConfigurationFilePath = path . join ( pluginPlatformsFolderPath , platformData . configurationFileName ) ;
74
+ if ( this . $fs . exists ( pluginConfigurationFilePath ) . wait ( ) ) {
75
+ let pluginConfigurationFileContent = this . $fs . readFile ( pluginConfigurationFilePath ) . wait ( ) . toString ( ) ;
76
+ let configurationFileContent = this . $fs . readFile ( platformData . configurationFilePath ) . wait ( ) . toString ( ) ;
77
+ let resultXml = this . mergeXml ( pluginConfigurationFileContent , configurationFileContent ) . wait ( ) ;
78
+ this . $fs . writeFile ( platformData . configurationFilePath , resultXml ) . wait ( ) ;
74
79
}
75
80
81
+ if ( this . $fs . exists ( pluginPlatformsFolderPath ) . wait ( ) ) {
82
+ shelljs . rm ( "-rf" , pluginPlatformsFolderPath ) ;
83
+ }
84
+
76
85
// TODO: Add libraries
77
86
78
87
// Show message
@@ -120,14 +129,14 @@ export class PluginsService implements IPluginsService {
120
129
return npmCommandResult . split ( "@" ) [ 0 ] ; // returns plugin name
121
130
}
122
131
123
- private executeForAllInstalledPlatforms ( action : ( pluginDestinationPath : string , platform : string ) => void ) : void {
132
+ private executeForAllInstalledPlatforms ( action : ( pluginDestinationPath : string , platform : string , platformData : IPlatformData ) => void ) : void {
124
133
let availablePlatforms = _ . keys ( this . $platformsData . availablePlatforms ) ;
125
134
_ . each ( availablePlatforms , platform => {
126
135
let isPlatformInstalled = this . $fs . exists ( path . join ( this . $projectData . platformsDir , platform . toLowerCase ( ) ) ) . wait ( ) ;
127
136
if ( isPlatformInstalled ) {
128
137
let platformData = this . $platformsData . getPlatformData ( platform . toLowerCase ( ) ) ;
129
138
let pluginDestinationPath = path . join ( platformData . appDestinationDirectoryPath , constants . APP_FOLDER_NAME , "tns_modules" ) ;
130
- action ( pluginDestinationPath , platform . toLowerCase ( ) ) ;
139
+ action ( pluginDestinationPath , platform . toLowerCase ( ) , platformData ) ;
131
140
}
132
141
} ) ;
133
142
}
@@ -168,5 +177,14 @@ export class PluginsService implements IPluginsService {
168
177
return frameworkData . version ;
169
178
} ) . future < string > ( ) ( ) ;
170
179
}
180
+
181
+ private mergeXml ( xml1 : string , xml2 : string ) : IFuture < string > {
182
+ let future = new Future < string > ( ) ;
183
+ xmlmerge . merge ( xml1 , xml2 , ( mergedXml : string ) => { // TODO: process errors
184
+ future . return ( mergedXml ) ;
185
+ } ) ;
186
+
187
+ return future ;
188
+ }
171
189
}
172
190
$injector . register ( "pluginsService" , PluginsService ) ;
0 commit comments