Building Crystal Programs with Source File Hash baked in

This post is inspired by the blog post written by Pradeep Gowda about embedding Source file Hash while building Go programs.

The one issue with checking in the binary with source code is, you can’t be sure if the binary is built with the latest source that is adjacent to it.

https://www.btbytes.com/posts/2020-05-05-01-golang-hash-builds.html

I adopted a similar technique while building Crystal programs. This is much simpler with Crystal because of the macros.

hello.cr file:

SOURCE_HASH = {{ `sha256sum #{__FILE__} | cut -d' ' -f1`.chomp.stringify }}

puts SOURCE_HASH

Build step:

$ crystal build hello.cr

Verify:

$ ./hello
cdb9db6f5ac4a0df1ed06de118c569fa225785685849d533c1ba550854b8b4ab
$ sha256sum hello.cr
cdb9db6f5ac4a0df1ed06de118c569fa225785685849d533c1ba550854b8b4ab  hello.cr

Similarly, if shards is used as the package manager for your Crystal Programs, then you can include the VERSION automatically while compiling the program.

VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }}

puts VERSION

Comments !