Automatically update a constant on every build

Hello!

I wonder, what would be a simple way to update a constant on every build? My intuitive approach was like the following, but that wouldn’t work.

{{ VERSION = Time.utc }}
puts VERSION

I’m pretty sure you can just do TIME = Time.utc. See the second codeblock in https://crystal-lang.org/reference/syntax_and_semantics/constants.html.

Thank you, but if I just do TIME = Time.utc, the constant will update on every run, rather than on every build, right? I might be missing something here, though.

Ah, in that case you could use https://crystal-lang.org/api/master/Crystal/Macros.html#`(command):MacroId-instance-method to do like

TIME = {{ `date`.stringify }}

Of course this would require that the system that builds the binary has a date command.

What’s the use case of this? Wouldn’t it be better to follow semver and just manually update the version when a release changes something?

2 Likes

It works, thanks!
That’s for a really small utility, and I always forget to bump the version.