Skip to content

Commit

Permalink
Use temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
morishin committed May 17, 2017
1 parent 00b2896 commit 68b1522
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
46 changes: 22 additions & 24 deletions Sources/ToyboxKit/Handler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ public struct XcodeOpener: PlaygroundOpenerType {
}

public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoaderType, Opener: PlaygroundOpenerType> {
private let autoremoveSuffix = ".autoremove"

public var rootURL: URL {
return Workspace.rootURL
}

public init() {
executeAutoremove()
}

public func bootstrap() -> Result<(), ToyboxError> {
Expand All @@ -80,27 +77,22 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
}

public func list(for platform: Platform? = nil) -> Result<[String], ToyboxError> {
var filteredPlaygrounds: [Playground]
let filteredPlaygrounds: [Playground]
if let platform = platform {
filteredPlaygrounds = playgrounds.filter { $0.platform == platform }
} else {
filteredPlaygrounds = playgrounds
}
filteredPlaygrounds = filteredPlaygrounds.filter { !$0.name.hasSuffix(autoremoveSuffix) }

return .success(filteredPlaygrounds.map { String(describing: $0) })
}

public func create(_ name: String?, for platform: Platform, force: Bool = false, autoremove: Bool = false) -> Result<Playground, ToyboxError> {
var baseName: String = name ?? generateDefaultFileName()
if autoremove {
baseName = baseName.appending(autoremoveSuffix)
}
let targetPath = fullPath(from: baseName)
public func create(_ name: String?, for platform: Platform, force: Bool = false, temporary: Bool = false) -> Result<Playground, ToyboxError> {
let baseName: String = name ?? generateDefaultFileName()
let targetPath = fullPath(from: baseName, temporary: temporary)

if isExist(at: targetPath) {
do {
if force {
if force || temporary {
let manager = FileManager()
try manager.removeItem(at: targetPath)
} else {
Expand All @@ -110,7 +102,7 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
return .failure(ToyboxError.createError(baseName))
}
}
guard case let .success(createdURL) = copyTemplate(of: platform, for: "\(baseName).playground") else {
guard case let .success(createdURL) = copyTemplate(of: platform, for: baseName, temporary: temporary) else {
return .failure(ToyboxError.createError(baseName))
}
guard case let .success(playground) = Playground.load(from: createdURL) else {
Expand All @@ -119,8 +111,8 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
return .success(playground)
}

public func open(_ name: String, with xcodePath: URL? = nil) -> Result<(), ToyboxError> {
let path = fullPath(from: name)
public func open(_ name: String, with xcodePath: URL? = nil, temporary: Bool = false) -> Result<(), ToyboxError> {
let path = fullPath(from: name, temporary: temporary)
if isExist(at: path) {
Opener.open(at: path, with: xcodePath)
} else {
Expand All @@ -129,16 +121,20 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
return .success()
}

private func fullPath(from name: String) -> URL {
return Workspace.rootURL.appendingPathComponent("\(name).playground")
private func fullPath(from name: String, temporary: Bool = false) -> URL {
if temporary {
return temporaryDirectory.appendingPathComponent("\(name).playground")
} else {
return Workspace.rootURL.appendingPathComponent("\(name).playground")
}
}

private func templatePath(of platform: Platform) -> URL {
return Loader.templatePath(of: platform)
}

private func copyTemplate(of platform: Platform, for name: String) -> Result<URL, ToyboxError> {
let destinationPath = Workspace.rootURL.appendingPathComponent(name)
private func copyTemplate(of platform: Platform, for name: String, temporary: Bool = false) -> Result<URL, ToyboxError> {
let destinationPath = fullPath(from: name, temporary: temporary)
let sourcePath = templatePath(of: platform)
let manager = FileManager()
do {
Expand Down Expand Up @@ -179,10 +175,12 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
}
}

private func executeAutoremove() {
playgrounds
.filter { $0.name.hasSuffix(autoremoveSuffix) }
.forEach { try? FileManager.default.removeItem(at: $0.path) }
private var temporaryDirectory: URL {
if #available(OSX 10.12, *) {
return FileManager.default.temporaryDirectory
} else {
return URL(fileURLWithPath: NSTemporaryDirectory())
}
}
}

Expand Down
13 changes: 7 additions & 6 deletions Sources/toybox/Commands/Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ struct CreateOptions: OptionsProtocol {
let noOpen: Bool
let enableStandardInput: Bool
let xcodePath: URL?
let autoremove: Bool
let noSave: Bool

static func create(_ platform: Platform) -> (String?) -> ([String]) -> (Bool) -> (Bool) -> (Bool) -> (Bool) -> CreateOptions {
return { xcodePathString in { fileNames in { force in { noOpen in { standardInput in { autoremove in
return { xcodePathString in { fileNames in { force in { noOpen in { standardInput in { noSave in
let xcodePath: URL?
if let xcodePathString = xcodePathString {
xcodePath = URL(fileURLWithPath: xcodePathString)
Expand All @@ -27,7 +27,7 @@ struct CreateOptions: OptionsProtocol {
noOpen: noOpen,
enableStandardInput: standardInput,
xcodePath: xcodePath,
autoremove: autoremove)
noSave: noSave)
} } } } }
}
}
Expand All @@ -40,7 +40,7 @@ struct CreateOptions: OptionsProtocol {
<*> m <| Switch(flag: "f", key: "force", usage: "Whether to overwrite existing playground")
<*> m <| Switch(key: "no-open", usage: "Whether to open new playground")
<*> m <| Switch(key: "input", usage: "Whether to enable standard input")
<*> m <| Switch(key: "rm", usage: "Remove playground file automatically")
<*> m <| Switch(key: "no-save", usage: "Remove playground file automatically")
}
}

Expand All @@ -58,7 +58,7 @@ struct CreateCommand: CommandProtocol {
}

let fileName = options.fileName
switch handler.create(fileName, for: options.platform, force: options.force, autoremove: options.autoremove) {
switch handler.create(fileName, for: options.platform, force: options.force, temporary: options.noSave) {
case let .success(playground):

if options.enableStandardInput {
Expand All @@ -71,7 +71,8 @@ struct CreateCommand: CommandProtocol {

if !options.noOpen {
_ = handler.open(playground.name,
with: options.xcodePath)
with: options.xcodePath,
temporary: options.noSave)
}
return .success()
case let .failure(error):
Expand Down
41 changes: 24 additions & 17 deletions Tests/ToyboxKit/HandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class HandlerTests: XCTestCase {
return handler.rootURL.appendingPathComponent("\(name).playground")
}

func temporaryPlaygroundURL(for name: String) -> URL {
if #available(OSX 10.12, *) {
return manager.temporaryDirectory.appendingPathComponent("\(name).playground")
} else {
return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(name).playground")
}
}

func testList() {
_ = handler.bootstrap()
guard case let .success(list0) = handler.list() else {
Expand Down Expand Up @@ -63,18 +71,15 @@ class HandlerTests: XCTestCase {
XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "hello").path))
}

func testCreateWithAutoremoveOption() {
func testCreateWithTemporaryOption() {
_ = handler.create("hello", for: .iOS, temporary: true)
XCTAssertTrue(manager.fileExists(atPath: temporaryPlaygroundURL(for: "hello").path))
XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "hello").path))
let result = handler.create("hello", for: .iOS, autoremove: true)
if case .failure(_) = result {
XCTFail()
}
XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "hello.autoremove").path))
}

func testListHidesAutoremoveFile() {
func testListDoesNotShowTemporaryFile() {
_ = handler.create("foo", for: .iOS)
_ = handler.create("bar", for: .iOS, autoremove: true)
_ = handler.create("bar", for: .iOS, temporary: true)

guard case let .success(list1) = handler.list() else {
XCTFail()
Expand All @@ -97,16 +102,18 @@ class HandlerTests: XCTestCase {
XCTAssertTrue(AssertOpener.opened)
}

func testAutoremoveFileWhenHandlerCreated() {
let handler = TestingPlaygroundHandler()
_ = handler.create("foo", for: .iOS)
_ = handler.create("bar", for: .iOS, autoremove: true)
XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "foo").path))
XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "bar.autoremove").path))
func testOpenTemporaryFile() {
struct AssertOpener: PlaygroundOpenerType {
static var opened = false

_ = TestingPlaygroundHandler()
XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "foo").path))
XCTAssertFalse(manager.fileExists(atPath: playgroundURL(for: "bar.autoremove").path))
static func open(at path: URL, with xcodePath: URL?) {
opened = true
}
}
let handler = PlaygroundHandler<TestingStorage, PackagedTemplateLoader, AssertOpener>()
_ = handler.create("foobar", for: .iOS, temporary: true)
_ = handler.open("foobar", temporary: true)
XCTAssertTrue(AssertOpener.opened)
}

override func tearDown() {
Expand Down

0 comments on commit 68b1522

Please sign in to comment.