Skip to content

Commit

Permalink
[archive] Accept a directory path input for --output option
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed Apr 9, 2016
1 parent 982ee91 commit 417e30d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
29 changes: 28 additions & 1 deletion Source/CarthageIntegration/archive.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,36 @@ teardown() {
}

@test "carthage archive after carthage build --no-skip-current produces a zipped framework of all frameworks" {
run carthage build --no-skip-current
run carthage build --platform mac --no-skip-current
[ "$status" -eq 0 ]
run carthage archive
[ "$status" -eq 0 ]
[ -e Ra.framework.zip ]
}

@test "carthage archive --output with a non-existing path ends with '/' should produce a zip file into the specified directory" {
run carthage build --platform mac --no-skip-current
[ "$status" -eq 0 ]
rm -rf FooBar
run carthage archive --output FooBar/
[ "$status" -eq 0 ]
[ -e FooBar/Ra.framework.zip ]
}

@test "carthage archive --output with an existing directory path should produce a zip file into the specified directory" {
run carthage build --platform mac --no-skip-current
[ "$status" -eq 0 ]
mkdir FooBar
run carthage archive --output FooBar
[ "$status" -eq 0 ]
[ -e FooBar/Ra.framework.zip ]
}

@test "carthage archive --output with a file path should produce a zip file at the given path" {
run carthage build --platform mac --no-skip-current
[ "$status" -eq 0 ]
rm -rf FooBar
run carthage archive --output FooBar/GreatName.framework.zip
[ "$status" -eq 0 ]
[ -e FooBar/GreatName.framework.zip ]
}
29 changes: 28 additions & 1 deletion Source/carthage/Archive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,41 @@ public struct ArchiveCommand: CommandType {
return SignalProducer(error: error)
}

let outputPath = options.outputPath ?? "\(frameworks.first!).zip"
let outputPath = self.outputPathWithOptions(options, frameworks: frameworks)
let outputURL = NSURL(fileURLWithPath: outputPath, isDirectory: false)

if let directory = outputURL.URLByDeletingLastPathComponent {
_ = try? NSFileManager.defaultManager().createDirectoryAtURL(directory, withIntermediateDirectories: true, attributes: nil)
}

return zipIntoArchive(outputURL, paths).on(completed: {
carthage.println(formatting.bullets + "Created " + formatting.path(string: outputPath))
})
}
}
.waitOnCommand()
}

/// Returns an appropriate output file path for the resulting zip file using
/// the given option and frameworks.
private func outputPathWithOptions(options: Options, frameworks: [String]) -> String {
let defaultOutputPath = "\(frameworks.first!).zip"

return options.outputPath.map { path -> String in
if path.hasSuffix("/") {
// The given path should be a directory.
return path + defaultOutputPath
}

var isDirectory: ObjCBool = false
if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDirectory) && isDirectory {
// If the given path is an existing directory, output a zip file
// into that directory.
return (path as NSString).stringByAppendingPathComponent(defaultOutputPath)
} else {
// Use the given path as the final output.
return path
}
} ?? defaultOutputPath
}
}

0 comments on commit 417e30d

Please sign in to comment.