Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create workflow converting markdown-based changlog to lua-based changelog #6444

Merged
merged 22 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8b57008
Create script converting markdown-based changlog to lua-based changelog
Zjonn Sep 10, 2024
ac69c29
Cleanup script
Zjonn Sep 16, 2024
4eff7f9
Check if user has at least python3.8 version
Zjonn Sep 16, 2024
1906fa2
Merge branch 'develop' into dd-6237-convert-md-to-lua
Zjonn Sep 16, 2024
c0e7dc5
Add workflow converting markdown-based changelog to lua-based changelog
Zjonn Sep 22, 2024
c395c27
Add lua_changelog to .gitignore
Zjonn Sep 22, 2024
c7700bf
Add changelog snippets files
Zjonn Sep 22, 2024
6ee8db9
Fix workflow
Zjonn Sep 22, 2024
a8b2404
Merge branch 'develop' into dd-6237-convert-md-to-lua
Zjonn Sep 22, 2024
a043ba0
Add header to generated file. Name created .lua dictionary as Changelog
Zjonn Oct 4, 2024
fa430a9
Use '--' for comments instead of '#'
Zjonn Oct 6, 2024
c73873d
Move new scripts to .github/workflows/scripts/python
Zjonn Oct 6, 2024
4506917
Add a README.md file describing how to configure the Python environment
Zjonn Oct 6, 2024
f902217
Add Lua changelog validation step
Zjonn Oct 6, 2024
1ab6b1e
Make run-syntax-test.sh executable
Zjonn Oct 6, 2024
66f993c
Install tooling
Zjonn Oct 6, 2024
c5977f8
Merge branch 'develop' into dd-6237-convert-md-to-lua
Zjonn Oct 6, 2024
b14c784
Fix Lua changelog validation job
Zjonn Oct 7, 2024
91e2d0a
Remove comma from template
Zjonn Oct 7, 2024
00aeaea
Move bash workflow files to subdirectory
Zjonn Oct 7, 2024
ce5c74d
Fix typo
Zjonn Oct 7, 2024
181699c
Merge branch 'develop' into dd-6237-convert-md-to-lua
Zjonn Oct 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions .github/workflows/docs-convert-changelog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) 2024 Daniel 'Zjonn' Dubiel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

name: Convert Markdown changelog to Lua

on:
workflow_dispatch:
inputs:
reference:
required: true
type: choice
description: The reference (branch or tag) to use to compile the changelog snippets from
options:
- develop
- deploy/fafbeta
- deploy/fafdevelop

workflow_call:
inputs:
reference:
required: true
type: string
description: The reference (branch or tag) to use to compile the changelog snippets from

jobs:
convert_changlog:
name: Convert changelog
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
sparse-checkout: |
scripts
changelog

- name: Setup python
uses: actions/setup-python@v5
with:
python-version: '3.8'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r scripts/requirements.txt

- name: Convert changelog to Lua
run: |
out_dir=lua_changelog
mkdir $out_dir
for file in changelog/*.md; do
out_file=$(basename ${file%.md}.lua)
python3 scripts/markdown2lua.py "${file}" "${out_dir}/${out_file}"
done

- name: Add the Lua changelog as an artifact
uses: actions/upload-artifact@v4
with:
name: lua_changelog
path: |
lua_changelog
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ textures/ui/common/BUTTON/BACKUP/
*/dds/*
.idea
*.DS_Store
act.exe
act.exe
*.venv
lua_changelog
1 change: 1 addition & 0 deletions changelog/snippets/other.6237.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#6237) Convert a markdown-based changelog file into a Lua-based changelog file
1 change: 1 addition & 0 deletions changelog/snippets/other.6238.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#6237) Create a Github Workflow to convert markdown changelog files into Lua changelog files
76 changes: 76 additions & 0 deletions scripts/markdown2lua.py
Zjonn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import mdformat

from argparse import ArgumentParser
from pathlib import Path

SCRIPT_NAME = Path(__file__).name

MAX_LINE_LENGTH = 150

LUA_DESC_LINE = ' "{line}"'
LUA_FILE = """Changelog = {{
version = {version},
description = {{
{description}
}}
}},
"""

HEADER_SEPARATOR = "#" * MAX_LINE_LENGTH
HEADER = f"""{HEADER_SEPARATOR}
#{f"This file was autogenerated using {SCRIPT_NAME:}":^{MAX_LINE_LENGTH - 2}}#
Zjonn marked this conversation as resolved.
Show resolved Hide resolved
#{{source:^{MAX_LINE_LENGTH - 2}}}#
{HEADER_SEPARATOR}

"""


def get_parser():
ap = ArgumentParser(description="Converts Markdown release file to Lua.")
ap.add_argument(
"input_file",
help="Markdown file",
type=Path,
)
ap.add_argument(
"output_file",
help="Lua file",
type=Path,
)
return ap


def convert_changelog(markdown: Path, lua: Path):
version = markdown.stem

source_info = f"Source: {markdown}"
header = HEADER.format(source=source_info)
lua_content = markdown2lua(version, markdown.read_text())
lua.write_text(header + lua_content)


def markdown2lua(version: str, content: str) -> str:
formatted_md = mdformat.text(
content,
options={
"wrap": MAX_LINE_LENGTH,
},
)

escaped_md = escape_special_symbols(formatted_md)
return LUA_FILE.format(
version=version,
description=",\n".join(
LUA_DESC_LINE.format(line=line) for line in escaped_md.splitlines()
),
)


def escape_special_symbols(text: str) -> str:
return text.replace("\\", "\\\\").replace('"', '\\"')


if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
convert_changelog(args.input_file, args.output_file)
1 change: 1 addition & 0 deletions scripts/requirements.txt
Zjonn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mdformat==0.7.17
40 changes: 40 additions & 0 deletions scripts/setup_python_env.sh
Zjonn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
set -euo pipefail

# For debug
# set -x

VENV_NAME=".venv"
PYTHON_REQUIREMENTS="requirements.txt"
SCRIPT_PATH=$(realpath "${BASH_SOURCE[0]}")

SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
VENV_PATH="${SCRIPT_DIR}/${VENV_NAME}"

# Check if the user has at least python3.8 (It will reach end-of-life at the end of 2024)
REQUIRED_VERSION="3.8"
INSTALLED_VERSION=$(python3 --version 2>&1 | awk '{print $2}')

if [ "$(printf '%s\n' "$REQUIRED_VERSION" "$INSTALLED_VERSION" | sort -V | head -n 1)" != "$REQUIRED_VERSION" ]; then
echo "At least python${REQUIRED_VERSION} is required, you have python${INSTALLED_VERSION}"
exit 1
fi

# Check if the script is being sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Error: This script must be sourced, not executed."
echo "Please run: source ${BASH_SOURCE[0]}"
exit 2
fi

# Create venv if it doesn't exist
if ! [ -f "$VENV_PATH" ]; then
python3 -m venv "$VENV_PATH"
fi

# shellcheck disable=SC1091
source "${VENV_PATH}/bin/activate"

# Update existing or install missing packages
python3 -m pip install --upgrade pip
python3 -m pip install --requirement "${SCRIPT_DIR}/${PYTHON_REQUIREMENTS}"
Loading