diff --git a/bin/nativescript.js b/bin/nativescript.js old mode 100644 new mode 100755 diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index 22878c6913..4b31d5274c 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -61,3 +61,4 @@ $injector.requireCommand("plugin|add", "./commands/plugin/add-plugin"); $injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin"); $injector.require("doctorService", "./services/doctor-service"); +$injector.require("projectFilesManager", "./services/project-files-manager"); \ No newline at end of file diff --git a/lib/declarations.ts b/lib/declarations.ts index 031322e9c5..0a905a41cd 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -64,3 +64,7 @@ interface IOptions extends ICommonOptions { keyStoreAliasPassword: string; sdk: string; } + +interface IProjectFilesManager { + processPlatformSpecificFiles(directoryPath: string, platform: string, excludedDirs?: string[]): IFuture; +} diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 9729c23b89..f025570e71 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -23,7 +23,8 @@ export class PlatformService implements IPlatformService { private $commandsService: ICommandsService, private $options: IOptions, private $broccoliBuilder: IBroccoliBuilder, - private $pluginsService: IPluginsService) { } + private $pluginsService: IPluginsService, + private $projectFilesManager: IProjectFilesManager) { } public addPlatforms(platforms: string[]): IFuture { return (() => { @@ -165,19 +166,9 @@ export class PlatformService implements IPlatformService { } // Process platform specific files - var contents = this.$fs.readDirectory(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait(); - var files: string[] = []; - - _.each(contents, d => { - let filePath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, d); - let fsStat = this.$fs.getFsStats(filePath).wait(); - if(fsStat.isDirectory() && d !== constants.APP_RESOURCES_FOLDER_NAME) { - this.processPlatformSpecificFiles(platform, this.$fs.enumerateFilesInDirectorySync(filePath)).wait(); - } else if(fsStat.isFile()) { - files.push(filePath); - } - }); - this.processPlatformSpecificFiles(platform, files).wait(); + let directoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME); + let excludedDirs = [constants.APP_RESOURCES_FOLDER_NAME]; + this.$projectFilesManager.processPlatformSpecificFiles(directoryPath, platform, excludedDirs).wait(); // Process node_modules folder this.$pluginsService.ensureAllDependenciesAreInstalled().wait(); @@ -342,33 +333,6 @@ export class PlatformService implements IPlatformService { return platformData.platformProjectService.isPlatformPrepared(platformData.projectRoot); } - private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any { - var regex = util.format("^(.+?)\\.(%s)(\\..+?)$", platforms.join("|")); - var parsed = fileName.match(new RegExp(regex, "i")); - if (parsed) { - return { - platform: parsed[2], - onDeviceName: parsed[1] + parsed[3] - }; - } - return undefined; - } - - private processPlatformSpecificFiles( platform: string, files: string[]): IFuture { - // Renames the files that have `platform` as substring and removes the files from other platform - return (() => { - _.each(files, fileName => { - var platformInfo = PlatformService.parsePlatformSpecificFileName(path.basename(fileName), this.$platformsData.platformsNames); - var shouldExcludeFile = platformInfo && platformInfo.platform !== platform; - if (shouldExcludeFile) { - this.$fs.deleteFile(fileName).wait(); - } else if (platformInfo && platformInfo.onDeviceName) { - this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait(); - } - }); - }).future()(); - } - private getApplicationPackages(buildOutputPath: string, validPackageNames: string[]): IFuture { return (() => { // Get latest package that is produced from build diff --git a/lib/services/plugins-service.ts b/lib/services/plugins-service.ts index de68eebb41..1690e9cef2 100644 --- a/lib/services/plugins-service.ts +++ b/lib/services/plugins-service.ts @@ -24,7 +24,8 @@ export class PluginsService implements IPluginsService { private $options: IOptions, private $logger: ILogger, private $errors: IErrors, - private $injector: IInjector) { } + private $injector: IInjector, + private $projectFilesManager: IProjectFilesManager) { } public add(plugin: string): IFuture { return (() => { @@ -101,7 +102,9 @@ export class PluginsService implements IPluginsService { if(this.$fs.exists(pluginPlatformsFolderPath).wait()) { shelljs.rm("-rf", pluginPlatformsFolderPath); - } + } + + this.$projectFilesManager.processPlatformSpecificFiles(pluginDestinationPath, platform).wait(); // TODO: Add libraries diff --git a/lib/services/project-files-manager.ts b/lib/services/project-files-manager.ts new file mode 100644 index 0000000000..cd331a7ce2 --- /dev/null +++ b/lib/services/project-files-manager.ts @@ -0,0 +1,56 @@ +/// +"use strict"; +import path = require("path"); +import util = require("util"); + +export class ProjectFilesManager implements IProjectFilesManager { + constructor(private $fs: IFileSystem, + private $platformsData: IPlatformsData) { } + + public processPlatformSpecificFiles(directoryPath: string, platform: string, excludedDirs?: string[]) { + return (() => { + var contents = this.$fs.readDirectory(directoryPath).wait(); + var files: string[] = []; + + _.each(contents, fileName => { + let filePath = path.join(directoryPath, fileName); + let fsStat = this.$fs.getFsStats(filePath).wait(); + if(fsStat.isDirectory() && (!excludedDirs || (excludedDirs && !_.contains(excludedDirs, fileName)))) { + this.processPlatformSpecificFilesCore(platform, this.$fs.enumerateFilesInDirectorySync(filePath)).wait(); + } else if(fsStat.isFile()) { + files.push(filePath); + } + }); + this.processPlatformSpecificFilesCore(platform, files).wait(); + + }).future()(); + } + + private processPlatformSpecificFilesCore(platform: string, files: string[]): IFuture { + // Renames the files that have `platform` as substring and removes the files from other platform + return (() => { + _.each(files, fileName => { + var platformInfo = ProjectFilesManager.parsePlatformSpecificFileName(path.basename(fileName), this.$platformsData.platformsNames); + var shouldExcludeFile = platformInfo && platformInfo.platform !== platform; + if (shouldExcludeFile) { + this.$fs.deleteFile(fileName).wait(); + } else if (platformInfo && platformInfo.onDeviceName) { + this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait(); + } + }); + }).future()(); + } + + private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any { + var regex = util.format("^(.+?)\\.(%s)(\\..+?)$", platforms.join("|")); + var parsed = fileName.match(new RegExp(regex, "i")); + if (parsed) { + return { + platform: parsed[2], + onDeviceName: parsed[1] + parsed[3] + }; + } + return undefined; + } +} +$injector.register("projectFilesManager", ProjectFilesManager); diff --git a/test/npm-support.ts b/test/npm-support.ts index d7a557f79e..ce66886dfa 100644 --- a/test/npm-support.ts +++ b/test/npm-support.ts @@ -19,6 +19,8 @@ import BroccoliBuilderLib = require("../lib/tools/broccoli/builder"); import NodeModulesTreeLib = require("../lib/tools/broccoli/trees/node-modules-tree"); import PluginsServiceLib = require("../lib/services/plugins-service"); import ChildProcessLib = require("../lib/common/child-process"); +import ProjectFilesManagerLib = require("../lib/services/project-files-manager"); +import Future = require("fibers/future"); import path = require("path"); import temp = require("temp"); @@ -53,6 +55,7 @@ function createTestInjector(): IInjector { testInjector.register("pluginsService", PluginsServiceLib.PluginsService); testInjector.register("npm", NpmLib.NodePackageManager); testInjector.register("childProcess", ChildProcessLib.ChildProcess); + testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager); testInjector.register("commandsServiceProvider", { registerDynamicSubCommands: () => {} }); diff --git a/test/platform-commands.ts b/test/platform-commands.ts index 3038102f2e..f5a56e7031 100644 --- a/test/platform-commands.ts +++ b/test/platform-commands.ts @@ -11,6 +11,7 @@ import StaticConfigLib = require("../lib/config"); import CommandsServiceLib = require("../lib/common/services/commands-service"); import optionsLib = require("../lib/options"); import hostInfoLib = require("../lib/common/host-info"); +import ProjectFilesManagerLib = require("../lib/services/project-files-manager"); import path = require("path"); import Future = require("fibers/future"); var assert = require("chai").assert; @@ -115,6 +116,7 @@ function createTestInjector() { }).future()(); } }); + testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager); return testInjector; } diff --git a/test/platform-service.ts b/test/platform-service.ts index bf0ed7001a..e2fc5ea3bf 100644 --- a/test/platform-service.ts +++ b/test/platform-service.ts @@ -18,6 +18,7 @@ import ProjectDataLib = require("../lib/project-data"); import ProjectHelperLib = require("../lib/common/project-helper"); import optionsLib = require("../lib/options"); import hostInfoLib = require("../lib/common/host-info"); +import ProjectFilesManagerLib = require("../lib/services/project-files-manager"); import path = require("path"); import Future = require("fibers/future"); @@ -63,6 +64,7 @@ function createTestInjector() { return (() => { }).future()(); } }); + testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager); return testInjector; } diff --git a/test/plugins-service.ts b/test/plugins-service.ts index 781410308b..fdb5cd2083 100644 --- a/test/plugins-service.ts +++ b/test/plugins-service.ts @@ -17,6 +17,7 @@ import ProjectHelperLib = require("../lib/common/project-helper"); import PlatformsDataLib = require("../lib/platforms-data"); import ProjectDataServiceLib = require("../lib/services/project-data-service"); import helpers = require("../lib/common/helpers"); +import ProjectFilesManagerLib = require("../lib/services/project-files-manager"); import os = require("os"); import PluginsServiceLib = require("../lib/services/plugins-service"); @@ -64,6 +65,7 @@ function createTestInjector() { checkConsent: () => { return (() => { }).future()(); }, trackFeature: () => { return (() => { }).future()(); } }); + testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager); return testInjector; }