From 5b41cdb66717518258dd6b0e20653880d334d03e Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Fri, 31 Mar 2017 02:58:02 -0700 Subject: [PATCH] match plist indentation with xcode format Summary: xcode has its own way of indentation of the plist. fixes #11668 Closes https://github.com/facebook/react-native/pull/11670 Differential Revision: D4410865 fbshipit-source-id: 8c65e7719d228b07f58b1ccb86b369e319067f02 --- link/__fixtures__/Info.plist | 12 +++++++ link/__tests__/ios/writePlist.spec.js | 52 +++++++++++++++++++++++++++ link/ios/copyAssets.js | 8 ++--- link/ios/unlinkAssets.js | 8 ++--- link/ios/writePlist.js | 24 +++++++++++++ 5 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 link/__fixtures__/Info.plist create mode 100644 link/__tests__/ios/writePlist.spec.js create mode 100644 link/ios/writePlist.js diff --git a/link/__fixtures__/Info.plist b/link/__fixtures__/Info.plist new file mode 100644 index 000000000..b2e7b96ea --- /dev/null +++ b/link/__fixtures__/Info.plist @@ -0,0 +1,12 @@ + + + + + CFBundleDevelopmentRegion + en + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + + diff --git a/link/__tests__/ios/writePlist.spec.js b/link/__tests__/ios/writePlist.spec.js new file mode 100644 index 000000000..a109d56d6 --- /dev/null +++ b/link/__tests__/ios/writePlist.spec.js @@ -0,0 +1,52 @@ +'use strict'; + +jest.autoMockOff(); +jest.mock('fs'); + +let plistPath = null; +jest.mock('../../ios/getPlistPath', () => () => plistPath); + +const { readFileSync } = require.requireActual('fs') +const fs = require('fs'); + +const xcode = require('xcode'); +const path = require('path'); +const writePlist = require('../../ios/writePlist'); + +const projectPath = path.join(__dirname, '../../__fixtures__/project.pbxproj'); +const infoPlistPath = path.join(__dirname, '../../__fixtures__/Info.plist'); + +fs.__setMockFilesystem({ + 'Basic': { + 'project.pbxproj': readFileSync(projectPath).toString(), + } +}); + +const project = xcode.project('/Basic/project.pbxproj'); + +const plist = { + CFBundleDevelopmentRegion: 'en', + UISupportedInterfaceOrientations: [ + 'UIInterfaceOrientationPortrait' + ] +}; + +describe('ios::writePlist', () => { + beforeEach(() => { + project.parseSync(); + fs.writeFileSync.mockReset(); + }); + + it('should write a `.plist` file', () => { + plistPath = '/Basic/Info.plist'; + const result = writePlist(project, '/', plist); + const infoPlist = readFileSync(infoPlistPath).toString(); + expect(fs.writeFileSync).toHaveBeenCalledWith(plistPath, infoPlist); + }); + + it('when plistPath is null it should return null', () => { + plistPath = null; + expect(writePlist(project, '/', plist)).toBeNull(); + expect(fs.writeFileSync).not.toHaveBeenCalled(); + }); +}); diff --git a/link/ios/copyAssets.js b/link/ios/copyAssets.js index d2865cbd1..cc271991f 100644 --- a/link/ios/copyAssets.js +++ b/link/ios/copyAssets.js @@ -2,11 +2,10 @@ const fs = require('fs-extra'); const path = require('path'); const xcode = require('xcode'); const log = require('npmlog'); -const plistParser = require('plist'); const groupFilesByType = require('../groupFilesByType'); const createGroupWithMessage = require('./createGroupWithMessage'); const getPlist = require('./getPlist'); -const getPlistPath = require('./getPlistPath'); +const writePlist = require('./writePlist'); /** * This function works in a similar manner to its Android version, @@ -38,8 +37,5 @@ module.exports = function linkAssetsIOS(files, projectConfig) { project.writeSync() ); - fs.writeFileSync( - getPlistPath(project, projectConfig.sourceDir), - plistParser.build(plist) - ); + writePlist(project, projectConfig.sourceDir, plist); }; diff --git a/link/ios/unlinkAssets.js b/link/ios/unlinkAssets.js index 2acad3e21..ab31a1b9c 100644 --- a/link/ios/unlinkAssets.js +++ b/link/ios/unlinkAssets.js @@ -2,10 +2,9 @@ const fs = require('fs-extra'); const path = require('path'); const xcode = require('xcode'); const log = require('npmlog'); -const plistParser = require('plist'); const groupFilesByType = require('../groupFilesByType'); const getPlist = require('./getPlist'); -const getPlistPath = require('./getPlistPath'); +const writePlist = require('./writePlist'); const difference = require('lodash').difference; /** @@ -47,8 +46,5 @@ module.exports = function unlinkAssetsIOS(files, projectConfig) { project.writeSync() ); - fs.writeFileSync( - getPlistPath(project, projectConfig.sourceDir), - plistParser.build(plist) - ); + writePlist(project, projectConfig.sourceDir, plist); }; diff --git a/link/ios/writePlist.js b/link/ios/writePlist.js new file mode 100644 index 000000000..e8fe6715a --- /dev/null +++ b/link/ios/writePlist.js @@ -0,0 +1,24 @@ +const plistParser = require('plist'); +const getPlistPath = require('./getPlistPath'); +const fs = require('fs'); + +/** + * Writes to Info.plist located in the iOS project + * + * Returns `null` if INFOPLIST_FILE is not specified or file is non-existent. + */ +module.exports = function writePlist(project, sourceDir, plist) { + const plistPath = getPlistPath(project, sourceDir); + + if (!plistPath) { + return null; + } + + // We start with an offset of -1, because Xcode maintains a custom + // indentation of the plist. + // Ref: https://github.com/facebook/react-native/issues/11668 + return fs.writeFileSync( + plistPath, + plistParser.build(plist, { indent: '\t', offset: -1 }) + '\n' + ); +};