Skip to content

Commit

Permalink
feat: add support for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
apirogov committed Feb 22, 2024
1 parent 0d8d97c commit 6d13c6b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
11 changes: 10 additions & 1 deletion .somesy.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) <small>(2024-02-13)</small> { id="0.2.1" }
## [v0.3.0](https://github.com/Materials-Data-Science-and-Informatics/fair-python-cookiecutter/tree/v0.3.0) <small>(2024-02-27)</small> { 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

Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <a.pirogov@fz-juelich.de>"]
maintainers = ["Anton Pirogov <a.pirogov@fz-juelich.de>"]
Expand Down
17 changes: 15 additions & 2 deletions src/fair_python_cookiecutter/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
29 changes: 10 additions & 19 deletions ...cutter/template/hooks/post_gen_project.sh → ...ookiecutter/template/post_gen_project.bat
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
#!/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
# ----------------------------
:<<"::CMDLITERAL"
:COMMON
::CMDLITERAL

: #All following code must be hybrid (work for bash and cmd.exe)
: # ------------------------------------------------------------

Expand All @@ -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 .
Expand All @@ -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
14 changes: 14 additions & 0 deletions src/fair_python_cookiecutter/template/post_gen_project.sh
Original file line number Diff line number Diff line change
@@ -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 :) <--------"

0 comments on commit 6d13c6b

Please sign in to comment.