Skip to content

Commit

Permalink
Allow passing arguments to compiled scripts
Browse files Browse the repository at this point in the history
now the installer is not added as a `--compile` step but instead baked into the binary itself
  • Loading branch information
phil294 committed Jul 20, 2023
1 parent b4c13bf commit 10bc544
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The released binaries are special because they need to be portable. We achieve t
1. Get `linuxdeploy-x86_64.AppImage` from https://github.com/linuxdeploy/linuxdeploy/, into this `build` folder
1. Get `linuxdeploy-plugin-gtk.sh` from https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh
1. In that same file, *delete the line* `export GTK_THEME="$APPIMAGE_GTK_THEME" # Custom themes are broken` (this is a temporary fix ([issue](https://github.com/linuxdeploy/linuxdeploy-plugin-gtk/issues/39)))
1. Instead of `shards build`, run `./build.sh --release`. This also does shards build, but also does the AppImage magic and attaches the installer. The `--release` flag results in slower compilation and faster output binary.
1. Instead of `shards build`, run `./build.sh --release`. This also does shards build, but also does the AppImage magic. The `--release` flag results in slower compilation and faster output binary.
1. Find your final binary `ahk_x11-[version]-x86_64.AppImage` in the `build` folder. It's about 30 MiB in size.

There's a script to call `./build.sh`, make a new release and publish it etc., it's `../release.sh`. You most likely can't run it yourself though.
Expand Down
15 changes: 1 addition & 14 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version=$(shards version)

cd ..
# Cannot overwrite CRYSTAL_LIBRARY_PATH because crystal#12380, need link-flag instead.
# -no-pie prevents
# -no-pie prevents
shards build -Dpreview_mt --link-flags="-L$PWD/build" \
"$@"
# Flags lik -Dgc_none or --release or --debug should be passed from outside
Expand All @@ -25,17 +25,4 @@ rm -rf AppDir
bin_name=ahk_x11-"$version"-x86_64.AppImage
mv ahk_x11-x86_64.AppImage "$bin_name"

# Attaching the installer:
# The installer is not shipped separately and instead bundled with the binary by doing this.
# Bundling is the same thing as compiling a script as a user.
# It is possible to repeatedly compile a binary, with each script being appended at the end each time.
# Only the last one actually executed - and only if no params are passed to the program.
# There's no point in compiling multiple times, but it allows us to ship a default script (the installer)
# for when no arguments are passed.
# In other words, this is possible for a user:
# ahk_x11 --compile script1.ahk && ./script1 --compile script2.ahk && ./script2
# but no one will ever do that.
./"$bin_name" --compile ../src/installer.ahk tmp
mv tmp "$bin_name"

echo "success!"
64 changes: 33 additions & 31 deletions src/ahk_x11.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,41 @@ end

script_file = nil
version = {{ read_file("./shard.yml").split("\n")[1][9..] }}
if ARGV[0]?
if ARGV[0] == "-v" || ARGV[0] == "--version"
puts "AHK_X11 version: #{version}\nTargets to partially implement Classic Windows AutoHotkey specification: v1.0.24 (2004). AutoHotkey is a scripting language."
::exit
elsif ARGV[0] == "-h" || ARGV[0] == "--help"
puts "AHK_X11 is a Linux implementation for AutoHotkey classic version 1.0.24 (2004). Internal version: #{version}. Full up to date documentation can be found at https://phil294.github.io/AHK_X11/.\n\nPossible methods of invocation:\n\nahk_x11.AppImage \"path to script.ahk\"\nahk_x11.AppImage <<< $'MsgBox, 1\\nMsgBox, 2'\nahk_x11.AppImage --repl\nahk_x11.AppImage --windowspy\nahk_x11.AppImage --compile \"path to script.ahk\" \"optional: output executable file path\"\n\nAlternatively, just run the program without arguments to open the graphical installer. Once installed, you should be able to run and/or compile any .ahk file in your file manager by selecting it from the right click context menu."
::exit
elsif ARGV[0] == "--repl"
lines = ["#Persistent"]
elsif ARGV[0] == "--windowspy"
lines = {{ read_file("./src/window-spy.ahk").split("\n") }}
elsif ARGV[0] == "--compile"
build_error "Syntax: ahk_x11 --compile FILE_NAME [OUTPUT_FILENAME]" if ARGV.size < 2
Compiler.new.compile(filename_to_path(ARGV[1]), ARGV[2]? ? filename_to_path(ARGV[2]) : nil)
::exit
else
script_file = filename_to_path(ARGV[0])
begin
ahk_str = File.read(script_file)
rescue
build_error "File '#{ARGV[0]}' could not be read."
lines = Compiler.new.extract.try &.split('\n')
if ! lines
# Only needed for installer script, this can't (yet) really be part of ahk code. TODO: rm on exit
File.write("/tmp/tmp_ahk_x11_logo.png", logo_blob)
if ARGV[0]?
if ARGV[0] == "-v" || ARGV[0] == "--version"
puts "AHK_X11 version: #{version}\nTargets to partially implement Classic Windows AutoHotkey specification: v1.0.24 (2004). AutoHotkey is a scripting language."
::exit
elsif ARGV[0] == "-h" || ARGV[0] == "--help"
puts "AHK_X11 is a Linux implementation for AutoHotkey classic version 1.0.24 (2004). Internal version: #{version}. Full up to date documentation can be found at https://phil294.github.io/AHK_X11/.\n\nPossible methods of invocation:\n\nahk_x11.AppImage \"path to script.ahk\"\nahk_x11.AppImage <<< $'MsgBox, 1\\nMsgBox, 2'\nahk_x11.AppImage --repl\nahk_x11.AppImage --windowspy\nahk_x11.AppImage --compile \"path to script.ahk\" \"optional: output executable file path\"\n\nAlternatively, just run the program without arguments to open the graphical installer. Once installed, you should be able to run and/or compile any .ahk file in your file manager by selecting it from the right click context menu."
::exit
elsif ARGV[0] == "--repl"
lines = ["#Persistent"]
elsif ARGV[0] == "--windowspy"
lines = {{ read_file("./src/window-spy.ahk").split("\n") }}
elsif ARGV[0] == "--compile"
build_error "Syntax: ahk_x11 --compile FILE_NAME [OUTPUT_FILENAME]" if ARGV.size < 2
Compiler.new.compile(filename_to_path(ARGV[1]), ARGV[2]? ? filename_to_path(ARGV[2]) : nil)
::exit
else
script_file = filename_to_path(ARGV[0])
begin
ahk_str = File.read(script_file)
rescue
build_error "File '#{ARGV[0]}' could not be read."
end
lines = ahk_str.split(/\r?\n/)
end
lines = ahk_str.split(/\r?\n/)
end
else
stdin = Hacks.get_all_stdin_if_available
if stdin
lines = stdin.split('\n')
else
lines = Compiler.new.extract.try &.split('\n')
abort "Argument missing." if ! lines
# Only needed for installer script, this can't (yet) really be part of ahk code. TODO: rm on exit
File.write("/tmp/tmp_ahk_x11_logo.png", logo_blob)
stdin = Hacks.get_all_stdin_if_available
if stdin
lines = stdin.split('\n')
else
lines = {{ read_file("./src/installer.ahk").split("\n") }}
end
end
end

Expand Down

0 comments on commit 10bc544

Please sign in to comment.