-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DO NOT MERGE] Prototype for SPI integration #809
Draft
daveyc123
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
daveyc123:spi-prototype
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,147 @@ export async function createNewProject(ctx: WorkspaceContext): Promise<void> { | |
} | ||
} | ||
|
||
/** | ||
* Prompts the user to input project details and then executes `swift package init` | ||
* to create the project. | ||
*/ | ||
export async function browsePackageIndex(ctx: WorkspaceContext): Promise<void> { | ||
// The context key `swift.addPackageRefactoringAvailable` only works if the extension has been | ||
// activated. As such, we also have to allow this command to run when no workspace is | ||
// active. Show an error to the user if the command is unavailable. | ||
if (!ctx.toolchain.swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0))) { | ||
vscode.window.showErrorMessage( | ||
"Browsing the package index is only available starting in swift version 6.0.0." | ||
); | ||
return; | ||
} | ||
|
||
const panel = vscode.window.createWebviewPanel( | ||
"swiftPackageIndex", | ||
"Swift Package Index", | ||
vscode.ViewColumn.One, | ||
{ | ||
retainContextWhenHidden: true, | ||
enableScripts: true, | ||
} | ||
); | ||
|
||
panel.webview.onDidReceiveMessage( | ||
message => { | ||
switch (message.command) { | ||
case "addSwiftPackage": | ||
addPackageDependency(ctx, message.url, message.version); | ||
return; | ||
default: | ||
// Log message | ||
vscode.window.showErrorMessage(`An error occurred when adding packages`); | ||
return; | ||
} | ||
}, | ||
undefined, | ||
ctx.subscriptions | ||
); | ||
|
||
panel.webview.html = `<html> | ||
<iframe name="vscode_iframe" id="mainframe" src="http://localhost:8080/" style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"> | ||
Your browser doesn't support iframes | ||
</iframe> | ||
<script> | ||
const vscode = acquireVsCodeApi(); | ||
window.addEventListener('message', e => { | ||
action = e.data.action; | ||
if (action === "addPackage") { | ||
url = e.data.url | ||
version = e.data.version | ||
|
||
if (url && version) { | ||
vscode.postMessage({ | ||
command: 'addSwiftPackage', | ||
url: url, | ||
version: version | ||
}) | ||
} else { | ||
console.log("Unknown message: " + e) | ||
} | ||
} else { | ||
console.log("Unknown message: " + e) | ||
} | ||
}, false); | ||
</script> | ||
|
||
</html>`; | ||
} | ||
|
||
export async function addPackageDependency( | ||
ctx: WorkspaceContext, | ||
url?: string, | ||
version?: string | ||
): Promise<void> { | ||
// The context key `swift.addPackageRefactoringAvailable` only works if the extension has been | ||
// activated. As such, we also have to allow this command to run when no workspace is | ||
// active. Show an error to the user if the command is unavailable. | ||
if (!ctx.toolchain.swiftVersion.isGreaterThanOrEqual(new Version(6, 0, 0))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of having this command available when you have no package loaded? |
||
vscode.window.showErrorMessage( | ||
"Adding a new swift package dependency is only available starting in swift version 6.0.0." | ||
); | ||
return; | ||
} | ||
|
||
if (!ctx.currentFolder) { | ||
vscode.window.showErrorMessage("An error occurred when adding packages"); | ||
return; | ||
} | ||
const folderContext = ctx.currentFolder; | ||
|
||
if (!url) { | ||
url = await vscode.window.showInputBox({ | ||
prompt: "Enter the URL for the package", | ||
validateInput(value) { | ||
if (value.trim() === "") { | ||
return "URL cannot be empty."; | ||
} | ||
return undefined; | ||
}, | ||
}); | ||
|
||
if (!url) { | ||
return; | ||
} | ||
} | ||
|
||
if (!version) { | ||
version = await vscode.window.showInputBox({ | ||
prompt: "Enter the version for the package", | ||
validateInput(value) { | ||
if (value.trim() === "") { | ||
return "Version cannot be empty."; | ||
} | ||
return undefined; | ||
}, | ||
}); | ||
|
||
if (!version) { | ||
return; | ||
} | ||
} | ||
|
||
const resolveTask = createSwiftTask( | ||
["package", "add-dependency", url, "--branch", version], | ||
"Adding Package Dependency", | ||
{ | ||
cwd: folderContext.folder, | ||
scope: folderContext.workspaceFolder, | ||
prefix: folderContext.name, | ||
presentationOptions: { reveal: vscode.TaskRevealKind.Silent }, | ||
}, | ||
folderContext.workspaceContext.toolchain | ||
); | ||
|
||
await executeTaskWithUI(resolveTask, "Adding Package Dependency", folderContext); | ||
|
||
await openPackage(ctx); | ||
} | ||
|
||
/** | ||
* Run `swift package update` inside a folder | ||
* @param folderContext folder to run update inside | ||
|
@@ -840,6 +981,10 @@ function updateAfterError(result: boolean, folderContext: FolderContext) { | |
export function register(ctx: WorkspaceContext) { | ||
ctx.subscriptions.push( | ||
vscode.commands.registerCommand("swift.createNewProject", () => createNewProject(ctx)), | ||
vscode.commands.registerCommand("swift.browsePackageIndex", () => browsePackageIndex(ctx)), | ||
vscode.commands.registerCommand("swift.addPackageDependency", (url, version) => | ||
addPackageDependency(ctx, url, version) | ||
), | ||
vscode.commands.registerCommand("swift.resolveDependencies", () => | ||
resolveDependencies(ctx) | ||
), | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we break out some of the web view stuff, into a general function that can be used by other systems. Currently when you right click on a package dependency and select
View Repository
it opens a web page in your default browser. Would be cool if we could do that inside VSCode instead in a similar manner to how this works.