<commit_before>{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax, CPP, RecordWildCards #-}

module Main where

import Control.Exception (catch, SomeException)
import Prelude hiding (catch)
import System.Environment (getArgs)
import System.Console.GetOpt (getOpt, OptDescr(..), ArgOrder(..), ArgDescr(..), usageInfo)
import System.Directory (createDirectoryIfMissing)
import System.FilePath.Posix ((</>))

import Language
import Parse
import Swift
import Util

bbURL = "http://j.mp/burnbar"
version = " v0.5.28"

main = do
  args ← getArgs
  let (actions, _, _) = getOpt RequireOrder options args
  let (Options {..}) = foldr ($) defaults actions
  let copy = (("// 📏🔥 Generated with " ⧺ bbURL ⧺ version ⧺ "\n\n") ⧺)
  let write = (∘ copy) ∘ writeFile ∘ (root </>)
  spec ← spec ≫= return ∘ parse
  let (ent, int) = translator (swift transport interface) spec
  (createDir root ≫ write entFn ent ≫ write intFn int)
      `catch` handleEx "Error ¬ ¬: "
#ifdef DEBUG
  print (spec, ent, int)
#endif

data Options = Options { transport ∷ Typename, interface ∷ Typename , spec ∷ IO String,
                         root ∷ FilePath, entFn ∷ FilePath, intFn ∷ FilePath }

defaults = Options "Transport" "Interface" (readFile "spec.burnbar") "./" entFn intFn
  where { intFn = "Interface.swift"; entFn = "Entities.swift" }

options ∷ [OptDescr (Options → Options)]
options = let opt (k, f, a, h) = Option k f a h in map opt
  [ ("v", ["version"], NoArg ver, "print version number"), ("h", ["help"], NoArg use, "print help")
  , ("t", ["transport"], ReqArg (\a o → o {transport = a}) "Transport", "transport protocol name")
  , ("i", ["interface"], ReqArg (\a o → o {interface = a}) "Iterface", "interface class name")
  , ("r", ["interface-file"], ReqArg (\a o → o {intFn = a}) "Interface.swift", "interface out filename")
  , ("d", ["entities-file"], ReqArg (\a o → o {entFn = a}) "Entities.swift", "entities out filename")
  , ("s", ["spec-file"], ReqArg (\a o → o {spec = readFile a}) "spec.burnbar", "input spec file")
  , ("p", ["path"], ReqArg (\a o → o {root = a}) ".", "output path prefix") ]

use _ = error $ usageInfo ("Usage: burningbar [-vhtirdsp]\n" ⧺ bbURL ⧺ version) options
ver _ = error $ bbURL ⧺ version

createDir name = createDirectoryIfMissing True name `catch` handleEx "Can't create dir."
handleEx err (e ∷ SomeException) = error (err ⧺ show e)

<commit_msg>minor<commit_after>{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax, CPP, RecordWildCards #-}

module Main where

import Control.Exception (catch, SomeException)
import Prelude hiding (catch)
import System.Environment (getArgs)
import System.Console.GetOpt (getOpt, OptDescr(..), ArgOrder(..), ArgDescr(..), usageInfo)
import System.Directory (createDirectoryIfMissing)
import System.FilePath.Posix ((</>))

import Language
import Parse
import Swift
import Util

bbURL = "http://j.mp/burnbar"
version = " v0.5.28"

main = do
  args ← getArgs
  let (actions, _, _) = getOpt RequireOrder options args
  let (Options {..}) = foldr ($) defaults actions
  let copy = (("// 📏🔥 Generated with " ⧺ bbURL ⧺ version ⧺ "\n\n") ⧺)
  let write = (∘ copy) ∘ writeFile ∘ (root </>)
  spec ← spec ≫= return ∘ parse
  let (ent, int) = translator (swift transport interface) spec
  (createDir root ≫ write entFn ent ≫ write intFn int)
      `catch` handleEx
#ifdef DEBUG
  print (spec, ent, int)
#endif

data Options = Options { transport ∷ Typename, interface ∷ Typename , spec ∷ IO String,
                         root ∷ FilePath, entFn ∷ FilePath, intFn ∷ FilePath }

defaults = Options "Transport" "Interface" (readFile "spec.burnbar") "./" entFn intFn
  where { intFn = "Interface.swift"; entFn = "Entities.swift" }

options ∷ [OptDescr (Options → Options)]
options = let opt (k, f, a, h) = Option k f a h in map opt
  [ ("v", ["version"], NoArg ver, "print version number"), ("h", ["help"], NoArg use, "print help")
  , ("t", ["transport"], ReqArg (\a o → o {transport = a}) "Transport", "transport protocol name")
  , ("i", ["interface"], ReqArg (\a o → o {interface = a}) "Iterface", "interface class name")
  , ("r", ["interface-file"], ReqArg (\a o → o {intFn = a}) "Interface.swift", "interface out filename")
  , ("d", ["entities-file"], ReqArg (\a o → o {entFn = a}) "Entities.swift", "entities out filename")
  , ("s", ["spec-file"], ReqArg (\a o → o {spec = readFile a}) "spec.burnbar", "input spec file")
  , ("p", ["path"], ReqArg (\a o → o {root = a}) ".", "output path prefix") ]

use _ = error $ usageInfo ("Usage: burningbar [-vhtirdsp]\n" ⧺ bbURL ⧺ version) options
ver _ = error $ bbURL ⧺ version

createDir name = createDirectoryIfMissing True name `catch` handleEx
handleEx (e ∷ SomeException) = error (show e)


{'fraction_non_alphanumeric': 0.16673773987206822, 'fraction_numerical': 0.0017057569296375266, 'mean_word_length': 3.937894736842105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6228208', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1576, 'n_words': 591}