mirror of
https://github.com/nmattia/snack.git
synced 2024-09-11 11:55:36 +03:00
Add module dependencies
This commit is contained in:
parent
19d9940018
commit
3c27e2efb0
@ -14,3 +14,7 @@ popd
|
||||
pushd tests/nested
|
||||
snack build
|
||||
popd
|
||||
|
||||
pushd tests/dependencies
|
||||
snack build
|
||||
popd
|
||||
|
@ -53,6 +53,22 @@ let
|
||||
|
||||
singleOutModule = base: mod: singleOut base (moduleToFile mod);
|
||||
|
||||
singleOutModulePath = base: mod:
|
||||
"${singleOut base (moduleToFile mod)}/${moduleToFile mod}";
|
||||
|
||||
makeModuleSpecRec = base:
|
||||
pkgs.lib.fix
|
||||
(f: isMain: modName:
|
||||
makeModuleSpec
|
||||
modName
|
||||
(map (f false) (listModuleDependencies base modName))
|
||||
isMain
|
||||
) true;
|
||||
|
||||
buildFrom = base: modName: linkModuleObjects base
|
||||
(makeModuleSpecRec base modName);
|
||||
|
||||
|
||||
buildModule = base: mod:
|
||||
let
|
||||
objectName = mod.moduleName;
|
||||
@ -92,6 +108,36 @@ let
|
||||
];
|
||||
};
|
||||
|
||||
# Generate a list of haskell module names needed by the haskell file
|
||||
listModuleDependencies = base: modName:
|
||||
builtins.fromJSON
|
||||
(builtins.readFile (listModuleDependenciesJSON base modName));
|
||||
|
||||
listModuleDependenciesJSON = base: modName:
|
||||
pkgs.stdenv.mkDerivation
|
||||
{ name = "module-deps";
|
||||
src = null;
|
||||
builder = pkgs.writeScript "dependencies-json"
|
||||
''
|
||||
echo "preparing dependencies"
|
||||
source $stdenv/setup
|
||||
# Poor man's module parser
|
||||
FILTER=$(cat <<'EOF'
|
||||
s/import\s*\(qualified\|\)\s*\(\S*\)\s*\(.*\)/\2/p;
|
||||
EOF
|
||||
)
|
||||
JSON=$(cat <<'EOF'
|
||||
s/\(.*\)/"\1"/;
|
||||
$!s/$/,/;
|
||||
EOF
|
||||
)
|
||||
sed -n "$FILTER" ${singleOutModulePath base modName} \
|
||||
| (echo "["; sed "$JSON"; echo "]") > $out
|
||||
echo "done:preparing dependencies"
|
||||
cat $out
|
||||
'';
|
||||
};
|
||||
|
||||
# Returns an attribute set where the keys are the module names and the values
|
||||
# are the '.o's
|
||||
flattenModuleObjects = base: mod':
|
||||
@ -139,5 +185,10 @@ let
|
||||
## TODO: use ghc -M for module dependencies
|
||||
in
|
||||
{
|
||||
inherit linkModuleObjects makeModuleSpec;
|
||||
inherit
|
||||
buildFrom
|
||||
linkModuleObjects
|
||||
listModuleDependenciesJSON
|
||||
makeModuleSpec
|
||||
;
|
||||
}
|
||||
|
3
tests/dependencies/snack.nix
Normal file
3
tests/dependencies/snack.nix
Normal file
@ -0,0 +1,3 @@
|
||||
# This "snack" passing is ugly, figure out a nice way of passing snack-lib
|
||||
with (import ../../nix {}).snack-lib;
|
||||
buildFrom ./src "Foo"
|
3
tests/dependencies/src/BarFoo.hs
Normal file
3
tests/dependencies/src/BarFoo.hs
Normal file
@ -0,0 +1,3 @@
|
||||
module BarFoo where
|
||||
|
||||
barFoo = "bar foo"
|
2
tests/dependencies/src/Data/Foo.hs
Normal file
2
tests/dependencies/src/Data/Foo.hs
Normal file
@ -0,0 +1,2 @@
|
||||
module Data.Foo where
|
||||
dataFoo = "Data, Foo."
|
10
tests/dependencies/src/Foo.hs
Normal file
10
tests/dependencies/src/Foo.hs
Normal file
@ -0,0 +1,10 @@
|
||||
import Data.Foo
|
||||
import Haskell
|
||||
import qualified FooBar
|
||||
import qualified BarFoo as F
|
||||
|
||||
main = do
|
||||
putStrLn dataFoo
|
||||
putStrLn haskell
|
||||
putStrLn FooBar.fooBar
|
||||
putStrLn F.barFoo
|
2
tests/dependencies/src/FooBar.hs
Normal file
2
tests/dependencies/src/FooBar.hs
Normal file
@ -0,0 +1,2 @@
|
||||
module FooBar where
|
||||
fooBar = "foo bar"
|
2
tests/dependencies/src/Haskell.hs
Normal file
2
tests/dependencies/src/Haskell.hs
Normal file
@ -0,0 +1,2 @@
|
||||
module Haskell where
|
||||
haskell = "haskell!"
|
Loading…
Reference in New Issue
Block a user