Skip to content

Commit

Permalink
add swift support
Browse files Browse the repository at this point in the history
  • Loading branch information
tonka3000 committed Jan 31, 2024
1 parent ab2fa4f commit 9a983b6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "raycast",
"displayName": "Raycast",
"description": "",
"version": "0.15.0",
"version": "0.16.0",
"engines": {
"vscode": "^1.67.0"
},
Expand Down Expand Up @@ -128,6 +128,11 @@
"title": "Publish",
"category": "Raycast"
},
{
"command": "raycast.addswiftsupport",
"title": "Add Swift Support",
"category": "Raycast"
},
{
"command": "raycast.attachdebugger",
"title": "Attach Debugger",
Expand Down Expand Up @@ -233,6 +238,10 @@
"command": "raycast.publish",
"when": "raycast.workspaceEnabled"
},
{
"command": "raycast.addswiftsupport",
"when": "raycast.workspaceEnabled"
},
{
"command": "raycast.attachdebugger",
"when": "raycast.workspaceEnabled"
Expand Down
2 changes: 2 additions & 0 deletions src/cmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { addCommandArgumentCmd } from "./commands/addCommandArgument";
import { updateInternalState } from "./commands/updateInternalState";
import { gotoCommandDisabledByDefaultManifestLocationCmd } from "./commands/goto/disabledByDefault";
import { extensionIssuesCmd } from "./commands/extensionIssues";
import { addSwiftSupportCmd } from "./commands/swiftSupport";

export function registerAllCommands(manager: ExtensionManager) {
manager.registerCommand("lint", async () => lintCmd(manager));
Expand Down Expand Up @@ -61,4 +62,5 @@ export function registerAllCommands(manager: ExtensionManager) {
gotoCommandArgumentManifestLocationCmd(manager, args),
);
manager.registerCommand("command.arguments.add", async (...args: any[]) => addCommandArgumentCmd(manager, args));
manager.registerCommand("addswiftsupport", async () => addSwiftSupportCmd(manager));
}
65 changes: 65 additions & 0 deletions src/commands/swiftSupport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as afs from "fs/promises";
import { ExtensionManager } from "../manager";
import * as vscode from "vscode";
import { fileExists } from "../utils";
import path = require("path");
import { readManifestFile } from "../manifest";

async function addSwiftSupport(manager: ExtensionManager, rootFolder: string) {
const manifest = await readManifestFile(manager.getActiveWorkspacePackageFilename());
const packageName = manifest?.name;
if (!packageName) {
throw new Error("Could not get name from manifest");
}
const gitignoreFilename = path.join(rootFolder, ".gitignore");
const gitignore = [".DS_Store", ".build/", ".swiftpm/", ".vscode/", "Package.resolved"];
const sourcesFolder = path.join(rootFolder, "Sources");
await afs.mkdir(sourcesFolder, { recursive: true });
await afs.writeFile(gitignoreFilename, gitignore.join("\n"));
const swiftPackageFilename = path.join(rootFolder, "Package.swift");
const swiftPackage = `// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "${packageName}",
platforms: [.macOS(.v12)],
dependencies: [
.package(url: "https://github.com/raycast/extensions-swift-tools.git", from: "1.0.1"),
],
targets: [
.executableTarget(
name: "${packageName}",
dependencies: [
.product(name: "RaycastSwiftMacros", package: "extensions-swift-tools"),
.product(name: "RaycastSwiftPlugin", package: "extensions-swift-tools"),
.product(name: "RaycastTypeScriptPlugin", package: "extensions-swift-tools"),
]
),
]
)`;
await afs.writeFile(swiftPackageFilename, swiftPackage);

const source = `import Foundation
import RaycastSwiftMacros
@raycast func hello() -> String {
"Hello from Swift"
}
`;
await afs.writeFile(path.join(sourcesFolder, `${packageName}.swift`), source);
}

export async function addSwiftSupportCmd(manager: ExtensionManager) {
manager.logger.debug("Add Swift support");
const ws = manager.getActiveWorkspace();
if (!ws) {
throw new Error("No active workspace");
}
const swiftRootFolder = path.join(ws.uri.fsPath, "swift");
if (await fileExists(swiftRootFolder)) {
throw new Error("Swift folder already exists");
}
await addSwiftSupport(manager, swiftRootFolder);
vscode.window.showInformationMessage("Swift Support added successfully");
}

0 comments on commit 9a983b6

Please sign in to comment.