From 9e64099df60986e931a4932d2bce75a01eb62b4d Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 26 Feb 2025 08:19:39 -0800 Subject: [PATCH] Cleanup script and move the index to release (#49666) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49666 This change moves the index.js file to the `releases` folder, giving it a meaningful name. It also rename the prebuild folder to ios-prebuild to be more descriptive. It finally removes the old scripts that this refactoring supersedes. bypass-github-export-checks ## Changelog: [INTERNAL] - Move the infdex.js to the prepare-ios-prebuilds script. Reviewed By: cortinico Differential Revision: D70176043 fbshipit-source-id: e0c3f3bd53adf97fa388c37a746d274fd678f690 --- .../{prebuild => ios-prebuild}/build.js | 0 .../{prebuild => ios-prebuild}/cli.js | 0 .../compose-framework.js | 0 .../configuration.js | 0 .../{prebuild => ios-prebuild}/constants.js | 0 .../{prebuild => ios-prebuild}/folders.js | 0 .../setupDependencies.js | 0 .../swift-package.js | 0 .../{prebuild => ios-prebuild}/types.js | 0 .../releases/ios-prebuilds/dependencies.js | 57 --- scripts/releases/prebuild/index.js | 78 ---- scripts/releases/prepare-ios-prebuilds.js | 393 ++---------------- 12 files changed, 43 insertions(+), 485 deletions(-) rename scripts/releases/{prebuild => ios-prebuild}/build.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/cli.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/compose-framework.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/configuration.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/constants.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/folders.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/setupDependencies.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/swift-package.js (100%) rename scripts/releases/{prebuild => ios-prebuild}/types.js (100%) delete mode 100644 scripts/releases/ios-prebuilds/dependencies.js delete mode 100644 scripts/releases/prebuild/index.js diff --git a/scripts/releases/prebuild/build.js b/scripts/releases/ios-prebuild/build.js similarity index 100% rename from scripts/releases/prebuild/build.js rename to scripts/releases/ios-prebuild/build.js diff --git a/scripts/releases/prebuild/cli.js b/scripts/releases/ios-prebuild/cli.js similarity index 100% rename from scripts/releases/prebuild/cli.js rename to scripts/releases/ios-prebuild/cli.js diff --git a/scripts/releases/prebuild/compose-framework.js b/scripts/releases/ios-prebuild/compose-framework.js similarity index 100% rename from scripts/releases/prebuild/compose-framework.js rename to scripts/releases/ios-prebuild/compose-framework.js diff --git a/scripts/releases/prebuild/configuration.js b/scripts/releases/ios-prebuild/configuration.js similarity index 100% rename from scripts/releases/prebuild/configuration.js rename to scripts/releases/ios-prebuild/configuration.js diff --git a/scripts/releases/prebuild/constants.js b/scripts/releases/ios-prebuild/constants.js similarity index 100% rename from scripts/releases/prebuild/constants.js rename to scripts/releases/ios-prebuild/constants.js diff --git a/scripts/releases/prebuild/folders.js b/scripts/releases/ios-prebuild/folders.js similarity index 100% rename from scripts/releases/prebuild/folders.js rename to scripts/releases/ios-prebuild/folders.js diff --git a/scripts/releases/prebuild/setupDependencies.js b/scripts/releases/ios-prebuild/setupDependencies.js similarity index 100% rename from scripts/releases/prebuild/setupDependencies.js rename to scripts/releases/ios-prebuild/setupDependencies.js diff --git a/scripts/releases/prebuild/swift-package.js b/scripts/releases/ios-prebuild/swift-package.js similarity index 100% rename from scripts/releases/prebuild/swift-package.js rename to scripts/releases/ios-prebuild/swift-package.js diff --git a/scripts/releases/prebuild/types.js b/scripts/releases/ios-prebuild/types.js similarity index 100% rename from scripts/releases/prebuild/types.js rename to scripts/releases/ios-prebuild/types.js diff --git a/scripts/releases/ios-prebuilds/dependencies.js b/scripts/releases/ios-prebuilds/dependencies.js deleted file mode 100644 index 6712af964c0..00000000000 --- a/scripts/releases/ios-prebuilds/dependencies.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - * @oncall react_native - */ - -/*:: -export type Folder = RegExp; - -// We need to pass through the downloaded files and only keep the ones highlighted here. -// We can delete the rest of the files. -export type FilesToKeep = $ReadOnly<{ - headers: Folder | $ReadOnlyArray, - sources: $ReadOnlyArray, -}>; - - -export type Dependency = $ReadOnly<{ - name: string, - version: string, - url: URL, - prepareScript?: string, - filesToKeep: FilesToKeep, - copyHeaderRule?: 'skipFirstFolder', // We can use this field to handle specifics of 3rd party libraries -}>; -*/ - -const dependencies /*: $ReadOnlyArray */ = [ - { - name: 'glog', - version: '0.3.5', - url: new URL( - 'https://github.com/google/glog/archive/refs/tags/v0.3.5.tar.gz', - ), - prepareScript: './packages/react-native/scripts/ios-configure-glog.sh', - filesToKeep: { - headers: /src(\/(glog|base))?\/[a-zA-Z0-9_-]+\.h$/, // Keep all headers in src, src/glog and src/base - sources: [ - 'src/demangle.cc', - 'src/logging.cc', - 'src/raw_logging.cc', - 'src/signalhandler.cc', - 'src/symbolize.cc', - 'src/utilities.cc', - 'src/vlog_is_on.cc', - ], - }, - copyHeaderRule: 'skipFirstFolder', - }, -]; - -module.exports = dependencies; diff --git a/scripts/releases/prebuild/index.js b/scripts/releases/prebuild/index.js deleted file mode 100644 index 2911eb0f24e..00000000000 --- a/scripts/releases/prebuild/index.js +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - * @oncall react_native - */ - -const {buildDepenencies} = require('./build'); -const {getCLIConfiguration} = require('./cli'); -const {createFramework} = require('./compose-framework'); -const {cleanFolder} = require('./folders'); -const {setupDependencies} = require('./setupDependencies'); -const {createSwiftPackageFile} = require('./swift-package'); -const path = require('path'); - -require('../../babel-register').registerForScript(); - -const THIRD_PARTY_PATH = 'packages/react-native/third-party'; -const BUILD_DESTINATION = '.build'; - -const SCHEME = 'ReactNativeDependencies'; - -/** - * Main entry point - */ -async function main() { - const cli = await getCLIConfiguration(); - if (cli == null) { - return 0; - } - - const buildFolder = path.resolve(THIRD_PARTY_PATH, BUILD_DESTINATION); - const rootFolder = path.resolve(THIRD_PARTY_PATH); - - if (cli.tasks.setup) { - await cleanFolder(rootFolder); - await setupDependencies(cli.dependencies, rootFolder); - } - - if (cli.tasks.swiftpackage) { - // Create Package.swift file - await createSwiftPackageFile(SCHEME, cli.dependencies, rootFolder); - } - - if (cli.tasks.build) { - await cleanFolder(buildFolder); - await buildDepenencies( - SCHEME, - cli.configuration, - cli.dependencies, - cli.platforms, - rootFolder, - buildFolder, - ); - } - - if (cli.tasks.compose) { - await createFramework( - SCHEME, - cli.configuration, - cli.dependencies, - rootFolder, - buildFolder, - ); - } - - console.log(''); - console.log('🏁 Done!'); -} - -if (require.main === module) { - // eslint-disable-next-line no-void - void main(); -} diff --git a/scripts/releases/prepare-ios-prebuilds.js b/scripts/releases/prepare-ios-prebuilds.js index 79991546632..e249af454bc 100644 --- a/scripts/releases/prepare-ios-prebuilds.js +++ b/scripts/releases/prepare-ios-prebuilds.js @@ -9,374 +9,67 @@ * @oncall react_native */ -require('../babel-register').registerForScript(); - -/*:: -import type {Folder, FilesToKeep, Dependency} from './ios-prebuilds/dependencies'; -*/ - -const dependencies /*: $ReadOnlyArray */ = require('./ios-prebuilds/dependencies'); -const {execSync} = require('child_process'); -const fs = require('fs'); +const {buildDepenencies} = require('./ios-prebuild/build'); +const {getCLIConfiguration} = require('./ios-prebuild/cli'); +const {createFramework} = require('./ios-prebuild/compose-framework'); +const {cleanFolder} = require('./ios-prebuild/folders'); +const {setupDependencies} = require('./ios-prebuild/setupDependencies'); +const {createSwiftPackageFile} = require('./ios-prebuild/swift-package'); const path = require('path'); -const util = require('util'); -const {parseArgs} = require('util'); -const exec = util.promisify(require('child_process').exec); +require('../babel-register').registerForScript(); const THIRD_PARTY_PATH = 'packages/react-native/third-party'; const BUILD_DESTINATION = '.build'; -// tvOS is currently commented out because it requires additional patches. -const DESTINATIONS /*: { [string]: $ReadOnlyArray} */ = { - all: [ - 'generic/platform=iOS', - 'generic/platform=iOS Simulator', - 'generic/platform=macOS', - 'generic/platform=macOS,variant=Mac Catalyst', - // 'generic/platform=tvOS', - // 'generic/platform=tvOS Simulator', - 'generic/platform=visionOS', - 'generic/platform=visionOS Simulator', - ], - ios: ['generic/platform=iOS'], - 'ios-simulator': ['generic/platform=iOS Simulator'], - mac: ['generic/platform=macOS'], - 'mac-catalyst': ['generic/platform=macOS,variant=Mac Catalyst'], - tvos: [ - /*'generic/platform=tvOS'*/ - ], - 'tvos-simulator': [ - /*'generic/platform=tvOS Simulator'*/ - ], - xros: ['generic/platform=visionOS'], - xrsimulator: ['generic/platform=visionOS Simulator'], -}; - -async function _downloadDependency( - name /*: string*/, - url /*: URL*/, - destination /*: string*/, -) { - const filename = `${name}.tgz`; - const archiveDestination = `/tmp/${filename}`; - const command = `curl -L ${url.toString()} --output ${archiveDestination}`; - console.log(`Downloading ${filename}...`); - await exec(command); - - fs.mkdirSync(destination, {recursive: true}); - console.log(`Extracting ${filename} to ${destination}...`); - await exec( - `tar -xzf ${archiveDestination} -C ${destination} --strip-components 1`, - ); - console.log(`Cleaning up ${filename}...`); - await exec(`rm ${archiveDestination}`); -} - -function _prepareDependency(scriptPath /*: string*/, destination /*: string*/) { - console.log(`Running ${scriptPath}...`); - const finalPath = path.join(destination, 'prepare.sh'); - fs.copyFileSync(scriptPath, finalPath); - execSync('./prepare.sh', {cwd: destination, stdio: 'inherit'}); -} - -function _removeUnnecessaryFiles( - filesToKeep /*: FilesToKeep*/, - destination /*: string*/, -) { - const backupPath = `${destination}_backup`; - fs.mkdirSync(backupPath, {recursive: true}); - - console.log('Moving headers to backup folder...'); - const headers = filesToKeep.headers; - if (headers instanceof RegExp) { - const files = execSync(`find ${destination} -type f -name "*.h"`) - .toString() - .trim() - .split('\n'); - for (const file of files) { - if (headers.test(file)) { - const relativeFilePath = path - .relative(backupPath, file) - .split('/') - .slice(2) - .join('/'); - const destinationFile = `${backupPath}/${relativeFilePath}`; - - fs.mkdirSync(path.dirname(destinationFile), {recursive: true}); - fs.copyFileSync(file, destinationFile); - } - } - } else { - for (const file of headers) { - const filePath = path.join(destination, file); - const destFilePath = path.join(`${destination}_backup`, file); - fs.copyFileSync(filePath, destFilePath); - } - } - - console.log('Moving sources to backup folder...'); - for (const file of filesToKeep.sources) { - const filePath = path.join(destination, file); - const destFilePath = path.join(`${destination}_backup`, file); - fs.copyFileSync(filePath, destFilePath); - } - - console.log('Cleaning up...'); - fs.rmSync(destination, {recursive: true, force: true}); - fs.renameSync(`${destination}_backup`, destination); -} - -async function setupDependency( - dependency /*: Dependency*/, -) /*: Promise */ { - const {name, url} = dependency; - const destination = path.join(THIRD_PARTY_PATH, name); - - await _downloadDependency(name, url, destination); - - if (dependency.prepareScript) { - _prepareDependency(dependency.prepareScript, destination); - } - - _removeUnnecessaryFiles(dependency.filesToKeep, destination); -} - -async function build( - swiftPMFolder /* :string */, - buildDestinationPath /*: string */, - configuration /*: string */, - destination /*: string */, -) { - console.log(`Clean up ${buildDestinationPath}`); - fs.rmSync(buildDestinationPath, {recursive: true, force: true}); - - const platforms = DESTINATIONS[destination]; - if (!platforms) { - console.error( - `No valid destinations found for destination: ${destination}`, - ); - process.exit(1); - } - const configurations = - configuration === 'all' ? ['Debug', 'Release'] : [configuration]; - for (const platform of platforms) { - for (const config of configurations) { - console.log( - `Building ReactNativeDependencies for ${platform}. Configuration: ${config}`, - ); - const command = `xcodebuild -scheme "ReactNativeDependencies" \ - -destination "${platform}" \ - -configuration ${config} \ - -derivedDataPath "${BUILD_DESTINATION}"`; - execSync(command, {cwd: swiftPMFolder, stdio: 'inherit'}); - } - } -} - -async function copyHeadersToFrameworks( - dependency /*: Dependency*/, - swiftPMFolder /*:string*/, - buildDestinationPath /*: string */, -) { - const headers = dependency.filesToKeep.headers; - const {name} = dependency; - const sourceCodePath = path.join(THIRD_PARTY_PATH, name); - - let filePathMapping /*: { [string]: string }*/ = {}; - if (headers instanceof RegExp) { - const files = execSync(`find ${sourceCodePath} -type f -name "*.h"`) - .toString() - .trim() - .split('\n'); - for (const file of files) { - if (headers.test(file)) { - const relativeFilePath = path - .relative(buildDestinationPath, file) - .split('/') - .slice( - dependency.copyHeaderRule && - dependency.copyHeaderRule === 'skipFirstFolder' - ? 3 // Skip ../{name}/firstFolder - : 2, // Skip ../{name} which will be duplicated. - ) - .join('/'); - filePathMapping[file] = relativeFilePath; - } - } - } else { - headers.forEach(file => { - filePathMapping[path.join(sourceCodePath, file)] = file; - }); - } - - console.log(`Copying headers for ${name} to frameworks...`); - const productsFolder = path.join(buildDestinationPath, 'Build', 'Products'); - Object.entries(filePathMapping).forEach(([sourceFile, relativeFilePath]) => { - // For each file, we need to copy it in every slice of the frameworks we built. - fs.readdirSync(productsFolder).forEach(productFolderPath => { - const destinationFile = path.join( - productsFolder, - productFolderPath, - 'PackageFrameworks', - 'ReactNativeDependencies.framework', - 'Headers', - relativeFilePath, - ); - fs.mkdirSync(path.dirname(destinationFile), {recursive: true}); - fs.copyFileSync(sourceFile, destinationFile); - }); - }); -} - -function composeXCFrameworks( - thirdPartyFolder /*: string */, - buildDestinationPath /*: string */, -) { - console.log('Removing previous XCFramework'); - const outputFile = path.join( - thirdPartyFolder, - 'ReactNativeDependencies.xcframework', - ); - fs.rmSync(outputFile, {recursive: true, force: true}); - console.log('Composing XCFrameworks...'); - const frameworksFolder = path.join(buildDestinationPath, 'Build', 'Products'); - const frameworks = fs.readdirSync(frameworksFolder); - const frameworkPaths = frameworks.map(framework => - path.join( - frameworksFolder, - framework, - 'PackageFrameworks', - 'ReactNativeDependencies.framework', - ), - ); - const frameworksArgs = frameworkPaths - .map(framework => `-framework ${framework}`) - .join(' '); - const command = `xcodebuild -create-xcframework ${frameworksArgs} -output ${outputFile}`; - execSync(command, {stdio: 'inherit'}); -} - -const config = { - options: { - task: { - type: 'string', // valid values: 'all', 'prepare', 'build', 'create-xcframework' - default: 'all', - short: 't', - }, - slice: { - type: 'string', // valid values: 'all', 'ios', 'ios-simulator', 'mac', 'mac-catalyst', 'tvos', 'xros', 'xrsimulator' - default: 'all', - short: 's', - }, - configuration: { - type: 'string', // valid values: 'all', 'Debug', 'Release', - default: 'all', - short: 'c', - }, - help: { - type: 'boolean', - short: 'h', - }, - }, -}; - -function printHelp() { - console.log(` - Usage: node ./scripts/releases/prepare-ios-prebuilds.js [OPTIONS] - - This script prepares iOS prebuilds for React Native. It downloads the dependencies, prepare them, builds them and creates the XCFrameworks. - - Calling the script with no options will build all the dependencies for all the slices and configurations. - - Options: - --task, -t: the specific task that needs to be carried on. Default value is 'all'. Valid values are 'all', 'prepare', 'build', 'create-xcframework'. - --slice, -s: the specific slice that needs to be built. Default value is 'all'. Valid values are 'all', 'ios', 'ios-simulator', 'mac', 'mac-catalyst', 'tvos', 'tvos-simulator', 'xros', 'xrsimulator'. - --configuration, -c: the specific configuration that needs to be built. Default value is 'all'. Valid values are 'all', 'Debug', 'Release'. - --help, -h: print this help message. - `); -} - -const VALID_TASKS = ['all', 'prepare', 'build', 'create-xcframework']; -const VALID_SLICES = [ - 'all', - 'ios', - 'ios-simulator', - 'mac', - 'mac-catalyst', - 'tvos', - 'tvos-simulator', - 'xros', - 'xrsimulator', -]; -const VALID_CONFIGURATIONS = ['all', 'Debug', 'Release']; -function validateArgs( - task /*: string*/, - slice /*: string*/, - configuration /*: string*/, -) { - if (!VALID_TASKS.includes(task)) { - console.error( - `Invalid task: ${task}. Valid values are ${VALID_TASKS.join(', ')}`, - ); - process.exit(1); - } - - if (!VALID_SLICES.includes(slice)) { - console.error( - `Invalid slice: ${slice}. Valid values are ${VALID_SLICES.join(', ')}`, - ); - process.exit(1); - } - - if (!VALID_CONFIGURATIONS.includes(configuration)) { - console.error( - `Invalid configuration: ${configuration}. Valid values are ${VALID_CONFIGURATIONS.join(', ')}`, - ); - process.exit(1); - } -} +const SCHEME = 'ReactNativeDependencies'; +/** + * Main entry point + */ async function main() { - const { - values: {task, slice, configuration, help}, - } = parseArgs(config); - - if (help) { - printHelp(); - return; + const cli = await getCLIConfiguration(); + if (cli == null) { + return 0; } - validateArgs(task, slice, configuration); + const buildFolder = path.resolve(THIRD_PARTY_PATH, BUILD_DESTINATION); + const rootFolder = path.resolve(THIRD_PARTY_PATH); - console.log('Starting iOS prebuilds preparation...'); - const thirdPartyFolder = path.join(process.cwd(), THIRD_PARTY_PATH); - const buildDestinationPath = path.join(thirdPartyFolder, BUILD_DESTINATION); + if (cli.tasks.setup) { + await cleanFolder(rootFolder); + await setupDependencies(cli.dependencies, rootFolder); + } - if (task === 'all' || task === 'prepare') { - await Promise.all( - dependencies.map(dependency => setupDependency(dependency)), + if (cli.tasks.swiftpackage) { + // Create Package.swift file + await createSwiftPackageFile(SCHEME, cli.dependencies, rootFolder); + } + + if (cli.tasks.build) { + await cleanFolder(buildFolder); + await buildDepenencies( + SCHEME, + cli.configuration, + cli.dependencies, + cli.platforms, + rootFolder, + buildFolder, ); } - if (task === 'all' || task === 'build') { - await build(thirdPartyFolder, buildDestinationPath, configuration, slice); - - await Promise.all( - dependencies.map(dependency => - copyHeadersToFrameworks( - dependency, - thirdPartyFolder, - buildDestinationPath, - ), - ), + if (cli.tasks.compose) { + await createFramework( + SCHEME, + cli.configuration, + cli.dependencies, + rootFolder, + buildFolder, ); } - if (task === 'all' || task === 'create-xcframework') { - composeXCFrameworks(thirdPartyFolder, buildDestinationPath); - } - console.log('Done!'); + console.log(''); + console.log('🏁 Done!'); } if (require.main === module) {