Skip to content

Commit cce170f

Browse files
committed
adding more tests, refactoring
1 parent 45b0b14 commit cce170f

22 files changed

Lines changed: 1327 additions & 475 deletions

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 130

.github/workflows/cd.yml

Lines changed: 102 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# .github/workflows/cd.yml
12
name: TreeMapper CD
23

34
permissions:
@@ -7,7 +8,7 @@ on:
78
workflow_dispatch:
89
inputs:
910
version:
10-
description: 'Version to release'
11+
description: 'Version to release (e.g., 1.0.0)'
1112
required: true
1213
publish_to_pypi:
1314
description: 'Publish to PyPI'
@@ -19,191 +20,189 @@ on:
1920
- 'false'
2021

2122
jobs:
22-
create-release:
23-
name: Create GitHub Release
23+
prepare-release:
24+
name: Prepare Commit and Create GitHub Release/Tag
2425
runs-on: ubuntu-latest
26+
outputs:
27+
version: ${{ steps.set_outputs.outputs.version }}
28+
tag_name: ${{ steps.set_outputs.outputs.tag_name }}
29+
commit_sha: ${{ steps.commit_push.outputs.commit_sha }}
30+
upload_url: ${{ steps.create_release.outputs.upload_url }}
31+
release_id: ${{ steps.create_release.outputs.id }}
2532
steps:
2633
- name: Checkout Code
27-
uses: actions/checkout@v3
34+
uses: actions/checkout@v4
2835

2936
- name: Set up Python
30-
uses: actions/setup-python@v4
37+
uses: actions/setup-python@v5
3138
with:
3239
python-version: '3.11'
3340

34-
- name: Install Dependencies
35-
run: |
36-
python -m pip install --upgrade pip
37-
pip install -r requirements.txt
38-
pip install -e .
39-
40-
- name: Set version (Unix)
41-
if: matrix.os != 'windows-latest'
41+
- name: Set version in version.py
4242
run: |
43-
python -c "import re; content = open('src/treemapper/version.py', 'r').read(); open('src/treemapper/version.py', 'w').write(re.sub('__version__ = \".*\"', '__version__ = \"${{ github.event.inputs.version }}\"', content))"
44-
45-
- name: Set version (Windows)
46-
if: matrix.os == 'windows-latest'
47-
shell: pwsh
43+
VERSION="${{ github.event.inputs.version }}"
44+
echo "Setting version to $VERSION"
45+
sed -i -E "s/__version__ = \".*\"/__version__ = \"$VERSION\"/" src/treemapper/version.py
46+
echo "version.py content after change:"
47+
cat src/treemapper/version.py
48+
49+
- name: Commit and Push version bump
50+
id: commit_push
4851
run: |
49-
$content = Get-Content src/treemapper/version.py -Raw
50-
$newContent = $content -replace '__version__ = ".*"', '__version__ = "${{ github.event.inputs.version }}"'
51-
Set-Content src/treemapper/version.py -Value $newContent
52-
53-
- name: Create Release
52+
git config user.name github-actions[bot]
53+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
54+
git add src/treemapper/version.py
55+
if ! git diff --staged --quiet; then
56+
git commit -m "Release version ${{ github.event.inputs.version }}"
57+
else
58+
echo "No changes to commit."
59+
fi
60+
COMMIT_SHA=$(git rev-parse HEAD)
61+
echo "Commit SHA: $COMMIT_SHA"
62+
echo "commit_sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
63+
# Отправляем коммит в текущую ветку (важно для следующего шага)
64+
# Тег будет создан действием create-release
65+
git push origin HEAD
66+
67+
- name: Create GitHub Release and Tag
5468
id: create_release
5569
uses: actions/create-release@v1
5670
env:
5771
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5872
with:
5973
tag_name: v${{ github.event.inputs.version }}
6074
release_name: Release ${{ github.event.inputs.version }}
75+
commitish: ${{ steps.commit_push.outputs.commit_sha }} # Указываем SHA коммита
6176
draft: false
6277
prerelease: false
6378

