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

Support scripts w/ `@main`

Open helje5 opened this issue 4 years ago • 1 comments

It would be nice if swift-sh would support scripts that contain the new @main. Like:

@main
struct MyTool: App {
  
  var body: some Scene {
    WindowGroup { Text("Hello World!") }
  }
}

Suggested implementation:

  • grep the source file for @main or maybe @main[\t\n ]+struct if the script itself is not called main.swift
  • if found, do not copy the script to main.swift but instead keep the original script name (like MyTool.swift)
  • the hashbang line must be removed, it is only allowed in main.swift
  • use Swift package tools version 5.5 (required for working @main), and add this to the Package.swift:
// swift-tools-version:5.5
...
pkg.platforms = [ .macOS(.v11) ]

Another option might be to use the hashbang line to configure an explicit mode? Like:

#!/usr/bin/swift sh #@main

helje5 avatar Aug 23 '21 13:08 helje5

I agree with your suggested approach.

mxcl avatar Sep 24 '21 13:09 mxcl