<commit_before>module Gen where

import           Control.Monad
import           Data.List     (intercalate, intersperse, isPrefixOf)
import qualified Data.Map      as M
import           Data.Monoid
import           Gen.Cabal     (writeCabalFile)
import           Gen.Iface
import           Gen.Log
import           Gen.Modules
import           Gen.Names
import           Gen.State
import           Gen.Types
import           Data.List (sort)

jetpackGen :: IO ()
jetpackGen = do
  dbs       <- trace "loading package DB" getDB
  packages  <- trace "loading packages" (concat <$> mapM getPackageInfos dbs)
  modules   <- trace "loading modules"  (return $ M.fromList $ concatMap packageModulesIface packages)
  reexports <- trace "loading reexports plan" parseReexports
  deps      <- trace "loading cabal deps" parseDeps

  let
    reexportModule previouslyExportedSymbols (prefix, mod) =
      trace ("reexporting.. " <> mod) $ do
        reexports <- findReexports (mod, modules) previouslyExportedSymbols
        printReexports (mod, prefix) reexports previouslyExportedSymbols

  allExportsFinal <- foldM reexportModule [] reexports
  writeCabalFile reexports deps
  writeReexportModule reexports
  asSuccess $ putStrLn "done"
  writeFile (jetpackFolder ++ "full-exported-symbol-list.txt") (intercalate "\n" allExportsFinal)
  let reexportedPackages = ()
  return ()

parseReexports :: IO [(String, String)]
parseReexports = do
  f <- readFile "imports.md"
  return $ map ((\(x:y:_) -> (x,y)). words . drop 4) $ filter (isPrefixOf "  - ") $ lines f

parseDeps :: IO [String]
parseDeps = do
  f <- readFile "imports.md"
  return $ map (drop 4) $ filter (isPrefixOf "  * ") $ lines f

-- IDEA (add benchmark data .)
trace :: (Traversable a) => String -> IO (a b) -> IO (a b)
trace str action = do
  asStep $ putStrLn (str <> "... ")
  result <- action
  asSuccess $ putStrLn ("  OK (" <> show (length result) <> " elems)")
  return result

for :: [a] -> (a->b) -> [b]
for = flip map

writeReexportModule :: [(String, String)] -> IO ()
writeReexportModule reexports = writeFile "jetpack/src/Exports.hs" content
  where<commit_msg>initial Reexport Data<commit_after>module Gen where

import           Control.Monad
import           Data.List     (intercalate, intersperse, isPrefixOf)
import qualified Data.Map      as M
import           Data.Monoid
import           Gen.Cabal     (writeCabalFile)
import           Gen.Iface
import           Gen.Log
import           Gen.Modules
import           Gen.Names
import           Gen.State
import           Gen.Types
import           Data.List (sort)

jetpackGen :: IO ()
jetpackGen = do
  dbs       <- trace "loading package DB" getDB
  packages  <- trace "loading packages" (concat <$> mapM getPackageInfos dbs)
  modules   <- trace "loading modules"  (return $ M.fromList $ concatMap packageModulesIface packages)
  reexports <- trace "loading reexports plan" parseReexports
  deps      <- trace "loading cabal deps" parseDeps

  let
    reexportModule previouslyExportedSymbols (prefix, mod) =
      trace ("reexporting.. " <> mod) $ do
        reexports <- findReexports (mod, modules) previouslyExportedSymbols
        printReexports (mod, prefix) reexports previouslyExportedSymbols

  allExportsFinal <- foldM reexportModule [] reexports
  writeCabalFile reexports deps
  writeReexportModule reexports
  asSuccess $ putStrLn "done"
  writeFile (jetpackFolder ++ "full-exported-symbol-list.txt") (intercalate "\n" allExportsFinal)
  let reexportedPackages = ()
  return ()

data Reexport = Reexport
  { reexportPrefix :: String
  , reexportModule :: String
  } deriving (Eq, Show, Read)

parseReexports :: IO [(String, String)]
parseReexports = do
  f <- readFile "imports.md"
  return $ map ((\(x:y:_) -> (x,y)). words . drop 4) $ filter (isPrefixOf "  - ") $ lines f

parseDeps :: IO [String]
parseDeps = do
  f <- readFile "imports.md"
  return $ map (drop 4) $ filter (isPrefixOf "  * ") $ lines f

-- IDEA (add benchmark data .)
trace :: (Traversable a) => String -> IO (a b) -> IO (a b)
trace str action = do
  asStep $ putStrLn (str <> "... ")
  result <- action
  asSuccess $ putStrLn ("  OK (" <> show (length result) <> " elems)")
  return result

for :: [a] -> (a->b) -> [b]
for = flip map

writeReexportModule :: [(String, String)] -> IO ()
writeReexportModule reexports = writeFile "jetpack/src/Exports.hs" content
  where
{'fraction_non_alphanumeric': 0.11308709530323757, 'fraction_numerical': 0.0009119927040583675, 'mean_word_length': 2.970135746606335, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 29, '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': '6306790', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1364, 'n_words': 410}