Skip to content

Commit

Permalink
fix build error and add enhancements (#2)
Browse files Browse the repository at this point in the history
* fix build error and add "Update README and LaTeX Build File" workflow

Test commit

Update README and LaTeX build configuration with correct repository name

Update README.md

Test commit

Update README and LaTeX build configuration with correct repository name

update .gitignore

* add image_dirs to CMakeLists.txt

* Update ci.yml

add main branch to ci.yml

add main branch to ci.yml

Update README.md and tests

Revert "Update README and LaTeX build configuration with correct repository name"

This reverts commit b8d6caf.

Update README and LaTeX build configuration with correct repository name

Update update.yml

Update README.md

Update README and LaTeX build configuration with correct repository name

Update update.yml

Update README and LaTeX build configuration with correct repository name

Update update.yml

no additional '/workflows/CI' at the end

Update update.yml to change project name in CMakeLists.txt

Update update.yml to change project name in CMakeLists.txt

Update README and LaTeX build configuration with correct repository name

Update update.yml

Update update.yml

Update update.yml

Update README and LaTeX build configuration with correct repository name

Update README and LaTeX build configuration with correct repository name

Update update.yml

Update README.md

Update README and LaTeX build configuration with correct repository name

Update update.yml

Update README and LaTeX build configuration with correct repository name

Update update.yml

no additional '/workflows/CI' at the end

* add issues url

add issues url

add condition to check for setissueslinkurl cmd

* change checkout version to v4

* Update README and LaTeX build configuration with correct repository name

Revert "Update README and LaTeX build configuration with correct repository name"

This reverts commit 87b70c8.

* Update .github/workflows/ci.yml

Co-authored-by: Markus Hofbauer <[email protected]>

* Update CMakeLists.txt

Co-authored-by: Markus Hofbauer <[email protected]>

* Update README.md

Co-authored-by: Markus Hofbauer <[email protected]>

* Update write-gitid.sh

Co-authored-by: Markus Hofbauer <[email protected]>

* replace sed with python in update.yml

* remove fetch-all in ci.yml

* Update README and LaTeX build configuration with correct repository name

* Revert "Update README and LaTeX build configuration with correct repository name"

This reverts commit 139531a.

* update language declaration in CheatsheetTemplate.tex

* update python script

* Test new workflow

* Test new workflow 2

* Update README and LaTeX build configuration with correct repository name

* Revert "Update README and LaTeX build configuration with correct repository name"

This reverts commit f7a8dea.

* Revert "Test new workflow 2"

This reverts commit 8776885.

* Revert "Test new workflow"

This reverts commit 49c6219.

* Update scripts/update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* simplify update_files.py

* Test commit

* Test commit 2

* fix base_path in update_files.py

* Update README and LaTeX build configuration with correct repository name

* Revert "Test commit 2"

This reverts commit a65e3e4.

* Revert Test commit

* create PR instead of direct push in update.yml

* add git config back to update.yml

* fix typo in update.yml

* fix typo in update.yml

* update the README.md with PR info

* hide .py from language detector

* specify python version in update.yml

* Update .github/workflows/update.yml

Co-authored-by: Markus Hofbauer <[email protected]>

* change header update and remove unnecessary comments in update_files.py

* add test_update_files.py

* add test_update_files workflow

* fix unit tests

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* Update scripts/tests/test_update_files.py

Co-authored-by: Markus Hofbauer <[email protected]>

* use multi-line strings in test_update_files.py and add requirements.txt

* add unit test to ci.yml

* fix multi-line errors

* remove hours and minutes from write-gitid.sh

* remove misplaced "\" in write-gitid.sh

* Delete .gitattributes

---------

Co-authored-by: dariusptrs <[email protected]>
Co-authored-by: Markus Hofbauer <[email protected]>
  • Loading branch information
3 people authored Apr 1, 2024
1 parent cb367ba commit 91d709d
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 11 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/inhaltlicher_fehler.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: Inhaltlicher Fehler
description: Hilf uns Fehler zu Verbessern
title: ''
labels: [bug]
assignees: []

Expand Down
30 changes: 25 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,54 @@ name: CI

on:
push:
branches: [ master ]
branches: [ master, main ]
pull_request:
branches: [ master ]

branches: [ master, main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12.2'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r scripts/requirements.txt
- name: Run pytest
run: pytest

build:
runs-on: ubuntu-latest
container: makeappdev/uselatex:latest
steps:
- uses: actions/checkout@v4

- name: Configure Git safe directory with GITHUB_WORKSPACE
run: git config --global --add safe.directory $GITHUB_WORKSPACE

- name: Build
run: |
cmake --version; pdflatex --version
mkdir -p build && cd build
cmake ..
make
- name: Prepare Deployment
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
run: |
mkdir -p export
echo "# This branch is for deployment only" >> export/README.md
cp build/*.pdf export
cp build/git.id export
- name: Deploy
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
uses: JamesIves/[email protected]
with:
branch: gh-pages
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Update README and LaTeX Build File

on:
workflow_dispatch:

jobs:
update-and-create-pr:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12.2'

- name: Run update script
run: python scripts/update_files.py

- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Commit changes to a new branch
run: |
git checkout -b changes/${{ github.run_id }}
git add .
git commit -m "Apply automated updates"
git push -u origin changes/${{ github.run_id }}
- name: Create Pull Request
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr create --base main --head changes/${{ github.run_id }} --title "Specify project URLs and CMake project" --body "Please review the changes applied by the automated script."
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
build/

CheatsheetTemplate.pdf
/*.pdf

## Core latex/pdflatex auxiliary files:
*.aux
Expand Down Expand Up @@ -111,4 +111,6 @@ sympy-plots-for-*.tex/

# xindy
*.xdy
StochastischeSignale.pdf
git.id
*.DS_Store
*.pyc
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ add_custom_target(
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

set(IMAGE_DIRS
img
# Add subdirectories here
# img/logic
# img/rca
)

add_latex_document(
CheatsheetTemplate.tex
FORCE_PDF
IMAGE_DIRS img
IMAGE_DIRS ${IMAGE_DIRS}
DEPENDS writegitid
)
7 changes: 6 additions & 1 deletion CheatsheetTemplate.tex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
% Document Settings
% ======================================================================

% possible options: color/nocolor, english/german, threecolumn
% possible options: color/nocolor, english/ngerman, threecolumn
% defaults: color, english
\documentclass[english]{latex4ei/latex4ei_sheet}

Expand All @@ -29,6 +29,11 @@

\IfFileExists{git.id}{\input{git.id}}{}
\ifdefined\GitRevision\mydate{\GitNiceDate\ (git \GitRevision)}\fi
\ifdefined\GitIssuesURL
\ifdefined\setissueslinkurl
\setissueslinkurl{\GitIssuesURL} % Set the actual URL
\fi
\fi

% Title
% ----------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ cd build
cmake ..
make
```
## Disclaimer

After changing the repository name or transfering ownership, run the "Update README and LaTeX Build" workflow from the Actions tab and merge the generated pull request.
Make sure the `.tex` file has the same name as the repository in order for it to build correctly!
Empty file added scripts/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyfakefs==5.3.5
pytest==8.1.1
Empty file added scripts/tests/__init__.py
Empty file.
75 changes: 75 additions & 0 deletions scripts/tests/test_update_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import pytest
from scripts.update_files import check_tex_file_exists, update_readme_content, update_cmake_content
from pathlib import Path
import os

@pytest.fixture
def fake_base_path(fs):
"""Fixture to create a base path and mock environment variable."""
base_path = Path("/test/project")
fs.create_dir(base_path)
os.environ['GITHUB_REPOSITORY'] = 'user/correct_title'
return base_path

def test_check_tex_file_exists__file_does_not_exist__should_raise(fake_base_path, fs):
repo_name = "correct_title"
fs.create_file(fake_base_path / f"{repo_name}.tex")
check_tex_file_exists(repo_name, fake_base_path)

with pytest.raises(FileNotFoundError):
check_tex_file_exists("nonexistent_repo", Path(fake_base_path))

def test_update_readme_content(fake_base_path, fs):
initial_readme_content = """
# WrongTitle
Some content in the README file.
[![Actions Status](https://github.com/latex4ei/WrongCheatsheetTemplate/workflows/CI/badge.svg)](https://github.com/latex4ei/WrongCheatsheetTemplate)
## Section
More details here.
""".strip()
fs.create_file(fake_base_path / "README.md", contents=initial_readme_content)
repo_name = "correct_title"
github_repository = 'user/correct_title'

updated_content = update_readme_content(initial_readme_content, repo_name, github_repository)

expected_content = """
# correct_title
Some content in the README file.
[![Actions Status](https://github.com/user/correct_title/workflows/CI/badge.svg)](https://github.com/user/correct_title)
## Section
More details here.
""".strip()
assert updated_content == expected_content, "README.md content was not updated correctly."

def test_update_cmake_content(fake_base_path, fs):
initial_cmake_content = """
cmake_minimum_required(VERSION 3.12)
project(WrongProjectName NONE)
# Add the main LaTeX document
add_latex_document(
CheatsheetTemplate.tex
AnotherDoc.tex
FORCE_PDF
IMAGE_DIRS img
DEPENDS writegitid
)
""".strip()
fs.create_file(fake_base_path / "CMakeLists.txt", contents=initial_cmake_content)
repo_name = "correct_title"

updated_content = update_cmake_content(initial_cmake_content, repo_name)

expected_content = """
cmake_minimum_required(VERSION 3.12)
project(correct_title NONE)
# Add the main LaTeX document
add_latex_document(
correct_title.tex
AnotherDoc.tex
FORCE_PDF
IMAGE_DIRS img
DEPENDS writegitid
)
""".strip()
assert updated_content == expected_content, "CMakeLists.txt content was not updated correctly."
42 changes: 42 additions & 0 deletions scripts/update_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pathlib import Path
import re
import os
import sys

def check_tex_file_exists(repo_name: str, base_path: Path):
"""Check if a .tex file with the same name as the repository exists."""
tex_file_path = base_path / f"{repo_name}.tex"
if not tex_file_path.exists():
msg = f"No .tex file found with the name {tex_file_path.name}. File name must match repo name."
raise FileNotFoundError(msg)

def update_readme_content(content: str, repo_name: str, github_repository: str) -> str:
"""Updates the README.md content."""
content = re.sub(r'^# .+', f'# {repo_name}', content, count=1, flags=re.MULTILINE)
actions_status_line = f'[![Actions Status](https://github.com/{github_repository}/workflows/CI/badge.svg)](https://github.com/{github_repository})'
return re.sub(r'\[!\[Actions Status\].*', actions_status_line, content)

def update_cmake_content(content: str, repo_name: str) -> str:
"""Updates the CMakeLists.txt content."""
content = re.sub(r'project\(([^ ]*)', f'project({repo_name}', content, count=1)
return re.sub(r'^( *)[^\s#].*?\.tex', r'\1' + f'{repo_name}.tex', content, flags=re.MULTILINE, count=1)

def main(github_repository: str):
repo_name = Path(github_repository).name
base_path = Path(__file__).parent.parent

check_tex_file_exists(repo_name, base_path)

readme_path = base_path / 'README.md'
cmake_path = base_path / 'CMakeLists.txt'

readme_content = readme_path.read_text()
updated_readme_content = update_readme_content(readme_content, repo_name, github_repository)
readme_path.write_text(updated_readme_content)

cmake_content = cmake_path.read_text()
updated_cmake_content = update_cmake_content(cmake_content, repo_name)
cmake_path.write_text(updated_cmake_content)

if __name__ == "__main__":
main(os.getenv('GITHUB_REPOSITORY'))
11 changes: 10 additions & 1 deletion write-gitid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ fi

GITREV=$(git rev-list HEAD --count)
echo "\\def\\GitRevision{$GITREV}"> git.id
git show -s --format=%ci | xargs -I var_d $DATEBIN -d "var_d" +\\def\\GitNiceDate{%-d.\ %B\ %Y\ um\ %H:%M\ Uhr} >> git.id
git show -s --format=%ci | xargs -I var_d $DATEBIN -d "var_d" +\\def\\GitNiceDate{%-d.\ %B\ %Y} >> git.id

# Get the repository's remote URL
REPO_URL=$(git remote get-url origin)

# Convert SSH URL to HTTPS URL and append /issues for the issues page
# This also ensures it works for URLs ending with .git and without
ISSUES_URL=$(echo $REPO_URL | sed -e 's/^git@github\.com:/https:\/\/github\.com\//' -e 's/\.git$//' -e 's/$/\/issues/')

echo "\\def\\GitIssuesURL{$ISSUES_URL}" >> git.id

0 comments on commit 91d709d

Please sign in to comment.