Merge pull request #3189 from mattpolzin/make-js-executable

Make NodeJS backend's output file executable
This commit is contained in:
André Videla 2024-01-07 09:47:15 +00:00 committed by GitHub
commit 2b5030a325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 2 deletions

View File

@ -21,6 +21,12 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO
### Compiler changes
#### NodeJS Backend
* The NodeJS executable output to `build/exec/` now has its executable bit set.
That file already had a NodeJS shebang at the top, so now it is fully ready to
go after compilation.
### Library changes
#### Prelude

View File

@ -14,17 +14,21 @@ import Core.TT
import Libraries.Utils.Path
import System
import System.File.Permissions
import Data.Maybe
%default covering
envNode : String
envNode = "/usr/bin/env node"
findNode : IO String
findNode = do
Nothing <- idrisGetEnv "NODE"
| Just node => pure node
path <- pathLookup ["node"]
pure $ fromMaybe "/usr/bin/env node" path
pure $ fromMaybe envNode path
||| Compile a TT expression to Node
compileToNode :
@ -36,7 +40,7 @@ compileToNode c s tm = do
pure $ shebang ++ js
where
shebang : String
shebang = "#!/usr/bin/env node\n"
shebang = "#!\{envNode}\n"
||| Node implementation of the `compileExpr` interface.
compileExpr :
@ -51,6 +55,7 @@ compileExpr c s tmpDir outputDir tm outfile =
do es <- compileToNode c s tm
let out = outputDir </> outfile
Core.writeFile out es
coreLift_ $ chmodRaw out 0o755
pure (Just out)
||| Node implementation of the `executeExpr` interface.

View File

@ -0,0 +1,6 @@
module TestExecutable
import System
main : IO ()
main = putStrLn "Hi there!"

View File

@ -0,0 +1,2 @@
Hi there!
Hi there!

19
tests/node/executable/run Normal file
View File

@ -0,0 +1,19 @@
. ../../testutils.sh
idris2 --cg node -o node_executable TestExecutable.idr > /dev/null
# node still executes it:
node ./build/exec/node_executable
# it can be executed on its own due to shebang and executable bit:
# (/usr/bin/env as used in shebang is problematic in edge cases
# but still generally a good bet; we will make sure the test works
# even when /usr/bin/env cannot be used)
fallback () {
nodejs="$(command -v node)"
# cross-platform supported sed-ing (unlike with in-place option)
cp ./build/exec/node_executable ./build/exec/node_executable_src
sed "s#/usr/bin/env node#$nodejs#" ./build/exec/node_executable_src > ./build/exec/node_executable
./build/exec/node_executable
}
./build/exec/node_executable || fallback