From 4776ab95a5719e717c39ccfb60751ad2aa41902f Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 4 Nov 2017 23:28:58 +0300 Subject: [PATCH] Fix problems in XZ command when outputPath wasn't specified --- Sources/swcomp/XZCommand.swift | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Sources/swcomp/XZCommand.swift b/Sources/swcomp/XZCommand.swift index fb0f08d3..fdb1cd55 100644 --- a/Sources/swcomp/XZCommand.swift +++ b/Sources/swcomp/XZCommand.swift @@ -16,11 +16,25 @@ class XZCommand: Command { let outputPath = OptionalParameter() func execute() throws { - let fileData = try Data(contentsOf: URL(fileURLWithPath: self.archive.value), - options: .mappedIfSafe) - let outputPath = self.outputPath.value ?? FileManager.default.currentDirectoryPath + let inputURL = URL(fileURLWithPath: self.archive.value) + + let outputURL: URL + if let outputPath = self.outputPath.value { + outputURL = URL(fileURLWithPath: outputPath) + } else if inputURL.pathExtension == "xz" { + outputURL = inputURL.deletingPathExtension() + } else { + print(""" + ERROR: Unable to get output path. \ + No output parameter was specified. \ + Extension was: \(inputURL.pathExtension) + """) + exit(1) + } + + let fileData = try Data(contentsOf: inputURL, options: .mappedIfSafe) let decompressedData = try XZArchive.unarchive(archive: fileData) - try decompressedData.write(to: URL(fileURLWithPath: outputPath)) + try decompressedData.write(to: outputURL) } }