Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions builder/src/AbsoluteSrcDir.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module AbsoluteSrcDir
( AbsoluteSrcDir (..),
fromFilePath,
addRelative,
toFilePath,
)
where

import qualified System.Directory as Dir
import System.FilePath ((</>))

newtype AbsoluteSrcDir
= AbsoluteSrcDir FilePath

toFilePath :: AbsoluteSrcDir -> FilePath
toFilePath (AbsoluteSrcDir path) = path

fromFilePath :: FilePath -> IO AbsoluteSrcDir
fromFilePath srcDir =
AbsoluteSrcDir
<$> Dir.canonicalizePath srcDir

addRelative :: AbsoluteSrcDir -> FilePath -> FilePath
addRelative (AbsoluteSrcDir srcDir) path =
srcDir </> path
32 changes: 8 additions & 24 deletions builder/src/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ where
import qualified AST.Canonical as Can
import qualified AST.Optimized as Opt
import qualified AST.Source as Src
import AbsoluteSrcDir (AbsoluteSrcDir (..))
import qualified AbsoluteSrcDir
import qualified Compile
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar
Expand Down Expand Up @@ -75,31 +77,13 @@ makeEnv key root (Details.Details _ validOutline buildID locals foreigns _) =
case validOutline of
Details.ValidApp givenSrcDirs ->
do
srcDirs <- traverse (toAbsoluteSrcDir root) (NE.toList givenSrcDirs)
srcDirs <- traverse (Outline.toAbsoluteSrcDir root) (NE.toList givenSrcDirs)
return $ Env key root Parse.Application srcDirs buildID locals foreigns
Details.ValidPkg pkg _ _ ->
do
srcDir <- toAbsoluteSrcDir root (Outline.RelativeSrcDir "src")
srcDir <- Outline.toAbsoluteSrcDir root (Outline.RelativeSrcDir "src")
return $ Env key root (Parse.Package pkg) [srcDir] buildID locals foreigns

-- SOURCE DIRECTORY

newtype AbsoluteSrcDir
= AbsoluteSrcDir FilePath

toAbsoluteSrcDir :: FilePath -> Outline.SrcDir -> IO AbsoluteSrcDir
toAbsoluteSrcDir root srcDir =
AbsoluteSrcDir
<$> Dir.canonicalizePath
( case srcDir of
Outline.AbsoluteSrcDir dir -> dir
Outline.RelativeSrcDir dir -> root </> dir
)

addRelative :: AbsoluteSrcDir -> FilePath -> FilePath
addRelative (AbsoluteSrcDir srcDir) path =
srcDir </> path

-- FORK

-- PERF try using IORef semephore on file crawl phase?
Expand Down Expand Up @@ -242,7 +226,7 @@ crawlModule env@(Env _ root projectType srcDirs buildID locals foreigns) mvar do
do
let fileName = ModuleName.toFilePath name <.> "gren"

paths <- filterM File.exists (map (`addRelative` fileName) srcDirs)
paths <- filterM File.exists (map (`AbsoluteSrcDir.addRelative` fileName) srcDirs)

case paths of
[path] ->
Expand Down Expand Up @@ -899,8 +883,8 @@ getRootInfoHelp (Env _ _ _ srcDirs _ _ _) path absolutePath =
case matchingDirs of
d1 : d2 : _ ->
do
let p1 = addRelative d1 (FP.joinPath names <.> "gren")
let p2 = addRelative d2 (FP.joinPath names <.> "gren")
let p1 = AbsoluteSrcDir.addRelative d1 (FP.joinPath names <.> "gren")
let p2 = AbsoluteSrcDir.addRelative d2 (FP.joinPath names <.> "gren")
return $ Left $ Exit.BP_RootNameDuplicate name p1 p2
_ ->
return $ Right $ RootInfo absolutePath path (LInside name)
Expand All @@ -911,7 +895,7 @@ getRootInfoHelp (Env _ _ _ srcDirs _ _ _) path absolutePath =

isInsideSrcDirByName :: [String] -> AbsoluteSrcDir -> IO Bool
isInsideSrcDirByName names srcDir =
File.exists (addRelative srcDir (FP.joinPath names <.> "gren"))
File.exists (AbsoluteSrcDir.addRelative srcDir (FP.joinPath names <.> "gren"))

isInsideSrcDirByPath :: [String] -> AbsoluteSrcDir -> Maybe (FilePath, Either [String] [String])
isInsideSrcDirByPath segments (AbsoluteSrcDir srcDir) =
Expand Down
21 changes: 21 additions & 0 deletions builder/src/Gren/Outline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ module Gren.Outline
decoder,
defaultSummary,
flattenExposed,
toAbsoluteSrcDir,
sourceDirs,
testDirs,
)
where

