aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/FS.purs35
-rw-r--r--src/Glob.purs22
-rw-r--r--src/Loader.purs126
-rw-r--r--src/LoaderRef.purs25
-rw-r--r--src/LoaderUtil.purs9
-rw-r--r--src/OS.purs3
-rw-r--r--src/Options.purs50
-rw-r--r--src/Path.purs36
8 files changed, 100 insertions, 206 deletions
diff --git a/src/FS.purs b/src/FS.purs
index 68fe2f9..a56fe26 100644
--- a/src/FS.purs
+++ b/src/FS.purs
@@ -1,7 +1,6 @@
1module PursLoader.FS 1module PursLoader.FS
2 ( FS() 2 ( FS()
3 , readFileUtf8 3 , writeFileUtf8
4 , readFileUtf8Sync
5 ) where 4 ) where
6 5
7import Control.Monad.Aff (Aff(), makeAff) 6import Control.Monad.Aff (Aff(), makeAff)
@@ -12,34 +11,22 @@ import Data.Function
12 11
13foreign import data FS :: ! 12foreign import data FS :: !
14 13
15readFileUtf8 :: forall eff. String -> Aff (fs :: FS | eff) String 14writeFileUtf8 :: forall eff. String -> String -> Aff (fs :: FS | eff) Unit
16readFileUtf8 filepath = makeAff $ runFn3 readFileUtf8Fn filepath 15writeFileUtf8 filepath contents = makeAff $ runFn4 writeFileUtf8Fn filepath contents
17 16
18readFileUtf8Sync :: forall eff. String -> Eff (fs :: FS | eff) String 17foreign import writeFileUtf8Fn """
19readFileUtf8Sync filepath = readFileUtf8SyncFn filepath 18function writeFileUtf8Fn(filepath, contents, errback, callback) {
20
21foreign import readFileUtf8Fn """
22function readFileUtf8Fn(filepath, errback, callback) {
23 return function(){ 19 return function(){
24 var fs = require('fs'); 20 var fs = require('fs');
25 21
26 fs.readFile(filepath, 'utf-8', function(e, data){ 22 fs.writeFile(filepath, contents, function(error){
27 if (e) errback(e)(); 23 if (error) errback(error)();
28 else callback(data)(); 24 else callback()();
29 }); 25 });
30 }; 26 };
31} 27}
32""" :: forall eff. Fn3 String 28""" :: forall eff. Fn4 String
29 String
33 (Error -> Eff (fs :: FS | eff) Unit) 30 (Error -> Eff (fs :: FS | eff) Unit)
34 (String -> Eff (fs :: FS | eff) Unit) 31 (Unit -> Eff (fs :: FS | eff) Unit)
35 (Eff (fs :: FS | eff) Unit) 32 (Eff (fs :: FS | eff) Unit)
36
37foreign import readFileUtf8SyncFn """
38function readFileUtf8SyncFn(filepath) {
39 return function(){
40 var fs = require('fs');
41
42 return fs.readFileSync(filepath, {encoding: 'utf-8'});
43 };
44}
45""" :: forall eff. String -> (Eff (fs :: FS | eff) String)
diff --git a/src/Glob.purs b/src/Glob.purs
index 7bc9212..392d9e4 100644
--- a/src/Glob.purs
+++ b/src/Glob.purs
@@ -1,6 +1,6 @@
1module PursLoader.Glob 1module PursLoader.Glob
2 ( Glob() 2 ( Glob()
3 , glob 3 , globAll
4 ) where 4 ) where
5 5
6import Control.Monad.Aff (Aff(), makeAff) 6import Control.Monad.Aff (Aff(), makeAff)
@@ -11,21 +11,23 @@ import Data.Function
11 11
12foreign import data Glob :: ! 12foreign import data Glob :: !
13 13
14glob :: forall eff. String -> Aff (glob :: Glob | eff) [String] 14globAll :: forall eff. [String] -> Aff (glob :: Glob | eff) [[String]]
15glob pattern = makeAff $ runFn3 globFn pattern 15globAll patterns = makeAff $ runFn3 globAllFn patterns
16 16
17foreign import globFn """ 17foreign import globAllFn """
18function globFn(pattern, errback, callback) { 18function globAllFn(patterns, errback, callback) {
19 return function(){ 19 return function(){
20 var glob = require('glob'); 20 var glob = require('glob');
21 21
22 glob(pattern, function(e, data){ 22 var async = require('async');
23 if (e) errback(e)(); 23
24 else callback(data)(); 24 async.map(patterns, glob, function(error, result){
25 if (error) errback(new Error(error))();
26 else callback(result)();
25 }); 27 });
26 }; 28 };
27} 29}
28""" :: forall eff. Fn3 String 30""" :: forall eff. Fn3 [String]
29 (Error -> Eff (glob :: Glob | eff) Unit) 31 (Error -> Eff (glob :: Glob | eff) Unit)
30 ([String] -> Eff (glob :: Glob | eff) Unit) 32 ([[String]] -> Eff (glob :: Glob | eff) Unit)
31 (Eff (glob :: Glob | eff) Unit) 33 (Eff (glob :: Glob | eff) Unit)
diff --git a/src/Loader.purs b/src/Loader.purs
index fedc424..872a51c 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,81 @@ 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 ((!!), concat)
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 (joinWith)
17import Data.String (joinWith, split) 16import Data.String.Regex (match, noFlags, regex)
18import Data.String.Regex (Regex(), match, noFlags, regex)
19import Data.StrMap (StrMap(), fromList, lookup)
20import Data.Traversable (sequence)
21import Data.Tuple.Nested (tuple2)
22 17
23import PursLoader.ChildProcess (ChildProcess(), spawn) 18import PursLoader.ChildProcess (ChildProcess(), spawn)
24import PursLoader.FS (FS(), readFileUtf8, readFileUtf8Sync) 19import PursLoader.FS (FS(), writeFileUtf8)
25import PursLoader.Glob (Glob(), glob) 20import PursLoader.Glob (Glob(), globAll)
26import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, clearDependencies, addDependency, query, resourcePath) 21import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query)
27import PursLoader.LoaderUtil (getRemainingRequest, parseQuery) 22import PursLoader.LoaderUtil (parseQuery)
28import PursLoader.OS (eol) 23import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions)
29import PursLoader.Options (loaderSrcOption, pscMakeOptions, pscMakeDefaultOutput, pscMakeOutputOption)
30import PursLoader.Path (dirname, join, relative, resolve)
31 24
32foreign import cwd "var cwd = process.cwd();" :: String 25type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff)
33 26
34moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 27moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
35 28
36importRegex = regex "^\\s*import\\s+(?:qualified\\s+)?([\\w\\.]+)" noFlags { ignoreCase = true } 29pscCommand = "psc"
37
38bowerPattern = join [ "bower_components", "purescript-*", "src" ]
39 30
40pscMakeCommand = "psc-make" 31psciCommand = "psci"
41 32
42indexFilename = "index.js" 33psciFilename = ".psci"
43 34
44(!!!) = flip (!!) 35(!!!) = flip (!!)
45 36
46pursPattern :: [String] -> String 37foreign import cwd "var cwd = process.cwd();" :: String
47pursPattern srcs = join [ "{" ++ joinWith "," ([ bowerPattern ] <> srcs) ++ "}"
48 , "**"
49 , "*.purs"
50 ]
51
52type GraphModule = { file :: String, imports :: [String] }
53 38
54type Graph = StrMap GraphModule 39foreign import relative """
40function relative(from) {
41 return function(to){
42 var path = require('path');
43 return path.relative(from, to);
44 };
45}
46""" :: String -> String -> String
47
48mkPsci :: [[String]] -> [[String]] -> String
49mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis))
50 where
51 loadModule :: String -> String
52 loadModule a = ":m " ++ relative cwd a
55 53
56mkGraph :: forall eff. [String] -> Eff (fs :: FS | eff) Graph 54 loadForeign :: String -> String
57mkGraph files = (fromList <<< catMaybes) <$> sequence (parse <$> files) 55 loadForeign a = ":f " ++ relative cwd a
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 56
64mkDeps :: forall eff. String -> Graph -> [String] 57loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe 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 58loader' ref source = do
86 liftEff $ cacheable ref 59 liftEff $ cacheable ref
87 60
88 let request = getRemainingRequest ref 61 let parsed = parseQuery $ query ref
89 parsed = parseQuery $ query ref 62 srcs = fromMaybe [] (loaderSrcOption parsed)
90 srcs = loaderSrcOption parsed 63 ffis = fromMaybe [] (loaderFFIOption parsed)
91 opts = pscMakeOptions parsed 64 opts = pscOptions parsed
92 pattern = pursPattern $ fromMaybe [] srcs 65
93 key = match moduleRegex source >>= (!!!) 1 66 spawn pscCommand (srcs <> opts)
94 67
95 files <- glob pattern 68 srcss <- globAll srcs
96 graph <- liftEff $ mkGraph files 69 ffiss <- globAll ffis
97 70
98 let deps = fromMaybe [] $ flip mkDeps graph <$> key 71 let psciFile = mkPsci srcss ffiss
99 outputPath = fromMaybe pscMakeDefaultOutput $ pscMakeOutputOption parsed
100 indexPath = (\a -> join [ outputPath, a, indexFilename ]) <$> key
101 72
102 liftEff $ clearDependencies ref 73 writeFileUtf8 psciFilename psciFile
103 liftEff $ addDependency ref (resourcePath ref)
104 liftEff $ addDeps ref graph deps
105 74
106 spawn pscMakeCommand (opts <> files) 75 let moduleName = match moduleRegex source >>= (!!!) 1
107 indexFile <- sequence $ readFileUtf8 <$> indexPath 76 result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName
108 return indexFile
109 77
110type LoaderEff eff a = Eff (loader :: Loader, glob :: Glob, cp :: ChildProcess, fs :: FS | eff) a 78 return result
111 79
112loader :: forall eff. LoaderRef -> String -> LoaderEff eff Unit 80loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
113loader ref source = do 81loader ref source = do
114 callback <- async ref 82 callback <- async ref
115 runAff (\e -> callback (Just e) "") 83 runAff (\e -> callback (Just e) "")
116 (maybe (callback (Just $ error "Loader has failed to run") "") 84 (maybe (callback (Just (error "Loader has failed to run")) "")
117 (callback Nothing)) 85 (callback Nothing))
118 (loader' ref source) 86 (loader' ref source)
119 87
120loaderFn :: forall eff. Fn2 LoaderRef String (LoaderEff eff Unit) 88loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
121loaderFn = mkFn2 loader 89loaderFn = mkFn2 loader
diff --git a/src/LoaderRef.purs b/src/LoaderRef.purs
index 2d62754..2567b1e 100644
--- a/src/LoaderRef.purs
+++ b/src/LoaderRef.purs
@@ -3,16 +3,12 @@ module PursLoader.LoaderRef
3 , Loader() 3 , Loader()
4 , async 4 , async
5 , cacheable 5 , cacheable
6 , clearDependencies
7 , resourcePath
8 , addDependency
9 , query 6 , query
10 ) where 7 ) where
11 8
12import Control.Monad.Eff (Eff()) 9import Control.Monad.Eff (Eff())
13import Control.Monad.Eff.Exception (Error()) 10import Control.Monad.Eff.Exception (Error())
14 11
15import Data.Foreign (Foreign())
16import Data.Function (Fn3(), runFn3) 12import Data.Function (Fn3(), runFn3)
17import Data.Maybe (Maybe(), fromMaybe, isJust) 13import Data.Maybe (Maybe(), fromMaybe, isJust)
18 14
@@ -48,27 +44,6 @@ function cacheable(ref){
48 }; 44 };
49}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit 45}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
50 46
51foreign import clearDependencies """
52function clearDependencies(ref){
53 return function(){
54 return ref.clearDependencies();
55 };
56}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
57
58foreign import resourcePath """
59function resourcePath(ref){
60 return ref.resourcePath;
61}""" :: LoaderRef -> String
62
63foreign import addDependency """
64function addDependency(ref){
65 return function(dep){
66 return function(){
67 return ref.addDependency(dep);
68 };
69 };
70}""" :: forall eff. LoaderRef -> String -> Eff (loader :: Loader | eff) Unit
71
72foreign import query """ 47foreign import query """
73function query(ref){ 48function query(ref){
74 return ref.query; 49 return ref.query;
diff --git a/src/LoaderUtil.purs b/src/LoaderUtil.purs
index f22be44..86be124 100644
--- a/src/LoaderUtil.purs
+++ b/src/LoaderUtil.purs
@@ -1,18 +1,11 @@
1module PursLoader.LoaderUtil 1module PursLoader.LoaderUtil
2 ( getRemainingRequest 2 ( parseQuery
3 , parseQuery
4 ) where 3 ) where
5 4
6import Data.Foreign (Foreign()) 5import Data.Foreign (Foreign())
7 6
8import PursLoader.LoaderRef (LoaderRef()) 7import PursLoader.LoaderRef (LoaderRef())
9 8
10foreign import getRemainingRequest """
11function getRemainingRequest(ref){
12 var loaderUtils = require('loader-utils');
13 return loaderUtils.getRemainingRequest(ref);
14}""" :: LoaderRef -> String
15
16foreign import parseQuery """ 9foreign import parseQuery """
17function parseQuery(query){ 10function parseQuery(query){
18 var loaderUtils = require('loader-utils'); 11 var loaderUtils = require('loader-utils');
diff --git a/src/OS.purs b/src/OS.purs
deleted file mode 100644
index 590c3d6..0000000
--- a/src/OS.purs
+++ /dev/null
@@ -1,3 +0,0 @@
1module PursLoader.OS (eol) where
2
3foreign import eol "var eol = require('os').EOL;" :: String
diff --git a/src/Options.purs b/src/Options.purs
index c47bebc..51e9be5 100644
--- a/src/Options.purs
+++ b/src/Options.purs
@@ -1,17 +1,17 @@
1module PursLoader.Options 1module PursLoader.Options
2 ( pscMakeOptions 2 ( pscOptions
3 , pscMakeDefaultOutput
4 , pscMakeOutputOption
5 , loaderSrcOption 3 , loaderSrcOption
4 , loaderFFIOption
6 ) where 5 ) where
7 6
7import Data.Array (concat)
8import Data.Either (either) 8import Data.Either (either)
9 9
10import Data.Foreign (Foreign(), F()) 10import Data.Foreign (Foreign(), F())
11import Data.Foreign.Class (IsForeign, read, readProp) 11import Data.Foreign.Class (IsForeign, read, readProp)
12import Data.Foreign.NullOrUndefined (NullOrUndefined(), runNullOrUndefined) 12import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
13 13
14import Data.Maybe (Maybe(..), maybe) 14import Data.Maybe (Maybe(..), maybe, fromMaybe)
15 15
16noPreludeOpt = "no-prelude" 16noPreludeOpt = "no-prelude"
17 17
@@ -29,9 +29,11 @@ commentsOpt = "comments"
29 29
30noPrefixOpt = "no-prefix" 30noPrefixOpt = "no-prefix"
31 31
32requirePathOpt = "require-path"
33
32srcOpt = "src" 34srcOpt = "src"
33 35
34pscMakeDefaultOutput = "output" 36ffiOpt = "ffi"
35 37
36newtype Options 38newtype Options
37 = Options { noPrelude :: NullOrUndefined Boolean 39 = Options { noPrelude :: NullOrUndefined Boolean
@@ -42,7 +44,9 @@ newtype Options
42 , comments :: NullOrUndefined Boolean 44 , comments :: NullOrUndefined Boolean
43 , output :: NullOrUndefined String 45 , output :: NullOrUndefined String
44 , noPrefix :: NullOrUndefined Boolean 46 , noPrefix :: NullOrUndefined Boolean
47 , requirePath :: NullOrUndefined String
45 , src :: NullOrUndefined [String] 48 , src :: NullOrUndefined [String]
49 , ffi :: NullOrUndefined [String]
46 } 50 }
47 51
48instance isForeignOptions :: IsForeign Options where 52instance isForeignOptions :: IsForeign Options where
@@ -54,7 +58,9 @@ instance isForeignOptions :: IsForeign Options where
54 , comments: _ 58 , comments: _
55 , output: _ 59 , output: _
56 , noPrefix: _ 60 , noPrefix: _
61 , requirePath: _
57 , src: _ 62 , src: _
63 , ffi: _
58 } <$> readProp noPreludeOpt obj 64 } <$> readProp noPreludeOpt obj
59 <*> readProp noOptsOpt obj 65 <*> readProp noOptsOpt obj
60 <*> readProp noMagicDoOpt obj 66 <*> readProp noMagicDoOpt obj
@@ -63,26 +69,25 @@ instance isForeignOptions :: IsForeign Options where
63 <*> readProp commentsOpt obj 69 <*> readProp commentsOpt obj
64 <*> readProp outputOpt obj 70 <*> readProp outputOpt obj
65 <*> readProp noPrefixOpt obj 71 <*> readProp noPrefixOpt obj
66 <*> readProp srcOpt obj) 72 <*> readProp requirePathOpt obj
73 <*> readProp srcOpt obj
74 <*> readProp ffiOpt obj)
67 75
68class LoaderOption a where 76class LoaderOption a where
69 opt :: String -> NullOrUndefined a -> [String] 77 opt :: String -> NullOrUndefined a -> [String]
70 78
71instance booleanLoaderOption :: LoaderOption Boolean where 79instance booleanLoaderOption :: LoaderOption Boolean where
72 opt key opt = maybe [] (\a -> if a then ["--" ++ key] else []) 80 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
73 (runNullOrUndefined opt)
74 81
75instance stringLoaderOption :: LoaderOption String where 82instance stringLoaderOption :: LoaderOption String where
76 opt key opt = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) 83 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
77 (runNullOrUndefined opt)
78 84
79pscMakeOutputOption :: Foreign -> Maybe String 85instance arrayLoaderOption :: (LoaderOption a) => LoaderOption [a] where
80pscMakeOutputOption query = either (const Nothing) 86 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
81 (\(Options a) -> runNullOrUndefined a.output) 87 <$> (fromMaybe [] (runNullOrUndefined val)))
82 (read query)
83 88
84pscMakeOptions :: Foreign -> [String] 89pscOptions :: Foreign -> [String]
85pscMakeOptions query = either (const []) fold parsed 90pscOptions query = either (const []) fold parsed
86 where parsed = read query :: F Options 91 where parsed = read query :: F Options
87 fold (Options a) = opt noPreludeOpt a.noPrelude <> 92 fold (Options a) = opt noPreludeOpt a.noPrelude <>
88 opt noOptsOpt a.noOpts <> 93 opt noOptsOpt a.noOpts <>
@@ -91,9 +96,12 @@ pscMakeOptions query = either (const []) fold parsed
91 opt verboseErrorsOpt a.verboseErrors <> 96 opt verboseErrorsOpt a.verboseErrors <>
92 opt commentsOpt a.comments <> 97 opt commentsOpt a.comments <>
93 opt outputOpt a.output <> 98 opt outputOpt a.output <>
94 opt noPrefixOpt a.noPrefix 99 opt noPrefixOpt a.noPrefix <>
100 opt requirePathOpt a.requirePath <>
101 opt ffiOpt a.ffi
95 102
96loaderSrcOption :: Foreign -> Maybe [String] 103loaderSrcOption :: Foreign -> Maybe [String]
97loaderSrcOption query = either (const Nothing) 104loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
98 (\(Options a) -> runNullOrUndefined a.src) 105
99 (read query) 106loaderFFIOption :: Foreign -> Maybe [String]
107loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)
diff --git a/src/Path.purs b/src/Path.purs
deleted file mode 100644
index e071e35..0000000
--- a/src/Path.purs
+++ /dev/null
@@ -1,36 +0,0 @@
1module PursLoader.Path
2 ( dirname
3 , join
4 , relative
5 , resolve
6 ) where
7
8foreign import dirname """
9function dirname(filepath) {
10 var path = require('path');
11 return path.dirname(filepath);
12}
13""" :: String -> String
14
15foreign import join """
16function join(parts) {
17 var path = require('path');
18 return path.join.apply(path, parts);
19}
20""" :: [String] -> String
21
22foreign import relative """
23function relative(from) {
24 return function(to){
25 var path = require('path');
26 return path.relative(from, to);
27 };
28}
29""" :: String -> String -> String
30
31foreign import resolve """
32function resolve(filepath) {
33 var path = require('path');
34 return path.resolve(filepath);
35}
36""" :: String -> String