Skip to content

Commit fe972a9

Browse files
Fatme HavaluovaFatme Havaluova
Fatme Havaluova
authored and
Fatme Havaluova
committed
Handle platform specific files in plugin
1 parent f1e3cad commit fe972a9

File tree

6 files changed

+71
-43
lines changed

6 files changed

+71
-43
lines changed

bin/nativescript.js

100644100755
File mode changed.

lib/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ $injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
6161
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
6262

6363
$injector.require("doctorService", "./services/doctor-service");
64+
$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();
@@ -342,33 +333,6 @@ export class PlatformService implements IPlatformService {
342333
return platformData.platformProjectService.isPlatformPrepared(platformData.projectRoot);
343334
}
344335

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

0 commit comments

Comments
 (0)