Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Playground Book #103

Merged
merged 2 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 102 additions & 10 deletions bin/nef
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ MARKDOWN="markdown"
JEKYLL="jekyll"
CARBON="carbon"
PLAYGROUND="playground"
PLAYGROUND_BOOK="ipad"
COMPILE="compile"
CLEAN="clean"

Expand Down Expand Up @@ -33,10 +34,11 @@ printHelp() {
echo "${bold}Commands${reset}"
echo " ${green}${bold}$COMPILE${normal}${reset} Compile Xcode Playgrounds given a <path>"
echo " ${green}${bold}$CLEAN${normal}${reset} Clean a generated nef project from a <path>"
echo " ${green}${bold}$PLAYGROUND${normal}${reset} Build a playground compatible with external frameworks"
echo " ${green}${bold}$MARKDOWN${normal}${reset} Render Markdown files for given Xcode Playgrounds"
echo " ${green}${bold}$JEKYLL${normal}${reset} Render Markdown files that can be consumed from Jekyll to generate a microsite"
echo " ${green}${bold}$CARBON${normal}${reset} Export Carbon code snippets for given Xcode Playgrounds"
echo " ${green}${bold}$PLAYGROUND${normal}${reset} Build a ${bold}playground${reset} compatible with external frameworks"
echo " ${green}${bold}$PLAYGROUND_BOOK${normal}${reset} Build a playground compatible with ${bold}iPad${reset} and 3rd-party libraries"
echo " ${green}${bold}$MARKDOWN${normal}${reset} Render ${bold}Markdown${reset} files for given Xcode Playgrounds"
echo " ${green}${bold}$JEKYLL${normal}${reset} Render Markdown files that can be consumed from ${bold}Jekyll${reset} to generate a microsite"
echo " ${green}${bold}$CARBON${normal}${reset} Export ${bold}Carbon${reset} code snippets for given Xcode Playgrounds"
echo ""
}

Expand Down Expand Up @@ -112,6 +114,21 @@ printHelpPlayground() {
echo ""
}

##
# printHelpPlaygroundBook()
##
printHelpPlaygroundBook() {
echo ""
echo "${normal}nef ${green}${bold}$PLAYGROUND_BOOK${normal} ${required}${bold}--name${normal}${reset} <playground> ${required}${bold}--package${normal}${reset} <package.swift> ${required}${bold}--output${normal}${reset} <path-to-output>"
echo ""
echo " Build a Playground Book with 3r-party libraries defined in a Swift Package"
echo ""
echo " ${required}${bold}--name${reset}${normal} specify the name for the Playground Book that you are creating${reset}"
echo " ${required}${bold}--package${reset}${normal} path to the Swift Package with the dependencies${reset}"
echo " ${required}${bold}--output${reset}${normal} folder path where Playground Book will be created${reset}"
echo ""
}

##
# printHelpCompileClean()
##
Expand Down Expand Up @@ -168,6 +185,8 @@ markdown() {
--podfile ) printHelpPlayground; exit 1 ;;
--cartfile ) printHelpPlayground; exit 1 ;;

--package ) printHelpPlaygroundBook; exit 1;;

--use-cache) printHelpCompile; exit 1 ;;

--background) printHelpCarbon; exit 1 ;;
Expand All @@ -181,6 +200,7 @@ markdown() {
$JEKYLL ) printHelpJekyll; exit 1 ;;
$CARBON ) printHelpCarbon; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$PLAYGROUND_BOOK) printHelpPlaygroundBook; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
$CLEAN ) printHelpClean; exit 1 ;;
* ) printHelpMarkdown; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
Expand Down Expand Up @@ -221,6 +241,8 @@ jekyll() {
--podfile ) printHelpPlayground; exit 1 ;;
--cartfile ) printHelpPlayground; exit 1 ;;

--package ) printHelpPlaygroundBook; exit 1;;

--use-cache) printHelpCompile; exit 1 ;;

