swift-sh icon indicating copy to clipboard operation
swift-sh copied to clipboard

Autocompletion when editing script in Xcode doesn't work

Open lukaskubanek opened this issue 6 years ago • 9 comments
trafficstars

Today, I researched a way for handling helper scripts for my Mac application. I tested all three major players (swift-sh, Marathon, Beak) and I like the approach of swift-sh the most. Especially the shebang and the idea of editing out into Xcode while working on the script without having to maintain the Xcode project afterwards.

Unfortunately, there have been also two issues which bother me a bit. The first one being the management of the Homebrew formula which I described in mxcl/homebrew-made#4. The second one is the inability to get the autocompleting in Xcode working.

Here are the steps I've performed. First, I installed the tool from source (version 1.12.0):

$ git clone https://github.com/mxcl/swift-sh.git
$ cd swift-sh
$ swift build -c release
$ cp .build/
$ cp .build/release/swift-sh "/Applications/Xcode 10.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/"
$ cp .build/release/swift-sh-edit "/Applications/Xcode 10.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/"

Then I created a sample script from the README:

$ cat <<EOF > script
#!/usr/bin/swift sh
import PromiseKit // @mxcl ~> 6.5
print(Promise.value("Hi!"))
EOF
$ chmod u+x script
$ ./script
# => Promise("Hi!")

So far everything worked smoothly, so I tried to edit the script in Xcode 10.2 using $ swift sh edit script. Xcode opened but no matter which scheme I selected the autocompletion didn't work. The script scheme allowed me to run the script and the script-completion scheme behaved very strangely showing errors complaining about script not being main.swift. Below are some screenshots showing the problem.

@mxcl I must be missing something. Could you please give me a hint regarding what I'm doing wrong?

xcode-scheme-script xcode-scheme-script-completion

lukaskubanek avatar May 24 '19 21:05 lukaskubanek

It did work, but seemingly has stopped working. The completion scheme is not meant to be run, but is autogenerated by Xcode. It is required to get complete, but apparently is not working currently.

mxcl avatar May 24 '19 21:05 mxcl

Here are more thoughts about this issue and the edit command implementation. I just learned that there was a big change in the behavior which went from the standard Xcode project generated via swift package generate-xcodeproj to a custom one which generates the Xcode project manually using tuist/xcodeproj (#57 & #58). I have to say that the latter Xcode project looks somehow half-baked compared to the one generated by SPM. I generated the Xcode project with SPM and the completion worked like a charm. Unfortunately, there is the saving issue which was the main reason for the change in #57.

While going through other tools today, I stumbled upon the way Marathon handles this. After executing the edit command, the Xcode project generated via SPM gets opened but the command is waiting. After performing the change the command is terminated which apparently copies the changed file to its original location. Wouldn't this be a nicer approach?

EDIT: This is the routine in the Marathon codebase I'm referring to.

lukaskubanek avatar May 24 '19 21:05 lukaskubanek

Wouldn't this be a nicer approach?

I would not like this approach no. I need my terminal to be useful, not waiting for things, not to mention there are numerous ways you could lose work because the terminal command is killed while you continue to edit.

Honestly, I think it's a really terrible approach.

mxcl avatar Jul 20 '19 22:07 mxcl

Hi guys thank you so much for this project. I was trying to see if I could make this work with XCode 11, but I also never get any autocompletion out of it. Does anybody have any idea what needs to be done? Maybe I could try to hack it :)

rubenfonseca avatar Oct 04 '19 12:10 rubenfonseca

IIRC the trick was building a dylib or framework of the deps and providing that as a include/link path to the executable binary but not actually linking it.

I was inspired by what SwiftPM was doing for completion in its Package.swift.

mxcl avatar Oct 04 '19 12:10 mxcl

It works after adding Import Paths.

2020-09-10_17-42-27

nearfri avatar Sep 10 '20 08:09 nearfri

Thank you for pointing that out @nearfri!

jsorge avatar Sep 10 '20 21:09 jsorge

Supposedly fixed in 2.1.0, please confirm.

mxcl avatar Sep 22 '20 20:09 mxcl

This is working perfectly for me in Xcode 12.2 (swift 5.3.1) under macOS 10.15.7.

haikusw avatar Jan 05 '21 17:01 haikusw