react-native-legal
Version:
Acknowledge OSS libraries used in your React Native app
87 lines (86 loc) • 4.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerLicensePlistBuildPhaseUtil = exports.addSettingsBundleUtil = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Creates a Settings.bundle from a template and invokes a callback responsible for linking new file to the iOS project
*/
function addSettingsBundleUtil(iosProjectPath, addResourceFileToGroup) {
const settingsBundleFilename = 'Settings.bundle';
const settingsBundleRootPlistFilename = 'Root.plist';
const settingsBundlePath = path_1.default.join(iosProjectPath, settingsBundleFilename);
const settingsBundleRootPlistPath = path_1.default.join(settingsBundlePath, settingsBundleRootPlistFilename);
if (!fs_1.default.existsSync(settingsBundleRootPlistPath)) {
// Create `Settings.bundle` directory
if (!fs_1.default.existsSync(settingsBundlePath)) {
fs_1.default.mkdirSync(settingsBundlePath, { recursive: true });
}
// Create `Settings.bundle/Root.plist`
fs_1.default.writeFileSync(settingsBundleRootPlistPath, SETTINGS_BUNDLE_ROOT_PLIST_CONTENT);
// Link `Settings.bundle` to the resources
addResourceFileToGroup({ settingsBundleFilename });
console.log('Settings.bundle/Root.plist - ADDED');
}
else {
console.log('Settings.bundle/Root.plist already present - SKIP');
}
}
exports.addSettingsBundleUtil = addSettingsBundleUtil;
/**
* Creates a shell script build phase (if needed) and links it to native targets build phases
*/
function registerLicensePlistBuildPhaseUtil(projectTargetId, pbxproj) {
const nativeTargetSection = pbxproj.pbxNativeTargetSection();
const nativeTarget = nativeTargetSection[projectTargetId];
if (pbxproj.buildPhase(GENERATE_LICENSE_PLIST_BUILD_PHASE_COMMENT, projectTargetId)) {
console.log(`LicensePlist buildPhase already added in "${nativeTarget.name}" - SKIP`);
return pbxproj;
}
/**
* The build phase will generate licenses metadata using `LicensePlist` library and store it inside `Settings.bundle`.
*
* This will return an object with the uuid of the newly created build phase and its associated comment.
*/
const newBuildPhase = pbxproj.addBuildPhase([], 'PBXShellScriptBuildPhase', GENERATE_LICENSE_PLIST_BUILD_PHASE_COMMENT, projectTargetId, {
shellPath: '/bin/sh',
shellScript: '${PODS_ROOT}/LicensePlist/license-plist --output-path ./Settings.bundle',
});
/**
* In order to correctly link all metadata from `Settings.bundle` to the app
* the newly created build phase has to be invoked before `Copy Bundle Resources` build phase.
*
* To make sure that happens, let's put newly created build phase as a first in the sequence.
* It can be done by overriding `PBXNativeTarget["buildPhases"]` array.
*/
const newBuildPhasesInNativeTarget = [
{ value: newBuildPhase.uuid, comment: GENERATE_LICENSE_PLIST_BUILD_PHASE_COMMENT },
].concat(nativeTarget.buildPhases.filter(({ value }) => value !== newBuildPhase.uuid));
nativeTarget.buildPhases = newBuildPhasesInNativeTarget;
console.log(`LicensePlist buildPhase in nativeTarget "${nativeTarget.name}" - ADDED`);
return pbxproj;
}
exports.registerLicensePlistBuildPhaseUtil = registerLicensePlistBuildPhaseUtil;
const GENERATE_LICENSE_PLIST_BUILD_PHASE_COMMENT = 'Generate licenses with LicensePlist';
const SETTINGS_BUNDLE_ROOT_PLIST_CONTENT = `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>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSChildPaneSpecifier</string>
<key>Title</key>
<string>Licenses</string>
<key>File</key>
<string>com.mono0926.LicensePlist</string>
</dict>
</array>
</dict>
</plist>`;