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
This commit is contained in:
Koen Punt 2017-03-31 02:58:02 -07:00 committed by Facebook Github Bot
parent 01a0e10026
commit 8daf985fc3
5 changed files with 92 additions and 12 deletions

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
</dict>
</plist>

View File

@ -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();
});
});

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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'
);
};