Skip to content

Commit c575b6d

Browse files
committed
Add Compiler.Outline.findSourceFiles
1 parent e053e91 commit c575b6d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/Compiler/Outline.gren

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ module Compiler.Outline exposing
33
, AppOutline
44
, PkgOutline
55
, VersionConstraint(..)
6+
--
7+
, findSourceFiles
8+
--
69
, jsonDecoder
710
, toJson
811
)
912

1013

14+
import Bytes
1115
import SemanticVersion exposing (SemanticVersion)
1216
import SemanticVersionRange exposing (SemanticVersionRange)
17+
import FileSystem
1318
import FileSystem.Path as Path exposing (Path)
1419
import Json.Decode as Decode exposing (Decoder)
1520
import Json.Encode as Encode
@@ -18,6 +23,7 @@ import Compiler.ModuleName as ModuleName exposing (ModuleName)
1823
import Compiler.License as License exposing (License)
1924
import Compiler.Platform as Platform exposing (Platform)
2025
import Dict exposing (Dict)
26+
import Task exposing (Task)
2127

2228

2329
type Outline
@@ -58,6 +64,69 @@ type VersionConstraint a
5864
| LocalPath Path
5965

6066

67+
findSourceFiles : FileSystem.Permission -> Outline -> Path -> Task FileSystem.Error (Array { moduleName : String, source : String })
68+
findSourceFiles fsPerm outline outlinePath =
69+
let
70+
sourceDirs =
71+
when outline is
72+
App appOutline ->
73+
appOutline.sourceDirectories
74+
75+
Pkg _ ->
76+
[ Path.fromPosixString "src"
77+
]
78+
in
79+
Array.map (\srcDir -> Path.append srcDir outlinePath) sourceDirs
80+
|> Array.map (findSourceFilesHelp fsPerm Path.empty)
81+
|> Task.sequence
82+
|> Task.map Array.flatten
83+
|> Task.andThen (\paths -> Task.sequence <| Array.map (readSourceFile fsPerm) paths)
84+
85+
86+
findSourceFilesHelp : FileSystem.Permission -> Path -> Path -> Task FileSystem.Error (Array { absolute : Path, relative : Path })
87+
findSourceFilesHelp fsPerm local root =
88+
FileSystem.listDirectory fsPerm root
89+
|> Task.andThen
90+
(\paths ->
91+
paths
92+
|> Array.mapAndKeepJust
93+
(\entry ->
94+
when entry.entityType is
95+
FileSystem.Directory ->
96+
Just <| findSourceFilesHelp fsPerm (Path.append entry.path local) (Path.append entry.path root)
97+
98+
FileSystem.File ->
99+
if entry.path.extension == "js" || entry.path.extension == "gren" then
100+
Just <| Task.succeed [ { absolute = Path.append entry.path root, relative = Path.append entry.path local } ]
101+
102+
else
103+
Nothing
104+
105+
_ ->
106+
Nothing
107+
)
108+
|> Task.sequence
109+
|> Task.map Array.flatten
110+
)
111+
112+
113+
readSourceFile : FileSystem.Permission -> { absolute : Path, relative : Path } -> Task FileSystem.Error { moduleName : String, source : String }
114+
readSourceFile fsPerm { absolute, relative } =
115+
FileSystem.readFile fsPerm absolute
116+
|> Task.map
117+
(\source ->
118+
{ moduleName =
119+
{ relative | extension = "" }
120+
|> Path.toPosixString
121+
|> String.replace "/" "."
122+
, source =
123+
-- TODO: Error
124+
Bytes.toString source
125+
|> Maybe.withDefault ""
126+
}
127+
)
128+
129+
61130
jsonDecoder : Decoder Outline
62131
jsonDecoder =
63132
Decode.field "type" Decode.string

0 commit comments

Comments
 (0)