2020-05-21 04:46:39 +03:00
|
|
|
/**
|
2021-12-31 02:08:43 +03:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2020-05-21 04:46:39 +03:00
|
|
|
*
|
|
|
|
* 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';
|
|
|
|
|
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');
|
2020-10-20 23:14:04 +03:00
|
|
|
if (!RNCodegen) {
|
|
|
|
throw 'RNCodegen not found.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 04:46:39 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const path = require('path');
|
2021-10-29 21:05:42 +03:00
|
|
|
const yargs = require('yargs');
|
|
|
|
|
|
|
|
const argv = yargs
|
|
|
|
.option('p', {
|
|
|
|
alias: 'platform',
|
|
|
|
describe: 'Platform to generate native code artifacts for.',
|
|
|
|
})
|
|
|
|
.option('s', {
|
|
|
|
alias: 'schemaPath',
|
|
|
|
describe: 'The path to the schema file.',
|
|
|
|
})
|
|
|
|
.option('o', {
|
|
|
|
alias: 'outputDir',
|
|
|
|
describe:
|
|
|
|
'Path to directory where native code source files should be saved.',
|
|
|
|
})
|
|
|
|
.option('n', {
|
|
|
|
alias: 'libraryName',
|
|
|
|
describe: 'Name of specs library.',
|
|
|
|
default: 'FBReactNativeSpec',
|
|
|
|
})
|
|
|
|
.option('j', {
|
|
|
|
alias: 'javaPackageName',
|
|
|
|
describe: 'Name of Java package.',
|
|
|
|
default: 'com.facebook.fbreact.specs',
|
|
|
|
})
|
|
|
|
.option('t', {
|
|
|
|
alias: 'libraryType',
|
|
|
|
describe: 'all, components, or modules.',
|
|
|
|
default: 'all',
|
|
|
|
})
|
|
|
|
.usage('Usage: $0 <args>')
|
|
|
|
.demandOption(
|
|
|
|
['platform', 'schemaPath', 'outputDir'],
|
|
|
|
'Please provide platform, schema path, and output directory.',
|
|
|
|
).argv;
|
2020-05-21 04:46:39 +03:00
|
|
|
|
2020-10-21 00:09:09 +03:00
|
|
|
const GENERATORS = {
|
2021-10-25 21:38:24 +03:00
|
|
|
all: {
|
|
|
|
android: ['componentsAndroid', 'modulesAndroid'],
|
|
|
|
ios: ['componentsIOS', 'modulesIOS'],
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
android: ['componentsAndroid'],
|
|
|
|
ios: ['componentsIOS'],
|
|
|
|
},
|
|
|
|
modules: {
|
|
|
|
android: ['modulesAndroid'],
|
|
|
|
ios: ['modulesIOS'],
|
|
|
|
},
|
2020-10-21 00:09:09 +03:00
|
|
|
};
|
|
|
|
|
2020-09-11 11:12:25 +03:00
|
|
|
function generateSpec(
|
|
|
|
platform,
|
|
|
|
schemaPath,
|
|
|
|
outputDirectory,
|
|
|
|
libraryName,
|
2020-10-21 00:09:09 +03:00
|
|
|
packageName,
|
2021-10-25 21:38:24 +03:00
|
|
|
libraryType,
|
2020-09-11 11:12:25 +03:00
|
|
|
) {
|
2020-05-21 04:46:39 +03:00
|
|
|
const schemaText = fs.readFileSync(schemaPath, 'utf-8');
|
|
|
|
|
|
|
|
if (schemaText == null) {
|
|
|
|
throw new Error(`Can't find schema at ${schemaPath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!outputDirectory) {
|
2021-01-08 06:37:29 +03:00
|
|
|
outputDirectory = path.resolve(__dirname, '..', 'Libraries', libraryName);
|
2020-05-21 04:46:39 +03:00
|
|
|
}
|
|
|
|
mkdirp.sync(outputDirectory);
|
|
|
|
|
2020-10-21 00:09:09 +03:00
|
|
|
let schema;
|
|
|
|
try {
|
|
|
|
schema = JSON.parse(schemaText);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Can't parse schema to JSON. ${schemaPath}`);
|
2020-08-01 04:47:45 +03:00
|
|
|
}
|
2020-10-21 00:09:09 +03:00
|
|
|
|
2021-10-25 21:38:24 +03:00
|
|
|
if (GENERATORS[libraryType] == null) {
|
|
|
|
throw new Error(`Invalid library type. ${libraryType}`);
|
|
|
|
}
|
|
|
|
|
2020-10-21 00:09:09 +03:00
|
|
|
RNCodegen.generate(
|
|
|
|
{
|
|
|
|
libraryName,
|
|
|
|
schema,
|
|
|
|
outputDirectory,
|
|
|
|
packageName,
|
|
|
|
},
|
|
|
|
{
|
2021-10-25 21:38:24 +03:00
|
|
|
generators: GENERATORS[libraryType][platform],
|
2020-10-21 00:09:09 +03:00
|
|
|
},
|
|
|
|
);
|
2020-12-22 09:41:06 +03:00
|
|
|
|
|
|
|
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}`);
|
|
|
|
});
|
|
|
|
}
|
2020-05-21 04:46:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
2021-10-25 21:38:24 +03:00
|
|
|
generateSpec(
|
2021-10-29 21:05:42 +03:00
|
|
|
argv.platform,
|
|
|
|
argv.schemaPath,
|
|
|
|
argv.outputDir,
|
|
|
|
argv.libraryName,
|
|
|
|
argv.javaPackageName,
|
|
|
|
argv.libraryType,
|
2021-10-25 21:38:24 +03:00
|
|
|
);
|
2020-05-21 04:46:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|