Skip to content

Commit

Permalink
Update SwiftSDKGenerator and GeneratorCLI to use logger instead of print
Browse files Browse the repository at this point in the history
  • Loading branch information
xtremekforever committed Jan 15, 2025
1 parent 19119c1 commit 64658ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
12 changes: 7 additions & 5 deletions Sources/GeneratorCLI/GeneratorCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import struct SystemPackage.FilePath

@main
struct GeneratorCLI: AsyncParsableCommand {
static let logger = Logger(label: "org.swift.swift-sdk-generator")

static let configuration = CommandConfiguration(
commandName: "swift-sdk-generator",
subcommands: [MakeLinuxSDK.self, MakeWasmSDK.self],
Expand All @@ -29,7 +31,6 @@ struct GeneratorCLI: AsyncParsableCommand {
options: GeneratorOptions
) async throws {
let elapsed = try await ContinuousClock().measure {
let logger = Logger(label: "org.swift.swift-sdk-generator")
let generator = try await SwiftSDKGenerator(
bundleVersion: options.bundleVersion,
targetTriple: targetTriple,
Expand Down Expand Up @@ -57,7 +58,8 @@ struct GeneratorCLI: AsyncParsableCommand {
try await generatorTask.value
}

print("\nTime taken for this generator run: \(elapsed.intervalString).")
logger.info("")
logger.info("Time taken for this generator run: \(elapsed.intervalString).")
}
}

Expand Down Expand Up @@ -151,7 +153,7 @@ extension GeneratorCLI {
let current = try SwiftSDKGenerator.getCurrentTriple(isVerbose: self.verbose)
if let arch = hostArch {
let target = Triple(arch: arch, vendor: current.vendor!, os: current.os!)
print("deprecated: Please use `--host \(target.triple)` instead of `--host-arch \(arch)`")
GeneratorCLI.logger.warning("deprecated: Please use `--host \(target.triple)` instead of `--host-arch \(arch)`")
return target
}
return current
Expand Down Expand Up @@ -202,14 +204,14 @@ extension GeneratorCLI {
}
if let arch = generatorOptions.targetArch {
let target = Triple(arch: arch, vendor: nil, os: .linux, environment: .gnu)
print("deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`")
GeneratorCLI.logger.warning("deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`")
}
return Triple(arch: hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu)
}

func run() async throws {
if self.isInvokedAsDefaultSubcommand() {
print(
GeneratorCLI.logger.warning(
"deprecated: Please explicitly specify the subcommand to run. For example: $ swift-sdk-generator make-linux-sdk"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ extension SwiftSDKGenerator {
return result
}

print("Using downloaded artifacts in these locations:")
logger.info("Using downloaded artifacts in these locations:")
for path in results.map(\.path) {
print(path)
logger.info("\(path)")
}
}

Expand Down Expand Up @@ -108,13 +108,13 @@ extension SwiftSDKGenerator {
)
}

print("Downloading \(urls.count) Ubuntu packages...")
logger.info("Downloading \(urls.count) Ubuntu packages...")
try await inTemporaryDirectory { fs, tmpDir in
let downloadedFiles = try await self.downloadFiles(from: urls, to: tmpDir, client, engine)
report(downloadedFiles: downloadedFiles)
await report(downloadedFiles: downloadedFiles)

for fileName in urls.map(\.lastPathComponent) {
print("Extracting \(fileName)...")
logger.info("Extracting \(fileName)...")
try await fs.unpack(file: tmpDir.appending(fileName), into: sdkDirPath)
}
}
Expand Down Expand Up @@ -149,13 +149,13 @@ extension SwiftSDKGenerator {
return result
}
}
}

private func report(downloadedFiles: [(URL, UInt64)]) {
let byteCountFormatter = ByteCountFormatter()
private func report(downloadedFiles: [(URL, UInt64)]) {
let byteCountFormatter = ByteCountFormatter()

for (url, bytes) in downloadedFiles {
print("\(url)\(byteCountFormatter.string(fromByteCount: Int64(bytes)))")
for (url, bytes) in downloadedFiles {
logger.info("\(url)\(byteCountFormatter.string(fromByteCount: Int64(bytes)))")
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public actor SwiftSDKGenerator {
try await self.inTemporaryDirectory { _, tmp in
try await Shell.run(#"cd "\#(tmp)" && ar -x "\#(debFile)""#, shouldLogCommands: isVerbose)
if isVerbose {
try await print(Shell.readStdout("ls \(tmp)"))
let lsOutput = try await Shell.readStdout("ls \(tmp)")
logger.info("\(lsOutput)")
}

try await Shell.run(
Expand Down

0 comments on commit 64658ea

Please sign in to comment.