Skip to content

Commit 44423fa

Browse files
emarzionttuegel
andauthored
Remove build date from version info and add dirty flag (#2370)
* removing build date and adding gitDirty * Extract module VersionInfo * Materialize Nix expressions * VersionInfo: Add documentation * VersionInfo.getPackageRoot: Avoid non-terminating loop Co-authored-by: Thomas Tuegel <ttuegel@mailbox.org> Co-authored-by: ttuegel <ttuegel@users.noreply.github.com> Co-authored-by: Thomas Tuegel <thomas.tuegel@runtimeverification.com>
1 parent 5160710 commit 44423fa

File tree

4 files changed

+132
-40
lines changed

4 files changed

+132
-40
lines changed

kore/app/share/GlobalMain.hs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,12 @@ import Data.Text
4949
, pack
5050
)
5151
import qualified Data.Text.IO as Text
52-
import Data.Time.Format
53-
( defaultTimeLocale
54-
, formatTime
55-
)
56-
import Data.Time.LocalTime
57-
( ZonedTime
58-
, getZonedTime
59-
)
6052
import Data.Version
6153
( showVersion
6254
)
63-
import Development.GitRev
64-
( gitBranch
65-
, gitCommitDate
66-
, gitHash
67-
)
6855
import GHC.Stack
6956
( emptyCallStack
7057
)
71-
7258
import Options.Applicative
7359
( InfoMod
7460
, Parser
@@ -92,12 +78,11 @@ import Options.Applicative
9278
, value
9379
, (<**>)
9480
)
81+
import qualified Options.Applicative as Options
9582
import Options.Applicative.Help.Chunk
9683
( Chunk (..)
9784
, vsepChunks
9885
)
99-
100-
import qualified Options.Applicative as Options
10186
import qualified Options.Applicative.Help.Pretty as Pretty
10287
import System.Clock
10388
( Clock (Monotonic)
@@ -106,6 +91,9 @@ import System.Clock
10691
)
10792
import qualified System.Environment as Env
10893
import qualified Text.Megaparsec as Parser
94+
import Text.Read
95+
( readMaybe
96+
)
10997

11098
import Kore.ASTVerifier.DefinitionVerifier
11199
( sortModuleClaims
@@ -144,12 +132,11 @@ import Kore.Syntax.Definition
144132
, getModuleNameForError
145133
)
146134
import qualified Kore.Verified as Verified
135+
147136
import qualified Paths_kore as MetaData
148137
( version
149138
)
150-
import Text.Read
151-
( readMaybe
152-
)
139+
import VersionInfo
153140

154141
type Main = LoggerT IO
155142

@@ -289,27 +276,22 @@ mainGlobal
289276
-> IO (MainOptions options)
290277
mainGlobal exeName maybeEnv localOptionsParser modifiers = do
291278
options <- commandLineParse exeName maybeEnv localOptionsParser modifiers
292-
when (willVersion $ globalOptions options) (getZonedTime >>= mainVersion)
279+
when (willVersion $ globalOptions options) mainVersion
293280
return options
294281

295282
-- | main function to print version information
296-
mainVersion :: ZonedTime -> IO ()
297-
mainVersion time =
298-
mapM_ putStrLn
299-
[ "Kore version " ++ packageVersion
300-
, "Git:"
301-
, " revision:\t" ++ $gitHash
302-
, " branch:\t" ++ $gitBranch
303-
, " last commit:\t" ++ gitTime
304-
, "Build date:\t" ++ exeTime
305-
]
283+
mainVersion :: IO ()
284+
mainVersion =
285+
mapM_ putStrLn
286+
[ "Kore version " ++ packageVersion
287+
, "Git:"
288+
, " revision:\t" ++ gitHash ++ if gitDirty then " (dirty)" else ""
289+
, " branch:\t" ++ fromMaybe "<unknown>" gitBranch
290+
, " last commit:\t" ++ gitCommitDate
291+
]
306292
where
307293
packageVersion = showVersion MetaData.version
308-
formatGit (_:mm:dd:tt:yy:tz:_) = [yy,mm,dd,tt,tz]
309-
formatGit t = t
310-
gitTime = (unwords . formatGit . words) $gitCommitDate
311-
exeTime = formatTime defaultTimeLocale "%Y %b %d %X %z" time
312-
294+
VersionInfo { gitHash, gitDirty, gitBranch, gitCommitDate } = $versionInfo
313295