--background) printHelpCarbon; exit 1 ;;
Expand All @@ -234,6 +256,7 @@ jekyll() {
$JEKYLL ) ;;
$CARBON ) printHelpCarbon; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$PLAYGROUND_BOOK) printHelpPlaygroundBook; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
$CLEAN ) printHelpClean; exit 1 ;;
* ) printHelpJekyll; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
Expand Down Expand Up @@ -280,6 +303,8 @@ carbon() {
--podfile ) printHelpPlayground; exit 1 ;;
--cartfile ) printHelpPlayground; exit 1 ;;

--package ) printHelpPlaygroundBook; exit 1;;

--use-cache) printHelpCompile; exit 1 ;;

--background) shift; background=$1 ;;
Expand All @@ -293,6 +318,7 @@ carbon() {
$JEKYLL ) printHelpJekyll; exit 1;;
$CARBON ) ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$PLAYGROUND_BOOK) printHelpPlaygroundBook; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
$CLEAN ) printHelpClean; exit 1 ;;
* ) printHelpCarbon; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
Expand Down Expand Up @@ -336,6 +362,8 @@ playground() {
--podfile ) shift; podfile=$1 ;;
--cartfile ) shift; cartfile=$1 ;;

--package ) printHelpPlaygroundBook; exit 1;;

--use-cache) printHelpCompile; exit 1 ;;

--background) printHelpCarbon; exit 1 ;;
Expand All @@ -349,6 +377,7 @@ playground() {
$JEKYLL ) printHelpJekyll; exit 1 ;;
$CARBON ) printHelpCarbon; exit 1 ;;
$PLAYGROUND ) ;;
$PLAYGROUND_BOOK) printHelpPlaygroundBook; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
$CLEAN ) printHelpClean; exit 1 ;;
* ) printHelpPlayground $@; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
Expand All @@ -365,6 +394,62 @@ playground() {
nefc install "$projectName.app"
}

##
# playgroundBook(List<String> args)
# - Parameter `args`: list of command line arguments
##
playgroundBook() {
projectName=""
packagePath=""
outputPath=""

while [ "$1" != "" ]; do
case $2 in
-h | --help | help ) printHelpJekyll; exit 0 ;;
esac

case $1 in
--project ) printHelpJekyll; exit 1 ;;
--output ) shift; outputPath=$1 ;;
--main-page ) printHelpJekyll; exit 1 ;;

--name ) shift; projectName=$1 ;;
--platform ) printHelpPlayground; exit 1 ;;
--bow-version ) printHelpPlayground; exit 1 ;;
--bow-branch ) printHelpPlayground; exit 1 ;;
--podfile ) printHelpPlayground; exit 1 ;;
--cartfile ) printHelpPlayground; exit 1 ;;

--package ) shift; packagePath=$1 ;;

--use-cache) printHelpCompile; exit 1 ;;

--background) printHelpCarbon; exit 1 ;;
--theme) printHelpCarbon; exit 1 ;;
--size) printHelpCarbon; exit 1 ;;
--font) printHelpCarbon; exit 1 ;;
--lines) printHelpCarbon; exit 1 ;;
--watermark) printHelpCarbon; exit 1 ;;

$MARKDOWN ) printHelpMarkdown; exit 1 ;;
$JEKYLL ) printHelpJekyll; exit 1 ;;
$CARBON ) printHelpCarbon; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$PLAYGROUND_BOOK) ;;
$COMPILE ) printHelpCompile; exit 1 ;;
$CLEAN ) printHelpClean; exit 1 ;;
* ) printHelpPlaygroundBook; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
esac
shift
done

if [ "$projectName" == "" ] || [ "$packagePath" == "" ] || [ "$outputPath" == "" ]; then
printHelpPlaygroundBook; exit 1
else
nef-playground-book --name "$projectName" --package "$packagePath" --output "$outputPath"
fi
}

#: - Compile

##
Expand Down Expand Up @@ -392,6 +477,8 @@ compile() {
--podfile ) printHelpPlayground; exit 1 ;;
--cartfile ) printHelpPlayground; exit 1 ;;

--package ) printHelpPlaygroundBook; exit 1;;

