Change buck to take path to schema instead of fixture name

Summary: When we actually use the codegen we will be passing in a path to a schema. Refactoring the existing buck rules to take a path to a schema instead of a fixture name so it can be reused.

Reviewed By: mdvacca

Differential Revision: D13619358

fbshipit-source-id: 1180d2e80c11b93f3cbdb0f9e848ae37bff199f4
This commit is contained in:
Eli White 2019-01-11 15:28:34 -08:00 коммит произвёл Facebook Github Bot
Родитель 62264b7ce2
Коммит 7ef6716582
5 изменённых файлов: 90 добавлений и 13 удалений

Просмотреть файл

@ -4,16 +4,31 @@ load("@fbsource//tools/build_defs:fb_xplat_cxx_binary.bzl", "fb_xplat_cxx_binary
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "rn_xplat_cxx_library") load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "rn_xplat_cxx_library")
load("@fbsource//xplat/js/react-native-github/codegen:DEFS.bzl", "rn_codegen_test") load("@fbsource//xplat/js/react-native-github/codegen:DEFS.bzl", "rn_codegen_test")
fb_native.sh_binary(
name = "copy_fixture_schema",
main = "buck_tests/copy_fixture.sh",
resources = [
"buck_tests/copy-fixture.js",
"buck_tests/copy_fixture.sh",
"src/generators/__test_fixtures__/fixtures.js",
"xplat//js:setup_env",
],
visibility = ["PUBLIC"],
)
fb_native.sh_binary( fb_native.sh_binary(
name = "rn_codegen", name = "rn_codegen",
main = "buck_tests/generate_tests.sh", main = "buck_tests/generate_tests.sh",
resources = glob( resources = glob(
[ [
"**/*.js", "buck_tests/**/*.js",
"src/**/*.js",
], ],
) + [ ) + [
"buck_tests/generate-tests.js", "buck_tests/generate-tests.js",
"package.json",
"xplat//js:setup_env", "xplat//js:setup_env",
"yarn.lock",
], ],
visibility = ["PUBLIC"], visibility = ["PUBLIC"],
) )

Просмотреть файл

@ -8,6 +8,7 @@ load(
def rn_codegen_test( def rn_codegen_test(
fixture_name = ""): fixture_name = ""):
copy_schema_name = "copy_schema-{}".format(fixture_name)
generate_fixtures_rule_name = "generate_fixtures-{}".format(fixture_name) generate_fixtures_rule_name = "generate_fixtures-{}".format(fixture_name)
generate_component_descriptor_h_name = "generate_component_descriptor_h-{}".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_cpp_name = "generate_event_emitter_cpp-{}".format(fixture_name)
@ -16,10 +17,17 @@ def rn_codegen_test(
generate_props_h_name = "generated_props_h-{}".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) generate_shadow_node_h_name = "generated_shadow_node_h-{}".format(fixture_name)
fb_native.genrule(
name = copy_schema_name,
srcs = [],
cmd = "$(exe :copy_fixture_schema) {} $OUT".format(fixture_name),
out = "schema-{}.json".format(fixture_name),
)
fb_native.genrule( fb_native.genrule(
name = generate_fixtures_rule_name, name = generate_fixtures_rule_name,
srcs = [], srcs = [],
cmd = "$(exe :rn_codegen) {} $OUT".format(fixture_name), cmd = "$(exe :rn_codegen) $(location :{}) {} $OUT".format(copy_schema_name, fixture_name),
out = "codegenfiles-{}".format(fixture_name), out = "codegenfiles-{}".format(fixture_name),
) )

Просмотреть файл

@ -0,0 +1,33 @@
/**
* 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 fs = require('fs');
const fixtures = require('../src/generators/__test_fixtures__/fixtures.js');
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 outputPath = args[1];
const fixture = fixtures[fixtureName];
if (fixture == null) {
throw new Error(`Can't find fixture with name ${fixtureName}`);
}
fs.writeFileSync(outputPath, JSON.stringify(fixture, null, 2));

Просмотреть файл

@ -0,0 +1,11 @@
#!/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"
exec "$FLOW_NODE_BINARY" "$THIS_DIR/copy-fixture.js" "$@"

Просмотреть файл

@ -11,29 +11,39 @@
'use strict'; 'use strict';
const RNCodegen = require('../src/generators/RNCodegen.js'); const RNCodegen = require('../src/generators/RNCodegen.js');
const fs = require('fs');
const fixtures = require('../src/generators/__test_fixtures__/fixtures.js');
const mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
const args = process.argv.slice(2); const args = process.argv.slice(2);
if (args.length !== 2) { if (args.length !== 3) {
throw new Error( throw new Error(
'Expected to receive the fixture name and output directory as the only arg', `Expected to receive path to schema, library name, and output directory. Received ${args.join(
', ',
)}`,
); );
} }
const fixtureName = args[0]; const schemaPath = args[0];
const outputDirectory = args[1]; const libraryName = args[1];
const outputDirectory = args[2];
const schemaText = fs.readFileSync(schemaPath, 'utf-8');
if (schemaText == null) {
throw new Error(`Can't find schema at ${schemaPath}`);
}
mkdirp.sync(outputDirectory); mkdirp.sync(outputDirectory);
const fixture = fixtures[fixtureName];
if (fixture == null) { let schema;
throw new Error(`Can't find fixture with name ${fixtureName}`); try {
schema = JSON.parse(schemaText);
} catch (err) {
throw new Error(`Can't parse schema to JSON. ${schemaPath}`);
} }
RNCodegen.generate({ RNCodegen.generate({
libraryName: fixtureName, libraryName,
schema: fixture, schema,
outputDirectory, outputDirectory,
}); });