import AbsoluteSrcDir (AbsoluteSrcDir)
import qualified AbsoluteSrcDir
import Control.Monad (filterM, liftM)
import Data.Binary (Binary, get, getWord8, put, putWord8)
import qualified Data.Map as Map
Expand Down Expand Up @@ -204,6 +209,10 @@ toAbsolute root srcDir =
AbsoluteSrcDir dir -> dir
RelativeSrcDir dir -> root </> dir

toAbsoluteSrcDir :: FilePath -> SrcDir -> IO AbsoluteSrcDir
toAbsoluteSrcDir root srcDir =
AbsoluteSrcDir.fromFilePath (toAbsolute root srcDir)

detectDuplicates :: FilePath -> [SrcDir] -> IO (Maybe (FilePath, (FilePath, FilePath)))
detectDuplicates root srcDirs =
do
Expand All @@ -225,6 +234,18 @@ isDup paths =
OneOrMore.One _ -> Nothing
OneOrMore.More a b -> Just (OneOrMore.getFirstTwo a b)

sourceDirs :: Outline -> NE.List SrcDir
sourceDirs outline =
case outline of
App (AppOutline _ srcDirs _ _ _ _) ->
srcDirs
Pkg _ ->
NE.singleton (RelativeSrcDir "src")

testDirs :: Outline -> NE.List SrcDir
testDirs _ =
NE.singleton (RelativeSrcDir "tests")

-- JSON DECODE

type Decoder a =
Expand Down
44 changes: 44 additions & 0 deletions builder/src/Reporting/Exit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module Reporting.Exit
installToReport,
Reactor (..),
reactorToReport,
Format (..),
formatToReport,
newPackageOverview,
--
Solver (..),
Expand Down Expand Up @@ -2448,3 +2450,45 @@ replToReport problem =
corruptCacheReport
ReplBlocked ->
corruptCacheReport

-- FORMAT

data Format
= FormatPathUnknown FilePath
| FormatStdinWithFiles
| FormatNoOutline
| FormatBadOutline Outline

formatToReport :: Format -> Help.Report
formatToReport problem =
case problem of
FormatPathUnknown path ->
Help.report
"FILE NOT FOUND"
Nothing
"I cannot find this file:"
[ D.indent 4 $ D.red $ D.fromChars path,
D.reflow $ "Is there a typo?",
D.toSimpleNote $
"If you are just getting started, try working through the examples in the\
\ official guide https://guide.gren-lang.org to get an idea of the kinds of things\
\ that typically go in a src/Main.gren file."
]
FormatStdinWithFiles ->
Help.report
"INCOMPATIBLE FLAGS"
Nothing
"Files and stdin cannot be formatted at the same time."
[ D.reflow "You'll need to run `gren format` two separate times if you want to do both."
]
FormatNoOutline ->
Help.report
"FORMAT WHAT?"
Nothing
"I cannot find a gren.json so I am not sure what you want me to format.\
\ Normally you run `gren format` from within a project!"
[ D.reflow $ "If you need to format gren files outside of a project, tell me which files or directories to format:",
D.indent 4 $ D.green $ "gren format Example.gren"
]
FormatBadOutline outline ->
toOutlineReport outline
1 change: 1 addition & 0 deletions compiler/src/Reporting/Doc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Reporting.Doc
P.dullred,
P.dullcyan,
P.dullyellow,
P.dullwhite,
--
fromChars,
fromName,
Expand Down
2 changes: 2 additions & 0 deletions gren.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Executable gren
other-modules:
Bump
Diff
Format
Init
Install
Make
Expand All @@ -68,6 +69,7 @@ Executable gren
Terminal.Internal

-- from builder/
AbsoluteSrcDir
Build
BackgroundWriter
Deps.Diff
Expand Down
13 changes: 13 additions & 0 deletions terminal/impl/Terminal/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Terminal.Helpers
( version,
grenFile,
grenFileOrDirectory,
package,
)
where
Expand Down Expand Up @@ -72,6 +73,18 @@ exampleGrenFiles :: String -> IO [String]
exampleGrenFiles _ =
return ["Main.gren", "src/Main.gren"]

-- GREN FILE OR DIRECTORY

grenFileOrDirectory :: Parser FilePath
grenFileOrDirectory =
Parser
{ _singular = "gren file or directory",
_plural = "gren files and/or directories",
_parser = Just,
_suggest = \_ -> return [],
_examples = \_ -> return ["Main.gren", "src/Examples/"]
}

-- PACKAGE

package :: Parser Pkg.Name
Expand Down
Loading