From 6d13c6b9bf65478dda3899ac8459648466265765 Mon Sep 17 00:00:00 2001 From: "a.pirogov" Date: Thu, 22 Feb 2024 10:40:26 +0100 Subject: [PATCH] feat: add support for windows --- .github/workflows/ci.yml | 2 +- .somesy.toml | 11 ++++++- CHANGELOG.md | 3 +- CITATION.cff | 2 +- codemeta.json | 9 +++++- pyproject.toml | 2 +- src/fair_python_cookiecutter/main.py | 17 +++++++++-- ...st_gen_project.sh => post_gen_project.bat} | 29 +++++++------------ .../template/post_gen_project.sh | 14 +++++++++ 9 files changed, 62 insertions(+), 27 deletions(-) rename src/fair_python_cookiecutter/template/{hooks/post_gen_project.sh => post_gen_project.bat} (63%) mode change 100755 => 100644 create mode 100644 src/fair_python_cookiecutter/template/post_gen_project.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6cdeed..97d1d4a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,7 +72,7 @@ jobs: strategy: fail-fast: true matrix: - os: [ "ubuntu-latest", "macos-latest" ] + os: [ "ubuntu-latest", "macos-latest", "windows-latest" ] python-version: [ "3.8", "3.9", "3.10", "3.11" ] runs-on: ${{ matrix.os }} diff --git a/.somesy.toml b/.somesy.toml index e897b8b..dcb6004 100644 --- a/.somesy.toml +++ b/.somesy.toml @@ -1,6 +1,6 @@ [project] name = "fair-python-cookiecutter" -version = "0.2.0" +version = "0.3.0" description = "An opinionated cookiecutter template to kickstart a modern best-practice Python project with FAIR metadata. " license = "MIT" @@ -25,5 +25,14 @@ orcid = "https://orcid.org/0000-0002-5077-7497" author = true maintainer = true +[[project.people]] +family-names = "Soylu" +given-names = "Mustafa" +email = "m.soylu@fz-juelich.de" +orcid = "https://orcid.org/0000-0003-2637-0432" + +contribution = "Supported implementation of Windows support and testing the template" +contribution_types = ["code", "test"] + [config] verbose = true diff --git a/CHANGELOG.md b/CHANGELOG.md index 666e738..baf6723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,9 @@ Here we provide notes that summarize the most important changes in each released Please consult the changelog to inform yourself about breaking changes and security issues. -## [v0.2.1](https://github.com/Materials-Data-Science-and-Informatics/fair-python-cookiecutter/tree/v0.2.1) (2024-02-13) { id="0.2.1" } +## [v0.3.0](https://github.com/Materials-Data-Science-and-Informatics/fair-python-cookiecutter/tree/v0.3.0) (2024-02-27) { id="0.3.0" } +* added support for Windows * fixed a bug where instantiation of template fails if no repository URL is provided * improve usability and description of some CLI fields diff --git a/CITATION.cff b/CITATION.cff index 51449ff..3d80a1f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -3,7 +3,7 @@ type: software message: If you use this software, please cite it using this metadata. title: "fair-python-cookiecutter" -version: "0.2.0" +version: "0.3.0" abstract: "An opinionated cookiecutter template to kickstart a modern best-practice Python project with FAIR metadata." repository-code: "https://github.com/Materials-Data-Science-and-Informatics/fair-python-cookiecutter" diff --git a/codemeta.json b/codemeta.json index 7a31086..0692b52 100644 --- a/codemeta.json +++ b/codemeta.json @@ -18,7 +18,7 @@ ], "name": "fair-python-cookiecutter", "description": "An opinionated cookiecutter template to kickstart a modern best-practice Python project with FAIR metadata.", - "version": "0.2.0", + "version": "0.3.0", "keywords": [ "fair", "metadata", @@ -47,6 +47,13 @@ "familyName": "Pirogov", "email": "a.pirogov@fz-juelich.de", "@id": "https://orcid.org/0000-0002-5077-7497" + }, + { + "@type": "Person", + "givenName": "Mustafa", + "familyName": "Soylu", + "email": "m.soylu@fz-juelich.de", + "@id": "https://orcid.org/0000-0003-2637-0432" } ], "url": "https://materials-data-science-and-informatics.github.io/fair-python-cookiecutter" diff --git a/pyproject.toml b/pyproject.toml index e6130a1..aa85da7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] # ---- managed by somesy, see .somesy.toml ---- name = "fair-python-cookiecutter" -version = "0.2.0" +version = "0.3.0" description = "An opinionated cookiecutter template to kickstart a modern best-practice Python project with FAIR metadata." authors = ["Anton Pirogov "] maintainers = ["Anton Pirogov "] diff --git a/src/fair_python_cookiecutter/main.py b/src/fair_python_cookiecutter/main.py index 8bca01c..cc3d136 100644 --- a/src/fair_python_cookiecutter/main.py +++ b/src/fair_python_cookiecutter/main.py @@ -1,4 +1,5 @@ """Main functions for controlling the template creation.""" +import os from pathlib import Path from shutil import which from typing import Any, Dict @@ -80,11 +81,23 @@ def download_licenses( license.write_text((proj_root / "LICENSES" / f"{license_name}.txt").read_text()) -def finalize_repository(proj_root: Path, conf: CookiecutterConfig): +def init_git_repo(tmp_root: Path, proj_root: Path): + # NOTE: script is OS-agnostic, .bat extension is for windows, bash does not care + post_gen_script = tmp_root / "post_gen_project.bat" + # rewrite newlines correctly for the OS + post_gen_script.write_text(post_gen_script.read_text(), encoding="utf-8") + if os.name != "nt": + run_cmd(f"bash {post_gen_script}", cwd=proj_root) + else: + run_cmd(f"{post_gen_script}", cwd=proj_root) + + +def finalize_repository(tmp_root: Path, proj_root: Path, conf: CookiecutterConfig): """Finalize instantiated repository based on configuration.""" create_gl_issue_template_from_gh(proj_root) remove_unneeded_code(proj_root, conf) download_licenses(proj_root, conf) + init_git_repo(tmp_root, proj_root) def create_repository( @@ -110,6 +123,6 @@ def create_repository( **cc_args, ) repo_dir = output_dir / cc_json.project_slug - finalize_repository(repo_dir, conf) + finalize_repository(tmp_root, repo_dir, conf) return repo_dir diff --git a/src/fair_python_cookiecutter/template/hooks/post_gen_project.sh b/src/fair_python_cookiecutter/template/post_gen_project.bat old mode 100755 new mode 100644 similarity index 63% rename from src/fair_python_cookiecutter/template/hooks/post_gen_project.sh rename to src/fair_python_cookiecutter/template/post_gen_project.bat index 1b684af..aacbfb1 --- a/src/fair_python_cookiecutter/template/hooks/post_gen_project.sh +++ b/src/fair_python_cookiecutter/template/post_gen_project.bat @@ -1,16 +1,21 @@ -#!/usr/bin/env bash : # Magic to deactivate current Python venv (if one is enabled) in a cross-platform way : # See https://stackoverflow.com/questions/17510688/single-script-to-run-in-both-windows-batch-and-linux-bash :<<"::CMDLITERAL" : ---- code for cmd.exe ---- -ECHO "TODO: cmd.exe code" +for /f "delims=" %%a in ('python -c "import sys; print(sys.prefix if sys.base_prefix != sys.prefix else '')"') do set "VENV_PATH=%%a" +IF NOT "%VENV_PATH%" == "" ( + echo INFO: Deactivating currently active virtual environment "%VENV_PATH%" + REM Assuming the virtual environment needs to be activated first to provide the deactivate script + call "%VENV_PATH%\Scripts\activate.bat" + call "%VENV_PATH%\Scripts\deactivate.bat" +) : ---------------------------- GOTO :COMMON ::CMDLITERAL # ---- bash-specific code ---- venv=$(python -c "import sys; print(sys.prefix if sys.base_prefix != sys.prefix else '')") if [[ -n "$venv" ]]; then - echo Deactivating currently active virtual environment "$venv" ... + echo INFO: Deactivating currently active virtual environment "$venv" source "$venv/bin/activate" # make sure we have 'deactivate' available deactivate fi @@ -18,6 +23,7 @@ fi :<<"::CMDLITERAL" :COMMON ::CMDLITERAL + : #All following code must be hybrid (work for bash and cmd.exe) : # ------------------------------------------------------------ @@ -27,10 +33,6 @@ git init poetry install --with docs poetry run poe init-dev -echo "Downloading required license texts ..." -poetry run pip install pipx -poetry run pipx run reuse download --all - echo "Creating CITATION.cff and codemeta.json using somesy ..." git add . @@ -39,21 +41,10 @@ git add . echo "Creating first commit ..." -poetry run git commit \ - -m "generated project using fair-python-cookiecutter {{ cookiecutter._fpc_version }}" \ - -m "https://github.com/Materials-Data-Science-and-Informatics/fair-python-cookiecutter" +poetry run git commit -m "generated project using fair-python-cookiecutter" -m "https://github.com/Materials-Data-Science-and-Informatics/fair-python-cookiecutter" echo "Ensuring that the default branch is called 'main' ..." git branch -M main echo "--------> All done! Your project repository is ready :) <--------" - - -: # TODO: only do following in test mode - -: # sanity-check that main tasks all work -: # poetry install --with docs -: # poetry run poe lint --all-files -: # poetry run poe test -: # poetry run poe docs diff --git a/src/fair_python_cookiecutter/template/post_gen_project.sh b/src/fair_python_cookiecutter/template/post_gen_project.sh new file mode 100644 index 0000000..5bec597 --- /dev/null +++ b/src/fair_python_cookiecutter/template/post_gen_project.sh @@ -0,0 +1,14 @@ +: # Magic to deactivate current Python venv (if one is enabled) in a cross-platform way +: # See https://stackoverflow.com/questions/17510688/single-script-to-run-in-both-windows-batch-and-linux-bash +# ---- bash-specific code ---- +# venv=$(python -c "import sys; print(sys.prefix if sys.base_prefix != sys.prefix else '')") +if [[ -n "$venv" ]]; then + echo INFO: Deactivating currently active virtual environment "$venv" + source "$venv/bin/activate" # make sure we have 'deactivate' available + deactivate +fi +# ---------------------------- + + +echo "--------> All done! Your project repository is ready :) <--------" +