Skip to content

Commit e8c5eb5

Browse files
Merge pull request #53 from everythingfunctional/finishCommands
Finish commands
2 parents 3a5e426 + 69a430f commit e8c5eb5

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

app/Main.hs

+60-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Options.Applicative ( Parser
2828
import System.Directory ( doesDirectoryExist
2929
, doesFileExist
3030
)
31+
import System.Process ( runCommand )
3132
import Toml ( TomlCodec
3233
, (.=)
3334
)
@@ -40,6 +41,7 @@ data TomlSettings = TomlSettings {
4041
, tomlSettingsProjectName :: String
4142
, tomlSettingsLibrary :: (Maybe Library)
4243
, tomlSettingsExecutables :: [Executable]
44+
, tomlSettingsTests :: [Executable]
4345
}
4446

4547
data AppSettings = AppSettings {
@@ -49,6 +51,7 @@ data AppSettings = AppSettings {
4951
, appSettingsFlags :: [String]
5052
, appSettingsLibrary :: (Maybe Library)
5153
, appSettingsExecutables :: [Executable]
54+
, appSettingsTests :: [Executable]
5255
}
5356

5457
data Library = Library { librarySourceDir :: String }
@@ -74,18 +77,38 @@ main = do
7477

7578
app :: Arguments -> AppSettings -> IO ()
7679
app args settings = case command' args of
77-
Run -> putStrLn "Run"
78-
Test -> putStrLn "Test"
7980
Build -> build settings
81+
Run -> do
82+
build settings
83+
let buildPrefix = appSettingsBuildPrefix settings
84+
let
85+
executableNames = map
86+
(\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } ->
87+
sourceDir </> name
88+
)
89+
(appSettingsExecutables settings)
90+
let executables = map (buildPrefix </>) executableNames
91+
mapM_ runCommand executables
92+
Test -> do
93+
build settings
94+
let buildPrefix = appSettingsBuildPrefix settings
95+
let
96+
executableNames = map
97+
(\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } ->
98+
sourceDir </> name
99+
)
100+
(appSettingsTests settings)
101+
let executables = map (buildPrefix </>) executableNames
102+
mapM_ runCommand executables
80103

81104
build :: AppSettings -> IO ()
82105
build settings = do
83-
putStrLn "Building"
84106
let compiler = appSettingsCompiler settings
85107
let projectName = appSettingsProjectName settings
86108
let buildPrefix = appSettingsBuildPrefix settings
87109
let flags = appSettingsFlags settings
88110
let executables = appSettingsExecutables settings
111+
let tests = appSettingsTests settings
89112
executableDepends <- case appSettingsLibrary settings of
90113
Just librarySettings -> do
91114
let librarySourceDir' = librarySourceDir librarySettings
@@ -112,6 +135,19 @@ build settings = do
112135
mainFile
113136
)
114137
executables
138+
mapM_
139+
(\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } ->
140+
do
141+
buildProgram sourceDir
142+
executableDepends
143+
[".f90", ".f", ".F", ".F90", ".f95", ".f03"]
144+
(buildPrefix </> sourceDir)
145+
compiler
146+
flags
147+
name
148+
mainFile
149+
)
150+
tests
115151

116152
getArguments :: IO Arguments
117153
getArguments = execParser
@@ -159,6 +195,8 @@ settingsCodec =
159195
.= tomlSettingsLibrary
160196
<*> Toml.list executableCodec "executable"
161197
.= tomlSettingsExecutables
198+
<*> Toml.list executableCodec "test"
199+
.= tomlSettingsTests
162200

163201
libraryCodec :: TomlCodec Library
164202
libraryCodec = Library <$> Toml.string "source-dir" .= librarySourceDir
@@ -180,6 +218,7 @@ toml2AppSettings tomlSettings release = do
180218
executableSettings <- getExecutableSettings
181219
(tomlSettingsExecutables tomlSettings)
182220
projectName
221+
testSettings <- getTestSettings $ tomlSettingsTests tomlSettings
183222
return AppSettings
184223
{ appSettingsCompiler = tomlSettingsCompiler tomlSettings
185224
, appSettingsProjectName = projectName
@@ -212,6 +251,7 @@ toml2AppSettings tomlSettings release = do
212251
]
213252
, appSettingsLibrary = librarySettings
214253
, appSettingsExecutables = executableSettings
254+
, appSettingsTests = testSettings
215255
}
216256

217257
getLibrarySettings :: Maybe Library -> IO (Maybe Library)
@@ -239,3 +279,20 @@ getExecutableSettings [] projectName = do
239279
else return []
240280
else return []
241281
getExecutableSettings executables _ = return executables
282+
283+
getTestSettings :: [Executable] -> IO [Executable]
284+
getTestSettings [] = do
285+
defaultDirectoryExists <- doesDirectoryExist "test"
286+
if defaultDirectoryExists
287+
then do
288+
defaultMainExists <- doesFileExist ("test" </> "main.f90")
289+
if defaultMainExists
290+
then return
291+
[ Executable { executableSourceDir = "test"
292+
, executableMainFile = "main.f90"
293+
, executableName = "runTests"
294+
}
295+
]
296+
else return []
297+
else return []
298+
getTestSettings tests = return tests

example_project/test/main.f90

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
program runTests
2+
print *, "Running Tests"
3+
end program runTests

0 commit comments

Comments
 (0)