Last active
August 21, 2017 18:18
-
-
Save james-howard/6887542a389d93239a43acee2f12e393 to your computer and use it in GitHub Desktop.
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
function run() { | |
xcode = Application('Xcode') | |
var app = Application.currentApplication() | |
app.includeStandardAdditions = true; | |
docs = xcode.sourceDocuments() | |
for (var i = docs.length-1; i >= 0; i--) { | |
var doc = docs[i]; | |
var range = doc.selectedParagraphRange(); | |
if (!range) { | |
continue; | |
} | |
var start = range[0]; | |
var end = range[1]; | |
if (start > end) { | |
var tmp = start; | |
start = end; | |
end = tmp; | |
} | |
var path = doc.path(); | |
var basename = path.substring(path.lastIndexOf('/')+1); | |
var gitinfo = app.doShellScript( | |
`cd \`dirname '${path}'\`\n` + | |
`git remote -v | head -1\n` + | |
`git rev-parse --show-toplevel\n` + | |
`git rev-parse HEAD\n` | |
).split('\r'); | |
var remote = gitinfo[0]; | |
var gitroot = gitinfo[1]; | |
var branch = gitinfo[2]; | |
var relativePath = path.substring(gitroot.length+1); | |
var m = remote.match(/.*github.com\/(.*?)\/(.*?)\.git.*/); | |
if (m) { | |
var owner = m[1]; | |
var repo = m[2]; | |
var lineAnchor = `L${start}`; | |
if (end != start) { | |
lineAnchor += `-L${end}`; | |
} | |
var url = `https://github.com/${owner}/${repo}/blob/${branch}/${relativePath}#${lineAnchor}`; | |
app.openLocation(`ship+github://newissue/${owner}/${repo}?title=${escape(basename+":"+start)}&body=${escape(url)}`); | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put at ~/Library/Scripts/Applications/Xcode and use with Script Menu (turn on via Script Editor.app's preferences)