Skip to content
Merged
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
54 changes: 39 additions & 15 deletions Sources/AtCoderLibrary/API/OjApiCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@ enum OjApiCommand {
return (contest, problems)
}

static func submitCode(contestName: String, task: Character, ojApiPath: String) throws -> URL {
static func submitCode(contestName: String, task: String, ojApiPath: String) throws -> URL {
try precheck(path: ojApiPath)
let result = run(
ojApiPath,
"submit-code",
"--file",
"Sources/\(task.uppercased())/main.swift",
"--language",
"4055",
"https://atcoder.jp/contests/\(contestName)/tasks/\(contestName)_\(task.lowercased())"
)
guard result.succeeded else {
throw result.stderror
let contestURL = "https://atcoder.jp/contests/\(contestName)"
let contest = try getContest(url: contestURL, ojApiPath: ojApiPath)
guard let problem = contest.problems.first(where: {
$0.context.alphabet.uppercased() == task.uppercased()
}) else {
throw "The contest name or the task name is invalid."
}
print(result.stdout)
let response = try JSONDecoder().decode(SubmitCodeResponse.self, from: result.stdout.data(using: .utf8)!)
return response.result.url
let filePath = "Sources/\(task.uppercased())/main.swift"
let language = try guessLanguage(url: problem.url, filePath: filePath, ojApiPath: ojApiPath)
return try submitCode(url: problem.url, filePath: filePath, language: language, ojApiPath: ojApiPath)
}
}

Expand Down Expand Up @@ -63,4 +58,33 @@ private extension OjApiCommand {
let response = try JSONDecoder().decode(GetProblemResponse.self, from: result.stdout.data(using: .utf8)!)
return response.result
}

static func guessLanguage(url: URL, filePath: String, ojApiPath: String) throws -> Language {
try precheck(path: ojApiPath)
let result = run(ojApiPath, "guess-language-id", url.absoluteString, "--file=\(filePath)")
guard result.succeeded else {
throw result.stderror
}
print(result.stdout)
let response = try JSONDecoder().decode(GuessLanguageResponse.self, from: result.stdout.data(using: .utf8)!)
return response.result
}

static func submitCode(url: URL, filePath: String, language: Language, ojApiPath: String) throws -> URL {
let result = run(
ojApiPath,
"submit-code",
"--file",
filePath,
"--language",
language.id,
url.absoluteString
)
guard result.succeeded else {
throw result.stderror
}
print(result.stdout)
let response = try JSONDecoder().decode(SubmitCodeResponse.self, from: result.stdout.data(using: .utf8)!)
return response.result.url
}
}
9 changes: 9 additions & 0 deletions Sources/AtCoderLibrary/API/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ extension OjApiCommand {
let result: Problem
}

struct GuessLanguageResponse: Decodable {
let result: Language
}

struct SubmitCodeResponse: Decodable {
let result: Result

Expand Down Expand Up @@ -57,3 +61,8 @@ struct Context: Decodable {
let url: URL
}
}

struct Language: Decodable {
let id: String
let description: String
}
4 changes: 2 additions & 2 deletions Sources/AtCoderLibrary/Command/Submit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public struct Submit: ParsableCommand {
abstract: "Submit a your code."
)

@Argument(help: "Alphabet of the problem to be submitted.", transform: Character.init)
var task: Character
@Argument(help: "Alphabet of the problem to be submitted.")
var task: String

@Flag(name: .shortAndLong, help: "Run a UnitTest before submitting.")
var runTest: Bool = false
Expand Down
2 changes: 1 addition & 1 deletion Sources/AtCoderLibrary/Command/Submit/RunTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import SwiftShell

enum RunTest {
static func sampleCase(task: Character) throws {
static func sampleCase(task: String) throws {
try runAndPrint("swift", "test", "--filter", "\(task.uppercased())Tests/testExample")
}
}