From 7ef6716582315c01255967325a048a59a55bc84d Mon Sep 17 00:00:00 2001 From: Eli White Date: Fri, 11 Jan 2019 15:28:34 -0800 Subject: [PATCH] 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 --- codegen/BUCK | 17 +++++++++++++- codegen/DEFS.bzl | 10 ++++++++- codegen/buck_tests/copy-fixture.js | 33 ++++++++++++++++++++++++++++ codegen/buck_tests/copy_fixture.sh | 11 ++++++++++ codegen/buck_tests/generate-tests.js | 32 +++++++++++++++++---------- 5 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 codegen/buck_tests/copy-fixture.js create mode 100755 codegen/buck_tests/copy_fixture.sh diff --git a/codegen/BUCK b/codegen/BUCK index b9857c22d..6e17966cd 100644 --- a/codegen/BUCK +++ b/codegen/BUCK @@ -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//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( name = "rn_codegen", main = "buck_tests/generate_tests.sh", resources = glob( [ - "**/*.js", + "buck_tests/**/*.js", + "src/**/*.js", ], ) + [ "buck_tests/generate-tests.js", + "package.json", "xplat//js:setup_env", + "yarn.lock", ], visibility = ["PUBLIC"], ) diff --git a/codegen/DEFS.bzl b/codegen/DEFS.bzl index 7c73eb8f0..646c50726 100644 --- a/codegen/DEFS.bzl +++ b/codegen/DEFS.bzl @@ -8,6 +8,7 @@ load( def rn_codegen_test( fixture_name = ""): + copy_schema_name = "copy_schema-{}".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_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_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( name = generate_fixtures_rule_name, 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), ) diff --git a/codegen/buck_tests/copy-fixture.js b/codegen/buck_tests/copy-fixture.js new file mode 100644 index 000000000..fc9f09e34 --- /dev/null +++ b/codegen/buck_tests/copy-fixture.js @@ -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)); diff --git a/codegen/buck_tests/copy_fixture.sh b/codegen/buck_tests/copy_fixture.sh new file mode 100755 index 000000000..e0595a05e --- /dev/null +++ b/codegen/buck_tests/copy_fixture.sh @@ -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" "$@" diff --git a/codegen/buck_tests/generate-tests.js b/codegen/buck_tests/generate-tests.js index 84f90e67f..4e09af92c 100644 --- a/codegen/buck_tests/generate-tests.js +++ b/codegen/buck_tests/generate-tests.js @@ -11,29 +11,39 @@ 'use strict'; const RNCodegen = require('../src/generators/RNCodegen.js'); - -const fixtures = require('../src/generators/__test_fixtures__/fixtures.js'); +const fs = require('fs'); const mkdirp = require('mkdirp'); const args = process.argv.slice(2); -if (args.length !== 2) { +if (args.length !== 3) { 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 outputDirectory = args[1]; +const schemaPath = args[0]; +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); -const fixture = fixtures[fixtureName]; -if (fixture == null) { - throw new Error(`Can't find fixture with name ${fixtureName}`); +let schema; +try { + schema = JSON.parse(schemaText); +} catch (err) { + throw new Error(`Can't parse schema to JSON. ${schemaPath}`); } RNCodegen.generate({ - libraryName: fixtureName, - schema: fixture, + libraryName, + schema, outputDirectory, });