Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Filter configuration specific files #986

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ interface ILiveSyncServiceBase {
* If watch option is not specified executes full sync
* If watch option is specified executes partial sync
*/
sync(data: ILiveSyncData[], projectId: string, filePaths?: string[]): Promise<void>;
sync(data: ILiveSyncData[], projectId: string, projectFilesConfig: IProjectFilesConfig, filePaths?: string[]): Promise<void>;

/**
* Returns the `canExecute` method which defines if LiveSync operation can be executed on specified device.
Expand Down Expand Up @@ -1489,7 +1489,7 @@ interface IProjectFilesManager {
* @param {string[]} excludedDirs Directories which should be skipped.
* @returns {void}
*/
processPlatformSpecificFiles(directoryPath: string, platform: string, excludedDirs?: string[]): void;
processPlatformSpecificFiles(directoryPath: string, platform: string, projectFilesConfig?: IProjectFilesConfig, excludedDirs?: string[]): void;
}

interface IProjectFilesProvider {
Expand All @@ -1500,7 +1500,7 @@ interface IProjectFilesProvider {
/**
* Performs local file path mapping
*/
mapFilePath(filePath: string, platform: string, projectData?: any): string;
mapFilePath(filePath: string, platform: string, projectData: any, projectFilesConfig?: IProjectFilesConfig): string;

/**
* Returns information about file in the project, that includes file's name on device after removing platform or configuration from the name.
Expand All @@ -1509,13 +1509,13 @@ interface IProjectFilesProvider {
* @param {IProjectFilesConfig} projectFilesConfig configuration for additional parsing
* @return {IProjectFileInfo}
*/
getProjectFileInfo(filePath: string, platform: string, projectFilesConfig?: IProjectFilesConfig): IProjectFileInfo;
getProjectFileInfo(filePath: string, platform: string, projectFilesConfig: IProjectFilesConfig): IProjectFileInfo;
/**
* Parses file by removing platform or configuration from its name.
* @param {string} filePath Path to the project file.
* @return {string} Parsed file name or original file name in case it does not have platform/configuration in the filename.
*/
getPreparedFilePath(filePath: string): string;
getPreparedFilePath(filePath: string, projectFilesConfig: IProjectFilesConfig): string;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as net from "net";
let Table = require("cli-table");
import { platform, EOL } from "os";
import { ReadStream } from "tty";
import { Configurations } from "./constants";
import { EventEmitter } from "events";
import * as crypto from "crypto";

Expand Down Expand Up @@ -405,6 +406,13 @@ export async function connectEventuallyUntilTimeout(factory: () => net.Socket, t
});
}

export function getProjectFilesConfig(opts: { isReleaseBuild: boolean }): IProjectFilesConfig {
const projectFilesConfig: IProjectFilesConfig = {
configuration: opts.isReleaseBuild ? Configurations.Release.toLowerCase() : Configurations.Debug.toLowerCase()
};
return projectFilesConfig;
}

/**
* Tries to find the process id (PID) of the specified application identifier.
* This is specific implementation for iOS Simulator, where the running applications are real processes.
Expand Down
8 changes: 4 additions & 4 deletions services/livesync-service-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class LiveSyncServiceBase implements ILiveSyncServiceBase {
this.fileHashes = Object.create(null);
}

public async sync(data: ILiveSyncData[], projectId: string, filePaths?: string[]): Promise<void> {
public async sync(data: ILiveSyncData[], projectId: string, projectFilesConfig: IProjectFilesConfig, filePaths?: string[]): Promise<void> {
await this.syncCore(data, filePaths);
if (this.$options.watch) {
await this.$hooksService.executeBeforeHooks('watch');
this.partialSync(data, data[0].syncWorkingDirectory, projectId);
this.partialSync(data, data[0].syncWorkingDirectory, projectId, projectFilesConfig);
}
}

Expand All @@ -48,7 +48,7 @@ class LiveSyncServiceBase implements ILiveSyncServiceBase {
return isFileExcluded;
}

private partialSync(data: ILiveSyncData[], syncWorkingDirectory: string, projectId: string): void {
private partialSync(data: ILiveSyncData[], syncWorkingDirectory: string, projectId: string, projectFilesConfig: IProjectFilesConfig): void {
let that = this;
this.showFullLiveSyncInformation = true;
const gazeInstance = gaze(["**/*", "!node_modules/**/*", "!platforms/**/*"], { cwd: syncWorkingDirectory }, function (err: any, watcher: any) {
Expand Down Expand Up @@ -76,7 +76,7 @@ class LiveSyncServiceBase implements ILiveSyncServiceBase {
that.$logger.trace(`Skipping livesync for changed file ${filePath} as it is excluded in the patterns: ${dataItem.excludedProjectDirsAndFiles.join(", ")}`);
continue;
}
let mappedFilePath = that.$projectFilesProvider.mapFilePath(filePath, dataItem.platform, projectId);
let mappedFilePath = that.$projectFilesProvider.mapFilePath(filePath, dataItem.platform, projectId, projectFilesConfig);
that.$logger.trace(`Syncing filePath ${filePath}, mappedFilePath is ${mappedFilePath}`);
if (!mappedFilePath) {
that.$logger.warn(`Unable to sync ${filePath}.`);
Expand Down
10 changes: 5 additions & 5 deletions services/project-files-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ export class ProjectFilesManager implements IProjectFilesManager {
return localToDevicePaths;
}

public processPlatformSpecificFiles(directoryPath: string, platform: string, excludedDirs?: string[]): void {
public processPlatformSpecificFiles(directoryPath: string, platform: string, projectFilesConfig: IProjectFilesConfig, excludedDirs?: string[]): void {
let contents = this.$fs.readDirectory(directoryPath);
let files: string[] = [];

_.each(contents, fileName => {
let filePath = path.join(directoryPath, fileName);
let fsStat = this.$fs.getFsStats(filePath);
if (fsStat.isDirectory() && !_.includes(excludedDirs, fileName)) {
this.processPlatformSpecificFilesCore(platform, this.$fs.enumerateFilesInDirectorySync(filePath));
this.processPlatformSpecificFilesCore(platform, this.$fs.enumerateFilesInDirectorySync(filePath), projectFilesConfig);
} else if (fsStat.isFile()) {
files.push(filePath);
}
});

this.processPlatformSpecificFilesCore(platform, files);
this.processPlatformSpecificFilesCore(platform, files, projectFilesConfig);
}

private processPlatformSpecificFilesCore(platform: string, files: string[]): void {
private processPlatformSpecificFilesCore(platform: string, files: string[], projectFilesConfig: IProjectFilesConfig): void {
// Renames the files that have `platform` as substring and removes the files from other platform
_.each(files, filePath => {
let projectFileInfo = this.$projectFilesProvider.getProjectFileInfo(filePath, platform);
let projectFileInfo = this.$projectFilesProvider.getProjectFileInfo(filePath, platform, projectFilesConfig);
if (!projectFileInfo.shouldIncludeFile) {
this.$fs.deleteFile(filePath);
} else if (projectFileInfo.onDeviceFileName) {
Expand Down
7 changes: 4 additions & 3 deletions services/project-files-provider-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import { Configurations } from "../constants";

export abstract class ProjectFilesProviderBase implements IProjectFilesProvider {
abstract isFileExcluded(filePath: string): boolean;
abstract mapFilePath(filePath: string, platform: string, projectData: any): string;
abstract mapFilePath(filePath: string, platform: string, projectData: any, projectFilesConfig: IProjectFilesConfig): string;

constructor(private $mobileHelper: Mobile.IMobileHelper,
protected $options: ICommonOptions) { }

public getPreparedFilePath(filePath: string): string {
let projectFileInfo = this.getProjectFileInfo(filePath, "");
public getPreparedFilePath(filePath: string, projectFilesConfig?: IProjectFilesConfig): string {
let projectFileInfo = this.getProjectFileInfo(filePath, "", projectFilesConfig);
return path.join(path.dirname(filePath), projectFileInfo.onDeviceFileName);
}

public getProjectFileInfo(filePath: string, platform: string, projectFilesConfig?: IProjectFilesConfig): IProjectFileInfo {
let parsed = this.parseFile(filePath, this.$mobileHelper.platformNames, platform || "");
let basicConfigurations = [Configurations.Debug.toLowerCase(), Configurations.Release.toLowerCase()];
if (!parsed) {

let validValues = basicConfigurations.concat(projectFilesConfig && projectFilesConfig.additionalConfigurations || []),
value = projectFilesConfig && projectFilesConfig.configuration || basicConfigurations[0];
parsed = this.parseFile(filePath, validValues, value);
Expand Down
46 changes: 41 additions & 5 deletions test/unit-tests/mobile/project-files-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,18 @@ function createTestInjector(): IInjector {
}

async function createFiles(testInjector: IInjector, filesToCreate: string[]): Promise<string> {
let fs = testInjector.resolve("fs");
let directoryPath = temp.mkdirSync("Project Files Manager Tests");

_.each(filesToCreate, file => fs.writeFile(path.join(directoryPath, file), ""));
_.each(filesToCreate, file => createFile(testInjector, file, "", directoryPath));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createFile returns promise, currently noone is awaiting this promise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the method to return string. Thanks


return directoryPath;
}

function createFile(testInjector: IInjector, fileToCreate: string, fileContent: string, directoryPath?: string): string {
let fs = testInjector.resolve("fs");
directoryPath = !directoryPath ? temp.mkdirSync("Project Files Manager Tests") : directoryPath;

fs.writeFile(path.join(directoryPath, fileToCreate), fileContent);

return directoryPath;
}
Expand Down Expand Up @@ -170,7 +178,7 @@ describe("Project Files Manager Tests", () => {
let files = ["test.ios.x", "test.android.x"];
let directoryPath = await createFiles(testInjector, files);

projectFilesManager.processPlatformSpecificFiles(directoryPath, "android");
projectFilesManager.processPlatformSpecificFiles(directoryPath, "android", {});

let fs = testInjector.resolve("fs");
assert.isFalse(fs.exists(path.join(directoryPath, "test.ios.x")));
Expand All @@ -182,7 +190,7 @@ describe("Project Files Manager Tests", () => {
let files = ["index.ios.html", "index1.android.html", "a.test"];
let directoryPath = await createFiles(testInjector, files);

projectFilesManager.processPlatformSpecificFiles(directoryPath, "ios");
projectFilesManager.processPlatformSpecificFiles(directoryPath, "ios", {});

let fs = testInjector.resolve("fs");
assert.isFalse(fs.exists(path.join(directoryPath, "index1.android.html")));
Expand All @@ -191,11 +199,39 @@ describe("Project Files Manager Tests", () => {
assert.isTrue(fs.exists(path.join(directoryPath, "a.test")));
});

it("filters release specific files", async () => {
const directoryPath = createFile(testInjector, "test.debug.x", "debug");
const releaseFileContent = "release";
createFile(testInjector, "test.release.x", releaseFileContent, directoryPath);

projectFilesManager.processPlatformSpecificFiles(directoryPath, "android", { configuration: "release" });

const fs = testInjector.resolve("fs");
assert.isFalse(fs.exists(path.join(directoryPath, "test.debug.x")));
assert.isTrue(fs.exists(path.join(directoryPath, "test.x")));
assert.isFalse(fs.exists(path.join(directoryPath, "test.release.x")));
assert.isTrue(fs.readFile(path.join(directoryPath, "test.x")).toString() === releaseFileContent);
});

it("filters debug specific files by default", async () => {
const directoryPath = createFile(testInjector, "test.release.x", "release");
const debugFileContent = "debug";
createFile(testInjector, "test.debug.x", debugFileContent, directoryPath);

projectFilesManager.processPlatformSpecificFiles(directoryPath, "android", {});

const fs = testInjector.resolve("fs");
assert.isFalse(fs.exists(path.join(directoryPath, "test.debug.x")));
assert.isTrue(fs.exists(path.join(directoryPath, "test.x")));
assert.isFalse(fs.exists(path.join(directoryPath, "test.release.x")));
assert.isTrue(fs.readFile(path.join(directoryPath, "test.x")).toString() === debugFileContent);
});

it("doesn't filter non platform specific files", async () => {
let files = ["index1.js", "index2.js", "index3.js"];
let directoryPath = await createFiles(testInjector, files);

projectFilesManager.processPlatformSpecificFiles(directoryPath, "ios");
projectFilesManager.processPlatformSpecificFiles(directoryPath, "ios", {});

let fs = testInjector.resolve("fs");
assert.isTrue(fs.exists(path.join(directoryPath, "index1.js")));
Expand Down
26 changes: 13 additions & 13 deletions test/unit-tests/project-files-provider-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,49 @@ describe("ProjectFilesProviderBase", () => {
describe("getPreparedFilePath", () => {
it("returns correct path, when fileName does not contain platforms", () => {
let filePath = "/test/filePath.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});

it("returns correct path, when fileName contains android platform", () => {
let filePath = "/test/filePath.android.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});

it("returns correct path, when fileName contains ios platform", () => {
let filePath = "/test/filePath.iOS.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});

it("returns correct path, when fileName contains platform (case insensitive test)", () => {
let filePath = "/test/filePath.AnDroId.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});

it("returns correct path, when fileName contains debug configuration", () => {
let filePath = "/test/filePath.debug.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});

it("returns correct path, when fileName contains debug configuration (case insensitive test)", () => {
let filePath = "/test/filePath.DebUG.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});

it("returns correct path, when fileName contains release configuration", () => {
let filePath = "/test/filePath.release.ts",
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath);
preparedPath = projectFilesProviderBase.getPreparedFilePath(filePath, {});

assert.deepEqual(preparedPath, expectedFilePath);
});
Expand All @@ -101,42 +101,42 @@ describe("ProjectFilesProviderBase", () => {

it("process file without platforms in the name", () => {
let filePath = "/test/filePath.ts",
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "");
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "", {});

assert.deepEqual(projectFileInfo, getExpectedProjectFileInfo(filePath, true));
});

it("process file with android platform in the name", () => {
let filePath = "/test/filePath.android.ts",
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android");
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android", {});

assert.deepEqual(projectFileInfo, getExpectedProjectFileInfo(filePath, true));
});

it("process file with android platform in the name (case insensitive test)", () => {
let filePath = "/test/filePath.AndRoID.ts",
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android");
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android", {});

assert.deepEqual(projectFileInfo, getExpectedProjectFileInfo(filePath, true));
});

it("process file with iOS platform in the name", () => {
let filePath = "/test/filePath.ios.ts",
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android");
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android", {});

assert.deepEqual(projectFileInfo, getExpectedProjectFileInfo(filePath, false));
});

it("process file with debug configuration in the name", () => {
let filePath = "/test/filePath.debug.ts",
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android");
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android", {});

assert.deepEqual(projectFileInfo, getExpectedProjectFileInfo(filePath, true));
});

it("process file with release configuration in the name", () => {
let filePath = "/test/filePath.release.ts",
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android");
projectFileInfo = projectFilesProviderBase.getProjectFileInfo(filePath, "android", {});

assert.deepEqual(projectFileInfo, getExpectedProjectFileInfo(filePath, false));
});
Expand Down