Skip to content

Commit

Permalink
Support custom Java main entry name (close #440 #432 #403)
Browse files Browse the repository at this point in the history
  • Loading branch information
bummoblizard committed May 15, 2022
1 parent fbc7b39 commit 9760dff
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions CodeApp/DataModels/CloudCodeExecutionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
//

import SwiftUI
import ZIPFoundation

class CloudCodeExecutionManager: ObservableObject {

private static let judge0Server = "SERVER_URL"
private static let judge0Key = "YOUR_OWN_KEY"
private static let judge0Server = ""
private static let judge0Key = ""

@Published var isRunningCode = false
@Published var consoleContent = ""
Expand Down Expand Up @@ -51,7 +52,49 @@ class CloudCodeExecutionManager: ObservableObject {

}

private func generateJavaParameters(sourceURL: URL) -> [String: Any] {

let archive = Archive(accessMode: .create)!

do {
try archive.addEntry(with: sourceURL.lastPathComponent, fileURL: sourceURL)

let tempDir = FileManager().temporaryDirectory
let compileFile = tempDir.appendingPathComponent("compile")
let runFile = tempDir.appendingPathComponent("run")

try "javac \(sourceURL.lastPathComponent)".write(
to: compileFile, atomically: true, encoding: .utf8)
try "java \(sourceURL.deletingPathExtension().lastPathComponent)".write(
to: runFile, atomically: true, encoding: .utf8)

try archive.addEntry(with: "compile", fileURL: compileFile)
try archive.addEntry(with: "run", fileURL: runFile)
}catch {
return [:]
}


let base64 = archive.data!.base64EncodedString(options: .lineLength64Characters)

let parameters: [String: Any] = [
"language_id": 89, // Multi-file program
"stdin": stdin.base64Encoded()!,
"cpu_time_limit": 6,
"additional_files": base64,
]

return parameters
}

func runCode(directoryURL: URL, source: String, language: Int) {

if CloudCodeExecutionManager.judge0Server.isEmpty {
self.consoleContent = "Server-side execution is unsupported in TestFlight builds."
return
}


self.isRunningCode = true

self.consoleContent = "Running \(directoryURL.lastPathComponent) remotely..\n"
Expand All @@ -63,14 +106,17 @@ class CloudCodeExecutionManager: ObservableObject {
)!)
request.httpMethod = "POST"

let parameters: [String: Any] = [
"source_code": source.base64Encoded()!,
"language_id": language,
"stdin": stdin.base64Encoded()!,
"cpu_time_limit": 6,
"additional_files": returnDirectoryAsBase64(
url: directoryURL.deletingLastPathComponent(), fileURL: directoryURL),
]
let parameters: [String: Any] =
language == 62
? generateJavaParameters(sourceURL: directoryURL)
: [
"source_code": source.base64Encoded()!,
"language_id": language,
"stdin": stdin.base64Encoded()!,
"cpu_time_limit": 6,
"additional_files": returnDirectoryAsBase64(
url: directoryURL.deletingLastPathComponent(), fileURL: directoryURL),
]

do {
request.httpBody = try JSONSerialization.data(
Expand Down

0 comments on commit 9760dff

Please sign in to comment.