How to include .swift file from other .swift file in an immediate mode?

前端 未结 2 1892
刺人心
刺人心 2020-12-05 07:17

Having a file with function definition bar.swift:

func bar() {
    println(\"bar\")
}

And a script to be run in immediate mode

2条回答
  •  遥遥无期
    2020-12-05 08:04

    Write a bash script to concatenate the files. The script below pre-pends the library file to the front of your script before execution:

    #!/bin/bash
    
    cat $HOME/my_swift/my_library_to_add.swift $1.swift > t.swift
    swift t.swift
    

    As the resulted file is the single use, you can place it on the RAM drive. Here is the more advanced version that creates or reuses the tiny 1 Mb RAM drive as required.

    if [ ! -d /Volumes/swift_buffer ]; then
       diskutil erasevolume HFS+ 'swift_buffer' `hdiutil attach -nomount ram://2048`
    fi
    
    cat $HOME/my_swift/my_library_to_add.swift $1.swift > /Volumes/swift_buffer/t.swift   
    swift /Volumes/swift_buffer/t.swift
    

    This creates a tiny RAM drive with the capacity of 1 Mb only, big enough for any utility script and simple library.

    The mounted RAM drive will be visible in the Finder, from where it can be ejected. I do not dispose it directly in the script as allocating takes time.

提交回复
热议问题