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

refactor(create): cleanup copyTemplateFiles #1195

Merged
merged 4 commits into from
Nov 15, 2021
Merged
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
127 changes: 61 additions & 66 deletions bin/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ function copyJsAndCordovaLib (projectPath, projectName, use_shared) {
// like it should).
fs.symlinkSync(cordovaLibPathSrc, cordovaLibPathDest);
} else {
fs.copySync(path.join(projectAppPath, '.gitignore'), path.join(projectPath, '.gitignore'));

for (const p of ['include', 'Classes', 'VERSION', 'cordova.js', 'CordovaLib.xcodeproj/project.pbxproj']) {
fs.copySync(path.join(cordovaLibPathSrc, p), path.join(cordovaLibPathDest, p));
}
Expand All @@ -57,7 +55,7 @@ function copyJsAndCordovaLib (projectPath, projectName, use_shared) {
updateCordovaSubproject(projectXcodeProjPath, cordovaLibXcodePath);
}

function copyScripts (projectPath, projectName) {
function copyScripts (projectPath) {
const srcScriptsDir = path.join(ROOT, 'bin', 'templates', 'scripts', 'cordova');
const destScriptsDir = path.join(projectPath, 'cordova');

Expand All @@ -69,68 +67,72 @@ function copyScripts (projectPath, projectName) {

const nodeModulesDir = path.join(ROOT, 'node_modules');
if (fs.existsSync(nodeModulesDir)) fs.copySync(nodeModulesDir, path.join(destScriptsDir, 'node_modules'));

// CB-11792 do a token replace for __PROJECT_NAME__ in .xcconfig
const project_name_esc = projectName.replace(/&/g, '\\&');
utils.replaceFileContents(path.join(destScriptsDir, 'build-debug.xcconfig'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(destScriptsDir, 'build-release.xcconfig'), /__PROJECT_NAME__/g, project_name_esc);
}

/*
* Copy project template files into cordova project.
*
* @param {String} project_path path to cordova project
* @param {String} project_name name of cordova project
* @param {String} project_template_dir path to cordova-ios template directory
* @parm {BOOL} use_cli true if cli project
*/
function copyTemplateFiles (project_path, project_name, project_template_dir, package_name) {
const r = path.join(project_path, project_name);
function copyTemplateFiles (project_template_dir, project_path) {
fs.copySync(project_template_dir, project_path);

fs.removeSync(path.join(`${r}.xcodeproj`));
fs.copySync(path.join(project_template_dir, '__TEMP__.xcodeproj'), path.join(`${project_path}/__TEMP__.xcodeproj`));
fs.moveSync(path.join(project_path, '__TEMP__.xcodeproj'), path.join(`${r}.xcodeproj`));
// TODO: why two .gitignores?
const r = path.join(project_path, '__PROJECT_NAME__');
fs.moveSync(path.join(r, 'gitignore'), path.join(r, '.gitignore'));
fs.copySync(path.join(r, '.gitignore'), path.join(project_path, '.gitignore'));
}

fs.removeSync(path.join(project_path, `${project_name}.xcworkspace`));
fs.copySync(path.join(project_template_dir, '__TEMP__.xcworkspace'), path.join(`${project_path}/__TEMP__.xcworkspace`));
fs.moveSync(path.join(project_path, '__TEMP__.xcworkspace'), path.join(`${r}.xcworkspace`));
fs.moveSync(path.join(`${r}.xcworkspace`, 'xcshareddata', 'xcschemes', '__PROJECT_NAME__.xcscheme'), path.join(`${r}.xcworkspace`, 'xcshareddata', 'xcschemes', `${project_name}.xcscheme`));
function expandTokens (project_path, project_name, package_name) {
expandTokensInFileContents(project_path, project_name, package_name);
expandTokensInFileNames(project_path, project_name);
}

fs.removeSync(r);
fs.copySync(path.join(project_template_dir, '__PROJECT_NAME__'), path.join(`${project_path}/__PROJECT_NAME__`));
fs.moveSync(path.join(project_path, '__PROJECT_NAME__'), r);
function expandTokensInFileContents (project_path, project_name, package_name) {
// Expand __PROJECT_ID__ token in file contents
utils.replaceFileContents(path.join(project_path, '__PROJECT_NAME__.xcodeproj/project.pbxproj'), /__PROJECT_ID__/g, package_name);

// Expand __PROJECT_NAME__ token in file contents
for (const p of [
'cordova/build-debug.xcconfig',
'cordova/build-release.xcconfig',
'__PROJECT_NAME__.xcworkspace/contents.xcworkspacedata',
'__PROJECT_NAME__.xcworkspace/xcshareddata/xcschemes/__PROJECT_NAME__.xcscheme',
'__PROJECT_NAME__.xcodeproj/project.pbxproj',
'__PROJECT_NAME__/Classes/AppDelegate.h',
'__PROJECT_NAME__/Classes/AppDelegate.m',
'__PROJECT_NAME__/Classes/MainViewController.h',
'__PROJECT_NAME__/Classes/MainViewController.m',
'__PROJECT_NAME__/main.m',
'__PROJECT_NAME__/__PROJECT_NAME__-Info.plist',
'__PROJECT_NAME__/__PROJECT_NAME__-Prefix.pch'
]) {
expandProjectNameInFileContents(path.join(project_path, p), project_name);
}
}

fs.moveSync(path.join(r, '__PROJECT_NAME__-Info.plist'), path.join(r, `${project_name}-Info.plist`));
fs.moveSync(path.join(r, '__PROJECT_NAME__-Prefix.pch'), path.join(r, `${project_name}-Prefix.pch`));
fs.moveSync(path.join(r, 'gitignore'), path.join(r, '.gitignore'));
function expandTokensInFileNames (project_path, project_name) {
// Expand __PROJECT_NAME__ token in file & folder names
for (const p of [
'__PROJECT_NAME__.xcworkspace/xcshareddata/xcschemes/__PROJECT_NAME__.xcscheme',
'__PROJECT_NAME__.xcworkspace',
'__PROJECT_NAME__.xcodeproj',
'__PROJECT_NAME__/__PROJECT_NAME__-Info.plist',
'__PROJECT_NAME__/__PROJECT_NAME__-Prefix.pch',
'__PROJECT_NAME__'
]) {
expandProjectNameInBaseName(path.join(project_path, p), project_name);
}
}

/* replace __PROJECT_NAME__ and __PROJECT_ID__ with ACTIVITY and ID strings, respectively, in:
*
* - ./__PROJECT_NAME__.xcodeproj/project.pbxproj
* - ./__PROJECT_NAME__/Classes/AppDelegate.h
* - ./__PROJECT_NAME__/Classes/AppDelegate.m
* - ./__PROJECT_NAME__/Classes/MainViewController.h
* - ./__PROJECT_NAME__/Classes/MainViewController.m
* - ./__PROJECT_NAME__/Resources/main.m
* - ./__PROJECT_NAME__/Resources/__PROJECT_NAME__-info.plist
* - ./__PROJECT_NAME__/Resources/__PROJECT_NAME__-Prefix.plist
*/
function expandProjectNameInBaseName (f, projectName) {
const { dir, base } = path.parse(f);
const newBase = base.replace('__PROJECT_NAME__', projectName);
return fs.moveSync(f, path.join(dir, newBase));
}

function expandProjectNameInFileContents (f, projectName) {
// https://issues.apache.org/jira/browse/CB-12402 - Encode XML characters properly
const project_name_xml_esc = xmlescape(project_name);
utils.replaceFileContents(path.join(`${r}.xcworkspace`, 'contents.xcworkspacedata'), /__PROJECT_NAME__/g, project_name_xml_esc);
utils.replaceFileContents(path.join(`${r}.xcworkspace`, 'xcshareddata', 'xcschemes', `${project_name}.xcscheme`), /__PROJECT_NAME__/g, project_name_xml_esc);

const project_name_esc = project_name.replace(/&/g, '\\&');
utils.replaceFileContents(path.join(`${r}.xcodeproj`, 'project.pbxproj'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(`${r}.xcodeproj`, 'project.pbxproj'), /__PROJECT_ID__/g, package_name);
utils.replaceFileContents(path.join(r, 'Classes', 'AppDelegate.h'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(r, 'Classes', 'AppDelegate.m'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(r, 'Classes', 'MainViewController.h'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(r, 'Classes', 'MainViewController.m'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(r, 'main.m'), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(r, `${project_name}-Info.plist`), /__PROJECT_NAME__/g, project_name_esc);
utils.replaceFileContents(path.join(r, `${project_name}-Prefix.pch`), /__PROJECT_NAME__/g, project_name_esc);
const xmlExtensions = new Set(['.xcworkspacedata', '.xcscheme']);
const escape = xmlExtensions.has(path.extname(f))
? xmlescape
: s => s.replace(/&/g, '\\&');
utils.replaceFileContents(f, /__PROJECT_NAME__/g, escape(projectName));
}

/*
Expand Down Expand Up @@ -169,20 +171,13 @@ exports.createProject = (project_path, package_name, project_name, opts, config)

events.emit('verbose', `Copying iOS template project to ${project_path}`);

// create the project directory and copy over files
fs.ensureDirSync(project_path);
fs.copySync(path.join(project_template_dir, 'www'), path.join(project_path, 'www'));
copyTemplateFiles(project_template_dir, project_path);

// Copy project template files
copyTemplateFiles(project_path, project_name, project_template_dir, package_name);
copyScripts(project_path);

// Copy xcconfig files
fs.copySync(path.join(project_template_dir, 'pods-debug.xcconfig'), path.join(project_path, 'pods-debug.xcconfig'));
fs.copySync(path.join(project_template_dir, 'pods-release.xcconfig'), path.join(project_path, 'pods-release.xcconfig'));
expandTokens(project_path, project_name, package_name);

// CordovaLib stuff
copyJsAndCordovaLib(project_path, project_name, use_shared);
copyScripts(project_path, project_name);

events.emit('log', generateDoneMessage('create', use_shared));
return Promise.resolve();
Expand Down