Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Last active September 21, 2022 20:37
Show Gist options
  • Select an option

  • Save ramirez7/925c0ef38b24ebf64a5b50093669b455 to your computer and use it in GitHub Desktop.

Select an option

Save ramirez7/925c0ef38b24ebf64a5b50093669b455 to your computer and use it in GitHub Desktop.
parse-package-imports.sh
#! /usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
packagesRaw=$(./parse-package-imports.sh $1)
shift
readarray -t foundPackages <<< "$packagesRaw"
nix="haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ${foundPackages[@]} ])"
nix-shell -p "$nix" "$@"
#! /usr/bin/env bash
set -euo pipefail
shopt -s inherit_errexit
file="$1"
regex='^import[[:space:]]*"([A-Za-Z0-9-]+)"'
while IFS= read -r line
do
if [[ $line =~ $regex ]]
then
echo "${BASH_REMATCH[1]}"
fi
done < "$file"
{-# LANGUAGE PackageImports #-}
module Whatever where
import "puhoy" OI
import "there" THERE
import HMM
import "lol" LOL
x :: Int
x = 2
s :: String
s = "import \"hi\" HI"
{-# LANGUAGE PackageImports #-}
module Main where
import "timeit" System.TimeIt
main :: IO ()
main = timeIt $ putStrLn "puhoy there!"
@ramirez7
Copy link
Copy Markdown
Author

ramirez7 commented Sep 21, 2022

Seems to work:

$ ./package-import-nix-shell.sh test2.hs --run "runghc test2.hs"
puhoy there!
CPU time:   0.00s

Sanity check of the parser:

$ ./parse-package-imports.sh test1.hs
puhoy
there
lol

$ ./parse-package-imports.sh test2.hs
timeit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment