~jojo/Carth

ref: 749208029494a7c48ce04444a513424f4b998416 Carth/app/Prebaked.hs -rw-r--r-- 788 bytes
74920802JoJo update TODO 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Prebaked (readCompilerVersion) where

import Language.Haskell.TH
import Language.Haskell.TH.Syntax (Lift(..))
import Data.Maybe
import qualified Text.Megaparsec as M
import qualified Text.Megaparsec.Char as MC
import qualified Text.Megaparsec.Char.Lexer as ML
import Data.Void

type Parser = M.Parsec Void String

readCompilerVersion :: Q Exp
readCompilerVersion = do
    s <- runIO (readFile "carth.cabal")
    let (_, major, minor, patch) =
            head (catMaybes (map (M.parseMaybe pversion) (lines s)))
    lift (major, minor, patch)

pversion :: Parser (Int, Int, Int, Int)
pversion = do
    MC.string "version:" >> MC.space
    a <- num
    b <- dot >> num
    c <- dot >> num
    d <- dot >> num
    pure (a, b, c, d)
  where
    num = ML.decimal
    dot = MC.char '.'