Convert Xcode project to swift package
This commit is contained in:
9
Sources/Generator/Extensions/Data+Extensions.swift
Normal file
9
Sources/Generator/Extensions/Data+Extensions.swift
Normal file
@@ -0,0 +1,9 @@
|
||||
import Foundation
|
||||
|
||||
extension Data {
|
||||
|
||||
func createFolderAndWrite(to url: URL) throws {
|
||||
try url.ensureParentFolderExistence()
|
||||
try write(to: url)
|
||||
}
|
||||
}
|
8
Sources/Generator/Extensions/Decodable+Extensions.swift
Normal file
8
Sources/Generator/Extensions/Decodable+Extensions.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
import Foundation
|
||||
|
||||
extension JSONDecoder {
|
||||
|
||||
func decode<T>(from data: Data) throws -> T where T: Decodable {
|
||||
try self.decode(T.self, from: data)
|
||||
}
|
||||
}
|
15
Sources/Generator/Extensions/NSImage+Extensions.swift
Normal file
15
Sources/Generator/Extensions/NSImage+Extensions.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
extension NSImage {
|
||||
|
||||
func scaledDown(to size: NSSize) -> NSImage {
|
||||
guard self.size.width > size.width else {
|
||||
return self
|
||||
}
|
||||
return NSImage(size: size, flipped: false) { (resizedRect) -> Bool in
|
||||
self.draw(in: resizedRect)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
28
Sources/Generator/Extensions/NSSize+Extensions.swift
Normal file
28
Sources/Generator/Extensions/NSSize+Extensions.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
import Foundation
|
||||
|
||||
extension NSSize {
|
||||
|
||||
func scaledDown(to desiredWidth: CGFloat) -> NSSize {
|
||||
if width == desiredWidth {
|
||||
return self
|
||||
}
|
||||
|
||||
if width < desiredWidth {
|
||||
// Don't scale larger
|
||||
return self
|
||||
}
|
||||
|
||||
let height = height * desiredWidth / width
|
||||
return NSSize(width: desiredWidth, height: height)
|
||||
}
|
||||
}
|
||||
|
||||
extension NSSize {
|
||||
|
||||
var ratio: CGFloat {
|
||||
guard height != 0 else {
|
||||
return 0
|
||||
}
|
||||
return width / height
|
||||
}
|
||||
}
|
28
Sources/Generator/Extensions/Optional+Extensions.swift
Normal file
28
Sources/Generator/Extensions/Optional+Extensions.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
import Foundation
|
||||
import Metal
|
||||
|
||||
extension Optional {
|
||||
|
||||
func unwrapped<T>(_ closure: (Wrapped) -> T) -> T? {
|
||||
if case let .some(value) = self {
|
||||
return closure(value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func ifNil(_ closure: () -> Void) -> Self {
|
||||
if self == nil {
|
||||
closure()
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func ifNotNil(_ closure: () -> Void) -> Self {
|
||||
if self != nil {
|
||||
closure()
|
||||
}
|
||||
return self
|
||||
}
|
||||
}
|
76
Sources/Generator/Extensions/String+Extensions.swift
Normal file
76
Sources/Generator/Extensions/String+Extensions.swift
Normal file
@@ -0,0 +1,76 @@
|
||||
import Foundation
|
||||
|
||||
extension String {
|
||||
|
||||
var nonEmpty: String? {
|
||||
self.isEmpty ? nil : self
|
||||
}
|
||||
|
||||
var trimmed: String {
|
||||
trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
func indented(by indentation: String) -> String {
|
||||
components(separatedBy: "\n").joined(separator: "\n" + indentation)
|
||||
}
|
||||
|
||||
var withoutEmptyLines: String {
|
||||
components(separatedBy: "\n")
|
||||
.filter { !$0.trimmed.isEmpty }
|
||||
.joined(separator: "\n")
|
||||
}
|
||||
|
||||
func dropAfterLast(_ separator: String) -> String {
|
||||
guard contains(separator) else {
|
||||
return self
|
||||
}
|
||||
return components(separatedBy: separator).dropLast().joined(separator: separator)
|
||||
}
|
||||
|
||||
func dropBeforeFirst(_ separator: String) -> String {
|
||||
guard contains(separator) else {
|
||||
return self
|
||||
}
|
||||
return components(separatedBy: separator).dropFirst().joined(separator: separator)
|
||||
}
|
||||
|
||||
func lastComponentAfter(_ separator: String) -> String {
|
||||
components(separatedBy: separator).last!
|
||||
}
|
||||
|
||||
/**
|
||||
Insert the new content before the last occurence of the specified separator.
|
||||
|
||||
If the separator does not appear in the string, then the new content is simply appended.
|
||||
*/
|
||||
func insert(_ content: String, beforeLast separator: String) -> String {
|
||||
let parts = components(separatedBy: separator)
|
||||
guard parts.count > 1 else {
|
||||
return self + content
|
||||
}
|
||||
return parts.dropLast().joined(separator: separator) + content + separator + parts.last!
|
||||
}
|
||||
|
||||
func dropAfterFirst<T>(_ separator: T) -> String where T: StringProtocol {
|
||||
components(separatedBy: separator).first!
|
||||
}
|
||||
|
||||
func between(_ start: String, and end: String) -> String {
|
||||
dropBeforeFirst(start).dropAfterFirst(end)
|
||||
}
|
||||
}
|
||||
|
||||
extension Substring {
|
||||
|
||||
func between(_ start: String, and end: String) -> String {
|
||||
components(separatedBy: start).last!
|
||||
.components(separatedBy: end).first!
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
|
||||
func createFolderAndWrite(to url: URL) throws {
|
||||
try data(using: .utf8)!.createFolderAndWrite(to: url)
|
||||
}
|
||||
}
|
53
Sources/Generator/Extensions/URL+Extensions.swift
Normal file
53
Sources/Generator/Extensions/URL+Extensions.swift
Normal file
@@ -0,0 +1,53 @@
|
||||
import Foundation
|
||||
|
||||
extension URL {
|
||||
|
||||
func ensureParentFolderExistence() throws {
|
||||
try deletingLastPathComponent().ensureFolderExistence()
|
||||
}
|
||||
|
||||
func ensureFolderExistence() throws {
|
||||
guard !exists else {
|
||||
return
|
||||
}
|
||||
try FileManager.default.createDirectory(at: self, withIntermediateDirectories: true)
|
||||
}
|
||||
|
||||
var isDirectory: Bool {
|
||||
do {
|
||||
let resources = try resourceValues(forKeys: [.isDirectoryKey])
|
||||
guard let isDirectory = resources.isDirectory else {
|
||||
print("No isDirectory info for \(path)")
|
||||
return false
|
||||
}
|
||||
return isDirectory
|
||||
} catch {
|
||||
print("Failed to get directory information from \(path): \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var exists: Bool {
|
||||
FileManager.default.fileExists(atPath: path)
|
||||
}
|
||||
|
||||
/**
|
||||
Delete the file at the url.
|
||||
*/
|
||||
func delete() throws {
|
||||
try FileManager.default.removeItem(at: self)
|
||||
}
|
||||
|
||||
func copy(to url: URL) throws {
|
||||
if url.exists {
|
||||
try url.delete()
|
||||
}
|
||||
try url.ensureParentFolderExistence()
|
||||
try FileManager.default.copyItem(at: self, to: url)
|
||||
}
|
||||
|
||||
var size: Int? {
|
||||
let attributes = try? FileManager.default.attributesOfItem(atPath: path)
|
||||
return (attributes?[.size] as? NSNumber)?.intValue
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user