tavro-0.1.0.2: src/Tavro/Report.hs
module Tavro.Report
( Format (..),
FileResult (..),
render,
renderText,
renderJson,
CabalFormat (..),
renderCabal,
renderCabalJson,
renderSubmodules,
renderSubmodulesJson,
)
where
import Data.Aeson (Value, encode, object, (.=))
import Data.ByteString.Lazy qualified as LBS
import Data.Containers.ListUtils (nubOrd)
import Data.List (sort)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Tavro.Cabal (Package (..), Verdict (..))
import Tavro.Identify (Hit (..), How (..))
import Tavro.LicenseList (Entry (..), Kind (..))
data Format = Text | Json
deriving stock (Eq, Show)
data FileResult = FileResult
{ frPath :: FilePath,
frHits :: [Hit]
}
render :: Format -> Text -> [FileResult] -> LBS.ByteString
render fmt version rs = case fmt of
Text -> LBS.fromStrict (TE.encodeUtf8 (renderText rs))
Json -> renderJson version rs <> "\n"
-- | one line per file:
--
-- > LICENSE: MIT
-- > COPYING: GPL-2.0-only, GPL-2.0-or-later
-- > NOTICE: Apache-2.0 (lines 2-14)
-- > src/main.c: Apache-2.0 (header, lines 3-15)
-- > README: NOASSERTION
renderText :: [FileResult] -> Text
renderText = T.unlines . map line
where
line FileResult {..} = T.pack frPath <> ": " <> hits frHits
hits [] = "NOASSERTION"
hits hs = T.intercalate ", " (map hit hs)
hit Hit {..} = case hHow of
Whole -> eId hEntry
Inside -> eId hEntry <> " (" <> lines' hLines <> ")"
Header -> eId hEntry <> " (header, " <> lines' hLines <> ")"
lines' (a, b)
| a == b = "line " <> tshow a
| otherwise = "lines " <> tshow a <> "-" <> tshow b
renderJson :: Text -> [FileResult] -> LBS.ByteString
renderJson version rs =
encode $
object
[ "license_list_version" .= version,
"files" .= map file rs
]
where
file FileResult {..} = object ["path" .= frPath, "matches" .= map hit frHits]
hit :: Hit -> Value
hit Hit {..} =
object
[ "id" .= eId hEntry,
"name" .= eName hEntry,
"kind" .= (case eKind hEntry of License -> "license"; Exception -> "exception" :: Text),
"deprecated" .= eDeprecated hEntry,
"osi_approved" .= eOsiApproved hEntry,
"match" .= (case hHow of Whole -> "whole"; Inside -> "inside"; Header -> "header" :: Text),
"lines" .= [fst hLines, snd hLines]
]
-- | the shapes of Terra's %cargo_license macros
data CabalFormat
= -- | @BSD-3-Clause: aeson-2.3.3.0@, like %cargo_license_online
PerPackage
| -- | @# BSD-3-Clause@, like %cargo_license_summary_online
Summary
| -- | one line for License:, like %cargo_license_preamble_online
Preamble
deriving stock (Eq, Show)
renderCabal :: CabalFormat -> [Verdict] -> Text
renderCabal = renderVerdicts (\p -> pName p <> "-" <> pVersion p)
renderSubmodules :: CabalFormat -> [Verdict] -> Text
renderSubmodules = renderVerdicts pName
renderVerdicts :: (Package -> Text) -> CabalFormat -> [Verdict] -> Text
renderVerdicts label fmt vs = case fmt of
PerPackage -> T.unlines (nubOrd (sort [vExpression v <> ": " <> label (vPackage v) | v <- vs]))
Summary -> T.unlines (map ("# " <>) expressions)
-- an expression with operators in it goes in brackets, so AND binds
-- the way it reads
Preamble -> T.intercalate " AND " (map bracket expressions) <> "\n"
where
expressions = nubOrd (sort (map vExpression vs))
bracket e = if any (`elem` ["AND", "OR"]) (T.words e) then "(" <> e <> ")" else e
renderCabalJson :: Text -> [(Verdict, Maybe Text, [FilePath])] -> LBS.ByteString
renderCabalJson version vs =
encode
( object
[ "license_list_version" .= version,
"packages"
.= [ object
[ "name" .= pName (vPackage v),
"version" .= pVersion (vPackage v),
"local" .= pInProject (vPackage v),
"license" .= vExpression v,
"declared" .= declared,
"license_files" .= files,
"notes" .= vNotes v
]
| (v, declared, files) <- vs
]
]
)
<> "\n"
renderSubmodulesJson :: Text -> [(Verdict, [FilePath])] -> [FilePath] -> LBS.ByteString
renderSubmodulesJson version vs missing =
encode
( object
[ "license_list_version" .= version,
"submodules"
.= [ object
[ "path" .= pName (vPackage v),
"commit" .= pVersion (vPackage v),
"license" .= vExpression v,
"license_files" .= files,
"notes" .= vNotes v
]
| (v, files) <- vs
],
"not_checked_out" .= missing
]
)
<> "\n"
tshow :: (Show a) => a -> Text
tshow = T.pack . show