Skip to content

Commit

Permalink
Use websocket to communicate with extension thebaselab#1104
Browse files Browse the repository at this point in the history
  • Loading branch information
bummoblizard committed Aug 1, 2024
1 parent dcf4ea3 commit b1764a2
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 404 deletions.
67 changes: 66 additions & 1 deletion Code.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 54;
objectVersion = 60;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -530,6 +530,8 @@
945D4F582B50F55800DE0DBA /* VimKeyBufferLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945D4F572B50F55800DE0DBA /* VimKeyBufferLabel.swift */; };
945D4F592B50F55800DE0DBA /* VimKeyBufferLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945D4F572B50F55800DE0DBA /* VimKeyBufferLabel.swift */; };
9469AC9C26A33D52003F569F /* php.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9469AC9A26A33D19003F569F /* php.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
946C677D2C33BEE200100F3F /* SwiftWS in Frameworks */ = {isa = PBXBuildFile; productRef = 946C677C2C33BEE200100F3F /* SwiftWS */; };
946C67832C33C03400100F3F /* SwiftWS in Frameworks */ = {isa = PBXBuildFile; productRef = 946C67822C33C03400100F3F /* SwiftWS */; };
946DAB9E2BBD2F9600E73257 /* TreeSitterJavaScriptRunestone in Frameworks */ = {isa = PBXBuildFile; productRef = 946DAB9D2BBD2F9600E73257 /* TreeSitterJavaScriptRunestone */; };
946DABA42BBD98BB00E73257 /* TreeSitterPythonRunestone in Frameworks */ = {isa = PBXBuildFile; productRef = 946DABA32BBD98BB00E73257 /* TreeSitterPythonRunestone */; };
946DABA82BBD997800E73257 /* TreeSitterCRunestone in Frameworks */ = {isa = PBXBuildFile; productRef = 946DABA72BBD997800E73257 /* TreeSitterCRunestone */; };
Expand Down Expand Up @@ -642,6 +644,14 @@
949B3CBB25DBCCDC00BC83B5 /* MonacoIntellisenseExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949B3CBA25DBCCDB00BC83B5 /* MonacoIntellisenseExtension.swift */; };
949B3CC525DEA89B00BC83B5 /* String+toCString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949B3CC425DEA89A00BC83B5 /* String+toCString.swift */; };
949B3CC725DEAAA700BC83B5 /* node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949B3CC625DEAAA600BC83B5 /* node.swift */; };
949D787F2C33C4EB006D7480 /* Java.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D787E2C33C4EB006D7480 /* Java.swift */; };
949D78812C33C51E006D7480 /* OutputListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78802C33C51E006D7480 /* OutputListener.swift */; };
949D78832C33C563006D7480 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78822C33C563006D7480 /* Node.swift */; };
949D78862C33DA72006D7480 /* AppExtensionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78842C33DA48006D7480 /* AppExtensionService.swift */; };
949D78872C33DA72006D7480 /* AppExtensionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78842C33DA48006D7480 /* AppExtensionService.swift */; };
949D788A2C33DC3F006D7480 /* AppExtensionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78892C33DC3F006D7480 /* AppExtensionError.swift */; };
949D788B2C33DC3F006D7480 /* AppExtensionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78892C33DC3F006D7480 /* AppExtensionError.swift */; };
949D788C2C33DC3F006D7480 /* AppExtensionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949D78892C33DC3F006D7480 /* AppExtensionError.swift */; };
949E4025263563AC002C1997 /* harfbuzz.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 94D7DED42631D16C00FA0760 /* harfbuzz.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
949E402E263563AF002C1997 /* libpng.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 94D7DEC02631D0CA00FA0760 /* libpng.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
94A045EE280480E800182275 /* RemoteListSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94A045ED280480E800182275 /* RemoteListSection.swift */; };
Expand Down Expand Up @@ -763,6 +773,7 @@
94F52709259F9B7800337306 /* KeychainItemAccessibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94F52708259F9B7800337306 /* KeychainItemAccessibility.swift */; };
94F6B510280EFD07000DBAE2 /* FileDisplayName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94F6B50F280EFD07000DBAE2 /* FileDisplayName.swift */; };
94F6B511280EFD07000DBAE2 /* FileDisplayName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94F6B50F280EFD07000DBAE2 /* FileDisplayName.swift */; };
94F88FD92C5B4F3400326AD0 /* SwiftWS in Frameworks */ = {isa = PBXBuildFile; productRef = 94F88FD82C5B4F3400326AD0 /* SwiftWS */; };
94FA8977292FB86700163800 /* PDFViewerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94FA8975292FB86700163800 /* PDFViewerExtension.swift */; };
94FA8978292FB86700163800 /* PDFViewerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94FA8975292FB86700163800 /* PDFViewerExtension.swift */; };
94FC220B257F7D2A00B85D1B /* SourceControlEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94FC220A257F7D2900B85D1B /* SourceControlEntry.swift */; };
Expand Down Expand Up @@ -1825,6 +1836,11 @@
949B3CBA25DBCCDB00BC83B5 /* MonacoIntellisenseExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonacoIntellisenseExtension.swift; sourceTree = "<group>"; };
949B3CC425DEA89A00BC83B5 /* String+toCString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+toCString.swift"; sourceTree = "<group>"; };
949B3CC625DEAAA600BC83B5 /* node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = node.swift; sourceTree = "<group>"; };
949D787E2C33C4EB006D7480 /* Java.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Java.swift; sourceTree = "<group>"; };
949D78802C33C51E006D7480 /* OutputListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OutputListener.swift; sourceTree = "<group>"; };
949D78822C33C563006D7480 /* Node.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Node.swift; sourceTree = "<group>"; };
949D78842C33DA48006D7480 /* AppExtensionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppExtensionService.swift; sourceTree = "<group>"; };
949D78892C33DC3F006D7480 /* AppExtensionError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppExtensionError.swift; sourceTree = "<group>"; };
94A045ED280480E800182275 /* RemoteListSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteListSection.swift; sourceTree = "<group>"; };
94A045F02804816000182275 /* RemoteCreateSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteCreateSection.swift; sourceTree = "<group>"; };
94A045F3280481A900182275 /* RemoteTypeLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteTypeLabel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2052,6 +2068,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
946C67832C33C03400100F3F /* SwiftWS in Frameworks */,
946C677D2C33BEE200100F3F /* SwiftWS in Frameworks */,
94F88FD92C5B4F3400326AD0 /* SwiftWS in Frameworks */,
945AEA3827ECCB5F00BA193E /* NodeMobile.xcframework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2149,6 +2168,9 @@
94369B1025E3DDDA008419A0 /* NodeRunner */,
94369AFE25E3B933008419A0 /* Media.xcassets */,
94369B0025E3B933008419A0 /* ActionRequestHandler.swift */,
949D787E2C33C4EB006D7480 /* Java.swift */,
949D78822C33C563006D7480 /* Node.swift */,
949D78802C33C51E006D7480 /* OutputListener.swift */,
94369B0425E3B933008419A0 /* Info.plist */,
);
path = NodeExtension;
Expand Down Expand Up @@ -2343,6 +2365,14 @@
path = Local;
sourceTree = "<group>";
};
949D78882C33DC29006D7480 /* Shared */ = {
isa = PBXGroup;
children = (
949D78892C33DC3F006D7480 /* AppExtensionError.swift */,
);
path = Shared;
sourceTree = "<group>";
};
94A4438725A390C3006643FF /* KeychainWrapper */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2984,6 +3014,7 @@
9F3C2DF5291CCE6900BFF14C /* Types */ = {
isa = PBXGroup;
children = (
949D78882C33DC29006D7480 /* Shared */,
9F3C2DF6291CCE9A00BFF14C /* Remote.swift */,
9FC03E732920CF1700DECD1B /* Notification.swift */,
9F3C2DE92918E19B00BFF14C /* Theme.swift */,
Expand Down Expand Up @@ -3098,6 +3129,7 @@
9FA122782A8B6C9700E7B417 /* ActivityBarManager.swift */,
9F062FE52B58D40D006210AA /* FileTreeViewController.swift */,
9434C3EE2BA2CCBB00EB1CF6 /* WASMService.swift */,
949D78842C33DA48006D7480 /* AppExtensionService.swift */,
);
path = Managers;
sourceTree = "<group>";
Expand Down Expand Up @@ -3256,6 +3288,11 @@
dependencies = (
);
name = extension;
packageProductDependencies = (
946C677C2C33BEE200100F3F /* SwiftWS */,
946C67822C33C03400100F3F /* SwiftWS */,
94F88FD82C5B4F3400326AD0 /* SwiftWS */,
);
productName = extension;
productReference = 94369AFC25E3B933008419A0 /* extension.appex */;
productType = "com.apple.product-type.app-extension";
Expand Down Expand Up @@ -3402,6 +3439,7 @@
9F6BD94E2B46900400E2F8BC /* XCRemoteSwiftPackageReference "GCDWebServer" */,
94FD17002BAED26A003E1545 /* XCRemoteSwiftPackageReference "Runestone" */,
946DAB982BBD2F9600E73257 /* XCRemoteSwiftPackageReference "treesitterlanguages" */,
94F88FD72C5B4F3400326AD0 /* XCLocalSwiftPackageReference "SwiftWS" */,
);
productRefGroup = 944EEBF12563C381009D77FE /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -3613,6 +3651,7 @@
9419696A280316C7008AAEB2 /* openFilesApp.swift in Sources */,
9419696B280316C7008AAEB2 /* View+If.swift in Sources */,
9419696C280316C7008AAEB2 /* WorkSpaceStorage.swift in Sources */,
949D788B2C33DC3F006D7480 /* AppExtensionError.swift in Sources */,
94A045EF280480E800182275 /* RemoteListSection.swift in Sources */,
9419696D280316C7008AAEB2 /* DirectoryFolderMonitor.swift in Sources */,
94795C462931489C0057C12F /* ActivityBar.swift in Sources */,
Expand Down Expand Up @@ -3653,6 +3692,7 @@
9474D2922B6B454900CCC530 /* EditorImplementation.swift in Sources */,
9F062FE42B58D3F4006210AA /* FileTreeView.swift in Sources */,
94A045F22804816000182275 /* RemoteCreateSection.swift in Sources */,
949D78862C33DA72006D7480 /* AppExtensionService.swift in Sources */,
9FAE4994292D153F00B1B962 /* VideoViewerExtension.swift in Sources */,
944112A2282181E500A8F1D7 /* SFTPTerminalServiceProvider.swift in Sources */,
94196980280316C7008AAEB2 /* URL+relativePath.swift in Sources */,
Expand Down Expand Up @@ -3700,9 +3740,13 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
949D788C2C33DC3F006D7480 /* AppExtensionError.swift in Sources */,
94369B0125E3B933008419A0 /* ActionRequestHandler.swift in Sources */,
947313142BDFB80D004A9960 /* ExtensionCommunicationHelper.swift in Sources */,
94369B1325E3DE02008419A0 /* NodeRunner.mm in Sources */,
949D78832C33C563006D7480 /* Node.swift in Sources */,
949D787F2C33C4EB006D7480 /* Java.swift in Sources */,
949D78812C33C51E006D7480 /* OutputListener.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -3795,6 +3839,7 @@
94A777DC257B8C99008FE7B2 /* openFilesApp.swift in Sources */,
94B3DDFA260526D200C4F2B1 /* View+If.swift in Sources */,
94A777A2257ABDC3008FE7B2 /* WorkSpaceStorage.swift in Sources */,
949D788A2C33DC3F006D7480 /* AppExtensionError.swift in Sources */,
94A045EE280480E800182275 /* RemoteListSection.swift in Sources */,
94A777A8257ABEBE008FE7B2 /* DirectoryFolderMonitor.swift in Sources */,
94795C452931489C0057C12F /* ActivityBar.swift in Sources */,
Expand Down Expand Up @@ -3835,6 +3880,7 @@
9474D2912B6B454900CCC530 /* EditorImplementation.swift in Sources */,
9F062FE32B58D3F4006210AA /* FileTreeView.swift in Sources */,
94A045F12804816000182275 /* RemoteCreateSection.swift in Sources */,
949D78872C33DA72006D7480 /* AppExtensionService.swift in Sources */,
9FAE4993292D153F00B1B962 /* VideoViewerExtension.swift in Sources */,
944112A1282181E500A8F1D7 /* SFTPTerminalServiceProvider.swift in Sources */,
949B3CB925DADCB900BC83B5 /* URL+relativePath.swift in Sources */,
Expand Down Expand Up @@ -4400,6 +4446,13 @@
};
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
94F88FD72C5B4F3400326AD0 /* XCLocalSwiftPackageReference "SwiftWS" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = SwiftWS;
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCRemoteSwiftPackageReference section */
94196932280316C7008AAEB2 /* XCRemoteSwiftPackageReference "ZIPFoundation" */ = {
isa = XCRemoteSwiftPackageReference;
Expand Down Expand Up @@ -4516,6 +4569,14 @@
isa = XCSwiftPackageProductDependency;
productName = SwiftGit2;
};
946C677C2C33BEE200100F3F /* SwiftWS */ = {
isa = XCSwiftPackageProductDependency;
productName = SwiftWS;
};
946C67822C33C03400100F3F /* SwiftWS */ = {
isa = XCSwiftPackageProductDependency;
productName = SwiftWS;
};
946DAB9D2BBD2F9600E73257 /* TreeSitterJavaScriptRunestone */ = {
isa = XCSwiftPackageProductDependency;
package = 946DAB982BBD2F9600E73257 /* XCRemoteSwiftPackageReference "treesitterlanguages" */;
Expand Down Expand Up @@ -4920,6 +4981,10 @@
isa = XCSwiftPackageProductDependency;
productName = SwiftGit2;
};
94F88FD82C5B4F3400326AD0 /* SwiftWS */ = {
isa = XCSwiftPackageProductDependency;
productName = SwiftWS;
};
94FD17012BAED26A003E1545 /* Runestone */ = {
isa = XCSwiftPackageProductDependency;
package = 94FD17002BAED26A003E1545 /* XCRemoteSwiftPackageReference "Runestone" */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,51 @@
"version": "0.5.1"
}
},
{
"package": "swift-atomics",
"repositoryURL": "https://github.com/apple/swift-atomics.git",
"state": {
"branch": null,
"revision": "cd142fd2f64be2100422d658e7411e39489da985",
"version": "1.2.0"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "3d2dc41a01f9e49d84f0a3925fb858bed64f702d",
"version": "1.1.2"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "e4abde8be0e49dc7d66e6eed651254accdcd9533",
"version": "2.69.0"
}
},
{
"package": "swift-nio-transport-services",
"repositoryURL": "https://github.com/apple/swift-nio-transport-services",
"state": {
"branch": null,
"revision": "38ac8221dd20674682148d6451367f89c2652980",
"version": "1.21.0"
}
},
{
"package": "swift-system",
"repositoryURL": "https://github.com/apple/swift-system.git",
"state": {
"branch": null,
"revision": "d2ba781702a1d8285419c15ee62fd734a9437ff5",
"version": "1.3.2"
}
},
{
"package": "TreeSitter",
"repositoryURL": "https://github.com/tree-sitter/tree-sitter",
Expand Down
1 change: 1 addition & 0 deletions CodeApp/CodeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private func setup() {
wasmWebView.loadWorker()
initializeEnvironment()
Repository.initialize_libgit2()
AppExtensionService.shared.startServer()
}

private func versionNumberIncreased() -> Bool {
Expand Down
96 changes: 96 additions & 0 deletions CodeApp/Managers/AppExtensionService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// AppExtensionService.swift
// Code App
//
// Created by Ken Chung on 02/07/2024.
//

import Dynamic
import Foundation
import ios_system

class AppExtensionService: NSObject {
static let PORT = 50002
static let shared = AppExtensionService()
private var task: URLSessionWebSocketTask? = nil

func startServer() {
let BLE: AnyClass = (NSClassFromString("TlNFeHRlbnNpb24=".base64Decoded()!)!)
let ext = Dynamic(BLE).extensionWithIdentifier("thebaselab.VS-Code.extension", error: nil)
let frameworkDir = Bundle.main.privateFrameworksPath!
let frameworkDirBookmark = try! URL(fileURLWithPath: frameworkDir).bookmarkData()
let item = NSExtensionItem()
item.userInfo = [
"frameworksDirectoryBookmark": frameworkDirBookmark,
"port": AppExtensionService.PORT,
]
ext.setRequestInterruptionBlock(
{ uuid in
print("Extension server crashed, attempting to restart")
self.startServer()
} as RequestInterruptionBlock)

ext.beginExtensionRequestWithInputItems(
[item],
completion: { uuid in
let pid = ext.pid(forRequestIdentifier: uuid)
if let uuid = uuid {
print("Started extension request: \(uuid). Extension PID is \(pid)")
}
print("Extension server listening on 127.0.0.1:\(AppExtensionService.PORT)")
} as RequestBeginBlock)
}
func terminate() {
self.task?.cancel()
}

func call(
args: [String],
t_stdin: UnsafeMutablePointer<FILE>,
t_stdout: UnsafeMutablePointer<FILE>
) async throws {
defer {
self.task = nil
signal(SIGINT, SIG_DFL)
}

let signalCallback: sig_t = { signal in
AppExtensionService.shared.terminate()
}
signal(SIGINT, signalCallback)

let task = URLSession.shared.webSocketTask(
with: URL(string: "ws://127.0.0.1:\(String(AppExtensionService.PORT))/websocket")!)
self.task = task
task.resume()

let handle = FileHandle(fileDescriptor: fileno(t_stdin), closeOnDealloc: false)
handle.readabilityHandler = { fileHandle in
if let str = String(data: fileHandle.availableData, encoding: .utf8) {
Task {
try await task.send(.string(str))
print("Sending -> \(str)")
}
}
}

let frame = ExecutionRequestFrame(
args: args,
workingDirectoryBookmark: try? URL(
fileURLWithPath: FileManager.default.currentDirectoryPath
).bookmarkData()
)
try await task.send(.string(frame.stringRepresentation))
print("Sending -> \(frame.stringRepresentation)")

while let message = try? await task.receive() {
switch message {
case .string(let text):
print("Receiving <- \(text)")
fputs(text, t_stdout)
default:
continue
}
}
}
}
11 changes: 0 additions & 11 deletions CodeApp/Managers/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ class Executor {
}

func kill() {
if nodeUUID != nil {
let notificationName = CFNotificationName(
"com.thebaselab.code.node.stop" as CFString)
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
CFNotificationCenterPostNotification(
notificationCenter, notificationName, nil, nil, false)
}
if javascriptRunning {
javascriptRunning = false
return
Expand All @@ -117,10 +110,6 @@ class Executor {

ios_switchSession(persistentIdentifier.toCString())

if nodeUUID != nil {
ExtensionCommunicationHelper.writeToStdin(data: input + "\n")
}

stdin_file_input?.write(data)

if state == .running {
Expand Down
Loading

0 comments on commit b1764a2

Please sign in to comment.