From 47b9dc226e49f6b3b59bba209665038598dfb439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janne=20He=C3=9F?= Date: Wed, 7 Aug 2024 10:31:07 +0200 Subject: [PATCH] Allow overwriting the version with a .version file --- MAINTENANCE.md | 3 +++ main/Main.hs | 10 +++++++++- nixfmt.cabal | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/MAINTENANCE.md b/MAINTENANCE.md index 172b9901..ccf58b94 100644 --- a/MAINTENANCE.md +++ b/MAINTENANCE.md @@ -1,5 +1,8 @@ # Maintainer documentation +Writing to .version in the root of the project causes this version to be returned by `nixfmt --version` +rather than the version from the cabalfile. + ## Making a new release - Check the commit log if anything is missing from the change log. diff --git a/main/Main.hs b/main/Main.hs index 1efb8280..d7348a6e 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -1,11 +1,14 @@ {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE TemplateHaskell #-} module Main where import Control.Concurrent (Chan, forkIO, newChan, readChan, writeChan) +import Data.ByteString.Char8 (unpack) import Data.Either (lefts) +import Data.FileEmbed import Data.List (isSuffixOf) import Data.Text (Text) import qualified Data.Text.IO as TextIO (getContents, hPutStr, putStr) @@ -46,6 +49,11 @@ data Nixfmt = Nixfmt } deriving (Show, Data, Typeable) +versionFromFile :: String +versionFromFile = case $(embedFileIfExists ".version") of + Just ver -> unpack ver + _ -> showVersion version + options :: Nixfmt options = let defaultWidth = 100 @@ -67,7 +75,7 @@ options = &= help "Pretty print the internal AST, only for debugging" } - &= summary ("nixfmt v" ++ showVersion version) + &= summary ("nixfmt " ++ versionFromFile) &= help "Format Nix source code" data Target = Target diff --git a/nixfmt.cabal b/nixfmt.cabal index 35f532f5..33095527 100644 --- a/nixfmt.cabal +++ b/nixfmt.cabal @@ -31,7 +31,9 @@ executable nixfmt hs-source-dirs: main build-depends: base >= 4.12.0 && < 4.19 + , bytestring , cmdargs >= 0.10.20 && < 0.11 + , file-embed , nixfmt , unix >= 2.7.2 && < 2.9 , text >= 1.2.3 && < 2.2