Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -52,6 +52,16 @@
}
],
"commands": [
{
"command": "swift.browsePackageIndex",
"title": "Browse Package Index...",
"category": "Swift"
},
{
"command": "swift.addPackageDependency",
"title": "Add Package Dependency...",
"category": "Swift"
},
{
"command": "swift.createNewProject",
"title": "Create New Project...",
@@ -536,6 +546,14 @@
"command": "swift.createNewProject",
"when": "!swift.isActivated || swift.createNewProjectAvailable"
},
{
"command": "swift.browsePackageIndex",
"when": "swift.hasPackage"
},
{
"command": "swift.addPackageDependency",
"when": "swift.hasPackage"
},
{
"command": "swift.updateDependencies",
"when": "swift.hasPackage"
145 changes: 145 additions & 0 deletions src/commands.ts
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.browsePackageIndex` 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(
"Creating a new swift project is only available starting in swift version 6.0.0."
);
return;
}

const panel = vscode.window.createWebviewPanel(
Copy link
Contributor

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.

"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.addPackageDependency` 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))) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
),