2018-06-13 19:47:35 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass, KindSignatures, MultiWayIf #-}
|
2018-06-08 21:40:12 +03:00
|
|
|
|
2018-06-13 19:47:35 +03:00
|
|
|
module Data.Project (
|
|
|
|
-- * Projects
|
|
|
|
ProjectF (..)
|
|
|
|
, Project
|
|
|
|
, PBProject
|
|
|
|
, ProjectException (..)
|
|
|
|
, fromPB
|
|
|
|
, projectExtensions
|
|
|
|
, projectName
|
|
|
|
, projectFiles
|
|
|
|
, readFile
|
2018-10-23 22:28:21 +03:00
|
|
|
, readProjectFromPaths
|
2018-06-13 19:47:35 +03:00
|
|
|
) where
|
2018-04-20 21:10:27 +03:00
|
|
|
|
2018-06-13 19:47:35 +03:00
|
|
|
import Prelude hiding (readFile)
|
2018-10-25 17:21:14 +03:00
|
|
|
import Prologue
|
2018-06-13 19:47:35 +03:00
|
|
|
|
2018-10-17 01:48:08 +03:00
|
|
|
import Control.Effect
|
2018-10-22 17:26:15 +03:00
|
|
|
import Control.Effect.Error
|
2018-06-13 19:47:35 +03:00
|
|
|
import Data.Blob
|
2018-10-23 22:28:21 +03:00
|
|
|
import Data.File
|
2018-06-13 19:47:35 +03:00
|
|
|
import Data.Language
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Proto3.Suite
|
|
|
|
import System.FilePath.Posix
|
2018-10-23 22:28:21 +03:00
|
|
|
import Semantic.IO
|
2018-06-13 19:47:35 +03:00
|
|
|
|
|
|
|
-- | A 'ProjectF' contains all the information that semantic needs
|
|
|
|
-- to execute an analysis, diffing, or graphing pass. It is higher-kinded
|
|
|
|
-- in terms of the container type for paths and blobs, as well as the
|
|
|
|
-- path type (this is necessary because protobuf uses different vector
|
|
|
|
-- representations for @repeated string@ and @repeated Blob@.
|
|
|
|
-- You probably want to use the 'Project' or 'PB' type aliases.
|
|
|
|
data ProjectF (blobs :: * -> *) (paths :: * -> *) path = Project
|
|
|
|
{ projectRootDir :: path
|
|
|
|
, projectBlobs :: blobs Blob
|
2018-05-31 01:04:10 +03:00
|
|
|
, projectLanguage :: Language
|
2018-06-13 19:47:35 +03:00
|
|
|
, projectExcludeDirs :: paths path
|
|
|
|
} deriving (Functor, Generic)
|
|
|
|
|
|
|
|
deriving instance (Eq path, Eq (blobs Blob), Eq (paths path)) => Eq (ProjectF blobs paths path)
|
|
|
|
deriving instance (Show path, Show (blobs Blob), Show (paths path)) => Show (ProjectF blobs paths path)
|
|
|
|
|
|
|
|
-- | This 'Project' type is the one used during semantic's normal
|
|
|
|
-- course of diffing, evaluation, and graphing. You probably want to
|
|
|
|
-- use this one.
|
|
|
|
type Project = ProjectF [] [] FilePath
|
|
|
|
|
|
|
|
-- | This 'Project' type is protobuf-compatible, and corresponds with
|
|
|
|
-- the @Project@ message declaration present in types.proto.
|
|
|
|
type PBProject = ProjectF NestedVec UnpackedVec Text
|
|
|
|
|
|
|
|
deriving instance Message PBProject
|
|
|
|
instance Named PBProject where nameOf _ = "Project"
|
|
|
|
|
|
|
|
-- | Convert from a packed protobuf representation to a more useful one.
|
|
|
|
fromPB :: PBProject -> Project
|
|
|
|
fromPB Project {..} = Project
|
|
|
|
{ projectRootDir = T.unpack projectRootDir
|
|
|
|
, projectBlobs = toList projectBlobs
|
|
|
|
, projectLanguage = projectLanguage
|
|
|
|
, projectExcludeDirs = T.unpack <$> toList projectExcludeDirs
|
2018-06-11 21:43:35 +03:00
|
|
|
}
|
2018-06-08 19:41:07 +03:00
|
|
|
|
2018-06-12 21:10:28 +03:00
|
|
|
projectName :: Project -> Text
|
2018-05-31 01:04:10 +03:00
|
|
|
projectName = T.pack . dropExtensions . takeFileName . projectRootDir
|
2018-04-20 23:30:17 +03:00
|
|
|
|
2018-06-12 21:10:28 +03:00
|
|
|
projectExtensions :: Project -> [String]
|
2018-04-20 23:30:17 +03:00
|
|
|
projectExtensions = extensionsForLanguage . projectLanguage
|
2018-05-16 18:38:00 +03:00
|
|
|
|
2018-06-13 19:47:35 +03:00
|
|
|
projectFiles :: Project -> [File]
|
|
|
|
projectFiles = fmap toFile . projectBlobs
|
2018-06-08 19:41:07 +03:00
|
|
|
|
2018-06-13 19:47:35 +03:00
|
|
|
newtype ProjectException
|
|
|
|
= FileNotFound FilePath
|
|
|
|
deriving (Show, Eq, Typeable, Exception)
|
|
|
|
|
2018-10-17 01:48:08 +03:00
|
|
|
readFile :: (Member (Error SomeException) sig, Applicative m, Carrier sig m)
|
2018-06-13 19:47:35 +03:00
|
|
|
=> Project
|
|
|
|
-> File
|
2018-10-17 01:48:08 +03:00
|
|
|
-> m (Maybe Blob)
|
2018-06-13 19:47:35 +03:00
|
|
|
readFile Project{..} f =
|
|
|
|
let p = filePath f
|
|
|
|
candidate = find (\b -> blobPath b == p) projectBlobs
|
|
|
|
in if
|
|
|
|
| p == "/dev/null" -> pure Nothing
|
|
|
|
| isJust candidate -> pure candidate
|
|
|
|
| otherwise -> throwError (SomeException (FileNotFound p))
|
2018-10-23 22:28:21 +03:00
|
|
|
|
|
|
|
readProjectFromPaths :: MonadIO m => Maybe FilePath -> FilePath -> Language -> [FilePath] -> m Project
|
|
|
|
readProjectFromPaths maybeRoot path lang excludeDirs = do
|
|
|
|
isDir <- isDirectory path
|
|
|
|
let rootDir = if isDir
|
|
|
|
then fromMaybe path maybeRoot
|
|
|
|
else fromMaybe (takeDirectory path) maybeRoot
|
|
|
|
|
|
|
|
paths <- liftIO $ findFilesInDir rootDir exts excludeDirs
|
|
|
|
blobs <- liftIO $ traverse (readBlobFromFile' . toFile) paths
|
|
|
|
pure $ Project rootDir blobs lang excludeDirs
|
|
|
|
where
|
|
|
|
toFile path = File path lang
|
|
|
|
exts = extensionsForLanguage lang
|