Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use compile-time shell commands to determine build timestamp #438

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ MOLINILLO_SOURCES = $(shell find lib/molinillo -name '*.cr' 2> /dev/null)
SOURCES = $(SHARDS_SOURCES) $(MOLINILLO_SOURCES)
TEMPLATES = src/templates/*.ecr

SHARDS_CONFIG_BUILD_COMMIT := $(shell git rev-parse --short HEAD 2> /dev/null)
SOURCE_DATE_EPOCH := $(shell (git show -s --format=%ct HEAD || stat -c "%Y" Makefile || stat -f "%m" Makefile) 2> /dev/null)
EXPORTS := SHARDS_CONFIG_BUILD_COMMIT="$(SHARDS_CONFIG_BUILD_COMMIT)" SOURCE_DATE_EPOCH="$(SOURCE_DATE_EPOCH)"
DESTDIR ?=
PREFIX ?= /usr/local
BINDIR ?= $(DESTDIR)$(PREFIX)/bin
Expand All @@ -29,7 +32,7 @@ clean: phony

bin/shards: $(SOURCES) $(TEMPLATES) lib
@mkdir -p bin
$(CRYSTAL) build $(FLAGS) src/shards.cr -o bin/shards
$(EXPORTS) $(CRYSTAL) build $(FLAGS) src/shards.cr -o bin/shards

install: bin/shards phony
$(INSTALL) -m 0755 -d "$(BINDIR)" "$(MANDIR)/man1" "$(MANDIR)/man5"
Expand Down
8 changes: 6 additions & 2 deletions src/version.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Shards
VERSION = {{ read_file("#{__DIR__}/../VERSION").chomp }}
BUILD_SHA1 = {{ `git log --format=%h -n 1 2>/dev/null || echo ""`.stringify.chomp }}
BUILD_DATE = Time.unix({{ (env("SOURCE_DATE_EPOCH") || `date +%s`).to_i }}).to_s("%Y-%m-%d")
BUILD_SHA1 = {{ env("SHARDS_CONFIG_BUILD_COMMIT") || "" }}
{% if (t = env("SOURCE_DATE_EPOCH")) && !t.empty? %}
BUILD_DATE = Time.unix({{t.to_i}}).to_s("%Y-%m-%d")
{% else %}
BUILD_DATE = ""
{% end %}

def self.version_string
if BUILD_SHA1.empty?
Expand Down