Skip to content

Commit 0892dde

Browse files
Fatme HavaluovaFatme Havaluova
Fatme Havaluova
authored and
Fatme Havaluova
committed
Handle platform specific files in plugin
1 parent 3061fcd commit 0892dde

10 files changed

+84
-46
lines changed

bin/nativescript.js

100644100755
File mode changed.

lib/bootstrap.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ $injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wra
5858

5959
$injector.require("pluginsService", "./services/plugins-service");
6060
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
61-
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
61+
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
62+
63+
$injector.require("projectFilesManager", "./services/project-files-manager");

lib/declarations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ interface IOptions extends ICommonOptions {
6464
keyStoreAliasPassword: string;
6565
sdk: string;
6666
}
67+
68+
interface IProjectFilesManager {
69+
processPlatformSpecificFiles(directoryPath: string, platform: string, excludedDirs?: string[]): IFuture<void>;
70+
}

lib/services/platform-service.ts

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class PlatformService implements IPlatformService {
2323
private $commandsService: ICommandsService,
2424
private $options: IOptions,
2525
private $broccoliBuilder: IBroccoliBuilder,
26-
private $pluginsService: IPluginsService) { }
26+
private $pluginsService: IPluginsService,
27+
private $projectFilesManager: IProjectFilesManager) { }
2728

2829
public addPlatforms(platforms: string[]): IFuture<void> {
2930
return (() => {
@@ -165,19 +166,9 @@ export class PlatformService implements IPlatformService {
165166
}
166167

167168
// Process platform specific files
168-
var contents = this.$fs.readDirectory(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait();
169-
var files: string[] = [];
170-
171-
_.each(contents, d => {
172-
let filePath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, d);
173-
let fsStat = this.$fs.getFsStats(filePath).wait();
174-
if(fsStat.isDirectory() && d !== constants.APP_RESOURCES_FOLDER_NAME) {
175-
this.processPlatformSpecificFiles(platform, this.$fs.enumerateFilesInDirectorySync(filePath)).wait();
176-
} else if(fsStat.isFile()) {
177-
files.push(filePath);
178-
}
179-
});
180-
this.processPlatformSpecificFiles(platform, files).wait();
169+
let directoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
170+
let excludedDirs = [constants.APP_RESOURCES_FOLDER_NAME];
171+
this.$projectFilesManager.processPlatformSpecificFiles(directoryPath, platform, excludedDirs).wait();
181172

182173
// Process node_modules folder
183174
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
@@ -341,33 +332,6 @@ export class PlatformService implements IPlatformService {
341332
return platformData.platformProjectService.isPlatformPrepared(platformData.projectRoot);
342333
}
343334

344-
private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any {
345-
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms.join("|"));
346-
var parsed = fileName.toLowerCase().match(new RegExp(regex, "i"));
347-
if (parsed) {
348-
return {
349-
platform: parsed[2],
350-
onDeviceName: parsed[1] + parsed[3]
351-
};
352-
}
353-
return undefined;
354-
}
355-
356-
private processPlatformSpecificFiles( platform: string, files: string[]): IFuture<void> {
357-
// Renames the files that have `platform` as substring and removes the files from other platform
358-
return (() => {
359-
_.each(files, fileName => {
360-
var platformInfo = PlatformService.parsePlatformSpecificFileName(path.basename(fileName), this.$platformsData.platformsNames);
361-
var shouldExcludeFile = platformInfo && platformInfo.platform !== platform;
362-
if (shouldExcludeFile) {
363-
this.$fs.deleteFile(fileName).wait();
364-
} else if (platformInfo && platformInfo.onDeviceName) {
365-
this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait();
366-
}
367-
});
368-
}).future<void>()();
369-
}
370-
371335
private getApplicationPackages(buildOutputPath: string, validPackageNames: string[]): IFuture<IApplicationPackage[]> {
372336
return (() => {
373337
// Get latest package that is produced from build

lib/services/plugins-service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export class PluginsService implements IPluginsService {
2424
private $options: IOptions,
2525
private $logger: ILogger,
2626
private $errors: IErrors,
27-
private $injector: IInjector) { }
27+
private $injector: IInjector,
28+
private $projectFilesManager: IProjectFilesManager) { }
2829

2930
public add(plugin: string): IFuture<void> {
3031
return (() => {
@@ -101,7 +102,9 @@ export class PluginsService implements IPluginsService {
101102

102103
if(this.$fs.exists(pluginPlatformsFolderPath).wait()) {
103104
shelljs.rm("-rf", pluginPlatformsFolderPath);
104-
}
105+
}
106+
107+
this.$projectFilesManager.processPlatformSpecificFiles(pluginDestinationPath, platform).wait();
105108

106109
// TODO: Add libraries
107110

lib/services/project-files-manager.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
///<reference path="../.d.ts"/>
2+
"use strict";
3+
import path = require("path");
4+
import util = require("util");
5+
6+
export class ProjectFilesManager implements IProjectFilesManager {
7+
constructor(private $fs: IFileSystem,
8+
private $platformsData: IPlatformsData) { }
9+
10+
public processPlatformSpecificFiles(directoryPath: string, platform: string, excludedDirs?: string[]) {
11+
return (() => {
12+
var contents = this.$fs.readDirectory(directoryPath).wait();
13+
var files: string[] = [];
14+
15+
_.each(contents, fileName => {
16+
let filePath = path.join(directoryPath, fileName);
17+
let fsStat = this.$fs.getFsStats(filePath).wait();
18+
if(fsStat.isDirectory() && (!excludedDirs || (excludedDirs && !_.contains(excludedDirs, fileName)))) {
19+
this.processPlatformSpecificFilesCore(platform, this.$fs.enumerateFilesInDirectorySync(filePath)).wait();
20+
} else if(fsStat.isFile()) {
21+
files.push(filePath);
22+
}
23+
});
24+
this.processPlatformSpecificFilesCore(platform, files).wait();
25+
26+
}).future<void>()();
27+
}
28+
29+
private processPlatformSpecificFilesCore(platform: string, files: string[]): IFuture<void> {
30+
// Renames the files that have `platform` as substring and removes the files from other platform
31+
return (() => {
32+
_.each(files, fileName => {
33+
var platformInfo = ProjectFilesManager.parsePlatformSpecificFileName(path.basename(fileName), this.$platformsData.platformsNames);
34+
var shouldExcludeFile = platformInfo && platformInfo.platform !== platform;
35+
if (shouldExcludeFile) {
36+
this.$fs.deleteFile(fileName).wait();
37+
} else if (platformInfo && platformInfo.onDeviceName) {
38+
this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait();
39+
}
40+
});
41+
}).future<void>()();
42+
}
43+
44+
private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any {
45+
var regex = util.format("^(.+?)\\.(%s)(\\..+?)$", platforms.join("|"));
46+
var parsed = fileName.match(new RegExp(regex, "i"));
47+
if (parsed) {
48+
return {
49+
platform: parsed[2],
50+
onDeviceName: parsed[1] + parsed[3]
51+
};
52+
}
53+
return undefined;
54+
}
55+
}
56+
$injector.register("projectFilesManager", ProjectFilesManager);

test/npm-support.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import BroccoliBuilderLib = require("../lib/tools/broccoli/builder");
1919
import NodeModulesTreeLib = require("../lib/tools/broccoli/trees/node-modules-tree");
2020
import PluginsServiceLib = require("../lib/services/plugins-service");
2121
import ChildProcessLib = require("../lib/common/child-process");
22+
import ProjectFilesManagerLib = require("../lib/services/project-files-manager");
23+
import Future = require("fibers/future");
2224

2325
import path = require("path");
2426
import temp = require("temp");
@@ -53,6 +55,7 @@ function createTestInjector(): IInjector {
5355
testInjector.register("pluginsService", PluginsServiceLib.PluginsService);
5456
testInjector.register("npm", NpmLib.NodePackageManager);
5557
testInjector.register("childProcess", ChildProcessLib.ChildProcess);
58+
testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager);
5659
testInjector.register("commandsServiceProvider", {
5760
registerDynamicSubCommands: () => {}
5861
});

test/platform-commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import StaticConfigLib = require("../lib/config");
1111
import CommandsServiceLib = require("../lib/common/services/commands-service");
1212
import optionsLib = require("../lib/options");
1313
import hostInfoLib = require("../lib/common/host-info");
14+
import ProjectFilesManagerLib = require("../lib/services/project-files-manager");
1415
import path = require("path");
1516
import Future = require("fibers/future");
1617
var assert = require("chai").assert;
@@ -115,6 +116,7 @@ function createTestInjector() {
115116
}).future<IPluginData[]>()();
116117
}
117118
});
119+
testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager);
118120

119121
return testInjector;
120122
}

test/platform-service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ProjectDataLib = require("../lib/project-data");
1818
import ProjectHelperLib = require("../lib/common/project-helper");
1919
import optionsLib = require("../lib/options");
2020
import hostInfoLib = require("../lib/common/host-info");
21+
import ProjectFilesManagerLib = require("../lib/services/project-files-manager");
2122

2223
import path = require("path");
2324
import Future = require("fibers/future");
@@ -63,6 +64,7 @@ function createTestInjector() {
6364
return (() => { }).future<void>()();
6465
}
6566
});
67+
testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager);
6668

