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

Commit 5488b7a

Browse files
authored
Merge pull request #220 from NativeScript/lini/fix-custom-resources-location
fix: error when installing plugin with custom project configuration i…
2 parents 6e711cf + 402ef67 commit 5488b7a

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/hooks/before-prepare.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function ($logger, $projectData) {
99
utils.setLogger(_log);
1010

1111
if (utils.targetsAndroid($projectData.projectDir)) {
12-
utils.addIfNecessary($projectData.platformsDir);
12+
utils.addIfNecessary($projectData.platformsDir, $projectData.appResourcesDirectoryPath);
1313
}
1414

1515
function _log (str) {

src/hooks/utils.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ function checkForGoogleServicesJson(projectDir, resourcesDir) {
2727
}
2828
}
2929

30-
function addOnPluginInstall(platformsDir) {
30+
function addOnPluginInstall(platformsDir, resourcesDir) {
3131
if (buildGradleExists(platformsDir)) {
32-
addIfNecessary(platformsDir);
32+
addIfNecessary(platformsDir, resourcesDir);
3333
}
3434
}
3535

36-
function addIfNecessary(platformsDir) {
36+
function addIfNecessary(platformsDir, resourcesDir) {
3737
_amendBuildGradle(platformsDir, function(pluginImported, pluginApplied, fileContents) {
3838
if (!pluginImported) {
3939
fileContents.projectFileContents = _addPluginImport(fileContents.projectFileContents);
@@ -49,7 +49,7 @@ function addIfNecessary(platformsDir) {
4949
return fileContents;
5050
});
5151

52-
_copyGoogleServices(platformsDir);
52+
_copyGoogleServices(resourcesDir, platformsDir);
5353
}
5454

5555
function removeIfPresent(platformsDir) {
@@ -80,8 +80,8 @@ var _versionRegExp = '[^\'"]+';
8080
var _pluginImportName = 'com.google.gms:google-services';
8181
var _pluginApplicationName = 'com.google.gms.google-services';
8282

83-
function _copyGoogleServices(platformsDir) {
84-
var srcServicesFile = path.join(platformsDir, '..', 'app', 'App_Resources', 'Android', 'google-services.json');
83+
function _copyGoogleServices(resourcesDir, platformsDir) {
84+
var srcServicesFile = path.join(resourcesDir, 'Android', 'google-services.json');
8585
var dstServicesFile = path.join(platformsDir, 'android', 'app', 'google-services.json');
8686
if (fs.existsSync(srcServicesFile) && !fs.existsSync(dstServicesFile) && fs.existsSync(path.join(platformsDir, 'android', 'app'))) {
8787
// try to copy google-services config file to platform app directory
@@ -130,15 +130,15 @@ function _removePluginApplication(buildGradleContents) {
130130
function _addPluginImport(buildGradleContents) {
131131
var androidGradle = 'com.android.tools.build:gradle';
132132
var insertBeforeDoubleQuotes = 'classpath "' + androidGradle;
133-
var insertBeforeSingleQoutes = 'classpath \'' + androidGradle;
133+
var insertBeforeSingleQuotes = 'classpath \'' + androidGradle;
134134
var quoteToInsert = '"'
135135
var matchedString = insertBeforeDoubleQuotes;
136136
var ind = buildGradleContents.indexOf(insertBeforeDoubleQuotes);
137137

138138
if (ind === -1) {
139-
ind = buildGradleContents.indexOf(insertBeforeSingleQoutes);
139+
ind = buildGradleContents.indexOf(insertBeforeSingleQuotes);
140140
quoteToInsert = '\'';
141-
matchedString = insertBeforeSingleQoutes;
141+
matchedString = insertBeforeSingleQuotes;
142142
}
143143

144144
if (ind === -1) {

src/scripts/postinstall.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ var projDir = hook.findProjectDir();
66
hook.postinstall();
77

88
if (projDir) {
9-
utils.checkForGoogleServicesJson(projDir, path.join(projDir, 'app', 'App_Resources'));
10-
utils.addOnPluginInstall(path.join(projDir, 'platforms'));
9+
var resourcesDir;
10+
11+
try {
12+
var globalPath = require('child_process').execSync('npm root -g').toString().trim();
13+
var tns = require(path.join(globalPath, 'nativescript'));
14+
var project = tns.projectDataService.getProjectData(projDir);
15+
resourcesDir = project.appResourcesDirectoryPath;
16+
} catch (exc) {
17+
console.log('Push plugin cannot find project root. Project will be initialized during build.');
18+
}
19+
if (resourcesDir) {
20+
utils.checkForGoogleServicesJson(projDir, resourcesDir);
21+
utils.addOnPluginInstall(path.join(projDir, 'platforms'), resourcesDir);
22+
}
1123
}

0 commit comments

Comments
 (0)