react-native-macos/scripts/generate-specs-cli.js

113 строки
2.9 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
Generate FBReactNativeSpec ObjC++ source files using CocoaPods Summary: Move FBReactNativeSpec codegen invocation to shared `react_native_pods.rb` script and trigger codegen to run as part of `pod install` in both RNTester as well as React Native for iOS projects. These files need to be generated before CocoaPods generates the Pods project, so the codegen is invoked as part of a `pre_install` hook during `pod install`. The codegen hook can now take optional paths to allow it to run within different contexts (as part of a `react-native` repo checkout with access to the codegen source, or as part of a React Native iOS project that depends on the `react-native-codegen` npm package). ## Motivation The FBReactNativeSpec ObjC++ source files (FBReactNativeSpec.h and FBReactNativeSpec-generated.mm) can be generated on demand from the native module specs in `Libraries/` using `react-native-codegen`. They can therefore be removed from the repository, but before we do so, we must ensure they get generated when a React Native iOS workspace is created or updated. Invoking the codegen as part of the `pod install` step that creates a React Native iOS Xcode workspace ensures the specs are available in the following scenarios: * Whenever a new React Native iOS project is created. The `react-native init` command invokes `pod install`. * Whenever a React Native iOS project is upgraded. The `react-native upgrade` command invokes `pod install` as well. * For contributors to the open source project running the RNTester application, the codegen will be invoked when the RNTester workspace is generated by CocoaPods using `pod install` in `packages/rn-tester`. In any other case, the codegen can still be invoked directly via `scripts/generate-native-modules-specs.sh`. > **Note:** > The codegen will only process native modules in React Native's own Libraries directory. Changelog: [iOS][Changed] - Generate FBReactNativeSpec ObjC++ source files using CocoaPods. Reviewed By: fkgozali Differential Revision: D24348111 fbshipit-source-id: d62ae5c6f8ce6358bf96a2801c3cdb3d94dd868d
2020-10-20 23:14:04 +03:00
let RNCodegen;
try {
RNCodegen = require('../packages/react-native-codegen/lib/generators/RNCodegen.js');
chore: prefer the local react-native-codegen package (#32096) Summary: Currently, the build breaks if we move `react-native-codegen` from the template's dependencies to root. This is due to `scripts/generate-specs-cli.js` using the one installed under `node_modules` instead of the local one. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [Internal] [Fixed] - `scripts/generate-specs-cli.js` should prefer the local `react-native-codegen` package Pull Request resolved: https://github.com/facebook/react-native/pull/32096 Test Plan: 1. Make the following changes ```diff diff --git a/package.json b/package.json index 847c726a69b..78da8232988 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "promise": "^8.0.3", "prop-types": "^15.7.2", "react-devtools-core": "^4.13.0", + "react-native-codegen": "^0.0.7", "react-refresh": "^0.4.0", "regenerator-runtime": "^0.13.2", "scheduler": "^0.20.2", diff --git a/template/package.json b/template/package.json index 715614112ac..5e0762b1b25 100644 --- a/template/package.json +++ b/template/package.json @@ -21,7 +21,6 @@ "eslint": "7.14.0", "jest": "^26.6.3", "metro-react-native-babel-preset": "^0.66.2", - "react-native-codegen": "^0.0.7", "react-test-renderer": "17.0.2" }, "jest": { ``` 2. Run `scripts/test-manual-e2e.sh` ## Expected Behavior Task `:ReactAndroid:buildReactNdkLib` succeeds. ## Actual Behavior ``` > Task :ReactAndroid:buildReactNdkLib FAILED make: Entering directory '~/Source/react-native/ReactAndroid/src/main/jni/react/jni' fcntl(): Bad file descriptor make: Leaving directory '~/Source/react-native/ReactAndroid/src/main/jni/react/jni' ~/Library/Android/sdk/ndk/21.4.7075529/build/core/build-binary.mk:651: Android NDK: Module react_codegen_rncore depends on undefined modules: react_render_components_view ~/Library/Android/sdk/ndk/21.4.7075529/build/core/build-binary.mk:664: *** Android NDK: Note that old versions of ndk-build silently ignored this error case. If your project worked on those versions, the missing libraries were not needed and you can remove those dependencies from the module to fix your build. Alternatively, set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies. . Stop. FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':ReactAndroid:buildReactNdkLib'. > Process 'command '~/Library/Android/sdk/ndk/21.4.7075529/ndk-build'' finished with non-zero exit value 2 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.9/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 19s 20 actionable tasks: 20 executed Couldn't generate artifacts ``` Reviewed By: ShikaSD Differential Revision: D30581194 Pulled By: hramos fbshipit-source-id: 3f7a707b33377042502e50887856ff5641fdd52c
2021-08-26 22:00:48 +03:00
} catch (e) {
RNCodegen = require('react-native-codegen/lib/generators/RNCodegen.js');
Generate FBReactNativeSpec ObjC++ source files using CocoaPods Summary: Move FBReactNativeSpec codegen invocation to shared `react_native_pods.rb` script and trigger codegen to run as part of `pod install` in both RNTester as well as React Native for iOS projects. These files need to be generated before CocoaPods generates the Pods project, so the codegen is invoked as part of a `pre_install` hook during `pod install`. The codegen hook can now take optional paths to allow it to run within different contexts (as part of a `react-native` repo checkout with access to the codegen source, or as part of a React Native iOS project that depends on the `react-native-codegen` npm package). ## Motivation The FBReactNativeSpec ObjC++ source files (FBReactNativeSpec.h and FBReactNativeSpec-generated.mm) can be generated on demand from the native module specs in `Libraries/` using `react-native-codegen`. They can therefore be removed from the repository, but before we do so, we must ensure they get generated when a React Native iOS workspace is created or updated. Invoking the codegen as part of the `pod install` step that creates a React Native iOS Xcode workspace ensures the specs are available in the following scenarios: * Whenever a new React Native iOS project is created. The `react-native init` command invokes `pod install`. * Whenever a React Native iOS project is upgraded. The `react-native upgrade` command invokes `pod install` as well. * For contributors to the open source project running the RNTester application, the codegen will be invoked when the RNTester workspace is generated by CocoaPods using `pod install` in `packages/rn-tester`. In any other case, the codegen can still be invoked directly via `scripts/generate-native-modules-specs.sh`. > **Note:** > The codegen will only process native modules in React Native's own Libraries directory. Changelog: [iOS][Changed] - Generate FBReactNativeSpec ObjC++ source files using CocoaPods. Reviewed By: fkgozali Differential Revision: D24348111 fbshipit-source-id: d62ae5c6f8ce6358bf96a2801c3cdb3d94dd868d
2020-10-20 23:14:04 +03:00
if (!RNCodegen) {
throw 'RNCodegen not found.';
}
}
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const GENERATORS = {
android: ['componentsAndroid', 'modulesAndroid'],
Update iOS Fabric-related files to compile on OSS (#29810) Summary: Original PR contents: This pull request updates the Podspecs and associated build scripts, and some source files so they build on OSS. RNTester now compiles with `fabric_enabled` again. The following changes have been made: * Various spots that were pointing to the old `ReactCommon/fabric` location have now been updated to `ReactCommon/react/renderer` * Files that were attempting to use internal FB header `FBRCTFabricComponentsPlugins.h` were changed to use `RCTFabricComponentsPlugins.h` * `RCTFabricComponentsPlugins` in OSS was updated to include the `Image` fabric component (thanks tsapeta) * Replaced old `generate-rncore.sh` build script with new `generate-rncore.js` script which does not require `flow-node` and uses the `react-native-codegen` API directly, so there is no longer any need for an interim `schema-rncore.json` file. * Updated Yoga podspec which wasn't fully synced with changes from the main Yoga repo * Updated Fabric podspec with additional needed subspecs Additions to PR by hramos: * Replaced use of generate-rncore scripts with the original generate-native-modules-specs.sh script, which is now generate-specs.sh and supports both codegen for Native Modules and Components now (TurboModules/Fabric). * Codegen now runs at build time as part of the Xcode build pipeline instead of as part of `pod install`. The build script is injected by the FBReactNativeSpec pod, as the pod is part of both Fabric and non-Fabric builds. ## Changelog [General] [Fixed] - RNTester compiles with `fabric_enabled` again Pull Request resolved: https://github.com/facebook/react-native/pull/29810 Test Plan: RNTester now compiles and runs in the simulator again when `fabric_enabled` is set to `true`. ``` cd xplat/js/react-native-github/packages/rn-tester USE_FABRIC=1 pod install open RNTesterPods.xcworkspace # Build and run ``` Reviewed By: fkgozali Differential Revision: D24058507 Pulled By: hramos fbshipit-source-id: 8b2ea3694e6cb9aa23f83f087e2995fd4320e2bb
2020-12-22 19:33:56 +03:00
ios: ['componentsIOS', 'modulesIOS'],
};
function generateSpec(
platform,
schemaPath,
outputDirectory,
libraryName,
packageName,
) {
const schemaText = fs.readFileSync(schemaPath, 'utf-8');
if (schemaText == null) {
throw new Error(`Can't find schema at ${schemaPath}`);
}
if (!outputDirectory) {
outputDirectory = path.resolve(__dirname, '..', 'Libraries', libraryName);
}
mkdirp.sync(outputDirectory);
let schema;
try {
schema = JSON.parse(schemaText);
} catch (err) {
throw new Error(`Can't parse schema to JSON. ${schemaPath}`);
}
RNCodegen.generate(
{
libraryName,
schema,
outputDirectory,
packageName,
},
{
generators: GENERATORS[platform],
},
);
if (platform === 'android') {
// Move all components C++ files to a structured jni folder for now.
// Note: this should've been done by RNCodegen's generators, but:
// * the generators don't support platform option yet
// * this subdir structure is Android-only, not applicable to iOS
const files = fs.readdirSync(outputDirectory);
const jniOutputDirectory = `${outputDirectory}/jni/react/renderer/components/${libraryName}`;
mkdirp.sync(jniOutputDirectory);
files
.filter(f => f.endsWith('.h') || f.endsWith('.cpp'))
.forEach(f => {
fs.renameSync(`${outputDirectory}/${f}`, `${jniOutputDirectory}/${f}`);
});
}
if (platform === 'ios') {
const files = fs.readdirSync(outputDirectory);
const componentsOutputDirectory = `${outputDirectory}/react/renderer/components/${libraryName}`;
mkdirp.sync(componentsOutputDirectory);
files
.filter(
f =>
(f.endsWith('.h') && !f.startsWith(libraryName)) ||
f.endsWith('.cpp'),
)
.forEach(f => {
fs.renameSync(
`${outputDirectory}/${f}`,
`${componentsOutputDirectory}/${f}`,
);
});
}
}
function main() {
const args = process.argv.slice(2);
const platform = args[0];
const schemaPath = args[1];
const outputDir = args[2];
const libraryName = args[3] || 'FBReactNativeSpec';
const javaPackageName = args[4] || 'com.facebook.fbreact.specs';
generateSpec(platform, schemaPath, outputDir, libraryName, javaPackageName);
}
main();