Skip to content

Commit

Permalink
Open source the Codegen!
Browse files Browse the repository at this point in the history
Summary:
This is very much a work in progress. Moving it into the open source repo to be able to hook it up to generate some Fabric files.

Will continue to iterate on it in the open.

Reviewed By: hramos, mdvacca

Differential Revision: D13500969

fbshipit-source-id: 79082447dc52b5834f24eb72bc6e07200b324238
  • Loading branch information
elicwhite authored and facebook-github-bot committed Dec 20, 2018
1 parent 4d9f02f commit a3c6e1d
Show file tree
Hide file tree
Showing 42 changed files with 7,669 additions and 6 deletions.
120 changes: 120 additions & 0 deletions codegen/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "rn_xplat_cxx_library")
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "ANDROID", "APPLE")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load("@fbsource//tools/build_defs:fb_xplat_cxx_binary.bzl", "fb_xplat_cxx_binary")
load("@fbsource//xplat/js/react-native-github/codegen:DEFS.bzl", "rn_codegen_test")

fb_native.sh_binary(
name = "rn_codegen",
main = "buck_tests/generate_tests.sh",
resources = glob(
[
"**/*.js",
"buck_tests/generate-tests.js",
],
) + [
"xplat//js:setup_env",
],
visibility = ["PUBLIC"],
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_BOOLEAN_PROP",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_STRING_PROP",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_INTEGER_PROPS",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_FLOAT_PROPS",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_COLOR_PROP",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_ENUM_PROP",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_EVENT_PROPS",
)

rn_codegen_test(
fixture_name = "SINGLE_COMPONENT_WITH_EVENT_NESTED_OBJECT_PROPS",
)

rn_codegen_test(
fixture_name = "TWO_COMPONENTS_SAME_FILE",
)

rn_codegen_test(
fixture_name = "TWO_COMPONENTS_DIFFERENT_FILES",
)

fb_xplat_cxx_binary(
name = "rn_codegen_binary",
srcs = ["buck_tests/emptyFile.cpp"],
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
platforms = (ANDROID, APPLE),
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
visibility = ["PUBLIC"],
deps = [
":generated_fixture_library-SINGLE_COMPONENT_WITH_BOOLEAN_PROP",
":generated_fixture_library-SINGLE_COMPONENT_WITH_COLOR_PROP",
":generated_fixture_library-SINGLE_COMPONENT_WITH_ENUM_PROP",
":generated_fixture_library-SINGLE_COMPONENT_WITH_EVENT_NESTED_OBJECT_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_EVENT_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_FLOAT_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_INTEGER_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_STRING_PROP",
":generated_fixture_library-TWO_COMPONENTS_DIFFERENT_FILES",
":generated_fixture_library-TWO_COMPONENTS_SAME_FILE",
],
)

rn_xplat_cxx_library(
name = "rn_codegen_library",
srcs = ["buck_tests/emptyFile.cpp"],
headers = [],
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
platforms = (ANDROID, APPLE),
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
visibility = [
"PUBLIC",
],
deps = [
":generated_fixture_library-SINGLE_COMPONENT_WITH_BOOLEAN_PROP",
":generated_fixture_library-SINGLE_COMPONENT_WITH_COLOR_PROP",
":generated_fixture_library-SINGLE_COMPONENT_WITH_ENUM_PROP",
":generated_fixture_library-SINGLE_COMPONENT_WITH_EVENT_NESTED_OBJECT_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_EVENT_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_FLOAT_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_INTEGER_PROPS",
":generated_fixture_library-SINGLE_COMPONENT_WITH_STRING_PROP",
":generated_fixture_library-TWO_COMPONENTS_DIFFERENT_FILES",
":generated_fixture_library-TWO_COMPONENTS_SAME_FILE",
],
)
96 changes: 96 additions & 0 deletions codegen/DEFS.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "ANDROID", "APPLE")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load(
"//tools/build_defs/oss:rn_defs.bzl",
"react_native_xplat_target",
"rn_xplat_cxx_library",
)

def rn_codegen_test(
fixture_name = ""):
generate_fixtures_rule_name = "generate_fixtures-{}".format(fixture_name)
generate_component_descriptor_h_name = "generate_component_descriptor_h-{}".format(fixture_name)
generate_event_emitter_cpp_name = "generate_event_emitter_cpp-{}".format(fixture_name)
generate_event_emitter_h_name = "generate_event_emitter_h-{}".format(fixture_name)
generate_props_cpp_name = "generate_props_cpp-{}".format(fixture_name)
generate_props_h_name = "generated_props_h-{}".format(fixture_name)
generate_shadow_node_h_name = "generated_shadow_node_h-{}".format(fixture_name)

fb_native.genrule(
name = generate_fixtures_rule_name,
srcs = [],
cmd = "$(exe :rn_codegen) {} $OUT".format(fixture_name),
out = "codegenfiles-{}".format(fixture_name),
)

fb_native.genrule(
name = generate_component_descriptor_h_name,
cmd = "cp $(location :{})/ComponentDescriptors.h $OUT".format(generate_fixtures_rule_name),
out = "ComponentDescriptors.h",
)

fb_native.genrule(
name = generate_event_emitter_cpp_name,
cmd = "cp $(location :{})/EventEmitters.cpp $OUT".format(generate_fixtures_rule_name),
out = "EventEmitters.cpp",
)

fb_native.genrule(
name = generate_event_emitter_h_name,
cmd = "cp $(location :{})/EventEmitters.h $OUT".format(generate_fixtures_rule_name),
out = "EventEmitters.h",
)

fb_native.genrule(
name = generate_props_cpp_name,
cmd = "cp $(location :{})/Props.cpp $OUT".format(generate_fixtures_rule_name),
out = "Props.cpp",
)

fb_native.genrule(
name = generate_props_h_name,
cmd = "cp $(location :{})/Props.h $OUT".format(generate_fixtures_rule_name),
out = "Props.h",
)

fb_native.genrule(
name = generate_shadow_node_h_name,
cmd = "cp $(location :{})/ShadowNodes.h $OUT".format(generate_fixtures_rule_name),
out = "ShadowNodes.h",
)

# libs
rn_xplat_cxx_library(
name = "generated_fixture_library-{}".format(fixture_name),
srcs = [
":{}".format(generate_event_emitter_cpp_name),
":{}".format(generate_props_cpp_name),
],
headers = [
":{}".format(generate_component_descriptor_h_name),
":{}".format(generate_event_emitter_h_name),
":{}".format(generate_props_h_name),
":{}".format(generate_shadow_node_h_name),
],
exported_headers = {
"ComponentDescriptors.h": ":{}".format(generate_component_descriptor_h_name),
"EventEmitters.h": ":{}".format(generate_event_emitter_h_name),
"Props.h": ":{}".format(generate_props_h_name),
"ShadowNodes.h": ":{}".format(generate_shadow_node_h_name),
},
header_namespace = "react/components/{}".format(fixture_name),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
platforms = (ANDROID, APPLE),
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
deps = [
react_native_xplat_target("fabric/components/view:view"),
],
)
14 changes: 14 additions & 0 deletions codegen/buck_tests/emptyFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <react/components/SINGLE_COMPONENT_WITH_BOOLEAN_PROP/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_STRING_PROP/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_INTEGER_PROPS/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_FLOAT_PROPS/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_COLOR_PROP/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_ENUM_PROP/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_EVENT_NESTED_OBJECT_PROPS/ComponentDescriptors.h>
#import <react/components/SINGLE_COMPONENT_WITH_EVENT_PROPS/ComponentDescriptors.h>
#import <react/components/TWO_COMPONENTS_SAME_FILE/ComponentDescriptors.h>
#import <react/components/TWO_COMPONENTS_DIFFERENT_FILES/ComponentDescriptors.h>

int main(){
return 0;
}
39 changes: 39 additions & 0 deletions codegen/buck_tests/generate-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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.
*
* @flow
* @format
*/

'use strict';

const RNCodegen = require('../src/generators/RNCodegen.js');

const fixtures = require('../src/generators/__test_fixtures__/fixtures.js');
const mkdirp = require('mkdirp');

const args = process.argv.slice(2);
if (args.length !== 2) {
throw new Error(
'Expected to receive the fixture name and output directory as the only arg',
);
}

const fixtureName = args[0];
const outputDirectory = args[1];

mkdirp.sync(outputDirectory);
const fixture = fixtures[fixtureName];

if (fixture == null) {
throw new Error(`Can't find fixture with name ${fixtureName}`);
}

RNCodegen.generate({
libraryName: fixtureName,
schema: fixture,
outputDirectory,
});
15 changes: 15 additions & 0 deletions codegen/buck_tests/generate_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e
set -u

THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)

# shellcheck source=xplat/js/env-utils/setup_env_vars.sh
source "$THIS_DIR/../../../env-utils/setup_env_vars.sh"

pushd "$THIS_DIR/.." >/dev/null
"$INSTALL_NODE_MODULES"
popd >/dev/null

exec "$FLOW_NODE_BINARY" "$THIS_DIR/generate-tests.js" "$@"
7 changes: 7 additions & 0 deletions codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"jscodeshift": "^0.6.2",
"nullthrows": "^1.1.0"
},
"private": true
}
Loading

0 comments on commit a3c6e1d

Please sign in to comment.