Skip to content

Commit

Permalink
match plist indentation with xcode format
Browse files Browse the repository at this point in the history
Summary:
xcode has its own way of indentation of the plist.

fixes #11668
Closes facebook/react-native#11670

Differential Revision: D4410865

fbshipit-source-id: 8c65e7719d228b07f58b1ccb86b369e319067f02
  • Loading branch information
koenpunt authored and facebook-github-bot committed Mar 31, 2017
1 parent 69bd272 commit 5b41cdb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 12 deletions.
12 changes: 12 additions & 0 deletions link/__fixtures__/Info.plist
Original file line number Diff line number Diff line change
@@ -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>
52 changes: 52 additions & 0 deletions link/__tests__/ios/writePlist.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
8 changes: 2 additions & 6 deletions link/ios/copyAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
8 changes: 2 additions & 6 deletions link/ios/unlinkAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
};
24 changes: 24 additions & 0 deletions link/ios/writePlist.js
Original file line number Diff line number Diff line change
@@ -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'
);
};

0 comments on commit 5b41cdb

Please sign in to comment.