version command

commit 9df07ae34a6d1c7f53cee3abe2304ff81eb0a26c
Author: Sergey Ivanov <ivanovs-4@yandex.ru>
Date:   Tue Oct 24 22:25:34 2023 +0400

    `version` command for hbs2, hbs2-peer, git-hbs2
This commit is contained in:
Dmitry Zuikov 2024-01-28 07:51:46 +03:00
parent 889947bf51
commit e6ae5fb593
9 changed files with 117 additions and 21 deletions

View File

@ -26,7 +26,18 @@ inputs = {
}; };
outputs = { self, nixpkgs, haskell-flake-utils, ... }@inputs: outputs = { self, nixpkgs, haskell-flake-utils, ... }@inputs:
let
packageNames = [
"hbs2"
"hbs2-peer"
"hbs2-core"
"hbs2-storage-simple"
"hbs2-git"
"hbs2-qblf"
"hbs2-keyman"
"hbs2-share"
];
in
haskell-flake-utils.lib.simpleCabalProject2flake { haskell-flake-utils.lib.simpleCabalProject2flake {
inherit self nixpkgs; inherit self nixpkgs;
systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
@ -37,16 +48,7 @@ outputs = { self, nixpkgs, haskell-flake-utils, ... }@inputs:
db-pipe db-pipe
]; ];
packageNames = [ inherit packageNames;
"hbs2"
"hbs2-peer"
"hbs2-core"
"hbs2-storage-simple"
"hbs2-git"
"hbs2-qblf"
"hbs2-keyman"
"hbs2-share"
];
packageDirs = { packageDirs = {
"hbs2" = "./hbs2"; "hbs2" = "./hbs2";
@ -75,16 +77,38 @@ outputs = { self, nixpkgs, haskell-flake-utils, ... }@inputs:
justStaticExecutables justStaticExecutables
dontCheck dontCheck
(compose.overrideCabal (drv: {
preBuild = ''
export GIT_HASH="${self.rev or self.dirtyRev or "dirty"}"
'';
}))
]; ];
shellExtBuildInputs = {pkgs}: with pkgs; [ shell = {pkgs, ...}:
haskellPackages.haskell-language-server pkgs.haskellPackages.shellFor {
haskellPackages.htags packages = _: pkgs.lib.attrsets.attrVals packageNames pkgs.haskellPackages;
haskellPackages.hoogle # withHoogle = true;
pkg-config buildInputs = (
inputs.hspup.packages.${pkgs.system}.default with pkgs.haskellPackages; ([
inputs.fixme.packages.${pkgs.system}.default ghcid
]; cabal-install
haskell-language-server
htags
])
++
[ pkgs.pkg-config
inputs.hspup.packages.${pkgs.system}.default
inputs.fixme.packages.${pkgs.system}.default
]
);
shellHook = ''
export GIT_HASH="${self.rev or self.dirtyRev or "dirty"}"
'';
};
}; };

View File

@ -147,6 +147,7 @@ library
, HBS2.Net.Dialog.Client , HBS2.Net.Dialog.Client
, HBS2.Net.Dialog.Helpers.List , HBS2.Net.Dialog.Helpers.List
, HBS2.Net.Dialog.Helpers.Streaming , HBS2.Net.Dialog.Helpers.Streaming
, HBS2.Version
-- other-modules: -- other-modules:
@ -205,6 +206,7 @@ library
, streaming , streaming
, string-conversions , string-conversions
, suckless-conf , suckless-conf
, template-haskell
, temporary , temporary
, text , text
, time , time

View File

