Running executables directly from URL

Greetings. Is there a way to pull a package and execute its commands directly from the URL, taking into account dependancy rules, eg:

swift run --package-url https://github.com/jjrscott/echo --up-to-next-minor-from 0.1 echo "Hello World"

To reiterate, I explicitly wish to take advantage of dependancy rules such as --up-to-next-minor-from 0.0.0 but avoid going through the following long, complicated, annoying, and error prone process which, when called, creates a package, adds the command repo as a dependancy, then makes the call:

#! /bin/sh

PACKAGE_PATH=$(mktemp -d)
SCRATCH_PATH=$PACKAGE_PATH/build

shift
REPO_URL=$1
shift
DEPENDANCY_RULE_TYPE=$1
shift
DEPENDANCY_RULE_VALUE=$1
shift

COMMAND_NAME=$(basename $REPO_URL)
COMMAND_PACKAGE_PATH=$SCRATCH_PATH/checkouts/$COMMAND_NAME

swift package --quiet --package-path $PACKAGE_PATH --scratch-path $SCRATCH_PATH init >> $PACKAGE_PATH/logs.txt
swift package --quiet --package-path $PACKAGE_PATH --scratch-path $SCRATCH_PATH add-dependency $REPO_URL $DEPENDANCY_RULE_TYPE $DEPENDANCY_RULE_VALUE
swift package --quiet --package-path $PACKAGE_PATH --scratch-path $SCRATCH_PATH update 2>> $PACKAGE_PATH/logs.txt

swift run --quiet --package-path $COMMAND_PACKAGE_PATH $@

If named swift-package-run.sh can be called thus:

swift-package-run.sh --package-url https://github.com/jjrscott/echo --up-to-next-minor-from 0.0.0 echo "Hello World"

[disclaimer: does not answer the question as asked]

I do think this is the wrong command to be asking for; if you want to run something twice, at the very least there's an extra network request to check if any of the tags have changed. No, I think the interesting question here is "can we expose SwiftPM's tag-choosing as its own command", which would let you do

git clone --tags https://github.com/jjrscott/echo .
git switch --detach $(swift package choose-tag --up-to-next-minor-from 0.1)
swift run echo "hello"

or even

TAG="$(swift package choose-tag --up-to-next-minor-from 0.1 --package-url https://github.com/jjrscott/echo)"
git clone -b "$TAG" https://github.com/jjrscott/echo .
swift run echo "hello"

It's longer, sure, but it makes the network accesses explicit and avoidable. And you can still wrap it in a higher-level script if you want.

2 Likes

Probably not what you're looking for but in case you didn't know it, there's Mint:

You can run packages at different versions like so: mint run yonaskolb/XcodeGen@2.18.0

This is almost exactly what I need, with the only exception being that I think this should be built in to Swift Package Manager.

1 Like