--use-cache) flag="--use-cache" ;;

--background) printHelpCarbon; exit 1 ;;
Expand All @@ -405,6 +492,7 @@ compile() {
$JEKYLL ) printHelpJekyll; exit 1 ;;
$CARBON ) printHelpCarbon; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$PLAYGROUND_BOOK) printHelpPlaygroundBook; exit 1 ;;
$COMPILE ) shift; projectFolder=$1 ;;
$CLEAN ) printHelpClean; exit 1 ;;
* ) printHelpCompile; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
Expand Down Expand Up @@ -445,6 +533,8 @@ clean() {
--podfile ) printHelpPlayground; exit 1 ;;
--cartfile ) printHelpPlayground; exit 1 ;;

--package ) printHelpPlaygroundBook; exit 1;;

--use-cache) ;;

--background) printHelpCarbon; exit 1 ;;
Expand All @@ -458,6 +548,7 @@ clean() {
$JEKYLL ) printHelpJekyll; exit 1 ;;
$CARBON ) printHelpCarbon; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$PLAYGROUND_BOOK) printHelpPlaygroundBook; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
$CLEAN ) shift; projectFolder=$1 ;;
* ) printHelpClean; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1 ;;
Expand All @@ -477,12 +568,13 @@ set -e

while [ "$1" != "" ]; do
case $1 in
$MARKDOWN ) markdown $@; exit 0 ;;
$JEKYLL ) jekyll $@; exit 0 ;;
$CARBON ) carbon $@; exit 0 ;;
$PLAYGROUND ) playground $@; exit 0 ;;
$COMPILE ) compile $@; exit 0 ;;
$CLEAN ) clean $@; exit 0 ;;
$MARKDOWN ) markdown $@; exit 0 ;;
$JEKYLL ) jekyll $@; exit 0 ;;
$CARBON ) carbon $@; exit 0 ;;
$PLAYGROUND ) playground $@; exit 0 ;;
$PLAYGROUND_BOOK ) playgroundBook $@; exit 0;;
$COMPILE ) compile $@; exit 0 ;;
$CLEAN ) clean $@; exit 0 ;;
-h | --help | help ) printHelp; exit 0 ;;
-v | --version | version ) printVersion; exit 0 ;;
* ) printHelp; echo "${bold}[!] ${normal}${red}error:${reset} invalid argument: ${red}$1${reset}"; exit 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ public enum PlaygroundShellError: Error {
case describe(repository: String)
case linkPath(item: String)
}

