Skip to content

Commit

Permalink
Align Codegen between iOS and Android (#33864)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #33864

This Diff aligns the way in which iOS and Android codegen the modules and components.
Android takes all the JS in the project root folder and creates components starting from there.
iOS used to required to specify a specific path for each component, within a JSON field called `libraries`. This Diff let iOS work in the same way as android does

**Backward compatibility:** This diff still support the old way for iOS, but we are deprecating it.

## Changelog
[iOS][Added] - Support codegen from a single folder

Reviewed By: cortinico

Differential Revision: D36473005

fbshipit-source-id: 1e8cf0f9764f529c02e948984c74d1982a84030b
  • Loading branch information
Riccardo Cipolleschi authored and facebook-github-bot committed May 19, 2022
1 parent 1c1fbce commit 05aaba9
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 71 deletions.
4 changes: 2 additions & 2 deletions packages/rn-tester/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ rn_library(
"pfh:ReactNative_CommonInfrastructurePlaceholder",
"supermodule:xplat/default/public.react_native.playground",
],
native_component_spec_name = "MyNativeViewSpec",
native_component_spec_name = "AppSpecs",
skip_processors = True,
visibility = ["PUBLIC"],
deps = [
Expand Down Expand Up @@ -317,7 +317,7 @@ rn_xplat_cxx_library2(
reexport_all_header_dependencies = False,
visibility = ["PUBLIC"],
deps = [
":generated_components-MyNativeViewSpec",
":generated_components-AppSpecs",
"//xplat/js/react-native-github:RCTFabricComponentViewsBase",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#import "RNTMyNativeViewComponentView.h"

#import <react/renderer/components/MyNativeViewSpec/ComponentDescriptors.h>
#import <react/renderer/components/MyNativeViewSpec/EventEmitters.h>
#import <react/renderer/components/MyNativeViewSpec/Props.h>
#import <react/renderer/components/MyNativeViewSpec/RCTComponentViewHelpers.h>
#import <react/renderer/components/AppSpecs/ComponentDescriptors.h>
#import <react/renderer/components/AppSpecs/EventEmitters.h>
#import <react/renderer/components/AppSpecs/Props.h>
#import <react/renderer/components/AppSpecs/RCTComponentViewHelpers.h>

#import "RCTFabricComponentsPlugins.h"

Expand Down
19 changes: 3 additions & 16 deletions packages/rn-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,8 @@
"ws": "^6.1.4"
},
"codegenConfig": {
"libraries": [
{
"name": "ScreenshotManagerSpec",
"type": "modules",
"ios": {},
"android": {},
"jsSrcsDir": "NativeModuleExample"
},
{
"name": "MyNativeViewSpec",
"type": "components",
"ios": {},
"android": {},
"jsSrcsDir": "NativeComponentExample/js"
}
]
"name": "AppSpecs",
"type": "all",
"jsSrcsDir": "."
}
}
45 changes: 45 additions & 0 deletions scripts/codegen/__test_fixtures__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@

'use-strict';

const SINGLE_LIBRARY_CODEGEN_CONFIG = {
codegenConfig: {
libraries: [
{
name: 'react-native',
type: 'all',
jsSrcsDir: '.',
},
],
},
};

const MULTIPLE_LIBRARIES_CODEGEN_CONFIG = {
codegenConfig: {
libraries: [
{
name: 'react-native',
type: 'all',
jsSrcsDir: '.',
},
{
name: 'my-component',
type: 'components',
jsSrcsDir: 'component/js',
},
{
name: 'my-module',
type: 'module',
jsSrcsDir: 'module/js',
},
],
},
};

const NO_LIBRARIES_CONFIG_FILE = {
codegenConfig: {
name: 'AppModules',
type: 'all',
jsSrcsDir: '.',
},
};

const SCHEMA_TEXT = `
{
"modules": {
Expand Down Expand Up @@ -84,4 +126,7 @@ const SCHEMA = JSON.parse(SCHEMA_TEXT);
module.exports = {
schemaText: SCHEMA_TEXT,
schema: SCHEMA,
noLibrariesConfigFile: NO_LIBRARIES_CONFIG_FILE,
singleLibraryCodegenConfig: SINGLE_LIBRARY_CODEGEN_CONFIG,
multipleLibrariesCodegenConfig: MULTIPLE_LIBRARIES_CODEGEN_CONFIG,
};
132 changes: 132 additions & 0 deletions scripts/codegen/__tests__/generate-artifacts-executor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
'use strict';

const underTest = require('../generate-artifacts-executor');
const fixtures = require('../__test_fixtures__/fixtures');
const path = require('path');

const codegenConfigKey = 'codegenConfig';
const reactNativeDependencyName = 'react-native';
const rootPath = path.join(__dirname, '../../..');

describe('generateCode', () => {
it('executeNodes with the right arguents', () => {
// Define variables and expected values
Expand Down Expand Up @@ -69,3 +74,130 @@ describe('generateCode', () => {
expect(mkdirSyncInvocationCount).toBe(2);
});
});

describe('extractLibrariesFromJSON', () => {
it('throws if in react-native and no dependencies found', () => {
let libraries = [];
let configFile = {};
expect(() => {
underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
);
}).toThrow();
});

it('it skips if not into react-native and no dependencies found', () => {
let libraries = [];
let configFile = {};

underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
'some-node-module',
'node_modules/some',
);
expect(libraries.length).toBe(0);
});

it('extracts a single dependency when config has no libraries', () => {
let libraries = [];
let configFile = fixtures.noLibrariesConfigFile;
underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
'my-app',
'.',
);
expect(libraries.length).toBe(1);
expect(libraries[0]).toEqual({
library: 'my-app',
config: {
name: 'AppModules',
type: 'all',
jsSrcsDir: '.',
},
libraryPath: '.',
});
});

it("extract codegenConfig when it's empty", () => {
const configFile = {codegenConfig: {libraries: []}};
let libraries = [];
underTest._extractLibrariesFromJSON(
configFile,
codegenConfigKey,
libraries,
reactNativeDependencyName,
rootPath,
);
expect(libraries.length).toBe(0);
});

it('extract codegenConfig when dependency is one', () => {
const configFile = fixtures.singleLibraryCodegenConfig;
let libraries = [];
underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
reactNativeDependencyName,
rootPath,
);
expect(libraries.length).toBe(1);
expect(libraries[0]).toEqual({
library: reactNativeDependencyName,
config: {
name: 'react-native',
type: 'all',
jsSrcsDir: '.',
},
libraryPath: rootPath,
});
});

it('extract codegenConfig with multiple dependencies', () => {
const configFile = fixtures.multipleLibrariesCodegenConfig;
const myDependency = 'my-dependency';
const myDependencyPath = path.join(__dirname, myDependency);
let libraries = [];
underTest._extractLibrariesFromJSON(
configFile,
libraries,
codegenConfigKey,
myDependency,
myDependencyPath,
);
expect(libraries.length).toBe(3);
expect(libraries[0]).toEqual({
library: myDependency,
config: {
name: 'react-native',
type: 'all',
jsSrcsDir: '.',
},
libraryPath: myDependencyPath,
});
expect(libraries[1]).toEqual({
library: myDependency,
config: {
name: 'my-component',
type: 'components',
jsSrcsDir: 'component/js',
},
libraryPath: myDependencyPath,
});
expect(libraries[2]).toEqual({
library: myDependency,
config: {
name: 'my-module',
type: 'module',
jsSrcsDir: 'module/js',
},
libraryPath: myDependencyPath,
});
});
});
Loading

0 comments on commit 05aaba9

Please sign in to comment.