314296
--------------------
315297
-- Option Parsers --

kore/app/share/VersionInfo.hs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{-# LANGUAGE DeriveLift #-}
2+
{-# LANGUAGE NoDuplicateRecordFields #-}
3+
{-# LANGUAGE Strict #-}
4+
{-# LANGUAGE TemplateHaskell #-}
5+
6+
module VersionInfo
7+
( VersionInfo (..)
8+
, versionInfo
9+
) where
10+
11+
import Prelude.Kore
12+
13+
import Data.Aeson
14+
( FromJSON
15+
)
16+
import qualified Data.Aeson as Aeson
17+
import qualified Data.List as List
18+
import qualified Development.GitRev as GitRev
19+
import qualified GHC.Generics as GHC
20+
import Language.Haskell.TH
21+
( Exp
22+
, Q
23+
)
24+
import qualified Language.Haskell.TH as TH
25+
import Language.Haskell.TH.Syntax
26+
( Lift
27+
)
28+
import qualified Language.Haskell.TH.Syntax as TH
29+
import qualified System.Directory as Directory
30+
import System.FilePath
31+
( isRelative
32+
, joinPath
33+
, splitDirectories
34+
, takeDirectory
35+
, (</>)
36+
)
37+
38+
-- | Information about the current version of Kore.
39+
data VersionInfo =
40+
VersionInfo
41+
{ gitHash :: !String
42+
, gitCommitDate :: !String
43+
, gitBranch :: !(Maybe String)
44+
, gitDirty :: !Bool
45+
}
46+
deriving stock (GHC.Generic)
47+
deriving stock (Lift)
48+
49+
instance FromJSON VersionInfo
50+
51+
-- | Produce (at compile-time) information about the current version of Kore.
52+
versionInfo :: Q Exp
53+
versionInfo = do
54+
packageRoot <- getPackageRoot
55+
let versionFile = packageRoot </> "version.json"
56+
haveVersionFile <- Directory.doesFileExist versionFile & TH.runIO
57+
if haveVersionFile
58+
then readVersionInfoFile versionFile
59+
else defaultVersionInfo
60+
where
61+
readVersionInfoFile versionFile = do
62+
result <- Aeson.eitherDecodeFileStrict' versionFile & TH.runIO
63+
either fail (TH.lift @_ @VersionInfo) result
64+
defaultVersionInfo =
65+
[| VersionInfo
66+
{ gitHash = $(GitRev.gitHash)
67+
, gitCommitDate = $(GitRev.gitCommitDate)
68+
, gitBranch = Just $(GitRev.gitBranch)
69+
, gitDirty = $(GitRev.gitDirty)
70+
}
71+
|]
72+
73+
{- | Find the root of the package.
74+
75+
@getPackageRoot@ looks upward from the current file (i.e. the file into which it
76+
is spliced) to find the root directory of the package.
77+
78+
-}
79+
getPackageRoot :: Q FilePath
80+
getPackageRoot = do
81+
bot <- takeDirectory . TH.loc_filename <$> TH.location
82+
let parents = getParents bot
83+
TH.runIO $ findPackageRoot bot parents
84+
where
85+
isProjectRoot here = Directory.doesFileExist (here </> "package.yaml")
86+
87+
getParents bottom =
88+
bottom
89+
& splitDirectories
90+
& List.inits
91+
& reverse
92+
& map joinPath
93+
& filter (sameRelativity bottom)
94+
95+
sameRelativity bottom = \here -> isRelative bottom == isRelative here
96+
97+
-- Find the root directory of the current package. This module file can
98+
-- be moved safely because the package root is found at build time.
99+
findPackageRoot bot [] =
100+
fail ("Could not find package.yaml above " ++ bot)
101+
findPackageRoot bot (here : heres) = do
102+
foundRoot <- isProjectRoot here
103+
if foundRoot then Directory.makeAbsolute here else goUp
104+
where
105+
goUp = findPackageRoot bot heres
106+

kore/kore.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 2.2
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 31c0153b51498e9307cfa8992158de791fb8824f02b0c299f2e0b5dd9a03eab8
7+
-- hash: 3a3209955e7db4f112e2b2c2ab073b1d479448d68f21f84c4a14ce8f5f2293d9
88

99
name: kore
1010
version: 0.38.0.0
@@ -476,6 +476,7 @@ executable kore-exec
476476
main-is: Main.hs
477477
other-modules:
478478
GlobalMain
479+
VersionInfo
479480
Paths_kore
480481
hs-source-dirs:
481482
app/exec
@@ -556,6 +557,7 @@ executable kore-format
556557
main-is: Main.hs
557558
other-modules:
558559
GlobalMain
560+
VersionInfo
559561
Paths_kore
560562
hs-source-dirs:
561563
app/format
@@ -636,6 +638,7 @@ executable kore-parser
636638
main-is: Main.hs
637639
other-modules:
638640
GlobalMain
641+
VersionInfo
639642
Paths_kore
640643
hs-source-dirs:
641644
app/parser
@@ -790,6 +793,7 @@ executable kore-repl
790793
main-is: Main.hs
791794
other-modules:
792795
GlobalMain
796+
VersionInfo
793797
Paths_kore
794798
hs-source-dirs:
795799
app/repl

nix/kore.nix.d/kore.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@
530530
(hsPkgs.buildPackages.tasty-discover or (pkgs.buildPackages.tasty-discover or (errorHandler.buildToolDepError "tasty-discover")))
531531
];
532532
buildable = true;
533-
modules = [ "GlobalMain" "Paths_kore" ];
533+
modules = [ "GlobalMain" "VersionInfo" "Paths_kore" ];
534534
hsSourceDirs = [ "app/exec" "app/share" ];
535535
mainPath = ([
536536
"Main.hs"
@@ -602,7 +602,7 @@
602602
(hsPkgs.buildPackages.tasty-discover or (pkgs.buildPackages.tasty-discover or (errorHandler.buildToolDepError "tasty-discover")))
603603
];
604604
buildable = true;
605-
modules = [ "GlobalMain" "Paths_kore" ];
605+
modules = [ "GlobalMain" "VersionInfo" "Paths_kore" ];
606606
hsSourceDirs = [ "app/format" "app/share" ];
607607
mainPath = ([
608608
"Main.hs"
@@ -674,7 +674,7 @@
674674
(hsPkgs.buildPackages.tasty-discover or (pkgs.buildPackages.tasty-discover or (errorHandler.buildToolDepError "tasty-discover")))
675675
];
676676
buildable = true;
677-
modules = [ "GlobalMain" "Paths_kore" ];
677+
modules = [ "GlobalMain" "VersionInfo" "Paths_kore" ];
678678
hsSourceDirs = [ "app/parser" "app/share" ];
679679
mainPath = ([
680680
"Main.hs"
@@ -816,7 +816,7 @@
816816
(hsPkgs.buildPackages.tasty-discover or (pkgs.buildPackages.tasty-discover or (errorHandler.buildToolDepError "tasty-discover")))
817817
];
818818
buildable = true;
819-
modules = [ "GlobalMain" "Paths_kore" ];
819+
modules = [ "GlobalMain" "VersionInfo" "Paths_kore" ];
820820
hsSourceDirs = [ "app/repl" "app/share" ];
821821
mainPath = ([
822822
"Main.hs"

0 commit comments

Comments
 (0)