Repository: tomaroberts/nii2dcm (language: python)

Issue:
Create PyPI build GHA 
Currently manually push to PyPI. Implement GHA to push to PyPI on release:
- https://github.com/pypa/gh-action-pypi-publish
Create TestPyPI build GHA 
Push to TestPyPI on pull-request?:
- https://github.com/pypa/gh-action-pypi-publish#advanced-release-management


Discussion / hints:

Resolved by #25 

Fix patch:
diff --git a/.github/workflows/publish_pypi.yml b/.github/workflows/publish_pypi.yml
new file mode 100644
index 0000000..6f37c80
--- /dev/null
+++ b/.github/workflows/publish_pypi.yml
@@ -0,0 +1,99 @@
+# Publish to PyPI
+#
+# This workflow publishes nii2dcm on PyPI. The workflow triggers
+# on release. The PyPI release is labelled according to the git tag
+# specified in the GitHub Release user interface. For instance,
+# if the TestPyPI release is v0.1.2.post16, then the GitHub Release
+# tag should be entered as v0.1.3, which will propagate to PyPI.
+# Subsequent developer commits will become v0.1.3.post1, v0.1.3.post2,
+# and so on.
+#
+# This workflow will upload a Python Package using Twine when a release is created. For more information see:
+# https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
+#
+# This workflow uses actions that are not certified by GitHub.
+# They are provided by a third-party and are governed by
+# separate terms of service, privacy policy, and support
+# documentation.
+
+name: Publish package to PyPI
+
+on:
+  release:
+    types: [published]
+
+permissions:
+  contents: read
+  actions: write
+
+jobs:
+  testpypi-publish:
+
+    name: Publish to PyPI
+    runs-on: ubuntu-latest
+    environment:
+      name: pypi
+      url: https://pypi.org/p/nii2dcm
+    permissions:
+      contents: read
+      actions: write
+      id-token: write  # IMPORTANT: this permission is mandatory for PyPI trusted publishing
+
+    steps:
+    - uses: actions/checkout@v3
+      with:
+        fetch-depth: 0
+
+    - name: Set up Python
+      uses: actions/setup-python@v4
+      with:
+        python-version: '3.9'
+
+    - name: Install dependencies
+      run: |
+        python -m pip install --upgrade pip
+        pip install setuptools wheel
+        pip install -r requirements.txt
+
+    - name: Build package
+      run: pip install .
+
+    - name: Display version via dunamai
+      run: |
+        echo "version via dunamai:"
+        dunamai from any
+
+    - name: Display version via nii2dcm CLI
+      run: |
+        echo "version via nii2dcm CLI:"
+        nii2dcm -v
+
+    - name: Create dist/
+      run: |
+        python setup.py sdist bdist_wheel
+        twine check dist/*
+
+    - name: Publish package to PyPI
+      uses: pypa/gh-action-pypi-publish@release/v1
+      with:
+        user: __token__
+        password: ${{ secrets.PYPI_API_TOKEN }}
+        verbose: true
+
+    - name: Wait to allow PyPI to update
+      uses: GuillaumeFalourd/wait-sleep-action@v1
+      with:
+        time: '150'  # seconds
+
+    - name: Install latest PyPI version in fresh venv
+      run: |
+        NII2DCM_VERSION=`echo "$(nii2dcm -v)"`
+        echo $NII2DCM_VERSION
+        python -m venv nii2dcm-temp
+        source nii2dcm-temp/bin/activate
+        pip install --upgrade pip
+        pip install setuptools wheel
+        pip install nii2dcm==$NII2DCM_VERSION
+        nii2dcm -h
+        echo "nii2dcm version:"
+        nii2dcm -v
diff --git a/.gitignore b/.gitignore
index b0f0c11..d7c3b9c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -88,8 +88,5 @@ _im*
 *.nii
 *.nii.gz
 
-# Omit _version.py as required by setuptools_scm
-nii2dcm/_version.py
-
 # Permit Test Data
 !tests/data/DicomMRISVR/t2-svr-atlas-35wk.nii.gz
\ No newline at end of file
diff --git a/README.md b/README.md
index 7c9e671..68a5b73 100644
--- a/README.md
+++ b/README.md
@@ -101,7 +101,7 @@ nii2dcm SVR-output.nii.gz path/to/output/dir/ -d SVR
 <!-- ROADMAP -->
 ## Roadmap
 
-This project is in its infancy! Expect :bug::ant::beetle:
+This project is in its infancy! Expect bugs :bug::ant::beetle:
 
 There are many things I would like to test and implement. 
 [Raise an Issue](https://github.com/tomaroberts/nii2dcm/issues) if you have ideas or suggestions.
diff --git a/nii2dcm/_version.py b/nii2dcm/_version.py
new file mode 100644
index 0000000..b8bebde
--- /dev/null
+++ b/nii2dcm/_version.py
@@ -0,0 +1,2 @@
+from dunamai import Version, Style
+__version__ = Version.from_git().serialize(metadata=False, style=Style.SemVer)
diff --git a/pyproject.toml b/pyproject.toml
deleted file mode 100644
index e06e019..0000000
--- a/pyproject.toml
+++ /dev/null
@@ -1,11 +0,0 @@
-[build-system]
-requires = ["setuptools>=64.0.0", "wheel", "setuptools_scm[toml]>=6.2"]
-build-backend = "setuptools.build_meta"
-
-[project]
-name = "nii2dcm"
-dynamic = ["version"]
-
-[tool.setuptools_scm]
-local_scheme = "no-local-version"  # required for PyPI and TestPyPI
-write_to = "nii2dcm/_version.py"
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index 67f6d5e..91edd24 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,4 +2,5 @@ numpy==1.23.2
 matplotlib==3.6.2
 nibabel==5.0.0
 pydicom==2.3.0
-twine==4.0.2
\ No newline at end of file
+twine==4.0.2
+dunamai==1.18.0
\ No newline at end of file
diff --git a/setup.cfg b/setup.cfg
index 1a38b92..7ad1c06 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = nii2dcm
-version = attr: nii2dcm._version.__version__
+version = attr: nii2dcm.__init__.__version__
 description= nii2dcm: NIfTI to DICOM creation with Python
 url = https://github.com/tomaroberts/nii2dcm
 author = Tom Roberts
@@ -17,6 +17,7 @@ install_requires =
     nibabel==5.0.0
     pydicom==2.3.0
     twine==4.0.2
+    dunamai==1.18.0
 include_package_data=True
 
 [options.entry_points]
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..ce87831
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,7 @@
+from setuptools import setup
+from dunamai import Version, Style
+
+setup(
+    name="nii2dcm",
+    version=Version.from_git().serialize(metadata=False, style=Style.SemVer),
+)
\ No newline at end of file


Test patch:
diff --git a/.github/workflows/build_and_test_cli.yml b/.github/workflows/build_and_test_cli.yml
index d707658..9c29120 100644
--- a/.github/workflows/build_and_test_cli.yml
+++ b/.github/workflows/build_and_test_cli.yml
@@ -17,12 +17,10 @@ jobs:
         os: [ ubuntu-latest ]
         python-version: [ '3.9' ]
 
-    defaults:
-      run:
-        shell: bash -l {0}
-
     steps:
     - uses: actions/checkout@v3
+      with:
+        fetch-depth: 0
 
     - name: Set up Python
       uses: actions/setup-python@v4
@@ -32,6 +30,7 @@ jobs:
     - name: Install Python packages
       run: |
         python -m pip install --upgrade pip
+        pip install setuptools wheel
         pip install flake8 pytest pytest-cov
         if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
 
diff --git a/.github/workflows/publish_testpypi.yml b/.github/workflows/publish_testpypi.yml
index 79b4420..6cafc3e 100644
--- a/.github/workflows/publish_testpypi.yml
+++ b/.github/workflows/publish_testpypi.yml
@@ -1,7 +1,9 @@
 # Publish to TestPyPI
 #
-# This workflow publishes nii2dcm on TestPyPI prior to production release onto PyPI. The workflow is intended to catch
-# any issues arising during the release procedure to prevent unnecessary versioning issues on PyPI
+# This workflow publishes nii2dcm on TestPyPI prior to
+# production release onto PyPI. The workflow is intended to
+# catch any issues arising during the release procedure to
+# prevent unnecessary versioning issues on PyPI.
 #
 # This workflow will upload a Python Package using Twine when a release is created. For more information see:
 # https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
@@ -14,20 +16,24 @@
 name: Publish package to TestPyPI
 
 on:
-  pull_request
+  push:
+    branches:
+      - main
 
 permissions:
   contents: read
+  actions: write
 
 jobs:
   testpypi-publish:
 
-    name: Publish release to TestPyPI
     runs-on: ubuntu-latest
     environment:
       name: testpypi
       url: https://test.pypi.org/p/nii2dcm
     permissions:
+      contents: read
+      actions: write
       id-token: write  # IMPORTANT: this permission is mandatory for PyPI trusted publishing
 
     steps:
@@ -43,18 +49,27 @@ jobs:
     - name: Install dependencies
       run: |
         python -m pip install --upgrade pip
-        pip install build
+        pip install setuptools wheel
+        pip install -r requirements.txt
 
     - name: Build package
-      run: python -m build --sdist --wheel --outdir dist/
+      run: pip install .
 
-    - name: Display nii2dcm version
-      id: nii2dcm-version
+    - name: Display version via dunamai
       run: |
-        pip install -e .
-        echo "nii2dcm version:"
+        echo "version via dunamai:"
+        dunamai from any
+
+    - name: Display version via nii2dcm CLI
+      run: |
+        echo "version via nii2dcm CLI:"
         nii2dcm -v
 
+    - name: Create dist/
+      run: |
+        python setup.py sdist bdist_wheel
+        twine check dist/*
+
     - name: Publish package to TestPyPI
       uses: pypa/gh-action-pypi-publish@release/v1
       with: