From 78c42fa691ca7e675bdcb8312d0a027faa84c842 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Sat, 21 Jul 2018 18:30:51 -0400 Subject: [PATCH 1/3] Add `commandline_app` flag for Apps without a GUI. On Mac, this will create an App bundle which simply opens a Terminal and runs your code. I *believe* that on Windows, there's nothing special to be done; I think it will Just Work like this by default. --- src/BuildApp.jl | 9 ++++++++- src/mac_commandline_app.jl | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/mac_commandline_app.jl diff --git a/src/BuildApp.jl b/src/BuildApp.jl index e2c12c3..9c39166 100644 --- a/src/BuildApp.jl +++ b/src/BuildApp.jl @@ -7,12 +7,14 @@ export build_app_bundle @static if is_apple() include("sign_mac_app.jl") +include("mac_commandline_app.jl") function build_app_bundle(juliaprog_main; appname=splitext(basename(juliaprog_main))[1], builddir = "builddir", resources = String[], libraries = String[], verbose = false, bundle_identifier = nothing, app_version = "0.1", icns_file = nothing, certificate = nothing, entitlements_file = nothing, + commandline_app = false, ) builddir = abspath(builddir) @@ -38,6 +40,11 @@ function build_app_bundle(juliaprog_main; resourcesDir="$appDir/Resources" libsDir="$appDir/Libraries" + if commandline_app + # Redefine launcherDir to put the binary where applet expects it. + launcherDir = build_commandline_app_bundle(builddir, binary_name, appname) + end + mkpath(launcherDir) function has_files(files) @@ -157,7 +164,7 @@ function build_app_bundle(juliaprog_main; CFBundleDisplayName $appname CFBundleExecutable - $binary_name + $(commandline_app ? "applet" : binary_name) CFBundleIconFile $appname.icns CFBundleIdentifier diff --git a/src/mac_commandline_app.jl b/src/mac_commandline_app.jl new file mode 100644 index 0000000..9c567e6 --- /dev/null +++ b/src/mac_commandline_app.jl @@ -0,0 +1,21 @@ +function get_commandline_applescript(exe_dir, executable_path) + """ + set RootPath to POSIX path of (path to me) + tell application id "com.apple.terminal" + do script ("exec '" & RootPath & "$exe_dir/$executable_path'") + activate + end tell + """ +end + +function build_commandline_app_bundle(builddir, binary_name, appname) + + applescript_file = "$builddir/build_$appname.applescript" + exe_dir = "Contents/app" + write(applescript_file, get_commandline_applescript(exe_dir, binary_name)) + + app_path = "$builddir/$appname.app" + run(`osacompile -o $app_path $applescript_file`) + + return joinpath(app_path,exe_dir) +end From 8e605ddc2456b37b776bca1bc073110f44be27e7 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Sat, 21 Jul 2018 18:33:32 -0400 Subject: [PATCH 2/3] Add commandline_hello examples --- examples/commandline_hello.jl | 8 ++++++++ test/BuildApp.jl | 5 +++++ test/build_examples/commandline_hello.jl | 9 +++++++++ 3 files changed, 22 insertions(+) create mode 100644 examples/commandline_hello.jl create mode 100644 test/build_examples/commandline_hello.jl diff --git a/examples/commandline_hello.jl b/examples/commandline_hello.jl new file mode 100644 index 0000000..e5d4d02 --- /dev/null +++ b/examples/commandline_hello.jl @@ -0,0 +1,8 @@ +# A super-simple command line program that just prints hello. +# Build this with the `commandline_app=true` flag in `BuildApp.build_app_bundle`. + +Base.@ccallable function julia_main(ARGS::Vector{String})::Cint + println("Oh hi, World!") + println("Current working directory: $(pwd())") + return 0 +end diff --git a/test/BuildApp.jl b/test/BuildApp.jl index 0f12df8..2dab0af 100644 --- a/test/BuildApp.jl +++ b/test/BuildApp.jl @@ -21,6 +21,11 @@ builddir = mktempdir() end end +@testset "commandline_app" begin +@test 0 == include("build_examples/commandline_hello.jl") +@test success(`open $builddir/hello.app`) +end + function testRunAndKillProgramSucceeds(cmd) out, _, p = readandwrite(cmd) # Make sure it runs correctly diff --git a/test/build_examples/commandline_hello.jl b/test/build_examples/commandline_hello.jl new file mode 100644 index 0000000..f658017 --- /dev/null +++ b/test/build_examples/commandline_hello.jl @@ -0,0 +1,9 @@ +using ApplicationBuilder; using BuildApp + +# Allow this file to be called either as a standalone file to build the above +# example, or from runtests.jl using a provided builddir. +isdefined(:builddir) || (builddir=nothing) # nothing == default builddir. + +build_app_bundle(joinpath(@__DIR__,"..","..","examples","commandline_hello.jl"), + appname="hello", + commandline_app=true, builddir=builddir) From 42b2b194e547d32ca1e6d9fa60b929332afc425c Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Sat, 21 Jul 2018 20:08:37 -0400 Subject: [PATCH 3/3] Add readlines() to commandline_hello to keep terminal open on Windows --- examples/commandline_hello.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/commandline_hello.jl b/examples/commandline_hello.jl index e5d4d02..19c3796 100644 --- a/examples/commandline_hello.jl +++ b/examples/commandline_hello.jl @@ -2,7 +2,12 @@ # Build this with the `commandline_app=true` flag in `BuildApp.build_app_bundle`. Base.@ccallable function julia_main(ARGS::Vector{String})::Cint - println("Oh hi, World!") - println("Current working directory: $(pwd())") + println("Hi what's your name?") + name = readline() + println("Oh hi, $name\! It's a pleasure to meet you.") + println("By the way, here's the current working directory:\n'$(pwd())'") + + println("\nGoodbye! (Press enter to exit)") + readline() return 0 end