{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PostfixOperators #-}

-- | The logic to render C# auxiliary files is contained in this module
module Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.CSharpRenderer (
  CSharpProject(..)
) where

import Language.Drasil.Choices (ImplementationType(..))
import Language.Drasil.Code.Imperative.GOOL.ClassInterface (ReadMeInfo(..),
  PackageSym(..), AuxiliarySym(..))
import qualified
  Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.LanguagePolymorphic as
  G (doxConfig, readMe, sampleInput, makefile, noRunIfLib, doxDocConfig,
  docIfEnabled)
import Language.Drasil.Code.Imperative.GOOL.Data (AuxData(..), ad, PackData(..),
  packD)
import Language.Drasil.Code.Imperative.Build.AST (BuildConfig, Runnable,
  asFragment, buildAll, nativeBinary, osClassDefault, executable, sharedLibrary)
import Language.Drasil.Code.Imperative.Doxygen.Import (no)

import GOOL.Drasil (onCodeList, csName, csVersion)

import Prelude hiding (break,print,(<>),sin,cos,tan,floor)
import qualified Prelude as P ((<>))
import Text.PrettyPrint.HughesPJ (Doc)

-- | Holds a C# project.
newtype CSharpProject a = CSP {forall a. CSharpProject a -> a
unCSP :: a}

instance Functor CSharpProject where
  fmap :: forall a b. (a -> b) -> CSharpProject a -> CSharpProject b
fmap a -> b
f (CSP a
x) = forall a. a -> CSharpProject a
CSP (a -> b
f a
x)

instance Applicative CSharpProject where
  pure :: forall a. a -> CSharpProject a
pure = forall a. a -> CSharpProject a
CSP
  (CSP a -> b
f) <*> :: forall a b.
CSharpProject (a -> b) -> CSharpProject a -> CSharpProject b
<*> (CSP a
x) = forall a. a -> CSharpProject a
CSP (a -> b
f a
x)

instance Monad CSharpProject where
  CSP a
x >>= :: forall a b.
CSharpProject a -> (a -> CSharpProject b) -> CSharpProject b
>>= a -> CSharpProject b
f = a -> CSharpProject b
f a
x

instance PackageSym CSharpProject where
  type Package CSharpProject = PackData
  package :: ProgData
-> [CSharpProject (Auxiliary CSharpProject)]
-> CSharpProject (Package CSharpProject)
package ProgData
p = forall (m :: * -> *) a b. Monad m => ([a] -> b) -> [m a] -> m b
onCodeList (ProgData -> [AuxData] -> PackData
packD ProgData
p)

instance AuxiliarySym CSharpProject where
  type Auxiliary CSharpProject = AuxData
  type AuxHelper CSharpProject = Doc
  doxConfig :: String
-> GOOLState
-> Verbosity
-> CSharpProject (Auxiliary CSharpProject)
doxConfig = forall (r :: * -> *).
AuxiliarySym r =>
r (AuxHelper r)
-> String -> GOOLState -> Verbosity -> r (Auxiliary r)
G.doxConfig forall (r :: * -> *). AuxiliarySym r => r (AuxHelper r)
optimizeDox
  readMe :: ReadMeInfo -> CSharpProject (Auxiliary CSharpProject)
readMe ReadMeInfo
rmi =
    forall (r :: * -> *).
AuxiliarySym r =>
ReadMeInfo -> r (Auxiliary r)
G.readMe ReadMeInfo
rmi {
        langName :: String
langName = String
csName,
        langVersion :: String
langVersion = String
csVersion,
        invalidOS :: Maybe String
invalidOS = forall a. a -> Maybe a
Just String
"All OS's except Windows"}
  sampleInput :: ChunkDB
-> DataDesc -> [Expr] -> CSharpProject (Auxiliary CSharpProject)
sampleInput = forall (r :: * -> *).
AuxiliarySym r =>
ChunkDB -> DataDesc -> [Expr] -> r (Auxiliary r)
G.sampleInput

  optimizeDox :: CSharpProject (AuxHelper CSharpProject)
optimizeDox = forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc
no

  makefile :: [String]
-> ImplementationType
-> [Comments]
-> GOOLState
-> ProgData
-> CSharpProject (Auxiliary CSharpProject)
makefile [String]
fs ImplementationType
it [Comments]
cms = forall (r :: * -> *).
AuxiliarySym r =>
Maybe BuildConfig
-> Maybe Runnable
-> Maybe DocConfig
-> GOOLState
-> ProgData
-> r (Auxiliary r)
G.makefile ([String] -> ImplementationType -> Maybe BuildConfig
csBuildConfig [String]
fs ImplementationType
it)
    (ImplementationType -> Maybe Runnable -> Maybe Runnable
G.noRunIfLib ImplementationType
it Maybe Runnable
csRunnable) ([Comments] -> DocConfig -> Maybe DocConfig
G.docIfEnabled [Comments]
cms DocConfig
G.doxDocConfig)

  auxHelperDoc :: CSharpProject (AuxHelper CSharpProject) -> Doc
auxHelperDoc = forall a. CSharpProject a -> a
unCSP
  auxFromData :: String -> Doc -> CSharpProject (Auxiliary CSharpProject)
auxFromData String
fp Doc
d = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ String -> Doc -> AuxData
ad String
fp Doc
d

-- | Create a build configuration for C# files. Takes in 'FilePath's and the type of implementation.
csBuildConfig :: [FilePath] -> ImplementationType -> Maybe BuildConfig
csBuildConfig :: [String] -> ImplementationType -> Maybe BuildConfig
csBuildConfig [String]
fs ImplementationType
it = ([CommandFragment] -> CommandFragment -> [[CommandFragment]])
-> BuildName -> Maybe BuildConfig
buildAll (\[CommandFragment]
i CommandFragment
o -> [String -> String -> String -> CommandFragment
osClassDefault String
"CSC" String
"csc" String
"mcs"
  forall a. a -> [a] -> [a]
: ImplementationType -> [CommandFragment]
target ImplementationType
it forall a. [a] -> [a] -> [a]
++ [String -> CommandFragment
asFragment String
"-out:" forall a. Semigroup a => a -> a -> a
P.<> CommandFragment
o] forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (String -> CommandFragment
asFragment forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"-r:" ++)) [String]
fs
  forall a. [a] -> [a] -> [a]
++ [CommandFragment]
i]) (ImplementationType -> BuildName
outName ImplementationType
it)
  where target :: ImplementationType -> [CommandFragment]
target ImplementationType
Library = [String -> CommandFragment
asFragment String
"-t:library"]
        target ImplementationType
Program = []
        outName :: ImplementationType -> BuildName
outName ImplementationType
Library = BuildName
sharedLibrary
        outName ImplementationType
Program = BuildName
executable

-- | Default runnable information for C# files.
csRunnable :: Maybe Runnable
csRunnable :: Maybe Runnable
csRunnable = Maybe Runnable
nativeBinary