Last active
March 14, 2025 15:27
-
-
Save mr-fixit/5ad9c34ed48e5d05c69ab63f8b947dce to your computer and use it in GitHub Desktop.
generates the github URL of the topmost file in xcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# this script outputs the github URL of the frontmost file in Xcode. | |
# v0.2 | |
if [[ -z $(which jq) ]]; then | |
echo install jq with \'brew install jq\' | |
exit 1 | |
fi | |
TUPLE=$(osascript << HERE | |
tell application "Xcode" | |
set doc to document 1 whose name ends with (word -1 of (get name of window 1)) | |
set currentLines to selected paragraph range of doc | |
set L1 to get item 1 of currentLines | |
set L2 to get item 2 of currentLines | |
return path of doc & "," & L1 & "," & L2 | |
end tell | |
HERE | |
) | |
IFS=\, read -r filePath L1 L2 <<< ${TUPLE} | |
[[ -f "$filePath" ]] || (echo "no such file: $filePath"; exit 1) | |
if [[ "$filePath" =~ DerivedData ]]; then | |
# swift package manager | |
FPATH=${filePath#*checkouts/} | |
REPOS_DIR=${filePath%SourcePackages*}SourcePackages | |
ID=${FPATH%/Sources/*} | |
echo ID=$ID | |
FPATH=${FPATH#$ID*} | |
WSSTATE=${REPOS_DIR}/workspace-state.json | |
SPM=$(jq ".object.dependencies[] | select( .packageRef.identity == \"${ID}\" ) | { id:.packageRef.identity, loc:.packageRef.location, rev:.state.checkoutState.revision, vers:.state.checkoutState.version }" < $WSSTATE) | |
REV=$(echo $SPM | jq -r '.rev') | |
URL=$(echo $SPM | jq -r '.loc') | |
else | |
# file in git local working directory | |
cd "$(dirname ${filePath})" | |
URL=$(git remote get-url origin) | |
URL=${URL%.git} | |
REV=$(git rev-parse HEAD) | |
FPATH=$(git ls-files --full-name ${filePath}) | |
fi | |
TFM=${URL}/blob/${REV}/${FPATH} # TFM: the full monty | |
if [[ -n ${L1} ]]; then | |
TFM=${TFM}\#L${L1} | |
fi | |
if [[ -n ${L2} ]]; then | |
TFM=${TFM}\-L${L2} | |
fi | |
TFM=${TFM// /%20} # escape ' ' to '%20' | |
echo $TFM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment