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
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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...",
Expand Down Expand Up @@ -536,6 +546,14 @@
"command": "swift.createNewProject",
"when": "!swift.isActivated || swift.createNewProjectAvailable"
},
{
"command": "swift.browsePackageIndex",
"when": "swift.hasPackage && swift.addPackageRefactoringAvailable"
},
{
"command": "swift.addPackageDependency",
"when": "swift.hasPackage && swift.addPackageRefactoringAvailable"
},
{
"command": "swift.updateDependencies",
"when": "swift.hasPackage"
Expand Down Expand Up @@ -1163,4 +1181,4 @@
"vscode-languageclient": "^9.0.1",
"xml2js": "^0.6.2"
}
}
}
3 changes: 3 additions & 0 deletions src/WorkspaceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export class WorkspaceContext implements vscode.Disposable {
contextKeys.createNewProjectAvailable = toolchain.swiftVersion.isGreaterThanOrEqual(
new Version(5, 8, 0)
);
contextKeys.addPackageRefactoringAvailable = toolchain.swiftVersion.isGreaterThanOrEqual(
new Version(6, 0, 0)
);

const onChangeConfig = vscode.workspace.onDidChangeConfiguration(async event => {
// on toolchain config change, reload window
Expand Down
145 changes: 145 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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.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))) {
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
Expand Down Expand Up @@ -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)
),
Expand Down
7 changes: 7 additions & 0 deletions src/contextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ const contextKeys = {
set createNewProjectAvailable(value: boolean) {
vscode.commands.executeCommand("setContext", "swift.createNewProjectAvailable", value);
},

/**
* Whether the swift.addPackageRefactoringAvailable command is available.
*/
set addPackageRefactoringAvailable(value: boolean) {
vscode.commands.executeCommand("setContext", "swift.addPackageRefactoringAvailable", value);
},
};

export default contextKeys;