Skip to content

Commit 3692d83

Browse files
committed
fix: respect App_Resources directly from project
Currently App_Resources are copied to platforms folder and after that NativeScript CLI moves them to the correct places in native project and deletes the App_Resources folder in platforms. On the other side, nativescript-dev-webpack plugin copies the App_Resources to platforms folder only on initial start (not when some file is changed). This led to the problem that when native file is changed, NativeScript CLI moves App_Resources to the correct places in native project and deletes App_Resources folder inside platforms (the one that nativescript-dev-webpack plugin has previously copied on initial webpack's compilation). On the next native file change, NativeScript CLI tries to move the platforms/App_Resources to the correct places inside native project, but this directory doesn't exist. So `cp unable to find source directory` error is thrown. Fixes: #4377
1 parent 2874b0b commit 3692d83

File tree

5 files changed

+36
-60
lines changed

5 files changed

+36
-60
lines changed

lib/services/android-project-service.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,24 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
280280
}
281281
}
282282

283-
public prepareAppResources(appResourcesDirectoryPath: string, projectData: IProjectData): void {
284-
this.cleanUpPreparedResources(appResourcesDirectoryPath, projectData);
283+
public prepareAppResources(projectData: IProjectData): void {
284+
const platformData = this.getPlatformData(projectData);
285+
const projectAppResourcesPath = projectData.getAppResourcesDirectoryPath(projectData.projectDir);
286+
const platformsAppResourcesPath = this.getAppResourcesDestinationDirectoryPath(projectData);
287+
288+
this.cleanUpPreparedResources(projectAppResourcesPath, projectData);
289+
290+
this.$fs.ensureDirectoryExists(platformsAppResourcesPath);
291+
292+
this.$fs.copyFile(path.join(projectAppResourcesPath, platformData.normalizedPlatformName, "*"), platformsAppResourcesPath);
293+
const appResourcesDirStructureHasMigrated = this.$androidResourcesMigrationService.hasMigrated(projectAppResourcesPath);
294+
if (appResourcesDirStructureHasMigrated) {
295+
this.$fs.copyFile(path.join(projectAppResourcesPath, platformData.normalizedPlatformName, "src", "*"), platformsAppResourcesPath);
296+
} else {
297+
// https://github.com/NativeScript/android-runtime/issues/899
298+
// App_Resources/Android/libs is reserved to user's aars and jars, but they should not be copied as resources
299+
this.$fs.deleteDirectory(path.join(platformsAppResourcesPath, "libs"));
300+
}
285301
}
286302

287303
public async preparePluginNativeCode(pluginData: IPluginData, projectData: IProjectData): Promise<void> {

lib/services/ios-project-service.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -335,20 +335,23 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
335335
}
336336
}
337337

