-
Notifications
You must be signed in to change notification settings - Fork 225
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
Windows Support #343
base: master
Are you sure you want to change the base?
Windows Support #343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
// | ||
|
||
import Foundation | ||
import PathKit | ||
|
||
/// Type used for loading a template | ||
public protocol Loader { | ||
|
@@ -34,15 +33,17 @@ extension Loader { | |
|
||
// A class for loading a template from disk | ||
public class FileSystemLoader: Loader, CustomStringConvertible { | ||
public let paths: [Path] | ||
public let paths: [String] | ||
|
||
public init(paths: [Path]) { | ||
self.paths = paths | ||
public init(paths: [URL]) { | ||
self.paths = paths.map { | ||
$0.withUnsafeFileSystemRepresentation { String(cString: $0!) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we go through c-strings? Can't we just use the Note: not relevant if we just use (file) URLs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we cannot. The |
||
} | ||
} | ||
|
||
public init(bundle: [Bundle]) { | ||
self.paths = bundle.map { bundle in | ||
Path(bundle.bundlePath) | ||
self.paths = bundle.map { | ||
URL(fileURLWithPath: $0.bundlePath).withUnsafeFileSystemRepresentation { String(cString: $0!) } | ||
compnerd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
@@ -51,27 +52,19 @@ public class FileSystemLoader: Loader, CustomStringConvertible { | |
} | ||
|
||
public func loadTemplate(name: String, environment: Environment) throws -> Template { | ||
for path in paths { | ||
let templatePath = try path.safeJoin(path: Path(name)) | ||
|
||
if !templatePath.exists { | ||
continue | ||
} | ||
|
||
let content: String = try templatePath.read() | ||
return environment.templateClass.init(templateString: content, environment: environment, name: name) | ||
} | ||
|
||
throw TemplateDoesNotExist(templateNames: [name], loader: self) | ||
return try loadTemplate(names: [name], environment: environment) | ||
} | ||
|
||
public func loadTemplate(names: [String], environment: Environment) throws -> Template { | ||
for path in paths { | ||
for templateName in names { | ||
let templatePath = try path.safeJoin(path: Path(templateName)) | ||
let templatePath = URL(fileURLWithPath: templateName, relativeTo: URL(fileURLWithPath: path)) | ||
if !templatePath.withUnsafeFileSystemRepresentation({ String(cString: $0!) }).hasPrefix(path) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this equivilent to the old implementation? There doesn't seem to be an explict asolute path, perhaps this is happening for free by this construct? There also doesn't appear to be any test cases for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it should be equivalent to the previous implementation. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have this in a tiny extension on |
||
throw SuspiciousFileOperation(basePath: path, path: templateName) | ||
} | ||
|
||
if templatePath.exists { | ||
let content: String = try templatePath.read() | ||
if FileManager.default.fileExists(atPath: templatePath.path) { | ||
let content = try String(contentsOf: templatePath) | ||
return environment.templateClass.init(templateString: content, environment: environment, name: templateName) | ||
} | ||
} | ||
|
@@ -107,23 +100,11 @@ public class DictionaryLoader: Loader { | |
} | ||
} | ||
|
||
extension Path { | ||
func safeJoin(path: Path) throws -> Path { | ||
let newPath = self + path | ||
|
||
if !newPath.absolute().description.hasPrefix(absolute().description) { | ||
throw SuspiciousFileOperation(basePath: self, path: newPath) | ||
} | ||
|
||
return newPath | ||
} | ||
} | ||
|
||
class SuspiciousFileOperation: Error { | ||
let basePath: Path | ||
let path: Path | ||
let basePath: String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, I'd prefer a more concrete type. |
||
let path: String | ||
|
||
init(basePath: Path, path: Path) { | ||
init(basePath: String, path: String) { | ||
self.basePath = basePath | ||
self.path = path | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
// | ||
|
||
import Foundation | ||
import PathKit | ||
|
||
#if os(Linux) | ||
// swiftlint:disable:next prefixed_toplevel_constant | ||
|
@@ -41,19 +40,7 @@ open class Template: ExpressibleByStringLiteral { | |
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil) | ||
} | ||
|
||
try self.init(URL: url) | ||
} | ||
|
||
/// Create a template with a file found at the given URL | ||
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we're dropping these because we no longer have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that seems fair. |
||
public convenience init(URL: Foundation.URL) throws { | ||
try self.init(path: Path(URL.path)) | ||
} | ||
|
||
/// Create a template with a file found at the given path | ||
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead") | ||
public convenience init(path: Path, environment: Environment? = nil, name: String? = nil) throws { | ||
self.init(templateString: try path.read(), environment: environment, name: name) | ||
try self.init(templateString: String(contentsOf: url)) | ||
} | ||
|
||
// MARK: ExpressibleByStringLiteral | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,12 +111,12 @@ public struct Variable: Equatable, Resolvable { | |
} else if let string = context as? String { | ||
return resolve(bit: bit, collection: string) | ||
} else if let object = context as? NSObject { // NSKeyValueCoding | ||
#if os(Linux) | ||
return nil | ||
#else | ||
#if _runtime(_ObjC) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in #341, I don't there's need for this, and we can just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that commit needs to be dropped, I couldn't easily do so though. |
||
if object.responds(to: Selector(bit)) { | ||
return object.value(forKey: bit) | ||
} | ||
#else | ||
return nil | ||
#endif | ||
} else if let value = context as? DynamicMemberLookup { | ||
return value[dynamicMember: bit] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Strings? I'd rather use a more specific type like URL. Unfortunately Swift conflates local & remote urls 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can change this to
[URL]
.