mxcl / swift-sh

Easily script with third-party Swift dependencies.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Import local packages relative to the location of the script

jboulter11 opened this issue · comments

Problem:

Scripts inside a git repository which share local packages can only be run from one directory.
If I have a directory structure like this:

git repo root
| - Scripts/
    | - script.swift
    | - MyPackage/

And I import MyPackage in script.swift like this:
import MyPackage // ./MyPackage

I can only run the script from the Scripts directory. I can't specify an absolute path because it's a git repository that might be checked out to any location.

Proposed Solution:

Paths should be able to be specified as relative to the script's file location rather than only allowing relative paths with respect to the current working directory.

  1. Allow local paths to be specified with some different syntax, perhaps like:
    import MyPackage // #/path/to/package
  2. Detect location of script being run via command line inputs
  3. Combine location of script and location of package when resolving local paths with relative-to-script syntax to produce the path relative to the current working directory for the same execution as the existing relative paths.
    ./Scripts/script.swift + #/path/to/package == ./Scripts/path/to/package

Questions:

  • Are you open to pull requests for this functionality?
  • What should the syntax be exactly? I'm not in love with the # prefix, can we think of something better? ... could be an option?
  • Do we indeed get the location of the script relative to the working directory in the command line arguments, or does some other method need to be devised?

Seems like a bug, the path should be calculated relative to the script path, not the working directory.

This should be fixed, rather than introducing new syntax.

PR welcome for sure.

I have this working but need a proper way of getting the input script path to the ImportSpecification.DependencyName initializer.

My plan is to pipe it through like so tomorrow:
run -> ImportSpecification.init -> parse -> InputSpecification.DependencyName.init

I definitely don't want to touch all those methods, but the alternative would be to use a global or singleton to store it and that doesn't seem like a good idea to me. Let me know if you have any better ideas.

ImportSpecification should know the script directory and pass it up IMO.

Fixed with #106