Skip to content

Commit 2eae80d

Browse files
committed
fix(resource-update-command-tests): inject the new service in tests
fix(style): change indentations in new service to tabs
1 parent 2f92c16 commit 2eae80d

8 files changed

+106
-79
lines changed

lib/commands/resources-update.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
export class ResourcesUpdateCommand implements ICommand {
2-
public allowedParameters: ICommandParameter[] = [];
2+
public allowedParameters: ICommandParameter[] = [];
33

4-
constructor(private $projectData: IProjectData,
5-
private $errors: IErrors,
6-
private $projectV4MigrationService: IProjectV4MigrationService) {
7-
this.$projectData.initializeProjectData();
8-
}
4+
constructor(private $projectData: IProjectData,
5+
private $errors: IErrors,
6+
private $projectV4MigrationService: IProjectV4MigrationService) {
7+
this.$projectData.initializeProjectData();
8+
}
99

10-
public async execute(args: string[]): Promise<void> {
11-
await this.$projectV4MigrationService.migrate(this.$projectData.getAppResourcesDirectoryPath());
12-
}
10+
public async execute(args: string[]): Promise<void> {
11+
await this.$projectV4MigrationService.migrate(this.$projectData.getAppResourcesDirectoryPath());
12+
}
1313

14-
public async canExecute(args: string[]): Promise<boolean> {
15-
if (!args || args.length === 0) {
16-
// Command defaults to migrating the Android App_Resources, unless explicitly specified
17-
args = ["android"];
18-
}
14+
public async canExecute(args: string[]): Promise<boolean> {
15+
if (!args || args.length === 0) {
16+
// Command defaults to migrating the Android App_Resources, unless explicitly specified
17+
args = ["android"];
18+
}
1919

20-
for (const platform of args) {
21-
if (!this.$projectV4MigrationService.canMigrate(platform)) {
22-
this.$errors.failWithoutHelp("The iOS platform does not need to have its resources updated.");
23-
}
20+
for (const platform of args) {
21+
if (!this.$projectV4MigrationService.canMigrate(platform)) {
22+
this.$errors.failWithoutHelp("The iOS platform does not need to have its resources updated.");
23+
}
2424

25-
if (this.$projectV4MigrationService.hasMigrated(this.$projectData.getAppResourcesDirectoryPath())) {
26-
this.$errors.failWithoutHelp("The App_Resources have already been updated for the Android platform.");
27-
}
28-
}
25+
if (this.$projectV4MigrationService.hasMigrated(this.$projectData.getAppResourcesDirectoryPath())) {
26+
this.$errors.failWithoutHelp("The App_Resources have already been updated for the Android platform.");
27+
}
28+
}
2929

30-
return true;
31-
}
30+
return true;
31+
}
3232
}
3333

3434
$injector.registerCommand("resources-update", ResourcesUpdateCommand);

lib/services/prepare-platform-native-service.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,22 @@ export class PreparePlatformNativeService extends PreparePlatformService impleme
7272
if (platformData.normalizedPlatformName.toLowerCase() === "android") {
7373
const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
7474
const appResourcesDirStructureHasMigrated = this.$projectV4MigrationService.hasMigrated(appResourcesDirectoryPath);
75+
const appResourcesAndroid = path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName);
7576

7677
if (appResourcesDirStructureHasMigrated) {
77-
shell.cp("-Rf", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "src", "main"), appResourcesDestination);
78+
shell.cp("-Rf", path.join(appResourcesAndroid, "src", "main"), appResourcesDestination);
7879

7980
return;
8081
}
82+
83+
// https://github.com/NativeScript/android-runtime/issues/899
84+
// App_Resources/Android/libs is reserved to user's aars and jars, but they should not be copied as resources
85+
shell.cp("-Rf", path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
86+
shell.rm("-Rf", path.join(appResourcesDestination, "libs"));
87+
88+
this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
89+
90+
return;
8191
}
8292

8393
shell.cp("-Rf", path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);

lib/services/project-v4-migration-service.ts

+54-54
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,59 @@ import * as path from "path";
22
import * as shell from "shelljs";
33

44
export class ProjectV4MigrationService implements IProjectV4MigrationService {
5-
private static ANDROID_DIR = "Android";
6-
private static ANDROID_DIR_TEMP = "Android-Updated";
7-
private static ANDROID_DIR_OLD = "Android-Pre-v4";
8-
9-
constructor(private $fs: IFileSystem,
10-
private $logger: ILogger,
11-
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
12-
13-
canMigrate(platformString: string): boolean {
14-
if (platformString.toLowerCase() === this.$devicePlatformsConstants.iOS.toLowerCase()) {
15-
return false;
16-
}
17-
18-
return true;
19-
}
20-
21-
hasMigrated(appResourcesDir: string): boolean {
22-
return this.$fs.exists(path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR, "src", "main"));
23-
}
24-
25-
async migrate(appResourcesDir: string): Promise<void> {
26-
const originalAppResources = path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR);
27-
const appResourcesDestination = path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR_TEMP);
28-
const appMainSourceSet = path.join(appResourcesDestination, "src", "main");
29-
const appResourcesMainSourceSetResourcesDestination = path.join(appMainSourceSet, "res");
30-
31-
this.$fs.ensureDirectoryExists(appResourcesDestination);
32-
this.$fs.ensureDirectoryExists(appMainSourceSet);
33-
// create /java, /res and /assets in the App_Resources/Android/src/main directory
34-
this.$fs.ensureDirectoryExists(appResourcesMainSourceSetResourcesDestination);
35-
this.$fs.ensureDirectoryExists(path.join(appMainSourceSet, "java"));
36-
this.$fs.ensureDirectoryExists(path.join(appMainSourceSet, "assets"));
37-
38-
const isDirectory = (source: string) => this.$fs.getLsStats(source).isDirectory()
39-
const getDirectories = (source: string) =>
40-
this.$fs.readDirectory(source).map(name => path.join(source, name)).filter(isDirectory)
41-
42-
shell.cp(path.join(originalAppResources, "app.gradle"), path.join(appResourcesDestination, "app.gradle"));
43-
shell.cp(path.join(originalAppResources, "AndroidManifest.xml"), path.join(appMainSourceSet, "AndroidManifest.xml"));
44-
45-
let resourceDirectories = getDirectories(originalAppResources);
46-
47-
resourceDirectories.forEach(dir => {
48-
shell.cp("-Rf", dir, appResourcesMainSourceSetResourcesDestination);
49-
});
50-
51-
// rename the pre-v4 app_resources to ANDROID_DIR_OLD
52-
shell.mv(originalAppResources, path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR_OLD));
53-
// move the new, updated app_resources to App_Resources/Android, as the de facto resources
54-
shell.mv(appResourcesDestination, originalAppResources)
55-
56-
this.$logger.out(`Successfully updated your project's App_Resources/Android directory structure.\nThe previous version of App_Resources/Android has been renamed to App_Resources/${ProjectV4MigrationService.ANDROID_DIR_OLD}`);
57-
}
5+
private static ANDROID_DIR = "Android";
6+
private static ANDROID_DIR_TEMP = "Android-Updated";
7+
private static ANDROID_DIR_OLD = "Android-Pre-v4";
8+
9+
constructor(private $fs: IFileSystem,
10+
private $logger: ILogger,
11+
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
12+
13+
canMigrate(platformString: string): boolean {
14+
if (platformString.toLowerCase() === this.$devicePlatformsConstants.iOS.toLowerCase()) {
15+
return false;
16+
}
17+
18+
return true;
19+
}
20+
21+
hasMigrated(appResourcesDir: string): boolean {
22+
return this.$fs.exists(path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR, "src", "main"));
23+
}
24+
25+
async migrate(appResourcesDir: string): Promise<void> {
26+
const originalAppResources = path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR);
27+
const appResourcesDestination = path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR_TEMP);
28+
const appMainSourceSet = path.join(appResourcesDestination, "src", "main");
29+
const appResourcesMainSourceSetResourcesDestination = path.join(appMainSourceSet, "res");
30+
31+
this.$fs.ensureDirectoryExists(appResourcesDestination);
32+
this.$fs.ensureDirectoryExists(appMainSourceSet);
33+
// create /java, /res and /assets in the App_Resources/Android/src/main directory
34+
this.$fs.ensureDirectoryExists(appResourcesMainSourceSetResourcesDestination);
35+
this.$fs.ensureDirectoryExists(path.join(appMainSourceSet, "java"));
36+
this.$fs.ensureDirectoryExists(path.join(appMainSourceSet, "assets"));
37+
38+
const isDirectory = (source: string) => this.$fs.getLsStats(source).isDirectory();
39+
const getDirectories = (source: string) =>
40+
this.$fs.readDirectory(source).map(name => path.join(source, name)).filter(isDirectory);
41+
42+
shell.cp(path.join(originalAppResources, "app.gradle"), path.join(appResourcesDestination, "app.gradle"));
43+
shell.cp(path.join(originalAppResources, "AndroidManifest.xml"), path.join(appMainSourceSet, "AndroidManifest.xml"));
44+
45+
const resourceDirectories = getDirectories(originalAppResources);
46+
47+
resourceDirectories.forEach(dir => {
48+
shell.cp("-Rf", dir, appResourcesMainSourceSetResourcesDestination);
49+
});
50+
51+
// rename the pre-v4 app_resources to ANDROID_DIR_OLD
52+
shell.mv(originalAppResources, path.join(appResourcesDir, ProjectV4MigrationService.ANDROID_DIR_OLD));
53+
// move the new, updated app_resources to App_Resources/Android, as the de facto resources
54+
shell.mv(appResourcesDestination, originalAppResources);
55+
56+
this.$logger.out(`Successfully updated your project's App_Resources/Android directory structure.\nThe previous version of App_Resources/Android has been renamed to App_Resources/${ProjectV4MigrationService.ANDROID_DIR_OLD}`);
57+
}
5858
}
5959

