Skip to content

Commit

Permalink
Merge pull request #8 from JohnSundell/auxiliary-source-files
Browse files Browse the repository at this point in the history
Playground: Add support for auxiliary source files
  • Loading branch information
JohnSundell authored Dec 17, 2018
2 parents a878717 + 6da73ed commit 444b9ad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/Playground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Playground: Generatable {
public var autoRun: Bool
/// The code that the playground should contain (if nil, a default will be used)
public var code: String?
/// The auxiliary source files that the playground should include
public var auxiliarySourceFiles = [File]()

// MARK: - Initializer

Expand Down Expand Up @@ -61,6 +63,8 @@ public class Playground: Generatable {
let xmlFile = try folder.createFile(named: "contents.xcplayground")
try xmlFile.write(string: generateXML())

try copyAuxiliarySourceFiles(into: folder)

let workspace = Workspace(path: path + "playground.xcworkspace")
workspace.addReference(to: "self:")
try workspace.generate()
Expand All @@ -78,6 +82,18 @@ public class Playground: Generatable {
xml.append("</playground>")
return xml
}

private func copyAuxiliarySourceFiles(into folder: Folder) throws {
guard !auxiliarySourceFiles.isEmpty else {
return
}

let sourcesFolder = try folder.createSubfolder(named: "Sources")

for file in auxiliarySourceFiles {
try file.copy(to: sourcesFolder)
}
}
}

// MARK: - Private extensions
Expand Down
27 changes: 27 additions & 0 deletions Tests/XgenTests/XgenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ class XgenTests: XCTestCase {
let autoRunAttribute = xml.rootElement()?.attribute(forName: "executeOnSourceChanges")
XCTAssertEqual(autoRunAttribute?.stringValue, "false")
}

func testPlaygroundWithoutAuxiliarySourceFilesDoesNotHaveSourcesFolder() throws {
let playground = Playground(path: folder.path + "Playground")
try playground.generate()

let playgroundFolder = try folder.subfolder(named: "Playground.playground")
XCTAssertFalse(playgroundFolder.containsSubfolder(named: "Sources"))
}

func testGeneratingPlaygroundWithAuxiliarySourceFiles() throws {
let playground = Playground(path: folder.path + "Playground")
try playground.auxiliarySourceFiles = [
folder.createFile(named: "A.swift", contents: "//A"),
folder.createFile(named: "B.swift", contents: "//B")
]

try playground.generate()

let playgroundFolder = try folder.subfolder(named: "Playground.playground")
let sourcesFolder = try playgroundFolder.subfolder(named: "Sources")

let sourceFileNames = sourcesFolder.files.map { $0.nameExcludingExtension }
XCTAssertEqual(sourceFileNames, ["A", "B"])

let sources = try sourcesFolder.files.map { try $0.readAsString() }
XCTAssertEqual(sources, ["//A", "//B"])
}
}

// MARK: - Extensions
Expand Down

0 comments on commit 444b9ad

Please sign in to comment.