{-# 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 (PackageSym(..), AuxiliarySym(..))
import Language.Drasil.Code.Imperative.ReadMe.Import (ReadMeInfo(..))
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 Drasil.GOOL (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) = b -> CSharpProject b
forall a. a -> CSharpProject a
CSP (a -> b
f a
x)

instance Applicative CSharpProject where
  pure :: forall a. a -> CSharpProject a
pure = a -> CSharpProject a
forall a. a -> CSharpProject a
CSP
  (CSP a -> b
f) <*> :: forall a b.
CSharpProject (a -> b) -> CSharpProject a -> CSharpProject b
<*> (CSP a
x) = b -> CSharpProject b
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 = ([AuxData] -> PackData)
-> [CSharpProject AuxData] -> CSharpProject PackData
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 = CSharpProject (AuxHelper CSharpProject)
-> String
-> GOOLState
-> Verbosity
-> CSharpProject (Auxiliary CSharpProject)
forall (r :: * -> *).
AuxiliarySym r =>
r (AuxHelper r)
-> String -> GOOLState -> Verbosity -> r (Auxiliary r)
G.doxConfig CSharpProject (AuxHelper CSharpProject)
forall (r :: * -> *). AuxiliarySym r => r (AuxHelper r)
optimizeDox
  readMe :: ReadMeInfo -> CSharpProject (Auxiliary CSharpProject)
readMe ReadMeInfo
rmi =
    ReadMeInfo -> CSharpProject (Auxiliary CSharpProject)
forall (r :: * -> *).
AuxiliarySym r =>
ReadMeInfo -> r (Auxiliary r)
G.readMe ReadMeInfo
rmi {
        langName = csName,
        langVersion = csVersion,
        invalidOS = Just "All OS's except Windows"}
  sampleInput :: ChunkDB
-> DataDesc -> [Expr] -> CSharpProject (Auxiliary CSharpProject)
sampleInput = ChunkDB
-> DataDesc -> [Expr] -> CSharpProject (Auxiliary CSharpProject)
forall (r :: * -> *).
AuxiliarySym r =>
ChunkDB -> DataDesc -> [Expr] -> r (Auxiliary r)
G.sampleInput

  optimizeDox :: CSharpProject (AuxHelper CSharpProject)
optimizeDox = Doc -> CSharpProject Doc
forall a. a -> CSharpProject a
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 = Maybe BuildConfig
-> Maybe Runnable
-> Maybe DocConfig
-> GOOLState
-> ProgData
-> CSharpProject (Auxiliary CSharpProject)
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 = CSharpProject Doc -> Doc
CSharpProject (AuxHelper CSharpProject) -> Doc
forall a. CSharpProject a -> a
unCSP
  auxFromData :: String -> Doc -> CSharpProject (Auxiliary CSharpProject)
auxFromData String
fp Doc
d = AuxData -> CSharpProject AuxData
forall a. a -> CSharpProject a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AuxData -> CSharpProject AuxData)
-> AuxData -> CSharpProject AuxData
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"
  CommandFragment -> [CommandFragment] -> [CommandFragment]
forall a. a -> [a] -> [a]
: ImplementationType -> [CommandFragment]
target ImplementationType
it [CommandFragment] -> [CommandFragment] -> [CommandFragment]
forall a. [a] -> [a] -> [a]
++ [String -> CommandFragment
asFragment String
"-out:" CommandFragment -> CommandFragment -> CommandFragment
forall a. Semigroup a => a -> a -> a
P.<> CommandFragment
o] [CommandFragment] -> [CommandFragment] -> [CommandFragment]
forall a. [a] -> [a] -> [a]
++ (String -> CommandFragment) -> [String] -> [CommandFragment]
forall a b. (a -> b) -> [a] -> [b]
map (String -> CommandFragment
asFragment (String -> CommandFragment)
-> (String -> String) -> String -> CommandFragment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"-r:" ++)) [String]
fs
  [CommandFragment] -> [CommandFragment] -> [CommandFragment]
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