Skip to content

Commit

Permalink
Merge pull request #4 from navrocky/feature/pipe-syntax-and-shell
Browse files Browse the repository at this point in the history
Add pipe syntax, add shell, trim, toBool functions
  • Loading branch information
navrocky authored Feb 10, 2025
2 parents 9f2482d + a6c13da commit 7a70a6e
Show file tree
Hide file tree
Showing 24 changed files with 3,718 additions and 156 deletions.
3,051 changes: 3,051 additions & 0 deletions 3rdparty/inja/inja.hpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
endif()

add_subdirectory(cli)
add_subdirectory(lib)
add_subdirectory(test)
202 changes: 126 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,96 @@ much more powerfull then Mustache.
Inja syntax documented [here](https://pantor.github.io/inja/) and original Jinja syntax documented
[here](https://jinja.palletsprojects.com/en/stable/templates/).

Additional functions:
Also supported function [pipe](https://jinja.palletsprojects.com/en/stable/templates/#filters) calling syntax.
These are equal expressions:

- Split text by delimiter:
```
{{ split("A,B,C", ",") }}
{{ "A,B,C" | split(",") }}
```
split(text: string, delimiter: string): string
```

- Convenient convert boolean variable to boolean type:

```
varToBool(varName: string): boolean
```
{{ sh("198c126c-2691-463f-9708-1ee485ce4d68", "sed 's/-//g'") }}
{{ "198c126c-2691-463f-9708-1ee485ce4d68" | sh("sed 's/-//g'") }}
```

## Additional Inja functions

### sh

Execute shell script with provided stdin and returned stdout.

```
sh(stdin: string?, command: string): string
```

Example usage:
```
ID={{ "198c126c-2691-463f-9708-1ee485ce4d68" | sh("sed 's/-//g'") }}
GUID={{ sh("uuidgen") | upper }}
```

### split

Splits text by delimiter.

```
split(text: string, delimiter: string): string
```

### trim

Trims text. Removes spaces and new lines from begin and end of text.

This function supports strings "true", "yes", "on", "1" for `true` value, other values supposed to be a `false` value.
```
trim(text: string): string
```

Example usage:
```
{{ " some text " | trim }}
```

### toBool

Convenient convert any value to boolean type.

Example: `set DO_SMTH = varToBool("DO_SMTH")`
```
toBool(value: any): boolean
```

This function supports strings "true", "yes", "on", "1" for `true` value, other values supposed to be a `false` value.

Example usage:
```
{{ " Yes " | toBool }}
```

### varToBool

Convenient convert boolean variable to boolean type.

- Throw error:
```
varToBool(varName: string): boolean
```

This function supports strings "true", "yes", "on", "1" for `true` value, other values supposed to be a `false` value.

Example usage:

```
## set DO_SMTH = varToBool("DO_SMTH")
```

### error

Throws an error.

```
error(message: string)
```
```
error(message: string)
```

## Mstch examples
## More Inja examples

Simple variable substitution:
### Simple variable substitution

```sh
echo "Hello, {{ USER }}!" | muenvsubst
Expand All @@ -80,67 +143,54 @@ then output will be:
Hello, John!
```

## Inja examples
### Using variable and function

- Simple variable substitution:
```sh
echo "Hello, {{ USER }}!" | muenvsubst
```
```sh
muenvsubst <<EOF
{%- set username = upper(USER) -%}
Hello, {{ username }}!
EOF
```

then output will be:

```
Hello, JOHN!
```

### Render conditional block

then output will be:
```sh
USE_GREETER=no USE_GOODBYER=yes muenvsubst << EOF
## if USE_GREETER=="yes"
Hello, {{ USER }}!
## endif
## if USE_GOODBYER=="yes"
Goodbye, {{ USER }}!
## endif
EOF
```

```
Hello, John!
```
then output will be:

- Using variable and function:
```
Goodbye, John!
```

```sh
muenvsubst <<EOF
{%- set username = upper(USER) -%}
Hello, {{ username }}!
EOF
```
### Using split and loop

then output will be:
```
Hello, JOHN!
```
- Render conditional block using alternative statement syntax:
```sh
USE_GREETER=no USE_GOODBYER=yes muenvsubst << EOF
## if USE_GREETER=="yes"
Hello, {{ USER }}!
## endif
## if USE_GOODBYER=="yes"
Goodbye, {{ USER }}!
## endif
EOF
```
then output will be:
```
Goodbye, John!
```
```sh
USERS="John,Mark,Peter" muenvsubst << EOF
{%- for user in split(USERS,",") -%}
Hello, {{ user }}!
{%- endfor -%}
EOF
```

- Using split and loop:
```sh
USERS="John,Mark,Peter" muenvsubst << EOF
{%- for user in split(USERS,",") -%}
Hello, {{ user }}!
{%- endfor -%}
EOF
```
then output will be:
```
Hello, John!
Hello, Mark!
Hello, Peter!
```
then output will be:

```
Hello, John!
Hello, Mark!
Hello, Peter!
```
9 changes: 3 additions & 6 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
set(TARGET muenvsubst)

find_package(args-parser REQUIRED)
find_package(inja REQUIRED)

include_directories(${CMAKE_SOURCE_DIR})

set(SOURCES
inja_renderer.cpp
inja_renderer.h
main.cpp
tools.cpp
tools.h
)

add_executable(${TARGET} ${SOURCES})

target_link_libraries(${TARGET}
args-parser::args-parser
pantor::inja
muenvsubst-lib
)

include(GNUInstallDirs)
Expand Down
72 changes: 0 additions & 72 deletions cli/inja_renderer.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions cli/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <args-parser/all.hpp>

#include "inja_renderer.h"
#include "tools.h"
#include <lib/inja_renderer.h>
#include <lib/tools.h>

using namespace std;
using namespace std::placeholders;
Expand Down
2 changes: 2 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[requires]
args-parser/6.3.3
inja/3.4.0
nlohmann_json/3.11.3
catch2/3.7.1

[generators]
CMakeDeps
Expand Down
27 changes: 27 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set(TARGET muenvsubst-lib)

# find_package(inja REQUIRED)
find_package(nlohmann_json REQUIRED)

include_directories(${CMAKE_SOURCE_DIR}/3rdparty)

set(SOURCES
funcs/other.cpp
funcs/other.h
funcs/shell.cpp
funcs/shell.h
inja_renderer.cpp
inja_renderer.h
process_executor.cpp
process_executor.h
tools.cpp
tools.h
)

add_library(${TARGET} ${SOURCES}
funcs/string.h funcs/string.cpp)

target_link_libraries(${TARGET}
nlohmann_json::nlohmann_json
# pantor::inja
)
Loading

0 comments on commit 7a70a6e

Please sign in to comment.