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

-- | The logic to render Java auxiliary files is contained in this module
module Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.JavaRenderer (
  JavaProject(..)
) 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, BuildName(..),
  Ext(..), Runnable, NameOpts(NameOpts), asFragment, buildSingle,
  buildAllAdditionalName, includeExt, inCodePackage, interp, mainModule,
  mainModuleFile, packSep, withExt)
import Language.Drasil.Code.Imperative.Doxygen.Import (yes)

import GOOL.Drasil (onCodeList, jName, jVersion)

import Data.List (intercalate)
import Prelude hiding (break,print,sin,cos,tan,floor,(<>))
import Text.PrettyPrint.HughesPJ (Doc)

-- | Name options for Java files.
jNameOpts :: NameOpts
jNameOpts :: NameOpts
jNameOpts = NameOpts {
  packSep :: String
packSep = String
".",
  includeExt :: Bool
includeExt = Bool
False
}

-- | Holds a Java project.
newtype JavaProject a = JP {forall a. JavaProject a -> a
unJP :: a}

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

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

instance Monad JavaProject where
  JP a
x >>= :: forall a b. JavaProject a -> (a -> JavaProject b) -> JavaProject b
>>= a -> JavaProject b
f = a -> JavaProject b
f a
x

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

instance AuxiliarySym JavaProject where
  type Auxiliary JavaProject = AuxData
  type AuxHelper JavaProject = Doc
  doxConfig :: String
-> GOOLState -> Verbosity -> JavaProject (Auxiliary JavaProject)
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 -> JavaProject (Auxiliary JavaProject)
readMe ReadMeInfo
rmi =
    forall (r :: * -> *).
AuxiliarySym r =>
ReadMeInfo -> r (Auxiliary r)
G.readMe ReadMeInfo
rmi {
        langName :: String
langName = String
jName,
        langVersion :: String
langVersion = String
jVersion}
  sampleInput :: ChunkDB
-> DataDesc -> [Expr] -> JavaProject (Auxiliary JavaProject)
sampleInput = forall (r :: * -> *).
AuxiliarySym r =>
ChunkDB -> DataDesc -> [Expr] -> r (Auxiliary r)
G.sampleInput

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

  makefile :: [String]
-> ImplementationType
-> [Comments]
-> GOOLState
-> ProgData
-> JavaProject (Auxiliary JavaProject)
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
jBuildConfig [String]
fs ImplementationType
it)
    (ImplementationType -> Maybe Runnable -> Maybe Runnable
G.noRunIfLib ImplementationType
it ([String] -> Maybe Runnable
jRunnable [String]
fs)) ([Comments] -> DocConfig -> Maybe DocConfig
G.docIfEnabled [Comments]
cms DocConfig
G.doxDocConfig)

  auxHelperDoc :: JavaProject (AuxHelper JavaProject) -> Doc
auxHelperDoc = forall a. JavaProject a -> a
unJP
  auxFromData :: String -> Doc -> JavaProject (Auxiliary JavaProject)
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 Java files. Takes in 'FilePath's and the type of implementation.
jBuildConfig :: [FilePath] -> ImplementationType -> Maybe BuildConfig
jBuildConfig :: [String] -> ImplementationType -> Maybe BuildConfig
jBuildConfig [String]
fs ImplementationType
Program = ([CommandFragment] -> CommandFragment -> [[CommandFragment]])
-> BuildName -> BuildName -> Maybe BuildConfig
buildSingle (\[CommandFragment]
i CommandFragment
_ -> [String -> CommandFragment
asFragment String
"javac" forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map
  String -> CommandFragment
asFragment ([String] -> [String]
classPath [String]
fs) forall a. [a] -> [a] -> [a]
++ [CommandFragment]
i]) (BuildName -> String -> BuildName
withExt (BuildName -> BuildName
inCodePackage BuildName
mainModule)
  String
".class") forall a b. (a -> b) -> a -> b
$ BuildName -> BuildName
inCodePackage BuildName
mainModuleFile
jBuildConfig [String]
fs ImplementationType
Library = ([CommandFragment]
 -> CommandFragment -> CommandFragment -> [[CommandFragment]])
-> BuildName -> BuildName -> Maybe BuildConfig
buildAllAdditionalName (\[CommandFragment]
i CommandFragment
o CommandFragment
a ->
  [String -> CommandFragment
asFragment String
"javac" forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map String -> CommandFragment
asFragment ([String] -> [String]
classPath [String]
fs) forall a. [a] -> [a] -> [a]
++ [CommandFragment]
i,
    forall a b. (a -> b) -> [a] -> [b]
map String -> CommandFragment
asFragment [String
"jar", String
"-cvf"] forall a. [a] -> [a] -> [a]
++ [CommandFragment
o, CommandFragment
a]])
  (BuildName -> Ext -> BuildName
BWithExt BuildName
BPackName forall a b. (a -> b) -> a -> b
$ CommandFragment -> Ext
OtherExt forall a b. (a -> b) -> a -> b
$ String -> CommandFragment
asFragment String
".jar") BuildName
BPackName

-- | Default runnable information for Java files.
jRunnable :: [FilePath] -> Maybe Runnable
jRunnable :: [String] -> Maybe Runnable
jRunnable [String]
fs = BuildName -> NameOpts -> String -> [String] -> Maybe Runnable
interp (forall a b c. (a -> b -> c) -> b -> a -> c
flip BuildName -> String -> BuildName
withExt String
".class" forall a b. (a -> b) -> a -> b
$ BuildName -> BuildName
inCodePackage BuildName
mainModule)
  NameOpts
jNameOpts String
"java" ([String] -> [String]
classPath [String]
fs)

-- | Helper for formating file paths for use in 'jBuildConfig'.
classPath :: [FilePath] -> [String]
classPath :: [String] -> [String]
classPath [String]
fs = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
fs then [] else
  [String
"-cp", String
"\"" forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [[a]] -> [a]
intercalate String
":" ([String]
fs forall a. [a] -> [a] -> [a]
++ [String
"."]) forall a. [a] -> [a] -> [a]
++ String
"\""]