Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support finding an unprefixed Info.plist file #212

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/ConfigChanges/ConfigChanges.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ describe('config-changes module', function () {

const munger = new configChanges.PlatformMunger('ios', temp, platformJson, pluginInfoProvider);
munger.process(plugins_dir);
expect(spy).toHaveBeenCalledWith(path.join(temp, 'SampleApp', 'SampleApp-Info.plist'), 'utf8');
expect(spy).toHaveBeenCalledWith(path.join(temp, 'SampleApp', 'SampleApp-Info.plist'));
});

it('Test 026 : should move successfully installed plugins from queue to installed plugins section, and include/retain vars if applicable', function () {
Expand Down
22 changes: 21 additions & 1 deletion spec/ConfigChanges/ConfigFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ describe('ConfigFile tests', function () {
expect(ConfigFile.resolveConfigFilePath('project_dir', 'android', 'strings.xml')).toBe(stringsPath);
});

it('should return android values xml file path', function () {
const resPath = path.join(projectDir, 'res', 'values', 'colors.xml');
expect(ConfigFile.resolveConfigFilePath('project_dir', 'android', path.join('res', 'values', 'colors.xml'))).toBe(resPath);
});

it('should return ios config.xml file path', function () {
spyOn(ConfigFile, 'getIOSProjectname').and.returnValue('iospath');
const configPath = path.join('project_dir', 'iospath', 'config.xml');
Expand Down Expand Up @@ -106,7 +111,7 @@ describe('ConfigFile tests', function () {
const projName = 'XXX';
const expectedPlistPath = `${projName}${path.sep}${projName}-Info.plist`;

ConfigFile.__set__('getIOSProjectname', () => projName);
spyOn(ConfigFile, 'getIOSProjectname').and.returnValue(projName);
spyOn(require('fast-glob'), 'sync').and.returnValue([
`AAA/${projName}-Info.plist`,
`Pods/Target Support Files/Pods-${projName}/Info.plist`,
Expand All @@ -116,6 +121,21 @@ describe('ConfigFile tests', function () {

expect(ConfigFile.resolveConfigFilePath('', 'ios', '*-Info.plist')).toBe(expectedPlistPath);
});

it('should return Info.plist file', function () {
const projName = 'XXX';
const expectedPlistPath = path.join(projName, 'Info.plist');

spyOn(ConfigFile, 'getIOSProjectname').and.returnValue(projName);
spyOn(require('fast-glob'), 'sync').and.returnValue([
'AAA/Info.plist',
`Pods/Target Support Files/Pods-${projName}/Info.plist`,
`Pods/Target Support Files/Pods-${projName}/Pods-${projName}-Info.plist`,
expectedPlistPath
]);

expect(ConfigFile.resolveConfigFilePath('', 'ios', '*-Info.plist')).toBe(expectedPlistPath);
});
});
});
});
89 changes: 56 additions & 33 deletions src/ConfigChanges/ConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

const fs = require('node:fs');
const path = require('node:path');
const util = require('node:util');
const readChunk = require('read-chunk');

// Use delay loading to ensure plist and other node modules to not get loaded
Expand All @@ -29,6 +30,9 @@ const modules = {
get xml_helpers () { return require('../util/xml-helpers'); }
};

// Cache of project folder paths to Xcode project names
const xcodeprojMap = new Map();

/******************************************************************************
* ConfigFile class
*
Expand Down Expand Up @@ -73,13 +77,13 @@ class ConfigFile {
} else {
// plist file
this.type = 'plist';
// TODO: isBinaryPlist() reads the file and then parse re-reads it again.
// We always write out text plist, not binary.
// Do we still need to support binary plist?
// If yes, use plist.parseStringSync() and read the file once.
this.data = isBinaryPlist(filepath)
? modules.bplist.parseBuffer(fs.readFileSync(filepath))[0]
: modules.plist.parse(fs.readFileSync(filepath, 'utf8'));

const fileData = fs.readFileSync(filepath);
if (fileData.toString('utf8', 0, 6) === 'bplist') {
this.data = modules.bplist.parseBuffer(fileData)[0];
} else {
this.data = modules.plist.parse(fileData.toString('utf8'));
}
}
}

Expand Down Expand Up @@ -154,11 +158,34 @@ class ConfigFile {

this.is_changed = true;
}

// Find out the real name of an iOS project
//
// This caches the project name for a given directory path, assuming that
// it won't change over the course of a single Cordova command invocation
static getIOSProjectname (project_dir) {
if (xcodeprojMap.has(project_dir)) {
return xcodeprojMap.get(project_dir);
}

const matches = modules.glob.sync('*.xcodeproj', { cwd: project_dir, onlyDirectories: true });

if (matches.length !== 1) {
const msg = matches.length === 0
? 'Does not appear to be an xcode project, no xcode project file'
: 'There are multiple *.xcodeproj dirs';

throw new Error(`${msg} in ${project_dir}`);
}

const projectName = path.basename(matches[0], '.xcodeproj');
xcodeprojMap.set(project_dir, projectName);
return projectName;
}
}

// Some config-file target attributes are not qualified with a full leading directory, or contain wildcards.
// Resolve to a real path in this function.
// TODO: getIOSProjectname is slow because of glob, try to avoid calling it several times per project.
function resolveConfigFilePath (project_dir, platform, file) {
let filepath = path.join(project_dir, file);
let matches;
Expand All @@ -167,20 +194,32 @@ function resolveConfigFilePath (project_dir, platform, file) {

if (file.includes('*')) {
// handle wildcards in targets using glob.
matches = modules.glob.sync(`**/${file}`, {
matches = modules.glob.sync(`**/${file.replace('*-Info.plist', '*Info.plist')}`, {
fs,
cwd: project_dir,
absolute: true
}).map(path.normalize);

if (matches.length) filepath = matches[0];
if (matches.length) {
filepath = matches[0];
}

// [CB-5989] multiple Info.plist files may exist. default to $PROJECT_NAME-Info.plist
// [CB-5989] multiple Info.plist files may exist. default to Info.plist then $PROJECT_NAME-Info.plist
if (matches.length > 1 && file.includes('-Info.plist')) {
dpogue marked this conversation as resolved.
Show resolved Hide resolved
const projName = getIOSProjectname(project_dir);
const projName = ConfigFile.getIOSProjectname(project_dir);

// Try to find a unprefix Info.plist file first
let plistPath = path.join(project_dir, projName, 'Info.plist');
if (matches.includes(plistPath)) {
return plistPath;
}

// Then try to find one prefixed with the project name
const plistName = `${projName}-Info.plist`;
const plistPath = path.join(project_dir, projName, plistName);
if (matches.includes(plistPath)) return plistPath;
plistPath = path.join(project_dir, projName, plistName);
if (matches.includes(plistPath)) {
return plistPath;
}
}
return filepath;
}
Expand Down Expand Up @@ -216,7 +255,7 @@ function resolveConfigFilePath (project_dir, platform, file) {
if (platform === 'ios' || platform === 'osx') {
filepath = path.join(
project_dir,
module.exports.getIOSProjectname(project_dir),
ConfigFile.getIOSProjectname(project_dir),
'config.xml'
);
} else {
Expand All @@ -235,29 +274,13 @@ function resolveConfigFilePath (project_dir, platform, file) {
return filepath;
}

// Find out the real name of an iOS or OSX project
// TODO: glob is slow, need a better way or caching, or avoid using more than once.
function getIOSProjectname (project_dir) {
const matches = modules.glob.sync('*.xcodeproj', { cwd: project_dir, onlyDirectories: true });

if (matches.length !== 1) {
const msg = matches.length === 0
? 'Does not appear to be an xcode project, no xcode project file'
: 'There are multiple *.xcodeproj dirs';

throw new Error(`${msg} in ${project_dir}`);
}

return path.basename(matches[0], '.xcodeproj');
}

// determine if a plist file is binary
// i.e. they start with the magic header "bplist"
// TODO: Remove in next major
function isBinaryPlist (filename) {
return readChunk.sync(filename, 0, 6).toString() === 'bplist';
}

module.exports = ConfigFile;
module.exports.isBinaryPlist = isBinaryPlist;
module.exports.getIOSProjectname = getIOSProjectname;
module.exports.isBinaryPlist = util.deprecate(isBinaryPlist, 'isBinaryPlist is deprecated');
module.exports.resolveConfigFilePath = resolveConfigFilePath;