Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
guglielmo-boi committed Mar 26, 2024
2 parents 51f9daa + 4d1295c commit bab6aa8
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 179 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/dev-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Generate MQTT topics dev-build

on:
workflow_dispatch:
push:
branches:
- dev

jobs:
library:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x

- name: Run generator
run: |
sudo apt-get install -y graphviz
pip install -r requirements.txt
python3 src/generator/main.py . out
mkdir -p /tmp/workspace
cp -R out/* /tmp/workspace
- uses: actions/checkout@v2
with:
ref: dev-build

- name: Push generated files
id: commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git rm -r --ignore-unmatch */*
rm -rf *
cp -r /tmp/workspace/* .
git add .
if [[ $(git status --porcelain) ]]; then git commit -am "chore: automatically generated files" && git push; fi
echo "::set-output name=HASH::$(git rev-parse HEAD)"
86 changes: 43 additions & 43 deletions .github/workflows/build.yml → .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
name: Generate MQTT topics

on:
workflow_dispatch:
push:
branches:
- main

jobs:
library:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x

- name: Run generator
run: |
sudo apt-get install -y graphviz
pip install -r requirements.txt
python3 src/generator/main.py . out
mkdir -p /tmp/workspace
cp -R out/* /tmp/workspace
- uses: actions/checkout@v2
with:
ref: build

- name: Push generated files
id: commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git rm -r --ignore-unmatch */*
rm -rf *
cp -r /tmp/workspace/* .
git add .
if [[ $(git status --porcelain) ]]; then git commit -am "chore: automatically generated files" && git push; fi
echo "::set-output name=HASH::$(git rev-parse HEAD)"
name: Generate MQTT topics build

on:
workflow_dispatch:
push:
branches:
- main

jobs:
library:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x

- name: Run generator
run: |
sudo apt-get install -y graphviz
pip install -r requirements.txt
python3 src/generator/main.py . out
mkdir -p /tmp/workspace
cp -R out/* /tmp/workspace
- uses: actions/checkout@v2
with:
ref: build

- name: Push generated files
id: commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git rm -r --ignore-unmatch */*
rm -rf *
cp -r /tmp/workspace/* .
git add .
if [[ $(git status --porcelain) ]]; then git commit -am "chore: automatically generated files" && git push; fi
echo "::set-output name=HASH::$(git rev-parse HEAD)"
42 changes: 30 additions & 12 deletions src/generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
parser.add_argument('topics_tree_dir', type=str, help='directory path of topics_tree file')
parser.add_argument('output_dir', type=str, help='directory path of generated files')

with open('src/templates/topics.h.j2', 'r') as file:
topic_h_template = jinja2.Template(file.read())
with open('src/templates/topics.cpp.j2', 'r') as file:
topics_cpp_template = jinja2.Template(file.read())
with open('src/templates/message_parser.h.j2', 'r') as file:
message_parser_h_template = jinja2.Template(file.read())
with open('src/templates/message_parser.cpp.j2', 'r') as file:
message_parser_cpp_template = jinja2.Template(file.read())
with open('src/templates/mqtt_topics.h.j2', 'r') as file:
h_template = jinja2.Template(file.read())
with open('src/templates/mqtt_topics.cpp.j2', 'r') as file:
cpp_template = jinja2.Template(file.read())
mqtt_topics_h = jinja2.Template(file.read())
with open('src/templates/CMakeLists.txt.j2', 'r') as file:
cmake_template = jinja2.Template(file.read())

Expand Down Expand Up @@ -45,18 +51,30 @@
if role not in roles:
roles.append(role)
for variable in topic['variables']:
if variable['name'] not in variables:
variables.append(variable['name'])
if variable not in variables:
variables.append(variable)

print(f'Generating files...\n')

with open(os.path.join(args.output_dir, 'inc', 'mqtt_topics.h'), 'w') as file:
file.write(h_template.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated mqtt_topics.h')
with open(os.path.join(args.output_dir, 'inc', 'topics.h'), 'w') as file:
file.write(topic_h_template.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated topics.h')

with open(os.path.join(args.output_dir, 'src', 'topics.cpp'), 'w') as file:
file.write(topics_cpp_template.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated topics.cpp')

with open(os.path.join(args.output_dir, 'inc', 'message_parser.h'), 'w') as file:
file.write(message_parser_h_template.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated message_parser.h')

with open(os.path.join(args.output_dir, 'src', 'mqtt_topics.cpp'), 'w') as file:
file.write(cpp_template.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated mqtt_topics.cpp')
with open(os.path.join(args.output_dir, 'src', 'message_parser.cpp'), 'w') as file:
file.write(message_parser_cpp_template.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated message_parser.cpp')

with open(os.path.join(args.output_dir, 'mqtt_topics.h'), 'w') as file:
file.write(mqtt_topics_h.render(topics=topics, roles=roles, variables=variables, utils=utils))
print(f'✅ Generated mqtt_topics.h')

with open(os.path.join(args.output_dir, 'CMakeLists.txt'), 'w') as file:
file.write(cmake_template.render())
Expand All @@ -74,5 +92,5 @@
print('✅ Generated readme.md')

with open(os.path.join(args.output_dir, 'docs.md'), 'w') as file:
utils.generate_md(topics, file)
utils.generate_docs(topics, file)
print('✅ Generated docs.md')
30 changes: 13 additions & 17 deletions src/generator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def generate_dot(topics, filename):
s = Source(out_str, format="png")
s.render(outfile=filename + ".png")

def generate_md(topics, file):
def generate_docs(topics, file):
file.write("# Topics\n\n")
for topic in topics:
file.write(f"## {topic['alias'].replace('<', '&lt;')}\n")
Expand All @@ -37,9 +37,7 @@ def generate_md(topics, file):
file.write("Yes" if topic["retain"] else "No")
file.write("\n- **Variables**:\n")
for variable in topic["variables"]:
file.write(f" - {variable['name']} -> {variable['description']} ")
if "default" in variable:
file.write(f"(default: {variable['default']})\n")
file.write(variable)
file.write("\n")

def parse_report_tree(key: str, node: dict, parent_config: dict) -> list:
Expand Down Expand Up @@ -114,46 +112,44 @@ def parse_report_tree(key: str, node: dict, parent_config: dict) -> list:
return topics

def topic_enum_name(topic: str) -> str:
words = re.findall(r'[A-Z][a-z]*', topic)
return ('_'.join(words).upper())
return (topic[0].lower() + topic[1:])

def topic_get_name(topic: str) -> str:
words = re.findall(r'[A-Z][a-z]*', topic)
return ('GetTopic' + ''.join(words))
return ('GetTopic' + topic)

def topic_get_parameters(topic: dict) -> str:
def topic_get_variables(topic: dict) -> str:
ret = str()
for variable in topic['variables']:
if ret != '':
ret += ', '
ret += 'const std::string& ' + variable['name']
ret += 'const std::string& ' + variable

return ret

def topic_get_parameters_values(topic: dict) -> str:
def topic_get_variables_values(topic: dict) -> str:
ret = str()
for variable in topic['variables']:
if ret != '':
ret += ', '
ret += variable['name']
ret += variable

return ret

def topic_topic_with_variables(topic: dict) -> str:
ret = topic['topic']

for variable in topic['variables']:
ret = ret.replace(f'/<{variable["name"]}>/', f' + "/" + {variable["name"]} + "/" + ')
ret = ret.replace(f'/<{variable["name"]}>', f' + "/" + {variable["name"]}')
ret = ret.replace(f'<{variable["name"]}>/', f'{variable["name"]} + "/" + ')
ret = ret.replace(f'<{variable["name"]}>', f'{variable["name"]} + "/" + ')
ret = ret.replace(f'/<{variable}>/', f' + "/" + {variable} + "/" + ')
ret = ret.replace(f'/<{variable}>', f' + "/" + {variable}')
ret = ret.replace(f'<{variable}>/', f'{variable} + "/" + ')
ret = ret.replace(f'<{variable}>', f'{variable} + "/" + ')

split = ret.split()
for s in split:
if s != '"/"' and s != '+':
check = True
for variable in topic['variables']:
if s == variable['name']:
if s == variable:
check = False
if check:
split[split.index(s)] = f'"{s}"'
Expand Down
3 changes: 2 additions & 1 deletion src/templates/CMakeLists.txt.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(
${PROJECT_NAME}
STATIC
src/mqtt_topics.cpp
src/topics.cpp
src/message_parser.cpp
)

target_include_directories(
Expand Down

0 comments on commit bab6aa8

Please sign in to comment.