Skip to content

Commit 6205faf

Browse files
author
Alexey
committed
Merge pull request #41 from rnpm/fix/handle-missing-list
Handle missing Info.plist better
2 parents 81bc574 + a95180f commit 6205faf

File tree

9 files changed

+99
-46
lines changed

9 files changed

+99
-46
lines changed

src/ios/copyAssets.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const log = require('npmlog');
55
const plistParser = require('plist');
66
const groupFilesByType = require('../groupFilesByType');
77
const createGroup = require('./createGroup');
8-
const getPlistPath = require('./getPlistPath');
8+
const getPlist = require('./getPlist');
99

1010
/**
1111
* This function works in a similar manner to its Android version,
@@ -14,12 +14,12 @@ const getPlistPath = require('./getPlistPath');
1414
module.exports = function linkAssetsIOS(files, projectConfig) {
1515
const project = xcode.project(projectConfig.pbxprojPath).parseSync();
1616
const assets = groupFilesByType(files);
17-
const plistPath = path.join(projectConfig.sourceDir, getPlistPath(project));
17+
const plist = getPlist(project, projectConfig.sourceDir);
1818

19-
if (!fs.existsSync(plistPath)) {
19+
if (!plist) {
2020
return log.error(
2121
'ERRPLIST',
22-
`Could not locate Info.plist file at ${plistPath}. Check if your project has Info.plist set properly`
22+
`Could not locate Info.plist. Check if your project has 'INFOPLIST_FILE' set properly`
2323
);
2424
}
2525

@@ -32,10 +32,6 @@ module.exports = function linkAssetsIOS(files, projectConfig) {
3232
);
3333
}
3434

35-
const plist = plistParser.parse(
36-
fs.readFileSync(plistPath, 'utf-8')
37-
);
38-
3935
const fonts = (assets.font || [])
4036
.map(asset =>
4137
project.addResourceFile(

src/ios/getBuildProperty.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Gets build property from the main target build section
3+
*
4+
* It differs from the project.getBuildProperty exposed by xcode in the way that:
5+
* - it only checks for build property in the main target `Debug` section
6+
* - `xcode` library iterates over all build sections and because it misses
7+
* an early return when property is found, it will return undefined/wrong value
8+
* when there's another build section typically after the one you want to access
9+
* without the property defined (e.g. CocoaPods sections appended to project
10+
* miss INFOPLIST_FILE), see: https://github.com/alunny/node-xcode/blob/master/lib/pbxProject.js#L1765
11+
*/
12+
module.exports = function getBuildProperty(project, prop) {
13+
const target = project.getFirstTarget().firstTarget;
14+
const config = project.pbxXCConfigurationList()[target.buildConfigurationList];
15+
const buildSection = project.pbxXCBuildConfigurationSection()[config.buildConfigurations[0].value];
16+
17+
return buildSection.buildSettings[prop];
18+
};

src/ios/getPlist.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const getBuildProperty = require('./getBuildProperty');
2+
const plistParser = require('plist');
3+
const path = require('path');
4+
const fs = require('fs');
5+
6+
/**
7+
* Returns Info.plist located in the iOS project
8+
*
9+
* Returns `null` if INFOPLIST_FILE is not specified.
10+
*/
11+
module.exports = function getPlistPath(project, sourceDir) {
12+
const plistFile = getBuildProperty(project, 'INFOPLIST_FILE');
13+
14+
if (!plistFile) {
15+
return null;
16+
}
17+
18+
const plistPath = path.join(
19+
sourceDir,
20+
plistFile.replace(/"/g, '').replace('$(SRCROOT)', '')
21+
);
22+
23+
if (!fs.existsSync(plistPath)) {
24+
return null;
25+
}
26+
27+
return plistParser.parse(
28+
fs.readFileSync(plistPath, 'utf-8')
29+
);
30+
};

src/ios/getPlistPath.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/ios/unlinkAssets.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const log = require('npmlog');
55
const plistParser = require('plist');
66
const groupFilesByType = require('../groupFilesByType');
77
const createGroup = require('./createGroup');
8-
const getPlistPath = require('./getPlistPath');
8+
const getPlist = require('./getPlist');
99
const diff = require('lodash.difference');
1010

1111
/**
@@ -15,12 +15,12 @@ const diff = require('lodash.difference');
1515
module.exports = function unlinkAssetsIOS(files, projectConfig) {
1616
const project = xcode.project(projectConfig.pbxprojPath).parseSync();
1717
const assets = groupFilesByType(files);
18-
const plistPath = path.join(projectConfig.sourceDir, getPlistPath(project));
18+
const plist = getPlist(project, projectConfig.sourceDir);
1919

20-
if (!fs.existsSync(plistPath)) {
20+
if (!plist) {
2121
return log.error(
2222
'ERRPLIST',
23-
`Could not locate Info.plist file at ${plistPath}. Check if your project has Info.plist set properly`
23+
`Could not locate Info.plist file. Check if your project has 'INFOPLIST_FILE' set properly`
2424
);
2525
}
2626

@@ -31,10 +31,6 @@ module.exports = function unlinkAssetsIOS(files, projectConfig) {
3131
);
3232
}
3333

34-
const plist = plistParser.parse(
35-
fs.readFileSync(plistPath, 'utf-8')
36-
);
37-
3834
const fonts = (assets.font || [])
3935
.map(asset =>
4036
project.removeResourceFile(

src/link.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const promisify = (func) => () => new Promise((resolve, reject) =>
2121
*/
2222
const getProjectDependencies = () => {
2323
const pjson = require(path.join(process.cwd(), './package.json'));
24-
return Object.keys(pjson.dependencies).filter(name => name !== 'react-native');
24+
return Object.keys(pjson.dependencies || {}).filter(name => name !== 'react-native');
2525
};
2626

2727
const linkDependency = (project, dependency) => {

test/ios/getBuildProperty.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const chai = require('chai');
2+
const expect = chai.expect;
3+
const xcode = require('xcode');
4+
const getBuildProperty = require('../../src/ios/getBuildProperty');
5+
6+
const project = xcode.project('test/fixtures/project.pbxproj');
7+
8+
describe('ios::getBuildProperty', () => {
9+
10+
beforeEach(() => {
11+
project.parseSync();
12+
});
13+
14+
it('should return build property from main target', () => {
15+
const plistPath = getBuildProperty(project, 'INFOPLIST_FILE');
16+
expect(plistPath).to.equals('"Basic/Info.plist"');
17+
});
18+
19+
});

test/ios/getPlist.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const chai = require('chai');
2+
const expect = chai.expect;
3+
const xcode = require('xcode');
4+
const getPlist = require('../../src/ios/getPlist');
5+
6+
const project = xcode.project('test/fixtures/project.pbxproj');
7+
8+
describe('ios::getPlist', () => {
9+
10+
beforeEach(() => {
11+
project.parseSync();
12+
});
13+
14+
it('should return null when `.plist` file missing', () => {
15+
const plistPath = getPlist(project, process.cwd());
16+
expect(plistPath).to.equals(null);
17+
});
18+
19+
it.skip('should return parsed `plist`', () => {
20+
// @todo mock fs here
21+
});
22+
23+
});

test/ios/getPlistPath.spec.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy