Skip to content

Commit

Permalink
Merge pull request #77 from finestructure/sample-code
Browse files Browse the repository at this point in the history
sample code
  • Loading branch information
finestructure authored Nov 8, 2020
2 parents c3aab19 + 5a00700 commit 609cb21
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
34 changes: 14 additions & 20 deletions Sources/ArenaCore/ArenaCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ extension Arena {
// find libraries
packageInfo = Array(
zip(dependencies,
try dependencies.compactMap {
$0.path ?? $0.checkoutDir(packageDir: dependencyPackagePath)
}.compactMap { try getPackageInfo(in: $0) } )
try dependencies.compactMap { $0.sourceDir(packageDir: dependencyPackagePath) }
.compactMap { try getPackageInfo(in: $0) } )
)
let libs = packageInfo.flatMap { $0.1.libraries }
if libs.isEmpty { throw AppError.noLibrariesFound }
Expand Down Expand Up @@ -211,27 +210,22 @@ extension Arena {
// add playground
do {
try playgroundPath.mkdir()
let libsToImport = !libNames.isEmpty ? libNames : packageInfo.flatMap { $0.1.libraries }
let importClauses =
"""
// Playground generated with 🏟 Arena (https://github.com/finestructure/arena)
// ℹ️ If running the playground fails with an error "no such module ..."
// go to Product -> Build to re-trigger building the SPM package.
// ℹ️ Please restart Xcode if autocomplete is not working.
""" + "\n\n" +
libsToImport.map { "import \($0)" }.joined(separator: "\n") + "\n"
try importClauses.write(to: playgroundPath/"Contents.swift")
try """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='\(platform)' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>
""".write(to: playgroundPath/"contents.xcplayground")
if dependencies.count == 1,
let sourceDir = dependencies.first?.sourceDir(packageDir: dependencyPackagePath),
let sampleCode = PackageGenerator.sampleCode(path: sourceDir) {
try sampleCode.write(to: playgroundPath/"Contents.swift")
} else {
let libraries = !libNames.isEmpty ? libNames : packageInfo.flatMap { $0.1.libraries }
try PackageGenerator.importLibrariesClause(libraries: libraries)
.write(to: playgroundPath/"Contents.swift")
}
try PackageGenerator.contentsXCPlayground(platform: platform)
.write(to: playgroundPath/"contents.xcplayground")
}

if book {
let modules = dependencies
.compactMap { $0.path ?? $0.checkoutDir(packageDir: dependencyPackagePath) }
.compactMap { $0.sourceDir(packageDir: dependencyPackagePath) }
.compactMap(Module.init)
if modules.isEmpty { throw AppError.noSourcesFound }
try PlaygroundBook.make(named: Self.playgroundName, in: outputPath, with: modules)
Expand Down
7 changes: 7 additions & 0 deletions Sources/ArenaCore/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public struct Dependency: Equatable, Hashable, Codable {
requirement == .path ? nil : packageDir/".build/checkouts"/url.lastPathComponent(dropExtension: "git")
}

/// Returns path to the package source code. Local directory if the dependency is a `path` requirement, otherwise the checkout directory of the given package directory.
/// - Parameter packageDir: fallback package directory
/// - Returns: source directory
func sourceDir(packageDir: Path) -> Path? {
path ?? checkoutDir(packageDir: packageDir)
}

func packageClause(name: String? = nil) -> String {
#if swift(>=5.2)
let n = name.map { #"name: "\#($0)", "# } ?? ""
Expand Down
30 changes: 30 additions & 0 deletions Sources/ArenaCore/PackageGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Path


enum PackageGenerator {
static func productsClause(_ info: [(Dependency, PackageInfo)]) -> String {
info
Expand Down Expand Up @@ -72,6 +75,33 @@ extension PackageGenerator {
]
"""
}

static func importLibrariesClause(libraries: [String]) -> String {
"""
// Playground generated with 🏟 Arena (https://github.com/finestructure/arena)
// ℹ️ If running the playground fails with an error "no such module ..."
// go to Product -> Build to re-trigger building the SPM package.
// ℹ️ Please restart Xcode if autocomplete is not working.
""" + "\n\n" +
libraries.map { "import \($0)" }.joined(separator: "\n") + "\n"
}

static func contentsXCPlayground(platform: Platform) -> String {
"""
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='\(platform)' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>
"""
}

static func sampleCode(path: Path) -> String? {
let samplePath = path/".arena-sample.swift"
if samplePath.exists {
return try? String(contentsOf: samplePath)
}
return nil
}
}


Expand Down
12 changes: 12 additions & 0 deletions Tests/ArenaTests/PackageGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ class PackageGeneratorTests: XCTestCase {
record: false)
}

func test_importLibrariesClause() throws {
assertSnapshot(matching: PackageGenerator.importLibrariesClause(libraries: ["A", "B"]),
as: .lines,
record: false)
}

func test_contentsXCPlayground() throws {
assertSnapshot(matching: PackageGenerator.contentsXCPlayground(platform: .macos),
as: .lines,
record: false)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Playground generated with 🏟 Arena (https://github.com/finestructure/arena)
// ℹ️ If running the playground fails with an error "no such module ..."
// go to Product -> Build to re-trigger building the SPM package.
// ℹ️ Please restart Xcode if autocomplete is not working.

import A
import B

0 comments on commit 609cb21

Please sign in to comment.