extension PlaygroundShellError: CustomStringConvertible {
public var description: String {
switch self {
case .empty(let directory):
return "directory '\(directory)' is empty"
case .dependencies(let package):
return "could not resolve dependencies in package '\(package)'"
case .describe(let repository):
return "could not get information from repository '\(repository)'"
case .linkPath(let item):
return "could not follow symbolic links to item '\(item)'"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum SwiftPlaygroundError: Error {
case clean(item: String)
case structure
case package(packagePath: String)
case checkout
case checkout(info: String)
case modules(_ paths: [String])
case playgroundBook(info: String)
case ioError(info: String = "")
Expand All @@ -19,15 +19,15 @@ public enum SwiftPlaygroundError: Error {
return "could not create project structure"
case .package(let path):
return "could not build project 'Package.swift' :: \(path)"
case .checkout:
return "command 'swift package describe' failed"
case .checkout(let info):
return "command checkout failed: \(info)"
case .modules(let paths):
let packages = paths.map { $0.filename }.joined(separator: ", ")
return "could not extract any module from packages: \(packages)"
case .playgroundBook(let info):
return "could not create Playground Book (\(info))"
return "could not create Playground Book: \(info)"
case .ioError(let info):
return "failure running IO \(info.isEmpty ? "" : "(\(info))")"
return "failure running IO \(info.isEmpty ? "" : ": \(info)")"
}
}
}
6 changes: 3 additions & 3 deletions project/Component/NefSwiftPlayground/SwiftPlayground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public struct SwiftPlayground {

private func buildPackage(content: String, packageFilePath: String, packagePath: String, buildPath: String) -> EnvIO<(FileSystem, PlaygroundShell), SwiftPlaygroundError, Void> {
EnvIO { (system, shell) in
let writePackageIO = system.write(content: content, toFile: packageFilePath).mapLeft { _ in SwiftPlaygroundError.checkout }
let resolvePackageIO = shell.resolve(packagePath: packagePath, buildPath: buildPath).mapLeft { _ in SwiftPlaygroundError.checkout }
let writePackageIO = system.write(content: content, toFile: packageFilePath).mapLeft { e in SwiftPlaygroundError.checkout(info: e.description) }
let resolvePackageIO = shell.resolve(packagePath: packagePath, buildPath: buildPath).mapLeft { e in SwiftPlaygroundError.checkout(info: e.description) }

return writePackageIO.followedBy(resolvePackageIO)
}
Expand All @@ -115,7 +115,7 @@ public struct SwiftPlayground {
private func repositories(checkoutPath: String) -> EnvIO<FileSystem, SwiftPlaygroundError, [String]> {
EnvIO { fileSystem in
fileSystem.items(atPath: checkoutPath)
.mapLeft { _ in .checkout }
.mapLeft { e in .checkout(info: e.description) }
.map { repos in repos.filter { repo in !repo.filename.contains("swift-") } }
}
}
Expand Down
2 changes: 1 addition & 1 deletion project/Core/Render/CoreCarbon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct CarbonGenerator: InternalRender {
return !trace.contains("☓")
}

func render(node: Node) -> String {
internal func render(node: Node) -> String {
return node.carbon(downloader: self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion project/Core/Render/CoreJekyll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public struct JekyllGenerator: InternalRender {
self.permalink = permalink
}

func render(node: Node) -> String {
internal func render(node: Node) -> String {
return node.jekyll(permalink: permalink)
}
}
Expand Down
2 changes: 1 addition & 1 deletion project/Core/Render/CoreMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
public struct MarkdownGenerator: InternalRender {
public init() { }

func render(node: Node) -> String {
internal func render(node: Node) -> String {
return node.markdown()
}
}
Expand Down
5 changes: 5 additions & 0 deletions project/Core/Render/CoreRender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ protocol CoreMarkdown {
func markdown() -> String
}

protocol CoreCarbon {
func carbon(downloader: CarbonCodeDownloader) -> String
}

// Dependencies
extension Node: CoreJekyll {}
extension Node: CoreMarkdown {}
extension Node: CoreCarbon {}

// MARK: - default Render :: render(content:)
protocol InternalRender: CoreRender {
Expand Down
4 changes: 2 additions & 2 deletions project/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "nef-markdown-page", targets: ["MarkdownPage"]),
.executable(name: "nef-jekyll-page", targets: ["JekyllPage"]),
.executable(name: "nef-carbon-page", targets: ["CarbonPage"]),
.executable(name: "nef-swift-playground", targets: ["SwiftPlayground"]),
.executable(name: "nef-playground-book", targets: ["PlaygroundBook"]),
],
dependencies: [
.package(url: "https://github.com/bow-swift/bow", .branch("master")),
Expand Down Expand Up @@ -39,6 +39,6 @@ let package = Package(
.target(name: "MarkdownPage", dependencies: ["CLIKit", "NefMarkdown"], path: "UI/MarkdownPage"),
.target(name: "JekyllPage", dependencies: ["CLIKit", "NefJekyll"], path: "UI/JekyllPage"),
.target(name: "CarbonPage", dependencies: ["CLIKit", "NefCommon", "NefModels", "NefCore", "NefCarbon"], path: "UI/CarbonPage"),
.target(name: "SwiftPlayground", dependencies: ["BowEffects", "CLIKit", "nef"], path: "UI/SwiftPlayground"),
.target(name: "PlaygroundBook", dependencies: ["BowEffects", "CLIKit", "nef"], path: "UI/PlaygroundBook"),
]
)
Loading