UNPKG

nodekit-cli

Version:

A command-line tool for creating {NK} NodeKit applications

388 lines (332 loc) 15.7 kB
/** Licensed to OffGrid Networks (OGN) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. OGN licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var Q = require('q'); var fs = require('fs'); var path = require('path'); var shell = require('shelljs'); var xcode = require('xcode'); var unorm = require('unorm'); var plist = require('plist'); var URL = require('url'); var events = require('nodekit-cli')['nodekit-cli-common'].events; var xmlHelpers = require('nodekit-cli')['nodekit-cli-common'].xmlHelpers; var ConfigParser = require('nodekit-cli')['nodekit-cli-common'].ConfigParser; var NodeKitError = require('nodekit-cli')['nodekit-cli-common'].NodeKitError; var configMunger = require('./configMunger'); /*jshint sub:true*/ module.exports.prepare = function (nodekitProject) { var self = this; this._config = updateConfigFile(nodekitProject.projectConfig, configMunger.get(this.locations.root), this.locations); // Update own app dir with project's app assets and plugins' assets and js-files return Q.when(updateApp(nodekitProject, this.locations)) .then(function () { // update project according to nodekit.json changes. return updateProject(self._config, self.locations); }) .then(function () { handleIcons(nodekitProject.projectConfig, self.locations.xcodeNodeKitProj); }) .then(function () { self.events.emit('verbose', 'updated project successfully'); }); }; /** * Updates config files in project based on app's nodekit.json and config munge, * generated by plugins. * * @param {ConfigParser} sourceConfig A project's configuration that will * be merged into platform's nodekit.json * @param {ConfigChanges} configMunger An initialized ConfigChanges instance * for this platform. * @param {Object} locations A map of locations for this platform * * @return {ConfigParser} An instance of ConfigParser, that * represents current project's configuration. When returned, the * configuration is already dumped to appropriate nodekit.json file. */ function updateConfigFile(sourceConfig, configMunger, locations) { events.emit('verbose', 'Generating nodekit.json from defaults for platform "osx"'); // First cleanup current config and merge project's one into own // Overwrite platform nodekit.json with defaults_nodekit.json. shell.cp('-f', locations.defaultNodeKitJson, locations.nodekitJson); // Then apply config changes from global munge to all config files // in project (including project's config) configMunger.reapply_global_munge().save_all(); // Merge changes from app's nodekit.json into platform's one var config = new ConfigParser(locations.nodekitJson); xmlHelpers.mergeXml(sourceConfig.doc.getroot(), config.doc.getroot(), 'osx', /*clobber=*/true); config.write(); return config; } /** * Updates platform 'app' directory by replacing it with contents of * 'platform_app' and project app. Also copies project's overrides' folder into * the platform 'app' folder * * @param {Object} nodekitProject An object which describes nodekit project. * @param {Object} destinations An object that contains destination * paths for app files. */ function updateApp(nodekitProject, destinations) { shell.rm('-rf', destinations.app); shell.mkdir('-p', destinations.app); // Copy source files from project's app directory shell.cp('-rf', path.join(nodekitProject.locations.app, 'dist', '*'), destinations.app); // Override app sources by files in 'platform_app' directory // NodeKit removed platform_app // If project contains 'merges' for our platform, use them as another overrides var merges_path = path.join(nodekitProject.root, 'merges', 'osx'); if (fs.existsSync(merges_path)) { events.emit('verbose', 'Found "merges" for osx platform. Copying over existing "app" files.'); var overrides = path.join(merges_path, '*'); shell.cp('-rf', overrides, destinations.app); } } /** * Updates project structure and AndroidManifest according to project's configuration. * * @param {ConfigParser} platformConfig A project's configuration that will * be used to update project * @param {Object} locations A map of locations for this platform (In/Out) */ function updateProject(platformConfig, locations) { // CB-6992 it is necessary to normalize characters // because node and shell scripts handles unicode symbols differently // We need to normalize the name to NFD form since OSX uses NFD unicode form var name = unorm.nfd(platformConfig.name()); var pkg = platformConfig.ios_CFBundleIdentifier() || platformConfig.packageName(); var version = platformConfig.version(); var originalName = path.basename(locations.xcodeNodeKitProj); // Update package id (bundle id) var plistFile = path.join(locations.xcodeNodeKitProj, originalName + '-Info.plist'); var infoPlist = plist.parse(fs.readFileSync(plistFile, 'utf8')); infoPlist['CFBundleIdentifier'] = pkg; // Update version (bundle version) infoPlist['CFBundleShortVersionString'] = version; var CFBundleVersion = platformConfig.ios_CFBundleVersion() || default_CFBundleVersion(version); infoPlist['CFBundleVersion'] = CFBundleVersion; // Update Author if present var author = platformConfig.author(); var copyRight = infoPlist['NSHumanReadableCopyright']; if (copyRight && author) { infoPlist['NSHumanReadableCopyright'] = copyRight.replace('--AUTHOR--', author); } // replace Info.plist ATS entries according to <access> and <allow-navigation> nodekit.json entries var ats = writeATSEntries(platformConfig); if (Object.keys(ats).length > 0) { infoPlist['NSAppTransportSecurity'] = ats; } else { delete infoPlist['NSAppTransportSecurity']; } var info_contents = plist.build(infoPlist); info_contents = info_contents.replace(/<string>[\s\r\n]*<\/string>/g,'<string></string>'); fs.writeFileSync(plistFile, info_contents, 'utf-8'); events.emit('verbose', 'Wrote out OSX Bundle Identifier to "' + pkg + '"'); events.emit('verbose', 'Wrote out OSX Bundle Version to "' + version + '"'); return handleBuildSettings(platformConfig, locations).then(function() { if (name == originalName) { events.emit('verbose', 'OSX Product Name has not changed (still "' + originalName + '")'); return Q(); } // Update product name inside pbxproj file var proj = new xcode.project(locations.pbxproj); try { proj.parseSync(); } catch (err) { return Q.reject(new NodeKitError('An error occurred during parsing of project.pbxproj. Start weeping. Output: ' + err)); } proj.updateProductName(name); fs.writeFileSync(locations.pbxproj, proj.writeSync(), 'utf-8'); // Move the xcodeproj and other name-based dirs over. shell.mv(path.join(locations.xcodeNodeKitProj, originalName + '-Info.plist'), path.join(locations.xcodeNodeKitProj, name + '-Info.plist')); shell.mv(path.join(locations.xcodeNodeKitProj, originalName + '-Prefix.pch'), path.join(locations.xcodeNodeKitProj, name + '-Prefix.pch')); // CB-8914 remove userdata otherwise project is un-usable in xcode shell.rm('-rf',path.join(locations.xcodeProjDir,'xcuserdata/')); shell.mv(locations.xcodeProjDir, path.join(locations.root, name + '.xcodeproj')); shell.mv(locations.xcodeNodeKitProj, path.join(locations.root, name)); // Update locations with new paths locations.xcodeNodeKitProj = path.join(locations.root, name); locations.xcodeProjDir = path.join(locations.root, name + '.xcodeproj'); locations.pbxproj = path.join(locations.xcodeProjDir, 'project.pbxproj'); // Hack this shi*t var pbx_contents = fs.readFileSync(locations.pbxproj, 'utf-8'); pbx_contents = pbx_contents.split(originalName).join(name); fs.writeFileSync(locations.pbxproj, pbx_contents, 'utf-8'); events.emit('verbose', 'Wrote out OSX Product Name and updated XCode project file names from "'+originalName+'" to "' + name + '".'); // in case of updated paths we return them back to return Q(); }); } function handleBuildSettings(platformConfig, locations) { // nothing to do return Q(); } function handleIcons(projectConfig, platformRoot) { // Update icons var icons = projectConfig.getIcons('osx'); var appRoot = path.dirname(projectConfig.path); // See https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/OSXHIGuidelines/Designing.html // for application images sizes reference. var platformIcons = [ {dest: 'icon-512x512.png', width: 512, height: 512}, {dest: 'icon-256x256.png', width: 256, height: 256}, {dest: 'icon-128x128.png', width: 128, height: 128}, {dest: 'icon-64x64.png', width: 64, height: 64}, {dest: 'icon-32x32.png', width: 32, height: 32}, {dest: 'icon-16x16.png', width: 16, height: 16} ]; platformIcons.forEach(function (item) { var icon = icons.getBySize(item.width, item.height) || icons.getDefault(); if (icon){ var src = path.join(appRoot, icon.src); var dst = path.join(platformRoot, 'Images.xcassets/AppIcon.appiconset/', item.dest); events.emit('verbose', 'Copying icon from ' + src + ' to ' + dst); shell.cp('-f', src, dst); } }); } /* Parses all <access> and <allow-navigation> entries and consolidates duplicates (for ATS). Returns an object with a Hostname as the key, and the value an object with properties: { Hostname, // String NSExceptionAllowsInsecureHTTPLoads, // boolean NSIncludesSubdomains, // boolean NSExceptionMinimumTLSVersion, // String NSExceptionRequiresForwardSecrecy // boolean } */ function processAccessAndAllowNavigationEntries(config) { var accesses = config.getAccesses(); var allow_navigations = config.getAllowNavigations(); return allow_navigations // we concat allow_navigations and accesses, after processing accesses .concat(accesses.map(function(obj) { // map accesses to a common key interface using 'href', not origin obj.href = obj.origin; delete obj.origin; return obj; })) // we reduce the array to an object with all the entries processed (key is Hostname) .reduce(function(previousReturn, currentElement) { var obj = parseWhitelistUrlForATS(currentElement.href, currentElement.minimum_tls_version, currentElement.requires_forward_secrecy); if (obj) { // we 'union' duplicate entries var item = previousReturn[obj.Hostname]; if (!item) { item = {}; } for(var o in obj) { if (obj.hasOwnProperty(o)) { item[o] = obj[o]; } } previousReturn[obj.Hostname] = item; } return previousReturn; }, {}); } /* Parses a URL and returns an object with these keys: { Hostname, // String NSExceptionAllowsInsecureHTTPLoads, // boolean (default: false) NSIncludesSubdomains, // boolean (default: false) NSExceptionMinimumTLSVersion, // String (default: 'TLSv1.2') NSExceptionRequiresForwardSecrecy // boolean (default: true) } null is returned if the URL cannot be parsed, or is to be skipped for ATS. */ function parseWhitelistUrlForATS(url, minimum_tls_version, requires_forward_secrecy) { var href = URL.parse(url); var retObj = {}; retObj.Hostname = href.hostname; if (url === '*') { return { Hostname : '*' }; } // Guiding principle: we only set values in retObj if they are NOT the default if (!retObj.Hostname) { // check origin, if it allows subdomains (wildcard in hostname), we set NSIncludesSubdomains to YES. Default is NO var subdomain1 = '/*.'; // wildcard in hostname var subdomain2 = '*://*.'; // wildcard in hostname and protocol var subdomain3 = '*://'; // wildcard in protocol only if (href.pathname.indexOf(subdomain1) === 0) { retObj.NSIncludesSubdomains = true; retObj.Hostname = href.pathname.substring(subdomain1.length); } else if (href.pathname.indexOf(subdomain2) === 0) { retObj.NSIncludesSubdomains = true; retObj.Hostname = href.pathname.substring(subdomain2.length); } else if (href.pathname.indexOf(subdomain3) === 0) { retObj.Hostname = href.pathname.substring(subdomain3.length); } else { // Handling "scheme:*" case to avoid creating of a blank key in NSExceptionDomains. return null; } } if (minimum_tls_version && minimum_tls_version !== 'TLSv1.2') { // default is TLSv1.2 retObj.NSExceptionMinimumTLSVersion = minimum_tls_version; } var rfs = (requires_forward_secrecy === 'true'); if (requires_forward_secrecy && !rfs) { // default is true retObj.NSExceptionRequiresForwardSecrecy = false; } // if the scheme is HTTP, we set NSExceptionAllowsInsecureHTTPLoads to YES. Default is NO if (href.protocol === 'http:') { retObj.NSExceptionAllowsInsecureHTTPLoads = true; } else if (!href.protocol && href.pathname.indexOf('*:/') === 0) { // wilcard in protocol retObj.NSExceptionAllowsInsecureHTTPLoads = true; } return retObj; } /* App Transport Security (ATS) writer from <access> and <allow-navigation> tags in nodekit.json */ function writeATSEntries(config) { var pObj = processAccessAndAllowNavigationEntries(config); var ats = {}; for(var hostname in pObj) { if (pObj.hasOwnProperty(hostname)) { if (hostname === '*') { ats['NSAllowsArbitraryLoads'] = true; continue; } var entry = pObj[hostname]; var exceptionDomain = {}; for(var key in entry) { if (entry.hasOwnProperty(key) && key !== 'Hostname') { exceptionDomain[key] = entry[key]; } } if (!ats['NSExceptionDomains']) { ats['NSExceptionDomains'] = {}; } ats['NSExceptionDomains'][hostname] = exceptionDomain; } } return ats; } // Construct a default value for CFBundleVersion as the version with any // -rclabel stripped=. function default_CFBundleVersion(version) { return version.split('-')[0]; }