Skip to content

Commit 06b8d70

Browse files
authored
Merge the Codegen project into Blaze (#739)
Mostly out of convenience. So we don't have to be switching between projects and upgrading them all the time. Lots of time wasted due to that. Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 0ff98cb commit 06b8d70

213 files changed

Lines changed: 11105 additions & 2 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
env:
9898
HOMEBREW_NO_ANALYTICS: 1
9999
HOMEBREW_NO_AUTO_UPDATE: 1
100+
- run: npm ci
100101
- run: cmake --version
101102
- name: Configure Blaze (static)
102103
if: matrix.platform.type == 'static'

.github/workflows/website-build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
-DBLAZE_TEST:BOOL=OFF
2323
-DBLAZE_CONFIGURATION:BOOL=OFF
2424
-DBLAZE_ALTERSCHEMA:BOOL=OFF
25+
-DBLAZE_CODEGEN:BOOL=OFF
2526
-DBLAZE_TESTS:BOOL=OFF
2627
-DBLAZE_DOCS:BOOL=ON
2728
- run: cmake --build ./build --config Release --target doxygen

.github/workflows/website-deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
-DBLAZE_TEST:BOOL=OFF
3434
-DBLAZE_CONFIGURATION:BOOL=OFF
3535
-DBLAZE_ALTERSCHEMA:BOOL=OFF
36+
-DBLAZE_CODEGEN:BOOL=OFF
3637
-DBLAZE_TESTS:BOOL=OFF
3738
-DBLAZE_DOCS:BOOL=ON
3839
- run: cmake --build ./build --config Release --target doxygen

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ option(BLAZE_OUTPUT "Build the Blaze output formats library" ON)
1111
option(BLAZE_TEST "Build the Blaze test runner library" ON)
1212
option(BLAZE_CONFIGURATION "Build the Blaze configuration file library" ON)
1313
option(BLAZE_ALTERSCHEMA "Build the Blaze alterschema rule library" ON)
14+
option(BLAZE_CODEGEN "Build the Blaze codegen library" ON)
1415
option(BLAZE_TESTS "Build the Blaze tests" OFF)
1516
option(BLAZE_BENCHMARK "Build the Blaze benchmarks" OFF)
1617
option(BLAZE_CONTRIB "Build the Blaze contrib programs" OFF)
@@ -67,6 +68,10 @@ if(BLAZE_ALTERSCHEMA)
6768
add_subdirectory(src/alterschema)
6869
endif()
6970

71+
if(BLAZE_CODEGEN)
72+
add_subdirectory(src/codegen)
73+
endif()
74+
7075
if(BLAZE_CONTRIB)
7176
add_subdirectory(contrib)
7277
endif()
@@ -134,6 +139,10 @@ if(BLAZE_TESTS)
134139
add_subdirectory(test/alterschema)
135140
endif()
136141

142+
if(BLAZE_CODEGEN)
143+
add_subdirectory(test/codegen)
144+
endif()
145+
137146
if(PROJECT_IS_TOP_LEVEL)
138147
# Otherwise we need the child project to link
139148
# against the sanitizers too.

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# Programs
22
CMAKE = cmake
33
CTEST = ctest
4+
NPM = npm
45

56
# Options
67
PRESET = Debug
78
SHARED = OFF
89

910
all: configure compile test
1011

11-
configure: .always
12+
node_modules: package.json package-lock.json
13+
$(NPM) ci
14+
15+
configure: node_modules .always
1216
$(CMAKE) -S . -B ./build \
1317
-DCMAKE_BUILD_TYPE:STRING=$(PRESET) \
1418
-DCMAKE_COMPILE_WARNING_AS_ERROR:BOOL=ON \

cmake/FindCore.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ if(NOT Core_FOUND)
55
set(SOURCEMETA_CORE_INSTALL OFF CACHE BOOL "disable installation")
66
endif()
77

8-
set(SOURCEMETA_CORE_EXTENSION_OPTIONS OFF CACHE BOOL "disable")
98
set(SOURCEMETA_CORE_EXTENSION_BUILD OFF CACHE BOOL "disable")
109

1110
set(SOURCEMETA_CORE_CONTRIB_GOOGLETEST ${BLAZE_TESTS} CACHE BOOL "GoogleTest")

config.cmake.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if(NOT BLAZE_COMPONENTS)
1010
list(APPEND BLAZE_COMPONENTS test)
1111
list(APPEND BLAZE_COMPONENTS configuration)
1212
list(APPEND BLAZE_COMPONENTS alterschema)
13+
list(APPEND BLAZE_COMPONENTS codegen)
1314
endif()
1415

1516
include(CMakeFindDependencyMacro)
@@ -35,6 +36,12 @@ foreach(component ${BLAZE_COMPONENTS})
3536
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_compiler.cmake")
3637
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_output.cmake")
3738
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_alterschema.cmake")
39+
elseif(component STREQUAL "codegen")
40+
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_evaluator.cmake")
41+
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_compiler.cmake")
42+
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_output.cmake")
43+
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_alterschema.cmake")
44+
include("${CMAKE_CURRENT_LIST_DIR}/sourcemeta_blaze_codegen.cmake")
3845
else()
3946
message(FATAL_ERROR "Unknown Blaze component: ${component}")
4047
endif()

contrib/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ if(BLAZE_COMPILER AND BLAZE_EVALUATOR)
6060
endif()
6161
endif()
6262

63+
if(BLAZE_CODEGEN)
64+
sourcemeta_executable(NAMESPACE sourcemeta PROJECT blaze NAME contrib_typescript
65+
FOLDER "Blaze/Contrib" SOURCES typescript.cc)
66+
target_link_libraries(sourcemeta_blaze_contrib_typescript
67+
PRIVATE sourcemeta::blaze::codegen)
68+
target_link_libraries(sourcemeta_blaze_contrib_typescript
69+
PRIVATE sourcemeta::core::options)
70+
endif()
71+
6372
if(BLAZE_ALTERSCHEMA)
6473
sourcemeta_executable(NAMESPACE sourcemeta PROJECT blaze
6574
NAME contrib_canonicalize

contrib/typescript.cc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <sourcemeta/blaze/codegen.h>
2+
#include <sourcemeta/blaze/codegen_error.h>
3+
4+
#include <sourcemeta/core/json.h>
5+
#include <sourcemeta/core/jsonschema.h>
6+
#include <sourcemeta/core/options.h>
7+
8+
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
9+
#include <filesystem> // std::filesystem::path
10+
#include <iostream> // std::cout, std::cerr
11+
#include <sstream> // std::ostringstream
12+
#include <string> // std::string
13+
14+
auto main(int argc, char *argv[]) -> int {
15+
sourcemeta::core::Options options;
16+
options.option("default-prefix", {"p"});
17+
options.parse(argc, argv);
18+
19+
const auto &positional_arguments{options.positional()};
20+
if (positional_arguments.empty()) {
21+
std::cerr << "error: missing schema path\n";
22+
return EXIT_FAILURE;
23+
}
24+
25+
const std::filesystem::path schema_path{positional_arguments.front()};
26+
27+
try {
28+
const auto schema{sourcemeta::core::read_json(schema_path)};
29+
30+
const auto result{
31+
sourcemeta::blaze::compile(schema, sourcemeta::core::schema_walker,
32+
sourcemeta::core::schema_resolver,
33+
sourcemeta::blaze::default_compiler)};
34+
35+
const std::string prefix{
36+
options.contains("default-prefix")
37+
? std::string{options.at("default-prefix").front()}
38+
: "Schema"};
39+
40+
sourcemeta::blaze::generate<sourcemeta::blaze::TypeScript>(std::cout,
41+
result, prefix);
42+
} catch (const sourcemeta::blaze::CodegenUnsupportedKeywordError &error) {
43+
std::ostringstream pointer;
44+
sourcemeta::core::stringify(error.pointer(), pointer);
45+
std::cerr << "error: " << error.what() << "\n";
46+
std::cerr << " keyword: " << error.keyword() << "\n";
47+
std::cerr << " location: " << pointer.str() << "\n";
48+
std::cerr << " schema: ";
49+
sourcemeta::core::prettify(error.json(), std::cerr);
50+
std::cerr << "\n";
51+
return EXIT_FAILURE;
52+
} catch (
53+
const sourcemeta::blaze::CodegenUnsupportedKeywordValueError &error) {
54+
std::ostringstream pointer;
55+
sourcemeta::core::stringify(error.pointer(), pointer);
56+
std::cerr << "error: " << error.what() << "\n";
57+
std::cerr << " keyword: " << error.keyword() << "\n";
58+
std::cerr << " location: " << pointer.str() << "\n";
59+
std::cerr << " schema: ";
60+
sourcemeta::core::prettify(error.json(), std::cerr);
61+
std::cerr << "\n";
62+
return EXIT_FAILURE;
63+
} catch (const sourcemeta::blaze::CodegenUnexpectedSchemaError &error) {
64+
std::ostringstream pointer;
65+
sourcemeta::core::stringify(error.pointer(), pointer);
66+
std::cerr << "error: " << error.what() << "\n";
67+
std::cerr << " location: " << pointer.str() << "\n";
68+
std::cerr << " schema: ";
69+
sourcemeta::core::prettify(error.json(), std::cerr);
70+
std::cerr << "\n";
71+
return EXIT_FAILURE;
72+
}
73+
74+
return EXIT_SUCCESS;
75+
}

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)