Skip to content

Commit 44d70bb

Browse files
FatmeFatme
Fatme
authored and
Fatme
committed
Merge pull request #515 from NativeScript/fatme/fix-prepare
Process as platform specific files only these located in app folder
2 parents 8309939 + 2f79b4d commit 44d70bb

File tree

3 files changed

+120
-7
lines changed

3 files changed

+120
-7
lines changed

lib/services/platform-service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,16 @@ export class PlatformService implements IPlatformService {
157157
}
158158

159159
// Process platform specific files
160-
var contents = this.$fs.readDirectory(platformData.appDestinationDirectoryPath).wait();
160+
var contents = this.$fs.readDirectory(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait();
161161
var files: string[] = [];
162162

163163
_.each(contents, d => {
164-
var fsStat = this.$fs.getFsStats(path.join(platformData.appDestinationDirectoryPath, d)).wait();
164+
let filePath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, d);
165+
let fsStat = this.$fs.getFsStats(filePath).wait();
165166
if(fsStat.isDirectory() && d !== constants.APP_RESOURCES_FOLDER_NAME) {
166-
this.processPlatformSpecificFiles(platform, this.$fs.enumerateFilesInDirectorySync(path.join(platformData.appDestinationDirectoryPath, d))).wait();
167+
this.processPlatformSpecificFiles(platform, this.$fs.enumerateFilesInDirectorySync(filePath)).wait();
167168
} else if(fsStat.isFile()) {
168-
files.push(path.join(platformData.appDestinationDirectoryPath, d));
169+
files.push(filePath);
169170
}
170171
});
171172

test/platform-service.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import Future = require("fibers/future");
2525
var assert = require("chai").assert;
2626
require('should');
2727

28+
let temp = require("temp");
29+
temp.track();
30+
2831
function createTestInjector() {
2932
var testInjector = new yok.Yok();
3033

@@ -165,4 +168,115 @@ describe('Platform Service Tests', () => {
165168
});
166169
});
167170
});
171+
172+
describe("prepare platform unit tests", () => {
173+
let testInjector: IInjector, fs: IFileSystem;
174+
beforeEach(() => {
175+
testInjector = createTestInjector();
176+
testInjector.register("fs", fsLib.FileSystem);
177+
fs = testInjector.resolve("fs");
178+
});
179+
it("should process only files in app folder when preparing for iOS platform", () => {
180+
let tempFolder = temp.mkdirSync("prepare platform");
181+
182+
let appFolderPath = path.join(tempFolder, "app");
183+
fs.createDirectory(appFolderPath).wait();
184+
185+
let app1FolderPath = path.join(tempFolder, "app1");
186+
fs.createDirectory(app1FolderPath).wait();
187+
188+
let appDestFolderPath = path.join(tempFolder, "appDest");
189+
let appResourcesFolderPath = path.join(appDestFolderPath, "App_Resources");
190+
191+
// Add platform specific files to app and app1 folders
192+
let platformSpecificFiles = [
193+
"test1.ios.js", "test1-ios-js", "test2.android.js", "test2-android-js"
194+
];
195+
196+
let destinationDirectories = [appFolderPath, app1FolderPath];
197+
198+
_.each(destinationDirectories, directoryPath => {
199+
_.each(platformSpecificFiles, filePath => {
200+
let fileFullPath = path.join(directoryPath, filePath);
201+
fs.writeFile(fileFullPath, "testData").wait();
202+
});
203+
});
204+
205+
let platformsData = testInjector.resolve("platformsData");
206+
platformsData.platformsNames = ["ios", "android"];
207+
platformsData.getPlatformData = (platform: string) => {
208+
return {
209+
appDestinationDirectoryPath: appDestFolderPath,
210+
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
211+
normalizedPlatformName: "iOS"
212+
}
213+
};
214+
215+
let projectData = testInjector.resolve("projectData");
216+
projectData.projectDir = tempFolder;
217+
218+
let platformService = testInjector.resolve("platformService");
219+
platformService.preparePlatform("ios").wait();
220+
221+
// Asserts that the files in app folder are process as platform specific
222+
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app" , "test1.js")).wait());
223+
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
224+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2.js")).wait());
225+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
226+
227+
// Asserts that the files in app1 folder aren't process as platform specific
228+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app1")).wait());
229+
});
230+
it("should process only files in app folder when preparing for Android platform", () => {
231+
let tempFolder = temp.mkdirSync("prepare platform");
232+
233+
let appFolderPath = path.join(tempFolder, "app");
234+
fs.createDirectory(appFolderPath).wait();
235+
236+
let app1FolderPath = path.join(tempFolder, "app1");
237+
fs.createDirectory(app1FolderPath).wait();
238+
239+
let appDestFolderPath = path.join(tempFolder, "appDest");
240+
let appResourcesFolderPath = path.join(appDestFolderPath, "App_Resources");
241+
242+
// Add platform specific files to app and app1 folders
243+
let platformSpecificFiles = [
244+
"test1.ios.js", "test1-ios-js", "test2.android.js", "test2-android-js"
245+
];
246+
247+
let destinationDirectories = [appFolderPath, app1FolderPath];
248+
249+
_.each(destinationDirectories, directoryPath => {
250+
_.each(platformSpecificFiles, filePath => {
251+
let fileFullPath = path.join(directoryPath, filePath);
252+
fs.writeFile(fileFullPath, "testData").wait();
253+
});
254+
});
255+
256+
let platformsData = testInjector.resolve("platformsData");
257+
platformsData.platformsNames = ["ios", "android"];
258+
platformsData.getPlatformData = (platform: string) => {
259+
return {
260+
appDestinationDirectoryPath: appDestFolderPath,
261+
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
262+
normalizedPlatformName: "Android"
263+
}
264+
};
265+
266+
let projectData = testInjector.resolve("projectData");
267+
projectData.projectDir = tempFolder;
268+
269+
let platformService = testInjector.resolve("platformService");
270+
platformService.preparePlatform("android").wait();
271+
272+
// Asserts that the files in app folder are process as platform specific
273+
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app" , "test2.js")).wait());
274+
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
275+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1.js")).wait());
276+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
277+
278+
// Asserts that the files in app1 folder aren't process as platform specific
279+
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app1")).wait());
280+
});
281+
});
168282
});

test/stubs.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ export class ProjectDataStub implements IProjectData {
227227
}
228228

229229
export class PlatformsDataStub implements IPlatformsData {
230-
public get platformsNames(): string[] {
231-
return undefined;
232-
}
230+
public platformsNames: string[];
233231

234232
public getPlatformData(platform: string): IPlatformData {
235233
return {

0 commit comments

Comments
 (0)