Skip to content

Commit a52f275

Browse files
committed
Using contents from extension to create the playgrounds
1 parent c9bee49 commit a52f275

5 files changed

Lines changed: 18 additions & 12 deletions

File tree

PACore/Source/PlayAlways.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public struct PlayAlways {
2222
}
2323

2424
public let platform: String
25+
public let contents: String
2526

26-
public init(platform: String) {
27+
public init(platform: String, contents: String = "") {
2728
self.platform = platform.lowercased()
29+
self.contents = contents
2830
}
2931

3032
var dateString: String {
@@ -46,7 +48,11 @@ public struct PlayAlways {
4648
}
4749

4850
var importHeader: String {
49-
return "//: Playground - noun: a place where people can play\n\nimport \(systemFramework)\n\nvar str = \"Hello, playground\""
51+
return "//: Playground - noun: a place where people can play\n\nimport \(systemFramework)\n\n"
52+
}
53+
54+
var effectiveContents: String {
55+
return importHeader + (contents.isEmpty ? "var str = \"Hello, playground\"" : contents)
5056
}
5157

5258
var contentHeader: String {
@@ -98,7 +104,7 @@ public struct PlayAlways {
98104

99105
if createPlaygroundFolder(playgroundDir.path) &&
100106
writeFile("contents.xcplayground", at: playgroundDir.path, content: contentHeader) &&
101-
writeFile("Contents.swift", at: playgroundDir.path, content: importHeader) {
107+
writeFile("Contents.swift", at: playgroundDir.path, content: effectiveContents) {
102108

103109
return playgroundDir
104110
}

PlayAways/AppRouter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class AppRouter {
2020
self.persistence = persistence
2121
}
2222

23-
func createPlayground(with options: MenuOptions) {
23+
func createPlayground(with options: MenuOptions, contents: String = "") {
2424
let platform: PlaygroundPlatform
2525
var showOptions = false
2626

@@ -57,16 +57,16 @@ final class AppRouter {
5757

5858
guard let url = panel.url else { return }
5959

60-
createPlayground(for: platform, at: url, usingDefaultName: false)
60+
createPlayground(for: platform, at: url, usingDefaultName: false, contents: contents)
6161
} else {
6262
let url = storageLocationShowingPanelIfNeeded()
6363

64-
createPlayground(for: platform, at: url, usingDefaultName: true)
64+
createPlayground(for: platform, at: url, usingDefaultName: true, contents: contents)
6565
}
6666

6767
}
6868

69-
private func createPlayground(for platform: PlaygroundPlatform, at location: URL?, usingDefaultName defaultName: Bool) {
69+
private func createPlayground(for platform: PlaygroundPlatform, at location: URL?, usingDefaultName defaultName: Bool, contents: String) {
7070
var playgroundUrl: URL?
7171

7272
guard let location = location else {
@@ -85,7 +85,7 @@ final class AppRouter {
8585
}
8686

8787
do {
88-
playgroundUrl = try playgroundMaker.createPlayground(named: name, at: directory, platform: platform)
88+
playgroundUrl = try playgroundMaker.createPlayground(named: name, at: directory, platform: platform, contents: contents)
8989
} catch {
9090
handle(error)
9191
}

PlayAways/ExtensionHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class ExtensionHandler {
2929
options = .iOSWithPanel
3030
}
3131

32-
self?.router?.createPlayground(with: options)
32+
self?.router?.createPlayground(with: options, contents: buffer)
3333
}
3434
}
3535

PlayAways/PlayAlwaysMaker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import PACore
1111

1212
final class PlayAlwaysMaker: PlaygroundMaker {
1313

14-
func createPlayground(named name: String?, at destination: String, platform: PlaygroundPlatform) throws -> URL {
15-
let maker = PlayAlways(platform: platform.rawValue)
14+
func createPlayground(named name: String?, at destination: String, platform: PlaygroundPlatform, contents: String = "") throws -> URL {
15+
let maker = PlayAlways(platform: platform.rawValue, contents: contents)
1616
return try maker.createPlayground(fileName: name, atDestination: destination)
1717
}
1818

PlayAways/PlaygroundMaker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public enum PlaygroundPlatform: String {
1515
}
1616

1717
protocol PlaygroundMaker {
18-
func createPlayground(named name: String?, at destination: String, platform: PlaygroundPlatform) throws -> URL
18+
func createPlayground(named name: String?, at destination: String, platform: PlaygroundPlatform, contents: String) throws -> URL
1919
}

0 commit comments

Comments
 (0)