From fa01c5a4cb42d80ac147dc5ab512a0795dbe14da Mon Sep 17 00:00:00 2001 From: eric thul Date: Tue, 11 Aug 2015 20:27:04 -0400 Subject: Moving files to match module --- src/PursLoader/Loader.purs | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/PursLoader/Loader.purs (limited to 'src/PursLoader/Loader.purs') diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs new file mode 100644 index 0000000..e9e03c4 --- /dev/null +++ b/src/PursLoader/Loader.purs @@ -0,0 +1,106 @@ +module PursLoader.Loader + ( Effects() + , loader + , loaderFn + ) where + +import Control.Monad.Aff (Aff(), runAff) +import Control.Monad.Eff (Eff()) +import Control.Monad.Eff.Class (liftEff) +import Control.Monad.Eff.Exception (error) + +import Data.Array ((!!), concat) +import Data.Function (Fn2(), mkFn2) +import Data.Maybe (Maybe(..), fromMaybe, maybe) +import Data.String (joinWith) +import Data.String.Regex (match, noFlags, regex, test) + +import PursLoader.ChildProcess (ChildProcess(), spawn) +import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) +import PursLoader.Glob (Glob(), globAll) +import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) +import PursLoader.LoaderUtil (parseQuery) +import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) + +type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) + +moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } + +foreignRegex = regex "(?:^|\\n)\\s*foreign import\\s+" noFlags { ignoreCase = true } + +pscCommand = "psc" + +psciCommand = "psci" + +psciFilename = ".psci" + +(!!!) = flip (!!) + +foreign import cwd "var cwd = process.cwd();" :: String + +foreign import relative """ +function relative(from) { + return function(to){ + var path = require('path'); + return path.relative(from, to); + }; +} +""" :: String -> String -> String + +mkPsci :: [[String]] -> [[String]] -> String +mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) + where + loadModule :: String -> String + loadModule a = ":m " ++ relative cwd a + + loadForeign :: String -> String + loadForeign a = ":f " ++ relative cwd a + +findFFI :: forall eff. [[String]] -> String -> Aff (fs :: FS | eff) (Maybe String) +findFFI ffiss name = findFileUtf8 re (concat ffiss) + where + re = regex ("(?:^|\\n)//\\s*module\\s*" ++ name ++ "\\s*\\n") noFlags + +loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) +loader' ref source = do + liftEff $ cacheable ref + + let parsed = parseQuery $ query ref + srcs = fromMaybe [] (loaderSrcOption parsed) + ffis = fromMaybe [] (loaderFFIOption parsed) + opts = pscOptions parsed + + spawn pscCommand (srcs <> opts) + + srcss <- globAll srcs + ffiss <- globAll ffis + + let psciFile = mkPsci srcss ffiss + + writeFileUtf8 psciFilename psciFile + + let moduleName = match moduleRegex source >>= (!!!) 1 + hasForeign = test foreignRegex source + result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName + + liftEff (clearDependencies ref) + liftEff (addDependency ref (resourcePath ref)) + + foreignPath <- if hasForeign + then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) + else pure Nothing + + fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) + + return result + +loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit +loader ref source = do + callback <- async ref + runAff (\e -> callback (Just e) "") + (maybe (callback (Just (error "Loader has failed to run")) "") + (callback Nothing)) + (loader' ref source) + +loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) +loaderFn = mkFn2 loader -- cgit v1.2.3 From 03b840cb5fb8ff5217fefc9e1240a3131db309fc Mon Sep 17 00:00:00 2001 From: eric thul Date: Tue, 11 Aug 2015 20:57:07 -0400 Subject: PureScript 0.7 updates and migration to pulp --- src/PursLoader/Loader.purs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/PursLoader/Loader.purs') diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index e9e03c4..5373d2f 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs @@ -4,6 +4,8 @@ module PursLoader.Loader , loaderFn ) where +import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit) + import Control.Monad.Aff (Aff(), runAff) import Control.Monad.Eff (Eff()) import Control.Monad.Eff.Class (liftEff) @@ -36,18 +38,11 @@ psciFilename = ".psci" (!!!) = flip (!!) -foreign import cwd "var cwd = process.cwd();" :: String +foreign import cwd :: String -foreign import relative """ -function relative(from) { - return function(to){ - var path = require('path'); - return path.relative(from, to); - }; -} -""" :: String -> String -> String +foreign import relative :: String -> String -> String -mkPsci :: [[String]] -> [[String]] -> String +mkPsci :: Array (Array String) -> Array (Array String) -> String mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) where loadModule :: String -> String @@ -56,7 +51,7 @@ mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign < loadForeign :: String -> String loadForeign a = ":f " ++ relative cwd a -findFFI :: forall eff. [[String]] -> String -> Aff (fs :: FS | eff) (Maybe String) +findFFI :: forall eff. Array (Array String) -> String -> Aff (fs :: FS | eff) (Maybe String) findFFI ffiss name = findFileUtf8 re (concat ffiss) where re = regex ("(?:^|\\n)//\\s*module\\s*" ++ name ++ "\\s*\\n") noFlags @@ -79,7 +74,7 @@ loader' ref source = do writeFileUtf8 psciFilename psciFile - let moduleName = match moduleRegex source >>= (!!!) 1 + let moduleName = match moduleRegex source >>= (!!!) 1 >>= id hasForeign = test foreignRegex source result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName -- cgit v1.2.3 From 07de44be15efb0ca9a2d1443ab1e91076a25409c Mon Sep 17 00:00:00 2001 From: eric thul Date: Tue, 11 Aug 2015 21:21:47 -0400 Subject: Add all PureScript files as webpack dependencies Resolves #26 --- src/PursLoader/Loader.purs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/PursLoader/Loader.purs') diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index 5373d2f..3cb99cd 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs @@ -16,6 +16,7 @@ import Data.Function (Fn2(), mkFn2) import Data.Maybe (Maybe(..), fromMaybe, maybe) import Data.String (joinWith) import Data.String.Regex (match, noFlags, regex, test) +import Data.Traversable (sequence) import PursLoader.ChildProcess (ChildProcess(), spawn) import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) @@ -42,6 +43,8 @@ foreign import cwd :: String foreign import relative :: String -> String -> String +foreign import resolve :: String -> String + mkPsci :: Array (Array String) -> Array (Array String) -> String mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) where @@ -80,6 +83,7 @@ loader' ref source = do liftEff (clearDependencies ref) liftEff (addDependency ref (resourcePath ref)) + liftEff (sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss) foreignPath <- if hasForeign then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) -- cgit v1.2.3