|
| 1 | +import nimjl |
| 2 | +import std/[os, strformat, exitprocs] |
| 3 | + |
| 4 | +proc compileCurrentFile(curRun: static int) = |
| 5 | + let cmd = getCurrentCompilerExe() & &" cpp -d:run{curRun} -o:./run{curRun}.out " & currentSourcePath() |
| 6 | + debugEcho cmd |
| 7 | + discard execShellCmd(cmd) |
| 8 | + |
| 9 | +proc cleanUp() = |
| 10 | + var envdir = "./julia-custom-env" |
| 11 | + if dirExists(envdir): removeDir(envdir) |
| 12 | + |
| 13 | + envdir = "./julia-empty-env" |
| 14 | + if dirExists(envdir): removeDir(envdir) |
| 15 | + |
| 16 | + discard tryRemoveFile("run1.out") |
| 17 | + discard tryRemoveFile("run2.out") |
| 18 | + discard tryRemoveFile("run3.out") |
| 19 | + |
| 20 | + |
| 21 | +proc postJlInit() = |
| 22 | + echo "code executed after Julia VM is initialized and env is activated but before dependencies" |
| 23 | + let Pkg = Julia.getModule("Pkg") |
| 24 | + |
| 25 | + discard Pkg.update() |
| 26 | + discard Pkg.status() |
| 27 | + |
| 28 | +proc firstRunmain() = |
| 29 | + ## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info |
| 30 | + Julia.init(4): |
| 31 | + activate("./julia-custom-env") |
| 32 | + # Call postJlInit() if it exists |
| 33 | + # Call Pkg + external dependencies |
| 34 | + Pkg: |
| 35 | + add(name="LinearAlgebra") |
| 36 | + add("DSP") |
| 37 | + defer: Julia.exit() |
| 38 | + |
| 39 | +proc mainNoPkgSameEnv() = |
| 40 | + ## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info |
| 41 | + Julia.init(4): |
| 42 | + # activate julia venv |
| 43 | + activate("./julia-custom-env") |
| 44 | + defer: Julia.exit() |
| 45 | + |
| 46 | +proc mainNoPkgDifferentEnv() = |
| 47 | + ## See https://pkgdocs.julialang.org/dev/api/#Pkg.add for more info |
| 48 | + Julia.init(4): |
| 49 | + activate("./julia-empty-env") |
| 50 | + defer: Julia.exit() |
| 51 | + |
| 52 | +when isMainModule: |
| 53 | + when defined(run1): |
| 54 | + echo "First Run:" |
| 55 | + echo " * Pkg.status show empty deps" |
| 56 | + echo " * dependencies are downloaded" |
| 57 | + echo "===================================================" |
| 58 | + firstRunMain() |
| 59 | + echo "===================================================" |
| 60 | + elif defined(run2): |
| 61 | + echo "No Pkg in init but same env:" |
| 62 | + echo " * Pkg.status show deps in projects" |
| 63 | + echo "===================================================" |
| 64 | + mainNoPkgSameEnv() |
| 65 | + echo "===================================================" |
| 66 | + elif defined(run3): |
| 67 | + echo "No Pkg in init and different env:" |
| 68 | + echo " * Pkg.status show empty deps" |
| 69 | + echo "===================================================" |
| 70 | + mainNoPkgDifferentEnv() |
| 71 | + echo "===================================================" |
| 72 | + else: |
| 73 | + compileCurrentFile(curRun=1) |
| 74 | + compileCurrentFile(curRun=2) |
| 75 | + compileCurrentFile(curRun=3) |
| 76 | + discard execShellCmd("./run1.out") |
| 77 | + discard execShellCmd("./run2.out") |
| 78 | + discard execShellCmd("./run3.out") |
| 79 | + cleanUp() |
| 80 | + |
0 commit comments