@ -0,0 +1,39 @@
{-# LANGUAGE StrictData #-}
module HBS2.Version (inlineBuildVersion, BuildVersion(..)) where
import Data.Aeson
import Data.Data
import Data.Text (Text)
import Data.Text qualified as T
import Data.Version (Version, showVersion)
import GHC.Generics
import Language.Haskell.TH
import Language.Haskell.TH.Syntax (Lift (..), dataToExpQ)
import System.Environment (getEnvironment)
data BuildVersion = BuildVersion
{ buildVersion_git :: Maybe Text
, buildVersion_pkg :: Text
}
deriving (Generic, Eq, Show, Data)
instance ToJSON BuildVersion
-- | Lift text as expression in TH.
-- https://stackoverflow.com/questions/38143464/cant-find-inerface-file-declaration-for-variable
liftText :: Text -> Q Exp
liftText txt = AppE (VarE 'T.pack) <$> lift (T.unpack txt)
liftDataWithText :: (Data a) => a -> Q Exp
liftDataWithText = dataToExpQ (fmap liftText . cast)
lookupEnv :: String -> Q (Maybe Text)
lookupEnv key = fmap (T.strip . T.pack) . lookup key <$> runIO getEnvironment
inlineBuildVersion :: Version -> Q Exp
inlineBuildVersion version =
flip sigE [t|BuildVersion|] . liftDataWithText =<< do
buildVersion_git <- lookupEnv "GIT_HASH"
let buildVersion_pkg = (T.pack . showVersion) version
pure BuildVersion {..}

View File

@ -8,11 +8,16 @@ import HBS2Git.Export
import HBS2Git.Tools import HBS2Git.Tools
import HBS2Git.KeysCommand import HBS2Git.KeysCommand
import HBS2.Net.Proto.Definition() import HBS2.Net.Proto.Definition()
import HBS2.Version
import RunShow import RunShow
import Options.Applicative as O import Options.Applicative as O
import Control.Monad import Control.Monad
import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy qualified as LBS
import Paths_hbs2_git qualified as Pkg
main :: IO () main :: IO ()
main = join . customExecParser (prefs showHelpOnError) $ main = join . customExecParser (prefs showHelpOnError) $
@ -28,8 +33,12 @@ main = join . customExecParser (prefs showHelpOnError) $
<> command "show" (info pShow (progDesc "show various types of objects")) <> command "show" (info pShow (progDesc "show various types of objects"))
<> command "tools" (info pTools (progDesc "misc tools")) <> command "tools" (info pTools (progDesc "misc tools"))
<> command "key" (info pKeys (progDesc "manage keys")) <> command "key" (info pKeys (progDesc "manage keys"))
<> command "version" (info pVersion (progDesc "show program version"))
) )
pVersion = pure do
LBS.putStr $ Aeson.encode $(inlineBuildVersion Pkg.version)
pExport = do pExport = do
keyfile <- strArgument (metavar "KEIRING-FILE") keyfile <- strArgument (metavar "KEIRING-FILE")
pure $ runApp WithLog do pure $ runApp WithLog do

View File

@ -53,6 +53,7 @@ common shared-properties
, TypeApplications , TypeApplications
, TypeOperators , TypeOperators
, TypeFamilies , TypeFamilies
, TemplateHaskell
build-depends: hbs2-core, hbs2-peer build-depends: hbs2-core, hbs2-peer
@ -148,12 +149,14 @@ executable git-hbs2
other-modules: other-modules:
RunShow RunShow
Paths_hbs2_git
-- other-extensions: -- other-extensions:
build-depends: build-depends:
base, hbs2-git base, hbs2-git
, optparse-applicative , optparse-applicative
, http-types , http-types
, template-haskell
hs-source-dirs: git-hbs2 hs-source-dirs: git-hbs2
default-language: Haskell2010 default-language: Haskell2010

View File

@ -39,6 +39,8 @@ import HBS2.Storage.Operations.Missed
import HBS2.Data.Detect import HBS2.Data.Detect
import HBS2.System.Logger.Simple hiding (info) import HBS2.System.Logger.Simple hiding (info)
import HBS2.Version
import Paths_hbs2_peer qualified as Pkg
import Brains import Brains
import BrainyPeerLocator import BrainyPeerLocator
@ -84,6 +86,7 @@ import Control.Monad.Reader
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Writer.CPS qualified as W import Control.Monad.Trans.Writer.CPS qualified as W
import Crypto.Saltine (sodiumInit) import Crypto.Saltine (sodiumInit)
import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy qualified as LBS import Data.ByteString.Lazy qualified as LBS
import Data.ByteString qualified as BS import Data.ByteString qualified as BS
import Data.Cache qualified as Cache import Data.Cache qualified as Cache
@ -240,6 +243,7 @@ runCLI = do
<> command "poll" (info pPoll (progDesc "polling management")) <> command "poll" (info pPoll (progDesc "polling management"))
<> command "log" (info pLog (progDesc "set logging level")) <> command "log" (info pLog (progDesc "set logging level"))
<> command "bypass" (info pByPass (progDesc "bypass")) <> command "bypass" (info pByPass (progDesc "bypass"))
<> command "version" (info pVersion (progDesc "show program version"))
-- FIXME: bring-back-dialogue-over-separate-socket -- FIXME: bring-back-dialogue-over-separate-socket
-- <> command "dial" (info pDialog (progDesc "dialog commands")) -- <> command "dial" (info pDialog (progDesc "dialog commands"))
) )
@ -272,6 +276,9 @@ runCLI = do
m m
pVersion = pure do
LBS.putStr $ Aeson.encode $(inlineBuildVersion Pkg.version)
pRun = do pRun = do
runPeer <$> common runPeer <$> common

View File

@ -75,6 +75,7 @@ common common-deps
, heaps , heaps
, psqueues , psqueues
, string-conversions , string-conversions
, template-haskell
common shared-properties common shared-properties
ghc-options: ghc-options:
@ -119,6 +120,7 @@ common shared-properties
, RecursiveDo , RecursiveDo
, ScopedTypeVariables , ScopedTypeVariables
, StandaloneDeriving , StandaloneDeriving
, TemplateHaskell
, TupleSections , TupleSections
, TypeApplications , TypeApplications
, TypeFamilies , TypeFamilies
@ -197,6 +199,8 @@ executable hbs2-peer
, CLI.Common , CLI.Common
, CLI.RefChan , CLI.RefChan
, Paths_hbs2_peer
-- other-extensions: -- other-extensions:
build-depends: base, hbs2-peer, hbs2-keyman, vty build-depends: base, hbs2-peer, hbs2-keyman, vty

View File

@ -24,6 +24,8 @@ import HBS2.Storage.Simple
import HBS2.Storage.Simple.Extra import HBS2.Storage.Simple.Extra
import HBS2.Data.Bundle import HBS2.Data.Bundle
import HBS2.OrDie import HBS2.OrDie
import HBS2.Version
import Paths_hbs2 qualified as Pkg
import HBS2.System.Logger.Simple hiding (info) import HBS2.System.Logger.Simple hiding (info)
@ -38,6 +40,7 @@ import Control.Monad.Except
import Control.Monad.Trans.Maybe import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Resource import Control.Monad.Trans.Resource
import Crypto.Saltine.Core.Box qualified as Encrypt import Crypto.Saltine.Core.Box qualified as Encrypt
import Data.Aeson qualified as Aeson
import Data.ByteString.Char8 qualified as BS8 import Data.ByteString.Char8 qualified as BS8
import Data.ByteString.Lazy (ByteString) import Data.ByteString.Lazy (ByteString)
import Data.ByteString.Lazy qualified as LBS import Data.ByteString.Lazy qualified as LBS
@ -548,12 +551,15 @@ main = join . customExecParser (prefs showHelpOnError) $
<> command "reflog" (info pReflog (progDesc "reflog commands")) <> command "reflog" (info pReflog (progDesc "reflog commands"))
<> command "bundle" (info pBundle (progDesc "bundle commands")) <> command "bundle" (info pBundle (progDesc "bundle commands"))
<> command "anyref" (info pAnyRef (progDesc "anyref commands")) <> command "anyref" (info pAnyRef (progDesc "anyref commands"))
<> command "version" (info pVersion (progDesc "show program version"))
) )
common = do common = do
pref <- optional $ strOption ( short 'p' <> long "prefix" <> help "storage prefix" ) pref <- optional $ strOption ( short 'p' <> long "prefix" <> help "storage prefix" )
pure $ CommonOpts pref pure $ CommonOpts pref
pVersion = pure do
LBS.putStr $ Aeson.encode $(inlineBuildVersion Pkg.version)
pStore = do pStore = do
o <- common o <- common
file <- optional $ strArgument ( metavar "FILE" ) file <- optional $ strArgument ( metavar "FILE" )

View File

@ -52,6 +52,7 @@ common shared-properties
, RecordWildCards , RecordWildCards
, ScopedTypeVariables , ScopedTypeVariables
, StandaloneDeriving , StandaloneDeriving
, TemplateHaskell
, TupleSections , TupleSections
, TypeApplications , TypeApplications
, TypeFamilies , TypeFamilies
@ -60,7 +61,8 @@ common shared-properties
executable hbs2 executable hbs2
import: shared-properties import: shared-properties
main-is: Main.hs main-is: Main.hs
-- other-modules: other-modules:
Paths_hbs2
-- other-extensions: -- other-extensions:
build-depends: base, hbs2-core, hbs2-storage-simple build-depends: base, hbs2-core, hbs2-storage-simple
, aeson , aeson