6769
return testInjector;
6870
}
@@ -235,7 +237,7 @@ describe('Platform Service Tests', () => {
235237

236238
// Asserts that the files in app folder are process as platform specific
237239
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app" , "test1.js")).wait());
238-
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
240+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
239241
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2.js")).wait());
240242
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
241243

@@ -286,7 +288,7 @@ describe('Platform Service Tests', () => {
286288

287289
// Asserts that the files in app folder are process as platform specific
288290
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app" , "test2.js")).wait());
289-
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
291+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
290292
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1.js")).wait());
291293
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
292294

test/plugins-service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ProjectHelperLib = require("../lib/common/project-helper");
1717
import PlatformsDataLib = require("../lib/platforms-data");
1818
import ProjectDataServiceLib = require("../lib/services/project-data-service");
1919
import helpers = require("../lib/common/helpers");
20+
import ProjectFilesManagerLib = require("../lib/services/project-files-manager");
2021
import os = require("os");
2122

2223
import PluginsServiceLib = require("../lib/services/plugins-service");
@@ -64,6 +65,7 @@ function createTestInjector() {
6465
checkConsent: () => { return (() => { }).future<void>()(); },
6566
trackFeature: () => { return (() => { }).future<void>()(); }
6667
});
68+
testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager);
6769

6870
return testInjector;
6971
}

0 commit comments

Comments
 (0)