Version script "helpers"

During a conversation with @niemeyer it came to our attention that just echoing a value at the end of the script defined in version-script would make it, on one hand error prone and on the other not extensible.

The proposal is to instead of echo in the script to instead call these shell functions:

  • set-version
  • set-grade

So in an example this would look like:

version: 1.0
version-script: |
    # We just want the version with a date appended
    set-version $SNAPCRAFT_PROJECT_VERSION-$(date +%Y%m%d.%H%m)
    # The grade will be stable on the 15th of every month and devel for every other day
    if [ $(date +%d) = "15" ]; then
        set-grade stable
        set-grade devel

The helper functions setup by snapcraft will do the appropriate validations to determine if valid values are being passed on and provide appropriate error messages.

With the introduction of this feature, the echoing one would go away.


I am wondering why that would not just be two fragments:

version-script: |

grade-script: |


1 Like

@sergiusens That looks nice, thanks Sergio!

@apw I think I’d even keep set-grade inside version-script as Sergio suggested above. It pretty much never makes sense to have a different grade for the same version, in the sense that any sane process wouldn’t have the same string for both a beta and a final release. Most likely the preamble that is necessary for figuring out what the version is will also be the preamble that would be necessary for figuring the grade, so a single script is better.

1 Like

How are you handling backwards compatibility as this feature with its current form is already in use.

Perhaps it makes more sense to make your new form a new tag meta-script: perhaps. This would allow you to add further set-FOO helpers as they become useful, and the old semantics can be deprecated and removed over time.

version-script is much more clear than meta-script.

I was thinking of perhaps using backticks anywhere in the yaml to have the
command run (at the same time as the version-script is currently run –
after build). Unfortunately backticks aren’t allowed to start a yaml value,
so it would have to look like :

grade: "`echo stable`"

which is a bit hacky, but would let us have script interpolation for any
key without having to add extra *-script keys to everything.

The forum ate my backticks there… there are supposed to be backticks are
inside of the double-quotes example above.

Fixed the formatting.

Yeah, it does feel hacky, and we don’t really want to encourage people to interpolate strings arbitrarily. It quickly becomes messy, and there’s a whole workflow that these scripts need to sit inside of.

Is there any updated doc about this feature, I suppose it turned into snapcraftctl set-grade and snapcraftctl set-version commands?

UPDATE: Nevermind, I found it: Using external metadata