Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shebang only works when cd'ed next to the script #4

Open
ThinkChaos opened this issue Jul 6, 2015 · 1 comment
Open

Shebang only works when cd'ed next to the script #4

ThinkChaos opened this issue Jul 6, 2015 · 1 comment

Comments

@ThinkChaos
Copy link

Paths in the shebang are relative to the PWD when running the script.
That means that if you run any of the swift scripts from a directory that is not the script's one, it will fail to find the Carthage framework directory.

$ pwd
/path/to/talks/swiftsummit/2
$ ./colourful.swift
Roses are red
[...]
$ cd .. && 2/colourful.swift
2/colourful.swift:3:8: error: no such module 'PrettyColors'
import PrettyColors
       ^

I believe the only solution is to use a script to bootstrap the swift script.
The bootstrapper could be written in Swift, as long as it has no dependencies.

You could either carry the bootstrapper with every Swift script:

$ cat 2/run.sh
#!/usr/bin/env bash
cd "$(dirname "$0")" # cd to the script's directory
xcrun swift -F Carthage/Build/iOS colourful.swift
$ 2/run.sh
Roses are red
[...]

Or if you are willing place it in a directory in your $PATH:

$ cat ~/bin/shwift # ~/bin is in my PATH
#!/usr/bin/env bash

# No extra framework search paths
[[ $# -eq 1 ]] && exec xcrun swift "$1"

# Script is last argument
script="${@:$#}"
script_dir="$(dirname "$script")"

# for each but the last arg
while [[ $# -gt 1 ]]; do
    args="$args -F '$script_dir/$1'"
    shift
done

# no quotes for $args: we want it to unpack the args
xcrun swift $args "$script"
$ head -n 1 2/colourful.swift
#!/usr/bin/env shwift Carthage/Build/iOS
$ 2/colourful.swift
Roses are red
[...]

Please note that the second script is just a proof of concept. You should write a real script if you plan on using this method.

@kylef
Copy link

kylef commented Jul 6, 2015

Alternatively you could just use Cato.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants