module Proteome.Project where

import qualified Control.Lens as Lens (element, firstOf)
import Path (Abs, Dir, Path, dirname, parent)

import Proteome.Data.Env (Env)
import qualified Proteome.Data.Env as Env (currentProjectIndex, mainProject, projects)
import Proteome.Data.Project (Project)
import Proteome.Data.ProjectName (ProjectName (ProjectName))
import Proteome.Data.ProjectRoot (ProjectRoot (ProjectRoot))
import Proteome.Data.ProjectType (ProjectType (ProjectType))
import Proteome.Path (dropSlash)

allProjects ::
  Member (AtomicState Env) r =>
  Sem r [Project]
allProjects :: forall (r :: EffectRow).
Member (AtomicState Env) r =>
Sem r [Project]
allProjects = do
  Project
main <- (Env -> Project) -> Sem r Project
forall s s' (r :: EffectRow).
Member (AtomicState s) r =>
(s -> s') -> Sem r s'
atomicGets Env -> Project
Env.mainProject
  [Project]
extra <- (Env -> [Project]) -> Sem r [Project]
forall s s' (r :: EffectRow).
Member (AtomicState s) r =>
(s -> s') -> Sem r s'
atomicGets Env -> [Project]
Env.projects
  pure $ Project
main Project -> [Project] -> [Project]
forall a. a -> [a] -> [a]
: [Project]
extra

currentProject ::
  Member (AtomicState Env) r =>
  Sem r (Maybe Project)
currentProject :: forall (r :: EffectRow).
Member (AtomicState Env) r =>
Sem r (Maybe Project)
currentProject = do
  Int
index <- (Env -> Int) -> Sem r Int
forall s s' (r :: EffectRow).
Member (AtomicState s) r =>
(s -> s') -> Sem r s'
atomicGets Env -> Int
Env.currentProjectIndex
  Getting (Leftmost Project) [Project] Project
-> [Project] -> Maybe Project
forall a s. Getting (Leftmost a) s a -> s -> Maybe a
Lens.firstOf (Int -> IndexedTraversal' Int [Project] Project
forall (t :: * -> *) a.
Traversable t =>
Int -> IndexedTraversal' Int (t a) a
Lens.element Int
index) ([Project] -> Maybe Project)
-> Sem r [Project] -> Sem r (Maybe Project)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r [Project]
forall (r :: EffectRow).
Member (AtomicState Env) r =>
Sem r [Project]
allProjects

pathData :: Path Abs Dir -> (ProjectRoot, ProjectName, ProjectType)
pathData :: Path Abs Dir -> (ProjectRoot, ProjectName, ProjectType)
pathData Path Abs Dir
root =
  (
    Path Abs Dir -> ProjectRoot
ProjectRoot Path Abs Dir
root,
    Text -> ProjectName
ProjectName (Text -> ProjectName)
-> (Path Abs Dir -> Text) -> Path Abs Dir -> ProjectName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path Rel Dir -> Text
forall b t. Path b t -> Text
dropSlash (Path Rel Dir -> Text)
-> (Path Abs Dir -> Path Rel Dir) -> Path Abs Dir -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path Abs Dir -> Path Rel Dir
forall b. Path b Dir -> Path Rel Dir
dirname (Path Abs Dir -> ProjectName) -> Path Abs Dir -> ProjectName
forall a b. (a -> b) -> a -> b
$ Path Abs Dir
root,
    Text -> ProjectType
ProjectType (Text -> ProjectType)
-> (Path Abs Dir -> Text) -> Path Abs Dir -> ProjectType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path Rel Dir -> Text
forall b t. Path b t -> Text
dropSlash (Path Rel Dir -> Text)
-> (Path Abs Dir -> Path Rel Dir) -> Path Abs Dir -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path Abs Dir -> Path Rel Dir
forall b. Path b Dir -> Path Rel Dir
dirname (Path Abs Dir -> Path Rel Dir)
-> (Path Abs Dir -> Path Abs Dir) -> Path Abs Dir -> Path Rel Dir
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path Abs Dir -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent (Path Abs Dir -> ProjectType) -> Path Abs Dir -> ProjectType
forall a b. (a -> b) -> a -> b
$ Path Abs Dir
root
  )