lgiordani / punch

Update your version while having a drink
ISC License
90 stars 13 forks source link

Don't overwrite other parts of the punch_version.py file #41

Open tjanez opened 4 years ago

tjanez commented 4 years ago

Hey!

First of all, thanks for creating this great tool. I've been successfully using it for a while.

I have a small request, namely, to stop overwriting the whole punch_version.py file after performing a version bump.

The reason I would like this is that I would like to have a comment at the top of punch_version.py explaining what this file is and where to find more information since this file is usually in a repository's top-level directory and developers/users will frequently see it and wonder what it is and what it does.

Here is an example punch_version.py I would like to have (and retain after bumping):

# Punch version file.

# It contains the current version of the project.

# For more information, see: https://punch.readthedocs.io/.

major = 0
minor = 4
patch = 0

Full example/reproducer:

punch --version
cat << EOF > punch_config.py
# Punch configuration file.

# For more information, see: https://punch.readthedocs.io/.

__config_version__ = 1

GLOBALS = {
    'serializer': {
        'semver': '{{ major }}.{{ minor }}.{{ patch }}',
     }
}

# NOTE: The FILES list is not allowed to be empty, so we need to pass it at
# least a single valid file.
FILES = ["README.md"]

VERSION = ['major', 'minor', 'patch']

EOF
touch README.md
cat << EOF > punch_version.py
# Punch version file.

# It contains the current version of the project.

# For more information, see: https://punch.readthedocs.io/.

major = 0
minor = 4
patch = 0
EOF
punch --part minor
cat punch_version.py

And the output I get:

punch [tadej@toronto Punch]$ punch --version
Punch version 2.0.0
Copyright (C) 2016 Leonardo Giordani
This is free software, see the LICENSE file.
Source: https://github.com/lgiordani/punch
Documentation: http://punch.readthedocs.io/en/latest/
punch [tadej@toronto Punch]$ cat << EOF > punch_config.py
> # Punch configuration file.
> 
> # For more information, see: https://punch.readthedocs.io/.
> 
> __config_version__ = 1
> 
> GLOBALS = {
>     'serializer': {
>         'semver': '{{ major }}.{{ minor }}.{{ patch }}',
>      }
> }
> 
> # NOTE: The FILES list is not allowed to be empty, so we need to pass it at
> # least a single valid file.
> FILES = ["README.md"]
> 
> VERSION = ['major', 'minor', 'patch']
> 
> EOF
punch [tadej@toronto Punch]$ touch README.md
punch [tadej@toronto Punch]$ cat << EOF > punch_version.py
> # Punch version file.
> 
> # It contains the current version of the project.
> 
> # For more information, see: https://punch.readthedocs.io/.
> 
> major = 0
> minor = 4
> patch = 0
> EOF
punch [tadej@toronto Punch]$ punch --part minor
Warning: Cannot find any match for version {'major': 0, 'minor': 4, 'patch': 0} in file README.md
punch [tadej@toronto Punch]$ cat punch_version.py
major = 0
minor = 5
patch = 0
lgiordani commented 4 years ago

Hey @tjanez, thanks for using Punch. Your request makes a lot of sense to me, let me think about it, and I will add the feature

tjanez commented 4 years ago

Hey @tjanez, thanks for using Punch. Your request makes a lot of sense to me, let me think about it, and I will add the feature

@lgiordani, thanks for such a quick response and thanks for considering this!

lgiordani commented 3 years ago

Hi @tjanez. I worked a bit on this request, and found out that it can't be easily implemented. The problem is that in Python you can't load a module, change it, and save it back to the disk. Indeed, the current way I use to create the file punch_version.py is

        with open(version_filepath, 'w') as f:
            for key, part in self.parts.items():
                f.write("{0} = {1}\n".format(key, repr(part.value)))

There might be a simple solution, though. I might add a configuration value that is the text content of the file punch_version.py, something like (punch_config.py)

HEADER = """
# Punch version file.

# It contains the current version of the project.

# For more information, see: https://punch.readthedocs.io/.
"""

and add that text to the file punch_version.py every time I recreate it. What do you think about this? It might solve your issue without requiring too much code. Let me know!