Skip to content

Commit

Permalink
Add autoremove feature (#4)
Browse files Browse the repository at this point in the history
Add no-save feature
  • Loading branch information
morishin authored and giginet committed May 17, 2017
1 parent 0c285fc commit 5734d02
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ $ toybox create SpriteKitDemo --platform macos
$ toybox create UIKitDemo -f
# Create but don't open with Xcode
$ toybox create UIKitDemo --no-open
# Create and remove the file automatically
$ toybox create UIKitDemo --no-save
# Create and open with specific Xcode
$ toybox create UIKitDemo --xcode-path /Application/Xcode7.3.app
# Create Playground from standard input
Expand Down
32 changes: 22 additions & 10 deletions Sources/ToyboxKit/Handler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
return .success(filteredPlaygrounds.map { String(describing: $0) })
}

public func create(_ name: String?, for platform: Platform, force: Bool = false) -> Result<Playground, ToyboxError> {
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)
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 @@ -102,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 @@ -111,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 @@ -121,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 @@ -170,6 +174,14 @@ public struct PlaygroundHandler<Workspace: WorkspaceType, Loader: TemplateLoader
return []
}
}

private var temporaryDirectory: URL {
if #available(OSX 10.12, *) {
return FileManager.default.temporaryDirectory
} else {
return URL(fileURLWithPath: NSTemporaryDirectory())
}
}
}

public typealias ToyboxPlaygroundHandler = PlaygroundHandler<FileSystemWorkspace, PackagedTemplateLoader, XcodeOpener>
16 changes: 10 additions & 6 deletions Sources/toybox/Commands/Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ struct CreateOptions: OptionsProtocol {
let noOpen: Bool
let enableStandardInput: Bool
let xcodePath: URL?
let noSave: Bool

static func create(_ platform: Platform) -> (String?) -> ([String]) -> (Bool) -> (Bool) -> (Bool) -> CreateOptions {
return { xcodePathString in { fileNames in { force in { noOpen in { standardInput in
static func create(_ platform: Platform) -> (String?) -> ([String]) -> (Bool) -> (Bool) -> (Bool) -> (Bool) -> CreateOptions {
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 @@ -25,8 +26,9 @@ struct CreateOptions: OptionsProtocol {
force: force,
noOpen: noOpen,
enableStandardInput: standardInput,
xcodePath: xcodePath)
} } } }
xcodePath: xcodePath,
noSave: noSave)
} } } } }
}
}

Expand All @@ -38,6 +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(flag: "n", key: "no-save", usage: "Remove playground file automatically")
}
}

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

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

if options.enableStandardInput {
Expand All @@ -68,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
39 changes: 39 additions & 0 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,6 +71,23 @@ class HandlerTests: XCTestCase {
XCTAssertTrue(manager.fileExists(atPath: playgroundURL(for: "hello").path))
}

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))
}

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

guard case let .success(list1) = handler.list() else {
XCTFail()
return
}
XCTAssertEqual(list1.count, 1)
}

func testOpen() {
struct AssertOpener: PlaygroundOpenerType {
static var opened = false
Expand All @@ -77,6 +102,20 @@ class HandlerTests: XCTestCase {
XCTAssertTrue(AssertOpener.opened)
}

func testOpenTemporaryFile() {
struct AssertOpener: PlaygroundOpenerType {
static var opened = false

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() {
super.tearDown()

Expand Down

0 comments on commit 5734d02

Please sign in to comment.