64-
outputs:
65-
upload_url: ${{ steps.create_release.outputs.upload_url }}
66-
version: ${{ github.event.inputs.version }}
67-
release_id: ${{ steps.create_release.outputs.id }}
79+
- name: Set outputs for subsequent jobs
80+
id: set_outputs
81+
run: |
82+
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
83+
echo "tag_name=v${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
6884
6985
build-and-upload:
7086
name: Build and Upload Assets
71-
needs: create-release
72-
runs-on: ${{ matrix.os }}
87+
needs: prepare-release
7388
strategy:
89+
fail-fast: false
7490
matrix:
7591
include:
76-
- os: ubuntu-20.04
92+
- os: ubuntu-latest
7793
asset_name: linux
94+
python-version: '3.11'
7895
- os: macos-latest
7996
asset_name: macos
97+
python-version: '3.11'
8098
- os: windows-latest
8199
asset_name: windows
100+
python-version: '3.11'
101+
runs-on: ${{ matrix.os }}
82102
steps:
83-
- name: Checkout Code
84-
uses: actions/checkout@v3
103+
- name: Checkout Code at release tag
104+
uses: actions/checkout@v4
105+
with:
106+
ref: ${{ needs.prepare-release.outputs.tag_name }}
85107

86108
- name: Set up Python
87-
uses: actions/setup-python@v4
109+
uses: actions/setup-python@v5
88110
with:
89-
python-version: '3.11'
111+
python-version: ${{ matrix.python-version }}
90112

91113
- name: Cache pip Dependencies
92-
uses: actions/cache@v3
114+
uses: actions/cache@v4
115+
id: cache-pip
93116
with:
94117
path: ~/.cache/pip
95-
key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.cfg') }}
118+
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.cfg') }}
96119
restore-keys: |
120+
${{ runner.os }}-pip-${{ matrix.python-version }}-
97121
${{ runner.os }}-pip-
98122
99-
- name: Install Dependencies
123+
- name: Install Dependencies (including PyInstaller)
100124
run: |
101125
python -m pip install --upgrade pip
102-
pip install -r requirements.txt
103-
pip install -e .
104-
105-
- name: Set version (Unix)
106-
if: matrix.os != 'windows-latest'
107-
run: |
108-
python -c "import re; content = open('src/treemapper/version.py', 'r').read(); open('src/treemapper/version.py', 'w').write(re.sub('__version__ = \".*\"', '__version__ = \"${{ github.event.inputs.version }}\"', content))"
109-
110-
- name: Set version (Windows)
111-
if: matrix.os == 'windows-latest'
112-
shell: pwsh
113-
run: |
114-
$content = Get-Content src/treemapper/version.py -Raw
115-
$newContent = $content -replace '__version__ = ".*"', '__version__ = "${{ github.event.inputs.version }}"'
116-
Set-Content src/treemapper/version.py -Value $newContent
126+
pip install .[dev]
117127
118128
- name: Build with PyInstaller
119129
run: |
120-
python -m PyInstaller --clean -y --dist ./dist/${{ matrix.asset_name }} --workpath /tmp treemapper.spec
130+
python -m PyInstaller --clean -y --dist ./dist/${{ matrix.asset_name }} treemapper.spec
121131
122132
- name: Determine architecture
123133
id: arch
124134
shell: bash
125135
run: |
126-
if [ "${{ matrix.os }}" = "macos-latest" ]; then
127-
echo "ARCH=$(uname -m)" >> $GITHUB_OUTPUT
128-
elif [ "${{ matrix.os }}" = "ubuntu-20.04" ]; then
129-
echo "ARCH=$(uname -m)" >> $GITHUB_OUTPUT
130-
else
131-
echo "ARCH=x86_64" >> $GITHUB_OUTPUT
136+
ARCH=$(uname -m)
137+
if [[ "${{ runner.os }}" == "Windows" ]]; then
138+
if [[ "${{ runner.arch }}" == "X64" ]]; then ARCH="x86_64"; \
139+
elif [[ "${{ runner.arch }}" == "ARM64" ]]; then ARCH="arm64"; \
140+
else ARCH="unknown"; fi
141+
elif [[ "${{ runner.os }}" == "macOS" ]] && [[ "$ARCH" == "arm64" ]]; then
142+
echo "Detected ARM on macOS"
132143
fi
144+
echo "Determined ARCH: $ARCH"
145+
echo "arch=$ARCH" >> $GITHUB_OUTPUT
146+
133147
134148
- name: Upload Release Asset
135149
uses: actions/upload-release-asset@v1
136150
env:
137151
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
138152
with:
139-
upload_url: ${{ needs.create-release.outputs.upload_url }}
140-
asset_path: ./dist/${{ matrix.asset_name }}/treemapper${{ matrix.os == 'windows-latest' && '.exe' || '' }}
141-
asset_name: treemapper-${{ matrix.asset_name }}-${{ steps.arch.outputs.ARCH }}-v${{ needs.create-release.outputs.version }}${{ matrix.os == 'windows-latest' && '.exe' || '' }}
153+
upload_url: ${{ needs.prepare-release.outputs.upload_url }}
154+
# ---> ИЗМЕНЕНИЕ: Убран лишний /treemapper из пути <---
155+
asset_path: ./dist/${{ matrix.asset_name }}/${{ matrix.os == 'windows-latest' && 'treemapper.exe' || 'treemapper' }}
156+
asset_name: treemapper-${{ matrix.asset_name }}-${{ steps.arch.outputs.arch }}-v${{ needs.prepare-release.outputs.version }}${{ matrix.os == 'windows-latest' && '.exe' || '' }}
142157
asset_content_type: application/octet-stream
143158

144159
publish-to-pypi:
145160
name: Publish to PyPI
146-
needs: [ create-release, build-and-upload ]
161+
needs: [prepare-release, build-and-upload]
147162
if: github.event.inputs.publish_to_pypi == 'true'
148163
runs-on: ubuntu-latest
164+
environment:
165+
name: pypi
166+
url: https://pypi.org/p/treemapper
167+
permissions:
168+
id-token: write
149169
steps:
150-
- name: Checkout Code
151-
uses: actions/checkout@v3
170+
- name: Checkout Code at release tag
171+
uses: actions/checkout@v4
172+
with:
173+
ref: ${{ needs.prepare-release.outputs.tag_name }}
152174

153175
- name: Set up Python
154-
uses: actions/setup-python@v4
176+
uses: actions/setup-python@v5
155177
with:
156178
python-version: '3.11'
157179

158-
- name: Cache pip Dependencies
159-
uses: actions/cache@v3
160-
with:
161-
path: ~/.cache/pip
162-
key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.cfg') }}
163-
restore-keys: |
164-
${{ runner.os }}-pip-
165-
166-
- name: Install Dependencies
180+
- name: Install build tools
167181
run: |
168182
python -m pip install --upgrade pip
169-
pip install -r requirements.txt
170-
171-
- name: Set version (Unix)
172-
if: matrix.os != 'windows-latest'
173-
run: |
174-
python -c "import re; content = open('src/treemapper/version.py', 'r').read(); open('src/treemapper/version.py', 'w').write(re.sub('__version__ = \".*\"', '__version__ = \"${{ github.event.inputs.version }}\"', content))"
183+
pip install build
175184
176-
- name: Set version (Windows)
177-
if: matrix.os == 'windows-latest'
178-
shell: pwsh
179-
run: |
180-
$content = Get-Content src/treemapper/version.py -Raw
181-
$newContent = $content -replace '__version__ = ".*"', '__version__ = "${{ github.event.inputs.version }}"'
182-
Set-Content src/treemapper/version.py -Value $newContent
183-
184-
- name: Build and Publish to PyPI
185-
env:
186-
TWINE_USERNAME: __token__
187-
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
188-
run: |
189-
python -m build
190-
python -m twine upload dist/* --verbose
185+
- name: Build sdist and wheel
186+
run: python -m build
191187

188+
- name: Publish package distributions to PyPI
189+
uses: pypa/gh-action-pypi-publish@release/v1
192190

193191
update-main-branch:
194-
name: Update main branch
195-
needs: [ publish-to-pypi ]
192+
name: Update main branch (Merge Tag)
193+
needs: [prepare-release, build-and-upload]
196194
runs-on: ubuntu-latest
197195
steps:
198196
- name: Checkout main branch
199-
uses: actions/checkout@v3
197+
uses: actions/checkout@v4
200198
with:
201199
ref: main
202200
fetch-depth: 0
203201

204202
- name: Merge tag into main
205203
run: |
206-
git config user.name github-actions
207-
git config user.email github-actions@github.com
208-
git merge ${{ github.ref }} --no-ff -m "Merge tag ${{ github.ref_name }} into main"
209-
git push origin main
204+
git config user.name github-actions[bot]
205+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
206+
echo "Attempting to merge tag ${{ needs.prepare-release.outputs.tag_name }} into main"
207+
git merge ${{ needs.prepare-release.outputs.tag_name }} --no-ff -m "Merge tag ${{ needs.prepare-release.outputs.tag_name }} into main"
208+
git push origin main

0 commit comments

Comments
 (0)