60-
$injector.register("projectV4MigrationService", ProjectV4MigrationService);
60+
$injector.register("projectV4MigrationService", ProjectV4MigrationService);

test/debug.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function createTestInjector(): IInjector {
7676
}
7777
});
7878
testInjector.register("settingsService", SettingsService);
79+
testInjector.register("projectV4MigrationService", stubs.ProjectV4MigrationServiceStub);
7980

8081
return testInjector;
8182
}

test/npm-support.ts

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function createTestInjector(): IInjector {
9797
})
9898
});
9999
testInjector.register("httpClient", {});
100+
testInjector.register("projectV4MigrationService", stubs.ProjectV4MigrationServiceStub);
100101

101102
return testInjector;
102103
}

test/platform-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function createTestInjector() {
103103
message: (): void => undefined
104104
})
105105
});
106+
testInjector.register("projectV4MigrationService", stubs.ProjectV4MigrationServiceStub);
106107

107108
return testInjector;
108109
}

test/plugins-service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ function createTestInjector() {
112112
});
113113
testInjector.register("httpClient", {});
114114
testInjector.register("extensibilityService", {});
115+
testInjector.register("projectV4MigrationService", stubs.ProjectV4MigrationServiceStub);
116+
115117
return testInjector;
116118
}
117119

test/stubs.ts

+12
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,15 @@ export class EmulatorPlatformService implements IEmulatorPlatformService {
773773
return Promise.resolve();
774774
}
775775
}
776+
777+
export class ProjectV4MigrationServiceStub implements IProjectV4MigrationService {
778+
canMigrate(platformString: string): boolean {
779+
return true;
780+
}
781+
hasMigrated(appResourcesDir: string): boolean {
782+
return false;
783+
}
784+
migrate(appResourcesDir: string): Promise<void> {
785+
return Promise.resolve();
786+
}
787+
}

0 commit comments

Comments
 (0)