7
\$\begingroup\$

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:

release:
    $(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
endif
    $(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
\$\endgroup\$
2
  • 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\$ Commented Jun 7, 2018 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\$ Commented Jun 8, 2018 at 10:57

1 Answer 1

3
\$\begingroup\$

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:

release:
    $(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)
else
    $(eval f := 3)
endif
    @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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.