diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/FS.purs | 35 | ||||
-rw-r--r-- | src/Loader.purs | 23 | ||||
-rw-r--r-- | src/LoaderRef.purs | 24 |
3 files changed, 79 insertions, 3 deletions
diff --git a/src/FS.purs b/src/FS.purs index a56fe26..6955a63 100644 --- a/src/FS.purs +++ b/src/FS.purs | |||
@@ -1,12 +1,16 @@ | |||
1 | module PursLoader.FS | 1 | module PursLoader.FS |
2 | ( FS() | 2 | ( FS() |
3 | , writeFileUtf8 | 3 | , writeFileUtf8 |
4 | , findFileUtf8 | ||
4 | ) where | 5 | ) where |
5 | 6 | ||
6 | import Control.Monad.Aff (Aff(), makeAff) | 7 | import Control.Monad.Aff (Aff(), makeAff) |
7 | import Control.Monad.Eff (Eff()) | 8 | import Control.Monad.Eff (Eff()) |
8 | import Control.Monad.Eff.Exception (Error()) | 9 | import Control.Monad.Eff.Exception (Error()) |
9 | 10 | ||
11 | import Data.Maybe (Maybe(..)) | ||
12 | import Data.String.Regex (Regex()) | ||
13 | |||
10 | import Data.Function | 14 | import Data.Function |
11 | 15 | ||
12 | foreign import data FS :: ! | 16 | foreign import data FS :: ! |
@@ -30,3 +34,34 @@ function writeFileUtf8Fn(filepath, contents, errback, callback) { | |||
30 | (Error -> Eff (fs :: FS | eff) Unit) | 34 | (Error -> Eff (fs :: FS | eff) Unit) |
31 | (Unit -> Eff (fs :: FS | eff) Unit) | 35 | (Unit -> Eff (fs :: FS | eff) Unit) |
32 | (Eff (fs :: FS | eff) Unit) | 36 | (Eff (fs :: FS | eff) Unit) |
37 | |||
38 | findFileUtf8 :: forall eff. Regex -> [String] -> Aff (fs :: FS | eff) (Maybe String) | ||
39 | findFileUtf8 regexp filepaths = makeAff $ runFn6 findFileUtf8Fn Nothing Just regexp filepaths | ||
40 | |||
41 | foreign import findFileUtf8Fn """ | ||
42 | function findFileUtf8Fn(nothing, just, regex, filepaths, errback, callback) { | ||
43 | return function(){ | ||
44 | var fs = require('fs'); | ||
45 | |||
46 | var async = require('async'); | ||
47 | |||
48 | function findFile(filepath, callback) { | ||
49 | fs.readFile(filepath, {encoding: 'utf-8'}, function(error, result){ | ||
50 | if (error) callback(false); | ||
51 | else callback(regex.test(result)); | ||
52 | }); | ||
53 | } | ||
54 | |||
55 | async.detect(filepaths, findFile, function(result){ | ||
56 | if (!result) callback(nothing)(); | ||
57 | else callback(just(result))(); | ||
58 | }); | ||
59 | }; | ||
60 | } | ||
61 | """ :: forall eff. Fn6 (Maybe String) | ||
62 | (String -> Maybe String) | ||
63 | Regex | ||
64 | [String] | ||
65 | (Error -> Eff (fs :: FS | eff) Unit) | ||
66 | (Maybe String -> Eff (fs :: FS | eff) Unit) | ||
67 | (Eff (fs :: FS | eff) Unit) | ||
diff --git a/src/Loader.purs b/src/Loader.purs index 872a51c..e9e03c4 100644 --- a/src/Loader.purs +++ b/src/Loader.purs | |||
@@ -13,12 +13,12 @@ import Data.Array ((!!), concat) | |||
13 | import Data.Function (Fn2(), mkFn2) | 13 | import Data.Function (Fn2(), mkFn2) |
14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | 14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) |
15 | import Data.String (joinWith) | 15 | import Data.String (joinWith) |
16 | import Data.String.Regex (match, noFlags, regex) | 16 | import Data.String.Regex (match, noFlags, regex, test) |
17 | 17 | ||
18 | import PursLoader.ChildProcess (ChildProcess(), spawn) | 18 | import PursLoader.ChildProcess (ChildProcess(), spawn) |
19 | import PursLoader.FS (FS(), writeFileUtf8) | 19 | import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) |
20 | import PursLoader.Glob (Glob(), globAll) | 20 | import PursLoader.Glob (Glob(), globAll) |
21 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) | 21 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) |
22 | import PursLoader.LoaderUtil (parseQuery) | 22 | import PursLoader.LoaderUtil (parseQuery) |
23 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) | 23 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) |
24 | 24 | ||
@@ -26,6 +26,8 @@ type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | |||
26 | 26 | ||
27 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 27 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
28 | 28 | ||
29 | foreignRegex = regex "(?:^|\\n)\\s*foreign import\\s+" noFlags { ignoreCase = true } | ||
30 | |||
29 | pscCommand = "psc" | 31 | pscCommand = "psc" |
30 | 32 | ||
31 | psciCommand = "psci" | 33 | psciCommand = "psci" |
@@ -54,6 +56,11 @@ mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign < | |||
54 | loadForeign :: String -> String | 56 | loadForeign :: String -> String |
55 | loadForeign a = ":f " ++ relative cwd a | 57 | loadForeign a = ":f " ++ relative cwd a |
56 | 58 | ||
59 | findFFI :: forall eff. [[String]] -> String -> Aff (fs :: FS | eff) (Maybe String) | ||
60 | findFFI ffiss name = findFileUtf8 re (concat ffiss) | ||
61 | where | ||
62 | re = regex ("(?:^|\\n)//\\s*module\\s*" ++ name ++ "\\s*\\n") noFlags | ||
63 | |||
57 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) | 64 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) |
58 | loader' ref source = do | 65 | loader' ref source = do |
59 | liftEff $ cacheable ref | 66 | liftEff $ cacheable ref |
@@ -73,8 +80,18 @@ loader' ref source = do | |||
73 | writeFileUtf8 psciFilename psciFile | 80 | writeFileUtf8 psciFilename psciFile |
74 | 81 | ||
75 | let moduleName = match moduleRegex source >>= (!!!) 1 | 82 | let moduleName = match moduleRegex source >>= (!!!) 1 |
83 | hasForeign = test foreignRegex source | ||
76 | result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName | 84 | result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName |
77 | 85 | ||
86 | liftEff (clearDependencies ref) | ||
87 | liftEff (addDependency ref (resourcePath ref)) | ||
88 | |||
89 | foreignPath <- if hasForeign | ||
90 | then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) | ||
91 | else pure Nothing | ||
92 | |||
93 | fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) | ||
94 | |||
78 | return result | 95 | return result |
79 | 96 | ||
80 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 97 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |
diff --git a/src/LoaderRef.purs b/src/LoaderRef.purs index 2567b1e..f1efa04 100644 --- a/src/LoaderRef.purs +++ b/src/LoaderRef.purs | |||
@@ -4,6 +4,9 @@ module PursLoader.LoaderRef | |||
4 | , async | 4 | , async |
5 | , cacheable | 5 | , cacheable |
6 | , query | 6 | , query |
7 | , clearDependencies | ||
8 | , addDependency | ||
9 | , resourcePath | ||
7 | ) where | 10 | ) where |
8 | 11 | ||
9 | import Control.Monad.Eff (Eff()) | 12 | import Control.Monad.Eff (Eff()) |
@@ -48,3 +51,24 @@ foreign import query """ | |||
48 | function query(ref){ | 51 | function query(ref){ |
49 | return ref.query; | 52 | return ref.query; |
50 | }""" :: LoaderRef -> String | 53 | }""" :: LoaderRef -> String |
54 | |||
55 | foreign import clearDependencies """ | ||
56 | function clearDependencies(ref){ | ||
57 | return function(){ | ||
58 | return ref.clearDependencies(); | ||
59 | }; | ||
60 | }""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit | ||
61 | |||
62 | foreign import resourcePath """ | ||
63 | function resourcePath(ref){ | ||
64 | return ref.resourcePath; | ||
65 | }""" :: LoaderRef -> String | ||
66 | |||
67 | foreign import addDependency """ | ||
68 | function addDependency(ref){ | ||
69 | return function(dep){ | ||
70 | return function(){ | ||
71 | return ref.addDependency(dep); | ||
72 | }; | ||
73 | }; | ||
74 | }""" :: forall eff. LoaderRef -> String -> Eff (loader :: Loader | eff) Unit | ||