diff --git a/Sources/Playground.swift b/Sources/Playground.swift index e95fccc..399ef01 100644 --- a/Sources/Playground.swift +++ b/Sources/Playground.swift @@ -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 @@ -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() @@ -78,6 +82,18 @@ public class Playground: Generatable { xml.append("") 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 diff --git a/Tests/XgenTests/XgenTests.swift b/Tests/XgenTests/XgenTests.swift index 83b24eb..7a74018 100644 --- a/Tests/XgenTests/XgenTests.swift +++ b/Tests/XgenTests/XgenTests.swift @@ -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