So I have written a Makefile that has a release function that can be run like: make release bump=patch and I want it to be fully executable in the Makefile. The only issue with my current implementation is that there is a lot of redundancy and long lines of commands, which I think can be simplified a lot, although I haven't found any better solutions yet (mainly because of the incrementation). It then tags and pushes.

The required functionality is being able to pass in patch,minor,major and have it overwrite the current file with the new version commit that and push the new tag.

It just has that 'gross' feeling, which I strive to avoid.

My current makefile looks like:

    $(eval v := $(shell git describe --tags --abbrev=0 | sed -E -e 's/^v//' -e 's/(.*)-.*/\1/'))
ifeq ($(bump),patch)
    @echo "$v" | grep -Eo '[0-9]+$$' | tail -n 1 | awk '{print $$1 + 1}' | xargs -I '{}' sed -i '' -E 's/([0-9]+"$$)/{}"/' ._version_.py
else ifeq ($(bump),minor)
    @echo "$v" | grep -Eo '(\.[0-9]+\.)' | awk '{print $$1 + 1}' | xargs -I{} sed -i '' -E 's/(\.[0-9]+\.)/.{}./' ._version_.py
else ifeq ($(bump),major)
	@echo "$v" | grep -Eo '([0-9]+)' | head -n 1 | awk '{print $$1 + 1}' | xargs -I{} sed -i '' -E 's/([0-9]+)/{}/' ._version_.py
    $(eval a := $(shell grep -Eo '([0-9]+\.[0-9]+\.[0-9]+)' ._version_.py))
    @git tag "$a"
    @git commit -am "Bumped to version $a"
    @git push --tags
  • 1
    \$\begingroup\$ Why not to use plain shell script for this? With this tricks Makefile looks rather bloated. Also I guess it will be fine to use python script, cause, really, Unix coreutils doesn't suite well for semver increment. \$\endgroup\$ – sineemore Jun 7 '18 at 18:50
  • \$\begingroup\$ Of course we can, but I wanted to see how far I could push a Makefile. It was more of a 'I think I can do this, let's see if it's possible' as opposed to 'there's no other option' \$\endgroup\$ – Rambatino Jun 8 '18 at 10:57

I think there's an easier way to get this done:

  1. Extract the semver part from the tag (as you already did)
  2. Use conditionals to determine the field to increment
  3. Use a single Awk to increment the selected field

For example:

    $(eval v := $(shell git describe --tags --abbrev=0 | sed -Ee 's/^v|-.*//'))
ifeq ($(bump), major)
    $(eval f := 1)
else ifeq ($(bump), minor)
    $(eval f := 2)
    $(eval f := 3)
    @echo $(v) | awk -F. -v OFS=. -v f=$(f) '{ $$f++ } 1'

I would add some validation in case the version doesn't have major, minor, patch components.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.