yonaskolb / Beak

A command line interface for your Swift scripts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Script execution rules

yonaskolb opened this issue · comments

At the moment how Beak runs the swift script is by embedding your function call at the end of the script and then copying it into a main.swift in an executable Swift package.
This means any free code in the script will also be run

public func foo() {
  print("Foo called")
}
print("This still runs")
$ beak run foo
  This still runs
  Foo called

This is useful as it's an easy place to put code that should run before every function call. It also means we could potentially allow beak run without specifying a function. That means it will just run the script, giving it functionality similar to Marathon https://github.com/JohnSundell/Marathon.

The other option is to leave the script untouched and generate a new main.swift file that just includes the function call, while also linking the original script as a seperate file. This would in affect only run the passed function, as top level expressions aren't allowed in non main files.