-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathcode.swift
237 lines (207 loc) · 7.25 KB
/
code.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////
import Foundation
import ArgumentParser
import BlinkCode
import Network
class SharedFP {
let service: CodeFileSystemService
init(port: UInt16) {
let p = NWEndpoint.Port(rawValue: port)!
service = try! CodeFileSystemService.init(listenOn: p, tls: true, finished: { error in
if let error = error {
print("Listener failed - \(error)")
}
})
}
static var shared: SharedFP? = nil
static func startedFP(port: UInt16 = 50000) -> SharedFP {
guard let shared = shared,
shared.service.state == .ready else {
// We may need the WebServer to restart, instead of creating a new object.
// My theory is that this stops, and I don't get the new state because we are in background.
let shared = SharedFP(port: port)
self.shared = shared
return shared
}
return shared
}
}
enum FileLocationPathOrURL {
case fileLocationPath(FileLocationPath)
case url(URL)
init(_ str: String) throws {
if str.starts(with: "http://") || str.starts(with: "https://") {
if let url = URL(string: str) {
self = .url(url)
} else {
throw ArgumentParser.ValidationError("Invalid http(s) url")
}
} else {
self = .fileLocationPath(try FileLocationPath(str))
}
}
}
struct CodeCommand: NonStdIOCommand {
static var configuration = CommandConfiguration(
commandName: "code",
abstract: "Starts code editor",
discussion: discussion
)
static let discussion = """
To connect your Code instance to the Blink File System,
please install the blink-fs extension from the Marketplace.
To close, use your Blink close tab shortcut (default Cmd-W).
For more information, please read:
https://docs.blink.sh/advanced/code
"""
@OptionGroup var verboseOptions: VerboseOptions
var io = NonStdIO.standard
@Argument(
help: "Path to connect to or http(s) vscode like editor url",
transform: { try FileLocationPathOrURL($0) }
)
var pathOrUrl: FileLocationPathOrURL?
@Option(
help: "URL for vscode",
transform: {
guard let url = URL(string: $0) else {
throw ArgumentParser.ValidationError("Invalid vscode url")
}
return url
}
)
var vscodeURL: URL?
mutating func run() throws {
let session = Unmanaged<MCPSession>.fromOpaque(thread_context).takeUnretainedValue()
var path: FileLocationPath
try showBlinkFSWarning()
switch pathOrUrl {
case .url(var url):
var str = url.absoluteString
let githubCom = "https://github.com/"
let codespaces = "https://github.com/codespaces/"
if str.hasPrefix(githubCom) && !str.hasPrefix(codespaces) {
str = "https://github.dev/" + str[githubCom.endIndex...]
url = URL(string: str)!
}
let view = session.device?.view
DispatchQueue.main.async {
view?.addBrowserWebView(url, agent: "", injectUIO: true)
}
return
case .fileLocationPath(let p):
path = p
default:
// Start vscode.dev without blink-fs but with inject user IO.
// This is useful to connect to vscode tunnels (remote server) from vscode.dev.
let url = URL(string: "https://vscode.dev")!
let view = session.device?.view
DispatchQueue.main.async {
view?.addBrowserWebView(url, agent: "", injectUIO: true)
}
return
}
let fp = SharedFP.startedFP(port: 50000)
let port = fp.service.port
guard let rootURI = path.codeFileSystemURI else {
throw CommandError(message: "Could not parse path.")
}
let token = fp.service.registerMount(name: "xxx", root: rootURI)
var observers: [NSObjectProtocol] = [NSObject()]
observers[0] = NotificationCenter.default.addObserver(forName: .deviceTerminated, object: nil, queue: nil) { notification in
guard let device = notification.userInfo?["device"] as? TermDevice
else {
return
}
if let sessionDevice = session.device, sessionDevice == device {
fp.service.deregisterMount(token)
}
NotificationCenter.default.removeObserver(observers[0])
}
let url = vscodeURL ?? URL(string: "https://vscode.dev")!
let agent = "BlinkSH/15 (wss;\(port);\(token))"
let view = session.device?.view
DispatchQueue.main.async {
view?.addBrowserWebView(url, agent: agent, injectUIO: true)
}
}
func showBlinkFSWarning() throws {
if FileManager.default.fileExists(atPath: BlinkPaths.blinkCodeErrorLogURL().path, isDirectory: nil) {
return
}
print(Self.discussion)
print("Press enter to continue.")
let _ = io.in_.readLine()
}
}
@_cdecl("code_main")
public func code_main(argc: Int32, argv: Argv) -> Int32 {
setvbuf(thread_stdin, nil, _IONBF, 0)
setvbuf(thread_stdout, nil, _IONBF, 0)
setvbuf(thread_stderr, nil, _IONBF, 0)
let io = NonStdIO.standard
io.in_ = InputStream(file: thread_stdin)
io.out = OutputStream(file: thread_stdout)
io.err = OutputStream(file: thread_stderr)
return CodeCommand.main(Array(argv.args(count: argc)[1...]), io: io)
}
extension FileLocationPath {
// blinkfs:/path
// blinksftp://user@host:port/path
internal var codeFileSystemURI: URI? {
if proto == .local {
// /var/__PATH__/home/. -> /var/__PATH__/home
let resolvedPath = (filePath as NSString).standardizingPath
return try? URI(string: uriProtocolIdentifier +
resolvedPath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)
} else {
// "user@host#port" -> "user@host:port"
guard let hostPath = hostPath else {
return nil
}
let host = "/\(hostPath.replacingOccurrences(of: "#", with: ":"))"
return try? URI(string: "\(uriProtocolIdentifier)\(host)\(filePath)")
}
}
fileprivate var uriProtocolIdentifier: String {
switch proto {
case .local:
// local paths do not need a domain, just the colon separator.
// blinkfs:path/to/files
return "blinkfs:"
default:
// remote paths need a domain, so we add an extra slash for the host
// blinksftp://host/path/to
return "blinksftp:/"
}
}
}