Skip to content

Commit

Permalink
Codegen support (#77)
Browse files Browse the repository at this point in the history
* Support injecting autogenerated code.

* Codegen example.

* retrigger checks

* Fix a missing newline.

Also configure vscode to insert a newline at the end of a file.

* Uncommit the .vscode directory.
  • Loading branch information
bakhtiyarneyman authored Feb 21, 2024
1 parent 5534d1c commit c7d9df1
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 4 deletions.
23 changes: 19 additions & 4 deletions nix/build-spago-lock.nix
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@

# Read a workspace package. These are listed at the top of the
# lockfile, not in the main packages list.
readWorkspacePackage = src: name: attr:
readWorkspacePackage = src: extraSrcs: name: attr:
stdenv.mkDerivation {
name = name;
# The workspace packages list version ranges with their dependencies, so
Expand All @@ -117,12 +117,22 @@
else pathExists "${src}/${attr.path}";
installPhase = ''
cp -R . $out
${
if builtins.hasAttr name extraSrcs
then ''
echo "Copying extra sources from ${extraSrcs.${name}}..."
cp -r ${extraSrcs.${name}} $out/src
''
else ''
echo "No extra sources to copy for ${name}."
''
}
'';
};

# Read the workspace packages
workspacePackages = src: lock:
lib.mapAttrs (readWorkspacePackage src) lock.workspace.packages;
workspacePackages = src: extraSrcs: lock:
lib.mapAttrs (readWorkspacePackage src extraSrcs) lock.workspace.packages;

# Merges together the dependencies into a local output directory such that
# they can be used for incremental compilation.
Expand Down Expand Up @@ -290,6 +300,11 @@
corefn ? false,
purs ? pkgs.purs,
lockfile ? src + "/spago.lock",
# A record from a name of a spago package in this workspace to a derivation with additional
# source files. This is useful for injecting generated code (e.g. from `graphql-client`).
#
# Non-existent packages are silently ignored.
extraSrcs ? {},
}: let
lock = readSpagoLock lockfile;
workspaceDirs = builtins.attrValues (lib.mapAttrs (_: attr: attr.path) lock.workspace.packages);
Expand All @@ -300,5 +315,5 @@
};
in
fixDependencies {inherit purs corefn;}
(lockedPackages filteredSrc lock // workspacePackages filteredSrc lock);
(lockedPackages filteredSrc lock // workspacePackages filteredSrc extraSrcs lock);
}
1 change: 1 addition & 0 deletions nix/examples/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
{callPackage}: {
simple = callPackage ./simple {};
simple-ffi = callPackage ./simple-ffi {};
simple-codegen = callPackage ./simple-codegen {};
}
36 changes: 36 additions & 0 deletions nix/examples/simple-codegen/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
stdenv,
purix,
}: let
generated = stdenv.mkDerivation {
name = "generated";
src = ./.;
installPhase = ''
touch Generated.purs
echo 'module Generated where' >> Generated.purs
echo 'import Prelude' >> Generated.purs
echo 'answer :: Int' >> Generated.purs
echo 'answer = 42' >> Generated.purs
mkdir -p $out
cp Generated.purs $out
'';
};

lock = purix.buildSpagoLock {
src = ./.;
lockfile = ./spago.lock;
extraSrcs.simple-codegen = generated;
};
in
stdenv.mkDerivation {
name = "bin";
src = ./.;
buildPhase = ''
echo "Linking ..."
ln -s ${lock.simple-codegen}/output .
'';
installPhase = ''
mkdir -p $out
cp -r output $out
'';
}
Loading

0 comments on commit c7d9df1

Please sign in to comment.