Skip to content

Commit

Permalink
makefile support for release to PyPI (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
radurogojanumai authored Nov 28, 2024
1 parent 2e75851 commit e752e28
Showing 1 changed file with 72 additions and 5 deletions.
77 changes: 72 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,79 @@ test: ## run tests
.PHONY: all
all: clean install lint test ## run all commands

.PHONY: build
build: ## build package
poetry build
twine check --strict dist/*

.PHONY: examples
examples: ## run all examples
find ./examples -maxdepth 1 -type f -name "*.ipynb" -print -execdir jupyter nbconvert --to script {} \;
find ./examples -maxdepth 1 -type f -name "*.py" -print -execdir python {} \;

# Targets for Release Workflow/Automation
.PHONY: release-pypi bump-version update-vars-version build confirm-upload upload clean-dist docs

release-pypi: clean-dist build upload ## release pypi: build, check and upload to pypi

# Default files to update
PYPROJECT_TOML = pyproject.toml
INIT_FILE = src/mostlyai/qa/__init__.py

# Default bump type
BUMP_TYPE ?= PATCH
CURRENT_VERSION := $(shell grep -m 1 'version = ' $(PYPROJECT_TOML) | sed -e 's/version = "\(.*\)"/\1/')
# Assuming current_version is already set from pyproject.toml
NEW_VERSION := $(shell echo $(CURRENT_VERSION) | awk -F. -v bump=$(BUMP_TYPE) '{ \
if (bump == "PATCH") { \
printf("%d.%d.%d", $$1, $$2, $$3 + 1); \
} else if (bump == "MINOR") { \
printf("%d.%d.0", $$1, $$2 + 1); \
} else if (bump == "MAJOR") { \
printf("%d.0.0", $$1 + 1); \
} else { \
print "Error: Invalid BUMP_TYPE=" bump; \
exit 1; \
} \
}')

# Rule to bump the version
bump-version: ## bump the version in pyproject.toml and __init__.py
@echo "Bumping $(BUMP_TYPE) version from $(CURRENT_VERSION) to $(NEW_VERSION)"
@echo "Replaces $(CURRENT_VERSION) to $(NEW_VERSION) in $(PYPROJECT_TOML)"
@echo "Replaces $(CURRENT_VERSION) to $(NEW_VERSION) in $(INIT_FILE)"
@echo "Current directory: $(shell pwd)"
# Check if current version was found
@if [ -z "$(CURRENT_VERSION)" ]; then \
echo "Error: Could not find current version in $(PYPROJECT_TOML)"; \
exit 1; \
fi
# Replace the version in pyproject.toml
@if [[ "$(shell uname -s)" == "Darwin" ]]; then \
sed -i '' 's/version = "$(CURRENT_VERSION)"/version = "$(NEW_VERSION)"/g' $(PYPROJECT_TOML); \
sed -i '' 's/__version__ = "$(CURRENT_VERSION)"/__version__ = "$(NEW_VERSION)"/g' $(INIT_FILE); \
else \
sed -i 's/version = "$(CURRENT_VERSION)"/version = "$(NEW_VERSION)"/g' $(PYPROJECT_TOML); \
sed -i 's/__version__ = "$(CURRENT_VERSION)"/__version__ = "$(NEW_VERSION)"/g' $(INIT_FILE); \
fi
@VERSION=$$(poetry version -s)
@echo "Now we have version $(VERSION) in $(PYPROJECT_TOML) and $(INIT_FILE)."

update-vars-version: ## update the required variables after bump
$(eval VERSION := $(shell poetry version -s))
$(eval BRANCH := verbump_$(shell echo $(VERSION) | tr '.' '_'))
$(eval TAG := $(VERSION))
@echo "Updated VERSION to $(VERSION), BRANCH to $(BRANCH), TAG to $(TAG)"

build: ## build package
@echo "Step: building package"
poetry build
twine check --strict dist/*

confirm-upload: ## confirm before the irreversible zone
@echo "Are you sure you want to upload to PyPI? (yes/no)"
@read ans && [ $${ans:-no} = yes ]

upload: confirm-upload ## upload to PyPI (ensure the token is present in .pypirc file before running upload)
@twine upload dist/*$(VERSION)* --verbose
@echo "Uploaded version $(VERSION) to PyPI"

clean-dist: ## remove "volatile" directory dist
@echo "Step: cleaning dist directory"
@rm -rf dist
@echo "Cleaned up dist directory"

0 comments on commit e752e28

Please sign in to comment.