Skip to content

Commit e752e28

Browse files
makefile support for release to PyPI (#22)
1 parent 2e75851 commit e752e28

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed

Makefile

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,79 @@ test: ## run tests
2121
.PHONY: all
2222
all: clean install lint test ## run all commands
2323

24-
.PHONY: build
25-
build: ## build package
26-
poetry build
27-
twine check --strict dist/*
28-
2924
.PHONY: examples
3025
examples: ## run all examples
3126
find ./examples -maxdepth 1 -type f -name "*.ipynb" -print -execdir jupyter nbconvert --to script {} \;
3227
find ./examples -maxdepth 1 -type f -name "*.py" -print -execdir python {} \;
28+
29+
# Targets for Release Workflow/Automation
30+
.PHONY: release-pypi bump-version update-vars-version build confirm-upload upload clean-dist docs
31+
32+
release-pypi: clean-dist build upload ## release pypi: build, check and upload to pypi
33+
34+
# Default files to update
35+
PYPROJECT_TOML = pyproject.toml
36+
INIT_FILE = src/mostlyai/qa/__init__.py
37+
38+
# Default bump type
39+
BUMP_TYPE ?= PATCH
40+
CURRENT_VERSION := $(shell grep -m 1 'version = ' $(PYPROJECT_TOML) | sed -e 's/version = "\(.*\)"/\1/')
41+
# Assuming current_version is already set from pyproject.toml
42+
NEW_VERSION := $(shell echo $(CURRENT_VERSION) | awk -F. -v bump=$(BUMP_TYPE) '{ \
43+
if (bump == "PATCH") { \
44+
printf("%d.%d.%d", $$1, $$2, $$3 + 1); \
45+
} else if (bump == "MINOR") { \
46+
printf("%d.%d.0", $$1, $$2 + 1); \
47+
} else if (bump == "MAJOR") { \
48+
printf("%d.0.0", $$1 + 1); \
49+
} else { \
50+
print "Error: Invalid BUMP_TYPE=" bump; \
51+
exit 1; \
52+
} \
53+
}')
54+
55+
# Rule to bump the version
56+
bump-version: ## bump the version in pyproject.toml and __init__.py
57+
@echo "Bumping $(BUMP_TYPE) version from $(CURRENT_VERSION) to $(NEW_VERSION)"
58+
@echo "Replaces $(CURRENT_VERSION) to $(NEW_VERSION) in $(PYPROJECT_TOML)"
59+
@echo "Replaces $(CURRENT_VERSION) to $(NEW_VERSION) in $(INIT_FILE)"
60+
@echo "Current directory: $(shell pwd)"
61+
# Check if current version was found
62+
@if [ -z "$(CURRENT_VERSION)" ]; then \
63+
echo "Error: Could not find current version in $(PYPROJECT_TOML)"; \
64+
exit 1; \
65+
fi
66+
# Replace the version in pyproject.toml
67+
@if [[ "$(shell uname -s)" == "Darwin" ]]; then \
68+
sed -i '' 's/version = "$(CURRENT_VERSION)"/version = "$(NEW_VERSION)"/g' $(PYPROJECT_TOML); \
69+
sed -i '' 's/__version__ = "$(CURRENT_VERSION)"/__version__ = "$(NEW_VERSION)"/g' $(INIT_FILE); \
70+
else \
71+
sed -i 's/version = "$(CURRENT_VERSION)"/version = "$(NEW_VERSION)"/g' $(PYPROJECT_TOML); \
72+
sed -i 's/__version__ = "$(CURRENT_VERSION)"/__version__ = "$(NEW_VERSION)"/g' $(INIT_FILE); \
73+
fi
74+
@VERSION=$$(poetry version -s)
75+
@echo "Now we have version $(VERSION) in $(PYPROJECT_TOML) and $(INIT_FILE)."
76+
77+
update-vars-version: ## update the required variables after bump
78+
$(eval VERSION := $(shell poetry version -s))
79+
$(eval BRANCH := verbump_$(shell echo $(VERSION) | tr '.' '_'))
80+
$(eval TAG := $(VERSION))
81+
@echo "Updated VERSION to $(VERSION), BRANCH to $(BRANCH), TAG to $(TAG)"
82+
83+
build: ## build package
84+
@echo "Step: building package"
85+
poetry build
86+
twine check --strict dist/*
87+
88+
confirm-upload: ## confirm before the irreversible zone
89+
@echo "Are you sure you want to upload to PyPI? (yes/no)"
90+
@read ans && [ $${ans:-no} = yes ]
91+
92+
upload: confirm-upload ## upload to PyPI (ensure the token is present in .pypirc file before running upload)
93+
@twine upload dist/*$(VERSION)* --verbose
94+
@echo "Uploaded version $(VERSION) to PyPI"
95+
96+
clean-dist: ## remove "volatile" directory dist
97+
@echo "Step: cleaning dist directory"
98+
@rm -rf dist
99+
@echo "Cleaned up dist directory"

0 commit comments

Comments
 (0)