packages feed

HaskellAnalysisProgram-0.1.0: src/FileOperation.hs

{-|
Module      : FileOperation
Description : For operations regarding file system io
Copyright   : Copyright (C) 2019 S. Kamps
License     : -- This file is distributed under the  terms of the Apache License 2.0.
              For more information, see the file "LICENSE", which is included in the distribution.
Stability   : experimental
-}

module FileOperation
  where

import Control.Monad (when, forM, forM_)
import System.FilePath.Posix ((</>))
import System.Directory (doesDirectoryExist, removeDirectoryRecursive ,createDirectory, doesFileExist, removeFile)

import Settings

import qualified MetaHS.DataModel.MetaModel as MetaModel
import MetaHS.EDSL.MetaModel (writeMetaModel, writeMetaModelPretty, readMetaModel)

-- | Cleans the output directory based on the provided Settings.
mrproper :: Settings -- ^ IUT settings.
          -> IO ()   -- ^ Nothing is returned.
mrproper settings = do
    do
        let d = outputDirectory settings
        needsRemoval <- doesDirectoryExist d
        Control.Monad.when needsRemoval $ removeDirectoryRecursive d

    createDirectory $ outputDirectory settings
    return ()

-- | Checks whether the analysis output directory exists. If not it will be created.
setupOutputDirectory :: Settings -- ^ IUT settings.
                     -> IO ()
setupOutputDirectory settings = do
  let d = outputDirectory settings
  mkdir <- doesDirectoryExist d
  if mkdir
    then return ()
    else createDirectory $ outputDirectory settings
  return ()

-- | Checks whether the file exists.
isFileThere :: FilePath -- ^ IUT settings.
            -> IO Bool
isFileThere filePath = do
  let f = filePath
  needsRemoval <- doesFileExist f
  if needsRemoval
    then return True
    else return False


-- | Stores the MetaModel to two files based on the provided Settings.
writeMetaModeltoFile :: Settings               -- ^ File system settings.
                        -> MetaModel.MetaModel -- ^ The MetaModel to write.
                        -> IO ()               -- ^ Nothing is returned.
writeMetaModeltoFile settings metaModel = do
    writeMetaModel metaModel $ metaModelFile settings
    writeMetaModelPretty metaModel $ metaModelPrettyFile settings
    return ()


-- | Reads the MetaModel from a file based on the provided Settings.
readMetaModelFromFile :: Settings                -- ^ File system settings.
                      -> IO MetaModel.MetaModel  -- ^ The MetaModel.
readMetaModelFromFile settings = readMetaModel $ metaModelFile settings


-- | Returns the project directory.
datasetDirectory :: FilePath  -- ^ The FilePath for the projects directory.
datasetDirectory  = "dataset"


-- | Returns the input directory for the implementation-under-test based on the provided Settings.
inputDirectory :: Settings    -- ^ File system settings.
                 -> FilePath  -- ^ The FilePath for the projects directory.
inputDirectory settings =
    datasetDirectory </> "inputIUT" </> programName settings


-- | Determines the FilePath for the implementation-under-test sources directory.
sourcesDirectory :: Settings  -- ^ File system settings.
                 -> FilePath  -- ^ The FilePath for the sources sub-directory of the projects directory.
sourcesDirectory = inputDirectory -- </> sourceSubDirectory settings


-- | Determines the FilePath for the graphs output directory.
outputDirectory :: Settings -- ^ File system settings.
                -> FilePath -- ^ The FilePath for the output directory.
outputDirectory settings =
    datasetDirectory </> "outputIUT" </> programName settings


-- | Determines the FilePath for the MetaModel file.
metaModelFile :: Settings -- ^ File system settings.
              -> FilePath -- ^ The FilePath for the MetaModel file.
metaModelFile settings =
    outputDirectory settings </> "metaModel.txt"


-- | Determines the FilePath for the pretty printed MetaModel file.
metaModelPrettyFile :: Settings -- ^ File system settings.
                    -> FilePath -- ^ The FilePath for the pretty printed MetaModel file.
metaModelPrettyFile settings =
    outputDirectory settings </> "metaModel_pretty.txt"


-- | Determines the FilePath for the parse error file.
parseErrorsFile :: Settings -- ^ File system settings.
                -> FilePath -- ^ The FilePath for the parse errors file.
parseErrorsFile settings =
    outputDirectory settings </> "parseErrors.txt"

-- | Determines the FilePath for the metric data file.
metricOutputFile :: Settings -- ^ File system settings.
                 -> String   -- ^ Metrics's name (aka key)
                 -> FilePath -- ^ The FilePath for the metric data file.
metricOutputFile settings metricName =
    outputDirectory settings </> metricName ++ ".csv"