This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from g-Off/filesystem
adding the concept of a filesystem to the template
- Loading branch information
Showing
6 changed files
with
95 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// FileSystem.swift | ||
// | ||
// | ||
// Created by Geoffrey Foster on 2019-09-28. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol FileSystem { | ||
func read(path: String) throws -> String | ||
} | ||
|
||
struct LocalFileSystem: FileSystem { | ||
let baseURL: URL | ||
|
||
init(baseURL: URL) { | ||
self.baseURL = baseURL | ||
} | ||
|
||
func read(path: String) throws -> String { | ||
let fileURL = try templateURL(for: path) | ||
return try String(contentsOf: fileURL) | ||
} | ||
|
||
private func templateURL(for path: String) throws -> URL { | ||
var components = path.components(separatedBy: "/") | ||
guard !components.isEmpty else { | ||
throw FileSystemError(reason: "") | ||
} | ||
let filename = "_\(components.popLast()!).liquid" | ||
components.append(filename) | ||
let transformedPath = components.joined(separator: "/") | ||
return baseURL.appendingPathComponent(transformedPath) | ||
} | ||
} | ||
|
||
struct BlankFileSystem: FileSystem { | ||
func read(path: String) throws -> String { | ||
throw FileSystemError(reason: "This liquid context does not allow includes.") | ||
} | ||
} | ||
|
||
struct FileSystemError: Error { | ||
let reason: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters