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 1/2] 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 From c37f999b60b1c04113996120a49217a0c0351d38 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Wed, 7 Aug 2024 23:29:31 +0200 Subject: [PATCH 2/2] Rewrite versionFromFile using maybe To avoid this warning: main/Main.hs:54:3: warning: [-Woverlapping-patterns] Pattern match is redundant In a case alternative: Just ver -> ... | 54 | Just ver -> unpack ver | ^^^^^^^^^^^^^^^^^^^^^^ Which happens because to the compiler, the Template Haskell expression looks like a constant, always making one pattern redundant. It won't dig into that if we use a function on it though :) --- main/Main.hs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main/Main.hs b/main/Main.hs index d7348a6e..3f26a470 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -50,9 +50,7 @@ data Nixfmt = Nixfmt deriving (Show, Data, Typeable) versionFromFile :: String -versionFromFile = case $(embedFileIfExists ".version") of - Just ver -> unpack ver - _ -> showVersion version +versionFromFile = maybe (showVersion version) unpack $(embedFileIfExists ".version") options :: Nixfmt options =