1
1
mirror of https://github.com/github/semantic.git synced 2024-12-27 17:05:33 +03:00
semantic/src/Data/Project.hs

68 lines
2.1 KiB
Haskell
Raw Normal View History

2018-06-08 19:41:07 +03:00
{-# LANGUAGE DeriveAnyClass #-}
module Data.Project where
2018-04-20 21:10:27 +03:00
2018-06-08 19:41:07 +03:00
import Data.Blob
import Data.Language
2018-06-08 19:41:07 +03:00
import qualified Data.Text as T
import Prologue
2018-06-08 19:41:07 +03:00
import Proto3.Suite
import System.FilePath.Posix
2018-04-20 21:10:27 +03:00
2018-06-08 19:41:07 +03:00
data Project blobs paths path = Project
{ projectRootDir :: path
, projectBlobs :: blobs Blob
, projectLanguage :: Language
2018-06-08 19:41:07 +03:00
, projectEntryPaths :: paths path
, projectExcludeDirs :: paths path
} deriving (Functor, Generic, Named)
2018-06-08 19:41:07 +03:00
deriving instance ( MessageField path
, MessageField (paths path)
, MessageField (blobs Blob)
) => Message (Project blobs paths path)
deriving instance (Eq path, Eq (blobs Blob), Eq (paths path)) => Eq (Project blobs paths path)
deriving instance (Show path, Show (blobs Blob), Show (paths path)) => Show (Project blobs paths path)
type Concrete = Project [] [] FilePath
type PB = Project NestedVec UnpackedVec Text
fromPB :: PB -> Concrete
fromPB Project {..} = Project
{ projectRootDir = T.unpack projectRootDir
, projectBlobs = go projectBlobs
, projectLanguage = projectLanguage
, projectEntryPaths = T.unpack <$> go projectEntryPaths
, projectExcludeDirs = T.unpack <$> go projectExcludeDirs
} where go :: Foldable f => f a -> [a]
go = foldr (:) []
projectName :: Concrete -> Text
projectName = T.pack . dropExtensions . takeFileName . projectRootDir
2018-06-08 19:41:07 +03:00
projectExtensions :: Concrete -> [String]
projectExtensions = extensionsForLanguage . projectLanguage
2018-06-08 19:41:07 +03:00
projectEntryPoints :: Concrete -> [File]
projectEntryPoints (Project {..})= foldr go [] projectBlobs
where go b acc =
if blobPath b `elem` projectEntryPaths
then toFile b : acc
else acc
projectFiles :: Concrete -> [File]
projectFiles = fmap toFile . projectBlobs where
data File = File
{ filePath :: FilePath
, fileLanguage :: Language
} deriving (Eq, Ord, Show)
file :: FilePath -> File
file path = File path (languageForFilePath path)
where languageForFilePath = languageForType . takeExtension
2018-06-08 19:41:07 +03:00
toFile :: Blob -> File
toFile (Blob _ p l) = File p l