mirror of
https://github.com/idris-lang/Idris2.git
synced 2024-09-11 21:37:48 +03:00
Merge pull request #3189 from mattpolzin/make-js-executable
Make NodeJS backend's output file executable
This commit is contained in:
commit
2b5030a325
@ -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
|
||||
|
@ -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.
|
||||
|
6
tests/node/executable/TestExecutable.idr
Normal file
6
tests/node/executable/TestExecutable.idr
Normal file
@ -0,0 +1,6 @@
|
||||
module TestExecutable
|
||||
|
||||
import System
|
||||
|
||||
main : IO ()
|
||||
main = putStrLn "Hi there!"
|
2
tests/node/executable/expected
Normal file
2
tests/node/executable/expected
Normal file
@ -0,0 +1,2 @@
|
||||
Hi there!
|
||||
Hi there!
|
19
tests/node/executable/run
Normal file
19
tests/node/executable/run
Normal 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
|
Loading…
Reference in New Issue
Block a user