Skip to content

🛠️ A CLI tool that resolves import statements in Swift scripts — enabling file and folder-level inclusion to improve Swift scripting ergonomics.

Notifications You must be signed in to change notification settings

crisfeim/cli-swiftimport

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift import

A CLI tool that resolves import statements in Swift scripts — enabling file and folder-level inclusion to improve Swift scripting ergonomics.

Introduction

Given a scripting project structured as follows:

/b.swift
    func b() { print("b") }

/folder
    |- c.swift
        func c() { print("c") }
    |- d.swift
        func d() { print("d") }
    
/a.swift
    // import b.swift
    // import /folder
    
    b()
    c()
    d()

When running:

$ swiftimport a.swift 

Will resolve all imports and return a single concatenated file:

func b() { print("b") }
func c() { print("c") }
func d() { print("d") }

b()
c()
d()

Then you can pass the concatenation to the compilar to run the code:

tmp_file="swiftimport-$(date +"%Y%m%d-%H%M%S").swift"
swiftimport --input "$1" > "$tmp_file"
swift "$tmp_file"
rm "$tmp_file"

Use cases

  • Quick explorations and playgrounds
  • Small sized projects without Xcode
  • Expanding Swift scripting

Usage

Add this run script to your IDE/Text editor or run directly from terminal:

tmp_file="swiftimport-$(date +"%Y%m%d-%H%M%S").swift"
swiftimport --input "$1" > "$tmp_file"
swift "$tmp_file"
rm "$tmp_file"
terminal.mov

CodeRunner

If the swiftimport binary is in your $PATH, you can add preprocessing to your Swift compile script in CodeRunner:

[ -z "$CR_SUGGESTED_OUTPUT_FILE" ] && CR_SUGGESTED_OUTPUT_FILE="$PWD/${CR_FILENAME%.*}"

# Check if Xcode is installed
xcrun swiftc &>/dev/null
status=$?
if [ $status -eq 69 ]; then
    echo "To run swift code you need to open Xcode and accept the developer license agreement."
    exit 69
else
    if [ "$CR_FILENAME" = "main.swift" ]; then
        xcrun -sdk macosx swiftc -o "$CR_SUGGESTED_OUTPUT_FILE" *.swift "${@:1}" ${CR_DEBUGGING:+-g}
    else
        TMP_SWIFT="$CR_TMPDIR/swiftimport-$(date +"%Y%m%d-%H%M%S").swift"
        swiftimport --input "$CR_FILENAME" > "$TMP_SWIFT"
        xcrun -sdk macosx swiftc -o "$CR_SUGGESTED_OUTPUT_FILE" "$TMP_SWIFT" "${@:1}" ${CR_DEBUGGING:+-g}
    fi
    status=$?
fi

if [ $status -eq 0 ]; then
    echo "$CR_SUGGESTED_OUTPUT_FILE"
fi
exit $status
coderunner.mov

Installation

Clone repo and build binary:

swift build -c release

Then optionally move the binary to your global path:

sudo mv .build/release/swiftimport /usr/local/bin

So you can run it from anywhere with:

$ swiftimport --input yourfile.swift

About

🛠️ A CLI tool that resolves import statements in Swift scripts — enabling file and folder-level inclusion to improve Swift scripting ergonomics.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages