Skip to content

Commit 6eb4e2a

Browse files
-added header and source generation mode;
-changed command line interface; -set version to 2.0.0;
1 parent 37edf65 commit 6eb4e2a

20 files changed

+666
-116
lines changed

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
cmake_minimum_required(VERSION 3.18)
2-
project(hypertextcpp VERSION 1.2.1 DESCRIPTION "hypertextcpp")
2+
project(hypertextcpp VERSION 2.0.0 DESCRIPTION "hypertextcpp")
33

44
include(external/seal_lake)
55

6-
SealLake_Import(cmdlime 2.6.0
6+
SealLake_Import(cmdlime 2.7.0
77
GIT_REPOSITORY https://github.com/kamchatka-volcano/cmdlime
8-
GIT_TAG dev
8+
GIT_TAG v2.7.0
99
)
1010

1111
SealLake_Import(sfun 5.1.0
@@ -24,6 +24,10 @@ SealLake_Bundle(
2424
GIT_REPOSITORY https://github.com/ericniebler/range-v3
2525
GIT_TAG 0.12.0
2626
)
27+
SealLake_Import(fmt 9.1.0
28+
GIT_REPOSITORY https://github.com/fmtlib/fmt
29+
GIT_TAG 9.1.0
30+
)
2731

2832
set(SRC
2933
src/codenode.cpp
@@ -41,7 +45,8 @@ set(SRC
4145
src/utils.cpp
4246
src/node_utils.cpp
4347
src/single_header_transpiler_renderer.cpp
44-
src/shared_lib_transpiler_renderer.cpp)
48+
src/shared_lib_transpiler_renderer.cpp
49+
src/header_and_source_transpiler_renderer.cpp)
4550

4651

4752
SealLake_Executable(
@@ -54,6 +59,7 @@ SealLake_Executable(
5459
cmdlime::cmdlime
5560
sfun::sfun
5661
Microsoft.GSL::GSL
62+
fmt::fmt
5763
)
5864

5965
SealLake_OptionalSubProjects(tests examples)

README.md

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,33 +252,66 @@ Now the tasks list can be output to stdout by itself like this:
252252
### Command line parameters
253253
```console
254254
kamchatka-volcano@home:~$ hypertextcpp --help
255-
Usage: hypertextcpp <input> [params] [flags]
255+
Usage: hypertextcpp [commands] [flags]
256+
Flags:
257+
--help show usage info and exit
258+
Commands:
259+
generateHeaderOnly [options] generate header only file
260+
generateSharedLibrarySource [options] generate shared library source
261+
file
262+
generateHeaderAndSource [options] generate header and source files
263+
```
264+
265+
Single header renderer generation:
266+
```console
267+
kamchatka-volcano@home:~$ hypertextcpp generateHeaderOnly --help
268+
Usage: hypertextcpp generateHeaderOnly <input> [params] [flags]
269+
Arguments:
270+
<input> (path) .htcpp file to transpile
271+
-outputDir=<path> output dir
272+
(if empty, current working directory is used)
273+
(optional, default: "")
274+
-className=<string> generated class name
275+
(if empty, input file name is used)
276+
(optional, default: "")
277+
Flags:
278+
--help show usage info and exit
279+
```
280+
281+
Header and source renderer generation:
282+
```console
283+
kamchatka-volcano@home:~$ hypertextcpp generateHeaderAndSource --help
284+
Usage: hypertextcpp generateHeaderAndSource <input> -configClassName=<string> [params] [flags]
256285
Arguments:
257286
<input> (path) .htcpp file to transpile
258287
Parameters:
259-
-o, --output <path> output c++ file path
288+
-configClassName=<string> config class name
289+
-outputDir=<path> output dir
260290
(if empty, current working directory is used)
261291
(optional, default: "")
262-
-c, --class-name <string> generated class name
263-
(optional)
292+
-className=<string> generated class name
293+
(if empty, input file name is used)
294+
(optional, default: "")
264295
Flags:
265-
-s, --shared-lib generate result as shared library source
266-
files
267-
--class-pascalcase generate class name by using .htcpp filename
268-
in PascalCase
269-
--class-snakecase generate class name by using .htcpp filename
270-
in snake_case
271-
--class-lowercase generate class name by using .htcpp filename
272-
in lowercase
273-
--help show usage info and exit
274-
275-
276-
Process finished with exit code 0
296+
--help show usage info and exit
297+
```
277298

299+
Shared library renderer generation:
300+
```console
301+
kamchatka-volcano@home:~$ hypertextcpp generateSharedLibrarySource --help
302+
Usage: hypertextcpp generateSharedLibrarySource <input> [params] [flags]
303+
Arguments:
304+
<input> (path) .htcpp file to transpile
305+
-outputDir=<path> output dir
306+
(if empty, current working directory is used)
307+
(optional, default: "")
308+
Flags:
309+
--help show usage info and exit
278310
```
279311

312+
280313
### Single header renderer
281-
By default, the **hypertextcpp** transpiler works in a single header mode and generates a C++ header file that you're supposed to simply include in your project. A generated renderer class has the name of the `.htcpp` template file. You can override the name by using the `--class-name` parameter, or you can specify one of the following flags: `--class-pascalcase`, `--class-snakecase`, or `--class-lowercase` to use the `.htcpp` template's filename converted to the corresponding case as a class name.
314+
In this mode, the **hypertextcpp** generates a C++ header file that you're supposed to simply include in your project. A generated renderer class has the name of the `.htcpp` template file.
282315
Converting the template to C++ code each time you modify it is a laborious task, so it makes sense to add this step to your build process. To do this with CMake you can use `hypertextcpp_GenerateHeader` function from the `hypertextcpp.cmake` file.
283316
Note that this function launches the `hypertextcpp` executable, so it should be installed on your system first.
284317

@@ -288,23 +321,37 @@ cmake_minimum_required(VERSION 3.18)
288321
289322
290323
include(../../hypertextcpp.cmake)
291-
hypertextcpp_GenerateHeader(NAME todolist CLASS_NAME TodoList)
324+
hypertextcpp_GenerateHeader(
325+
TEMPLATE_FILE todolist.htcpp
326+
CLASS_NAME TodoList
327+
)
292328
293329
set(SRC
294330
todolist_printer.cpp
295331
todolist.h)
296332
333+
#Please note that to generate the header todolist.h, it must pe passed to the target sources
297334
add_executable(todolist_printer ${SRC})
298335
299336
target_compile_features(todolist_printer PUBLIC cxx_std_17)
300337
set_target_properties(todolist_printer PROPERTIES CXX_EXTENSIONS OFF)
301338
```
302339

303-
Now, every time you change the template, the corresponding header will be regenerated on the next build.
340+
Now, every time you change the template `todolist.htcpp`, the corresponding header will be regenerated on the next build.
341+
342+
### Header and source renderer
343+
It can feel quite wasteful to rebuild all object files that include the renderer header each time the template file is changed, so `hypertextcpp` supports the generation of the renderer as a header and implementation file. In this mode, the generated rendering methods aren't function templates, and you need to provide the config name as a command-line parameter and either define the config structure inside the template or include it from there (check `examples/ex_06` and `examples/ex_07`).
344+
To do this with CMake, you can use the `hypertextcpp_GenerateHeaderAndSource` function from the `hypertextcpp.cmake` file.
345+
346+
```
347+
hypertextcpp_GenerateHeaderAndSource(
348+
TEMPLATE_FILE todolist.htcpp
349+
CONFIG_CLASS_NAME PageParams)
350+
```
304351

305352
### Shared library renderer
306-
It can feel quite wasteful to rebuild your project each time the template file is changed, so **hypertextcpp** supports the generation of a C++ source file for building templates in the form of shared libraries and linking them dynamically from your application.
307-
It requires duplicating the config declaration in the .htcpp template, registering it with the `HTCPP_CONFIG` macro in both the template and the application source, generating the renderer code with the `--shared-lib` command line flag, building the library, and loading it using the tiny API installed from the `shared_lib_api/` directory. It sounds scarier than it is, so let's quickly update the todolist example to see how it works.
353+
**hypertextcpp** also supports the generation of a C++ source file for building templates in the form of shared libraries and linking them dynamically from your application. This way it's possible to rebuild the template and update it without restarting the application.
354+
It requires duplicating the config declaration in the .htcpp template, registering it with the `HTCPP_CONFIG` macro in both the template and the application source, generating the renderer code with the `generateSharedLibrarySource` command, building the library, and loading it using the tiny API installed from the `shared_lib_api/` directory. It sounds scarier than it is, so let's quickly update the todolist example to see how it works.
308355

309356
First we need to copy the config structure declaration in the template:
310357
[`examples/05/todolist.htcpp`](examples/05/todolist.htcpp)
@@ -336,7 +383,7 @@ First we need to copy the config structure declaration in the template:
336383
</html>
337384
```
338385

339-
Be sure to use an exact copy; any mismatch of the config structure between the template and the application can't be handled gracefully. So, if you try to load a template library with a different structure, you'll definitely crash the application and maybe hurt someone as a result.
386+
Be sure to use an exact copy; any mismatch in the config structure between the template and the application can't be handled gracefully. So, if you try to load a template library with a different structure, your application will abort with a runtime error. This means that by using a htcpp template in the form of shared libraries, you lose one of the main advantages of hypertextcpp—compile-time type safety. Because of this, it's recommended to use this mode only after your template config has stabilized and doesn't change often.
340387

341388
Next, we need to build our template renderer as a library. It's not possible to bundle multiple template files in one library, so we can build a library from a single `.htcpp` file by using the `hypertextcpp_BuildSharedLibrary` CMake function from `hypertextcpp.cmake`:
342389

examples/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ add_subdirectory(ex_01)
22
add_subdirectory(ex_02)
33
add_subdirectory(ex_03)
44
add_subdirectory(ex_04)
5-
add_subdirectory(ex_05)
5+
add_subdirectory(ex_05)
6+
add_subdirectory(ex_06)
7+
add_subdirectory(ex_07)

examples/ex_01/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ project(ex_01)
33

44
include(../../hypertextcpp.cmake)
55

6-
hypertextcpp_GenerateHeader(NAME todolist)
6+
hypertextcpp_GenerateHeader(
7+
TEMPLATE_FILE todolist.htcpp)
78

89
set(SRC
910
todolist_printer.cpp

examples/ex_02/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ project(ex_02)
33

44
include(../../hypertextcpp.cmake)
55

6-
hypertextcpp_GenerateHeader(NAME todolist)
6+
hypertextcpp_GenerateHeader(
7+
TEMPLATE_FILE todolist.htcpp
8+
OUTPUT_DIR template
9+
)
710

811
set(SRC
912
todolist_printer.cpp
10-
todolist.h)
13+
template/todolist.h)
1114

1215
add_executable(${PROJECT_NAME} ${SRC})
1316

examples/ex_02/todolist_printer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "todolist.h"
1+
#include "template/todolist.h"
22
#include <string>
33
#include <vector>
44

examples/ex_03/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(ex_03)
33

44
include(../../hypertextcpp.cmake)
55

6-
hypertextcpp_GenerateHeader(NAME todolist)
6+
hypertextcpp_GenerateHeader(TEMPLATE_FILE todolist.htcpp)
77

88
set(SRC
99
todolist_printer.cpp

examples/ex_04/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ project(ex_04)
33

44
include(../../hypertextcpp.cmake)
55

6-
hypertextcpp_GenerateHeader(NAME todolist CLASS_NAME TodoList)
6+
hypertextcpp_GenerateHeader(
7+
TEMPLATE_FILE todolist.htcpp
8+
CLASS_NAME TodoList
9+
)
710

811
set(SRC
912
todolist_printer.cpp

examples/ex_05/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ target_link_libraries(${PROJECT_NAME} ${CMAKE_DL_LIBS})
1414

1515
include(../../hypertextcpp.cmake)
1616
hypertextcpp_BuildSharedLibrary(
17-
NAME todolist
18-
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
17+
TEMPLATE_FILE todolist.htcpp
1918
)
2019
add_dependencies(${PROJECT_NAME} todolist)

examples/ex_06/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
project(ex_06)
3+
4+
include(../../hypertextcpp.cmake)
5+
6+
hypertextcpp_GenerateHeaderAndSource(
7+
TEMPLATE_FILE todolist.htcpp
8+
CLASS_NAME TodoList
9+
CONFIG_CLASS_NAME PageParams)
10+
11+
set(SRC
12+
todolist_printer.cpp
13+
todolist.h
14+
todolist.cpp)
15+
16+
add_executable(${PROJECT_NAME} ${SRC})
17+
18+
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_11)
19+
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_EXTENSIONS OFF)
20+

0 commit comments

Comments
 (0)