aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Loader.purs
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2015-07-05 10:13:47 -0400
committereric thul <thul.eric@gmail.com>2015-07-06 23:53:01 -0400
commit1983893bf09a5c2ea1946e156be5da170075af7e (patch)
treef99bda0c0ff3c50356d8ab152336ba461c292494 /src/Loader.purs
parent4558c6cf7879207166b1cc013e2e8112f558bb1d (diff)
downloadpurs-loader-1983893bf09a5c2ea1946e156be5da170075af7e.tar.gz
purs-loader-1983893bf09a5c2ea1946e156be5da170075af7e.tar.zst
purs-loader-1983893bf09a5c2ea1946e156be5da170075af7e.zip
Updating for PureScript 0.7
Resolves #14
Diffstat (limited to 'src/Loader.purs')
-rw-r--r--src/Loader.purs102
1 files changed, 17 insertions, 85 deletions
diff --git a/src/Loader.purs b/src/Loader.purs
index fedc424..0235da9 100644
--- a/src/Loader.purs
+++ b/src/Loader.purs
@@ -1,5 +1,5 @@
1module PursLoader.Loader 1module PursLoader.Loader
2 ( LoaderEff() 2 ( Effects()
3 , loader 3 , loader
4 , loaderFn 4 , loaderFn
5 ) where 5 ) where
@@ -9,113 +9,45 @@ import Control.Monad.Eff (Eff())
9import Control.Monad.Eff.Class (liftEff) 9import Control.Monad.Eff.Class (liftEff)
10import Control.Monad.Eff.Exception (error) 10import Control.Monad.Eff.Exception (error)
11 11
12import Data.Array ((!!), catMaybes, concat, filter, null) 12import Data.Array ((!!))
13import Data.Foldable (foldl)
14import Data.Function (Fn2(), mkFn2) 13import Data.Function (Fn2(), mkFn2)
15import Data.Maybe (Maybe(..), fromMaybe, maybe) 14import Data.Maybe (Maybe(..), fromMaybe, maybe)
16import Data.Set (Set(), empty, insert, member, toList, unions) 15import Data.String.Regex (match, noFlags, regex)
17import Data.String (joinWith, split)
18import Data.String.Regex (Regex(), match, noFlags, regex)
19import Data.StrMap (StrMap(), fromList, lookup)
20import Data.Traversable (sequence)
21import Data.Tuple.Nested (tuple2)
22 16
23import PursLoader.ChildProcess (ChildProcess(), spawn) 17import PursLoader.ChildProcess (ChildProcess(), spawn)
24import PursLoader.FS (FS(), readFileUtf8, readFileUtf8Sync) 18import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query)
25import PursLoader.Glob (Glob(), glob)
26import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, clearDependencies, addDependency, query, resourcePath)
27import PursLoader.LoaderUtil (getRemainingRequest, parseQuery) 19import PursLoader.LoaderUtil (getRemainingRequest, parseQuery)
28import PursLoader.OS (eol) 20import PursLoader.Options (loaderSrcOption, pscOptions)
29import PursLoader.Options (loaderSrcOption, pscMakeOptions, pscMakeDefaultOutput, pscMakeOutputOption)
30import PursLoader.Path (dirname, join, relative, resolve)
31 21
32foreign import cwd "var cwd = process.cwd();" :: String 22type Effects eff = (loader :: Loader, cp :: ChildProcess | eff)
33 23
34moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 24moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
35 25
36importRegex = regex "^\\s*import\\s+(?:qualified\\s+)?([\\w\\.]+)" noFlags { ignoreCase = true } 26pscCommand = "psc"
37
38bowerPattern = join [ "bower_components", "purescript-*", "src" ]
39
40pscMakeCommand = "psc-make"
41
42indexFilename = "index.js"
43 27
44(!!!) = flip (!!) 28(!!!) = flip (!!)
45 29
46pursPattern :: [String] -> String 30loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String)
47pursPattern srcs = join [ "{" ++ joinWith "," ([ bowerPattern ] <> srcs) ++ "}"
48 , "**"
49 , "*.purs"
50 ]
51
52type GraphModule = { file :: String, imports :: [String] }
53
54type Graph = StrMap GraphModule
55
56mkGraph :: forall eff. [String] -> Eff (fs :: FS | eff) Graph
57mkGraph files = (fromList <<< catMaybes) <$> sequence (parse <$> files)
58 where parse file = do source <- readFileUtf8Sync file
59 let key = match moduleRegex source >>= (!!!) 1
60 lines = split eol source
61 imports = catMaybes $ (\a -> match importRegex a >>= (!!!) 1) <$> lines
62 return $ (\a -> tuple2 a { file: file, imports: imports }) <$> key
63
64mkDeps :: forall eff. String -> Graph -> [String]
65mkDeps key graph = toList $ go empty key
66 where
67 go :: Set String -> String -> Set String
68 go acc key =
69 let node = fromMaybe {file: "", imports: []} (lookup key graph)
70 uniq = filter (not <<< flip member acc) node.imports
71 acc' = foldl (flip insert) acc node.imports
72 in if null uniq
73 then acc'
74 else unions $ go acc' <$> uniq
75
76addDeps :: forall eff. LoaderRef -> Graph -> [String] -> Eff (loader :: Loader | eff) Unit
77addDeps ref graph deps = const unit <$> (sequence $ add <$> deps)
78 where add dep = let res = lookup dep graph
79 path = (\a -> resolve a.file) <$> res
80 in maybe (pure unit) (addDependency ref) path
81
82type LoaderAff eff a = Aff (loader :: Loader, glob :: Glob, cp :: ChildProcess, fs :: FS | eff) a
83
84loader' :: forall eff. LoaderRef -> String -> LoaderAff eff (Maybe String)
85loader' ref source = do 31loader' ref source = do
86 liftEff $ cacheable ref 32 liftEff $ cacheable ref
87 33
88 let request = getRemainingRequest ref 34 let request = getRemainingRequest ref
89 parsed = parseQuery $ query ref 35 parsed = parseQuery $ query ref
90 srcs = loaderSrcOption parsed 36 srcs = fromMaybe [] (loaderSrcOption parsed)
91 opts = pscMakeOptions parsed 37 opts = pscOptions parsed
92 pattern = pursPattern $ fromMaybe [] srcs 38 moduleName = match moduleRegex source >>= (!!!) 1
93 key = match moduleRegex source >>= (!!!) 1 39 result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName
94
95 files <- glob pattern
96 graph <- liftEff $ mkGraph files
97
98 let deps = fromMaybe [] $ flip mkDeps graph <$> key
99 outputPath = fromMaybe pscMakeDefaultOutput $ pscMakeOutputOption parsed
100 indexPath = (\a -> join [ outputPath, a, indexFilename ]) <$> key
101
102 liftEff $ clearDependencies ref
103 liftEff $ addDependency ref (resourcePath ref)
104 liftEff $ addDeps ref graph deps
105
106 spawn pscMakeCommand (opts <> files)
107 indexFile <- sequence $ readFileUtf8 <$> indexPath
108 return indexFile
109 40
110type LoaderEff eff a = Eff (loader :: Loader, glob :: Glob, cp :: ChildProcess, fs :: FS | eff) a 41 spawn pscCommand (srcs <> opts)
42 return result
111 43
112loader :: forall eff. LoaderRef -> String -> LoaderEff eff Unit 44loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
113loader ref source = do 45loader ref source = do
114 callback <- async ref 46 callback <- async ref
115 runAff (\e -> callback (Just e) "") 47 runAff (\e -> callback (Just e) "")
116 (maybe (callback (Just $ error "Loader has failed to run") "") 48 (maybe (callback (Just (error "Loader has failed to run")) "")
117 (callback Nothing)) 49 (callback Nothing))
118 (loader' ref source) 50 (loader' ref source)
119 51
120loaderFn :: forall eff. Fn2 LoaderRef String (LoaderEff eff Unit) 52loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
121loaderFn = mkFn2 loader 53loaderFn = mkFn2 loader