Skip to content

Embed version string and git commit hash in binary #130

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ run --host_platform=//backend:regular_executable --incompatible_enable_cc_toolch
# We also need to setup an utf8 locale
test --test_env=LANG=en_US.utf8 --test_env=LOCALE_ARCHIVE

build --workspace_status_command "bash release/version.sh"

try-import .bazelrc.local
7 changes: 7 additions & 0 deletions backend/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_haskell//haskell:defs.bzl", "haskell_binary", "haskell_toolchain_library")
load("//release:version.bzl", "copy_stable_status")

constraint_setting(
name = "executable_linking",
Expand Down Expand Up @@ -31,6 +32,11 @@ platform(
visibility = ["//visibility:public"],
)

copy_stable_status(
name = "stable_status",
output = "stable-status.txt",
)

haskell_toolchain_library(name = "base")

haskell_binary(
Expand All @@ -47,6 +53,7 @@ haskell_binary(
":src/Sqlite.hs",
],
extra_srcs = [
":stable_status",
"//frontend:main.js",
"//frontend:src/index.js",
"//frontend:src/format.js",
Expand Down
27 changes: 27 additions & 0 deletions backend/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}

module Main where
Expand All @@ -12,18 +13,23 @@ import Control.Concurrent (threadDelay)
import Control.Exception (bracket, tryJust)
import Control.Monad (guard, when)
import Data.Aeson (decode, encode)
import qualified Data.ByteString.Char8 as BSC
import Data.FileEmbed (embedFileIfExists)
import Data.Foldable (asum, for_, traverse_)
import Data.Functor (void, (<&>))
import Data.List (isPrefixOf, stripPrefix)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe, isNothing)
import qualified Data.Text as Text
import Data.UUID (UUID)
import qualified Import
import qualified Language.Haskell.TH as TH
import Network.HTTP.Client (Request (..), RequestBody (..), defaultManagerSettings, httpLbs, newManager, parseRequest, responseBody, responseStatus)
import Network.HTTP.Types (Status (..))
import qualified Server
import System.Directory (createDirectoryIfMissing)
import System.Environment (getArgs, setEnv)
import System.FilePath.Find (filePath, find, (~~?))
import System.FilePath.Posix (takeBaseName)
import System.IO (stdin)
import System.IO.Error (isDoesNotExistError)
Expand All @@ -43,8 +49,29 @@ main = do
["server"] -> pure ()
"import" : args -> importWorkspace args
"import-graphviz" : args -> importGraphviz args
["--version"] -> putStrLn version
_ -> usageError

version :: String
version = do
let stableStatus =
BSC.unpack $
fromMaybe "" $
$( embedFileIfExists
$( do
found <- TH.runIO $ find (pure True) (filePath ~~? "**/stable-status.txt") "."
pure $
TH.LitE $
TH.StringL $ case found of
[path] -> path
[] -> ""
_ -> error "unexpectedly found multiple Version.hs files"
)
)
fields = Map.fromList $ words <$> lines stableStatus <&> \[k, v] -> (k, v)
lookupField k = fromMaybe "(unknown)" $ Map.lookup k fields
lookupField "STABLE_VERSION" <> " (built from " <> lookupField "STABLE_GIT_COMMIT" <> ")"

restartServer :: IO ()
restartServer = do
traverse_ attemptTerminate =<< getPidFromFile
Expand Down
19 changes: 19 additions & 0 deletions release/version.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def _copy_stable_status(ctx):
ctx.actions.run_shell(
inputs = [ctx.info_file],
outputs = [ctx.outputs.output],
mnemonic = "CopyStableStatus",
command = "sed -n '/^STABLE_/p' {INFO_FILE} >{OUTPUT}".format(
INFO_FILE = ctx.info_file.path,
OUTPUT = ctx.outputs.output.path,
),
)

copy_stable_status = rule(
implementation = _copy_stable_status,
attrs = {
"output" : attr.output(
mandatory = True,
),
},
)
4 changes: 4 additions & 0 deletions release/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -eu
echo "STABLE_VERSION 0.4.4"
echo "STABLE_GIT_COMMIT $(git rev-parse HEAD)"
Loading