<commit_before>module Cabal2Nix.Package ( cabal2nix, showNixPkg ) where

import Data.List
import Data.Maybe
import Distribution.Compiler
import Distribution.Package
import Distribution.PackageDescription
import Distribution.PackageDescription.Configuration
import Distribution.System
import Distribution.Version
import Text.PrettyPrint

import Cabal2Nix.License
import Cabal2Nix.Name
import Cabal2Nix.CorePackages

type PkgName         = String
type PkgVersion      = [Int]
type PkgSHA256       = String
type PkgURL          = String
type PkgDescription  = String
type PkgLicense      = License
type PkgDependencies = [Dependency] -- [CondTree ConfVar [Dependency] ()]
type PkgBuildTools   = [Dependency]
type PkgExtraLibs    = [String]
type PkgPlatforms    = [String]
type PkgMaintainers  = [String]

data Pkg = Pkg PkgName
               PkgVersion
               PkgSHA256
               PkgURL
               PkgDescription
               PkgLicense
               PkgDependencies
               PkgBuildTools
               PkgExtraLibs
               PkgPlatforms
               PkgMaintainers
  deriving (Show)

showNixPkg :: Pkg -> String
showNixPkg (Pkg name ver sha256 url desc lic deps tools libs platforms maintainers) = render doc
  where
    doc = braces (fsep $ punctuate comma $
                  map text ("cabal" : pkgBuildTools ++ pkgDeps)) <+>
            colon $$ text "" $$
          vcat [
            text "cabal.mkDerivation" <+> lparen <> text "self" <+>
              colon <+> lbrace,
            nest 2 $ vcat [
              attr "pname"   $ doubleQuotes (text name),
              attr "version" $ doubleQuotes showVer,
              attr "sha256"  $ doubleQuotes (text sha256),
              onlyIf pkgBuildTools $
                sep [
                  text "extraBuildInputs" <+> equals <+> lbrack,
                  nest 2 $ fsep $ map text pkgBuildTools,
                  rbrack <> semi
                ],
              onlyIf pkgDeps $<commit_msg>Adapt pretty-printing to coding guidelines. Closes #9.<commit_after>module Cabal2Nix.Package ( cabal2nix, showNixPkg ) where

import Data.List
import Data.Maybe
import Distribution.Compiler
import Distribution.Package
import Distribution.PackageDescription
import Distribution.PackageDescription.Configuration
import Distribution.System
import Distribution.Version
import Text.PrettyPrint

import Cabal2Nix.License
import Cabal2Nix.Name
import Cabal2Nix.CorePackages

type PkgName         = String
type PkgVersion      = [Int]
type PkgSHA256       = String
type PkgURL          = String
type PkgDescription  = String
type PkgLicense      = License
type PkgDependencies = [Dependency] -- [CondTree ConfVar [Dependency] ()]
type PkgBuildTools   = [Dependency]
type PkgExtraLibs    = [String]
type PkgPlatforms    = [String]
type PkgMaintainers  = [String]

data Pkg = Pkg PkgName
               PkgVersion
               PkgSHA256
               PkgURL
               PkgDescription
               PkgLicense
               PkgDependencies
               PkgBuildTools
               PkgExtraLibs
               PkgPlatforms
               PkgMaintainers
  deriving (Show)

prepunctuate :: Doc -> [Doc] -> [Doc]
prepunctuate _ []     = []
prepunctuate p (d:ds) = d : map (p <>) ds


showNixPkg :: Pkg -> String
showNixPkg (Pkg name ver sha256 url desc lic deps tools libs platforms maintainers) = render doc
  where
    doc = sep [
            lbrace <+> (fcat $ prepunctuate (comma <> text " ") $
                        map (nest 2 . text)
                            ("cabal" : pkgBuildTools ++ pkgDeps)),
            rbrace <> colon
          ] $$
          vcat [
            text "",
            text "cabal.mkDerivation" <+> lparen <> text "self" <>
              colon <+> lbrace,
            nest 2 $ vcat [
              attr "pname"   $ doubleQuotes (text name),
              attr "version" $ doubleQuotes showVer,
              attr "sha256"  $ doubleQuotes (text sha256),
              onlyIf pkgBuildTools $
                sep [
                  text "extraBuildInputs" <+> equals <+> lbrack,
                  nest 2 $ fsep $ map text pkgBuildTools,
                  rbrack <> semi
                ],
              onlyIf pkgDeps $
{'fraction_non_alphanumeric': 0.07416154936230515, 'fraction_numerical': 0.01086443079829948, 'mean_word_length': 1.9368932038834952, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6478080', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1163, 'n_words': 331}