338-
public prepareAppResources(appResourcesDirectoryPath: string, projectData: IProjectData): void {
339-
const platformFolder = path.join(appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName);
340-
const filterFile = (filename: string) => this.$fs.deleteFile(path.join(platformFolder, filename));
338+
public prepareAppResources(projectData: IProjectData): void {
339+
const platformData = this.getPlatformData(projectData);
340+
const projectAppResourcesPath = projectData.getAppResourcesDirectoryPath(projectData.projectDir);
341+
const platformsAppResourcesPath = this.getAppResourcesDestinationDirectoryPath(projectData);
342+
343+
this.$fs.deleteDirectory(platformsAppResourcesPath);
344+
this.$fs.ensureDirectoryExists(platformsAppResourcesPath);
341345

342-
filterFile(this.getPlatformData(projectData).configurationFileName);
343-
filterFile(constants.PODFILE_NAME);
346+
this.$fs.copyFile(path.join(projectAppResourcesPath, platformData.normalizedPlatformName, "*"), platformsAppResourcesPath);
344347

345-
// src folder should not be copied as the pbxproject will have references to its files
346-
this.$fs.deleteDirectory(path.join(appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, constants.NATIVE_SOURCE_FOLDER));
347-
this.$fs.deleteDirectory(path.join(appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, constants.NATIVE_EXTENSION_FOLDER));
348-
this.$fs.deleteDirectory(path.join(appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "watchapp"));
349-
this.$fs.deleteDirectory(path.join(appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "watchextension"));
348+
this.$fs.deleteFile(path.join(platformsAppResourcesPath, platformData.configurationFileName));
349+
this.$fs.deleteFile(path.join(platformsAppResourcesPath, constants.PODFILE_NAME));
350350

351-
this.$fs.deleteDirectory(this.getAppResourcesDestinationDirectoryPath(projectData));
351+
this.$fs.deleteDirectory(path.join(platformsAppResourcesPath, constants.NATIVE_SOURCE_FOLDER));
352+
this.$fs.deleteDirectory(path.join(platformsAppResourcesPath, constants.NATIVE_EXTENSION_FOLDER));
353+
this.$fs.deleteDirectory(path.join(platformsAppResourcesPath, "watchapp"));
354+
this.$fs.deleteDirectory(path.join(platformsAppResourcesPath, "watchextension"));
352355
}
353356

354357
public async processConfigurationFilesFromAppResources(projectData: IProjectData, opts: IRelease): Promise<void> {

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

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11

22
import { hook } from "../../common/helpers";
33
import { performanceLog } from "../../common/decorators";
4-
import * as path from "path";
5-
import { NativePlatformStatus, APP_FOLDER_NAME, APP_RESOURCES_FOLDER_NAME } from "../../constants";
4+
import { NativePlatformStatus } from "../../constants";
65

76
export class PrepareNativePlatformService implements IPrepareNativePlatformService {
87

98
constructor(
10-
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
11-
private $fs: IFileSystem,
129
public $hooksService: IHooksService,
1310
private $nodeModulesBuilder: INodeModulesBuilder,
1411
private $projectChangesService: IProjectChangesService,
@@ -34,10 +31,7 @@ export class PrepareNativePlatformService implements IPrepareNativePlatformServi
3431
await this.cleanProject(platformData, projectData, { release });
3532
}
3633

37-
// Move the native application resources from platforms/.../app/App_Resources
38-
// to the right places in the native project,
39-
// because webpack copies them on every build (not every change).
40-
this.prepareAppResources(platformData, projectData);
34+
platformData.platformProjectService.prepareAppResources(projectData);
4135

4236
if (hasChangesRequirePrepare) {
4337
await platformData.platformProjectService.prepareProject(projectData, prepareData);
@@ -58,43 +52,6 @@ export class PrepareNativePlatformService implements IPrepareNativePlatformServi
5852
return hasChanges;
5953
}
6054

61-
private prepareAppResources(platformData: IPlatformData, projectData: IProjectData): void {
62-
const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, APP_FOLDER_NAME);
63-
const appResourcesDestinationDirectoryPath = path.join(appDestinationDirectoryPath, APP_RESOURCES_FOLDER_NAME);
64-
65-
if (this.$fs.exists(appResourcesDestinationDirectoryPath)) {
66-
platformData.platformProjectService.prepareAppResources(appResourcesDestinationDirectoryPath, projectData);
67-
const appResourcesDestination = platformData.platformProjectService.getAppResourcesDestinationDirectoryPath(projectData);
68-
this.$fs.ensureDirectoryExists(appResourcesDestination);
69-
70-
if (platformData.normalizedPlatformName.toLowerCase() === "android") {
71-
const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
72-
const appResourcesDirStructureHasMigrated = this.$androidResourcesMigrationService.hasMigrated(appResourcesDirectoryPath);
73-
const appResourcesAndroid = path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName);
74-
75-
if (appResourcesDirStructureHasMigrated) {
76-
this.$fs.copyFile(path.join(appResourcesAndroid, "src", "*"), appResourcesDestination);
77-
78-
this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
79-
return;
80-
}
81-
82-
// https://github.com/NativeScript/android-runtime/issues/899
83-
// App_Resources/Android/libs is reserved to user's aars and jars, but they should not be copied as resources
84-
this.$fs.copyFile(path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
85-
this.$fs.deleteDirectory(path.join(appResourcesDestination, "libs"));
86-
87-
this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
88-
89-
return;
90-
}
91-
92-
this.$fs.copyFile(path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
93-
94-
this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
95-
}
96-
}
97-
9855
private async cleanProject(platformData: IPlatformData, projectData: IProjectData, options: { release: boolean }): Promise<void> {
9956
// android build artifacts need to be cleaned up
10057
// when switching between debug, release and webpack builds

lib/services/webpack/webpack.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ declare global {
7878
* @param {IProjectData} projectData DTO with information about the project.
7979
* @returns {void}
8080
*/
81-
prepareAppResources(appResourcesDirectoryPath: string, projectData: IProjectData): void;
81+
prepareAppResources(projectData: IProjectData): void;
8282

8383
/**
8484
* Defines if current platform is prepared (i.e. if <project dir>/platforms/<platform> dir exists).

test/stubs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ export class PlatformProjectServiceStub extends EventEmitter implements IPlatfor
425425
async updatePlatform(currentVersion: string, newVersion: string, canUpdate: boolean): Promise<boolean> {
426426
return Promise.resolve(true);
427427
}
428-
prepareAppResources(appResourcesDirectoryPath: string): void { }
428+
prepareAppResources(projectData: IProjectData): void { }
429429

430430
async preparePluginNativeCode(pluginData: IPluginData): Promise<void> {
431431
return Promise.resolve();

0 commit comments

Comments
 (0)