Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
.DS_Store

## User settings
xcuserdata/
Expand Down
2 changes: 0 additions & 2 deletions Generator/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ struct Generator {
}

extension Generator {

func generateSamples() -> [String] {
let objects = makeObjects()
return objects.map { $0.sample }
}
}

private extension Generator {

func makeObjects() -> [Object] {
var objects = [Object]()
var currentObject: Object?
Expand Down
23 changes: 13 additions & 10 deletions Generator/Line.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,28 @@ private extension Line {

let name = words[index + 1]
let type = words[index + 2]
let castedType = Object.Property.PropertyType(string: type)

return .init(name: name, type: type)
let property = Object.Property(name: name, type: type, castedType: castedType)
return property
}

func containsEndOfObject(_ line: String) -> Bool {
line.contains(endOfObject)
}

func findWords(_ line: String) -> [String] {
let declarationAndType = line
.components(separatedBy: ":")
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }
let delimiter: Character = ":"

guard
let declaration = declarationAndType.first,
let type = declarationAndType.last
else { return [] }
guard let index = line.firstIndex(of: delimiter) else {
return line.components(separatedBy: .whitespaces)
}

return declaration.components(separatedBy: .whitespaces) + [type]
let declaration = line.prefix(upTo: index)
let type = line.suffix(from: line.index(after: index))
let result = (declaration.components(separatedBy: .whitespaces) + [String(type)])
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }
return result
}
}
5 changes: 1 addition & 4 deletions Generator/Object/Object+Representation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation

let singleIndent = " "

let representationTemplate =
"""
struct %@ {
Expand All @@ -18,15 +17,14 @@ struct %@ {
"""

extension Object {

var representation: String {
representation(indent: "")
}
}

// MARK: Private
private extension Object {

private extension Object {
func representation(indent: String) -> String {
.init(
format: representationTemplate,
Expand Down Expand Up @@ -55,7 +53,6 @@ private extension Object {
}

extension Object.Property {

func representation(indent: String) -> String {
indent + "let " + name + ": " + type
}
Expand Down
6 changes: 2 additions & 4 deletions Generator/Object/Object+Sample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ extension %@ {
%@
"""

let defaultValue = " = <#default value" + "#" + ">"

let escaping = "@escaping "

extension Object {
Expand Down Expand Up @@ -76,9 +74,9 @@ private extension Object {
}

extension Object.Property {

var argument: String {
String(repeating: singleIndent, count: 2) + name + ": " + argumentType + defaultValue
let typeSuffix: String = argumentType + " = " + castedType.defaultValue(forName: name)
return String(repeating: singleIndent, count: 2) + name + ": " + typeSuffix
}

var body: String {
Expand Down
71 changes: 71 additions & 0 deletions Generator/Object/Object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ final class Object {
struct Property {
let name: String
let type: String
let castedType: PropertyType

enum PropertyType: Hashable {
case uuid
case string
case date
case timeInterval
case bool
case int
case decimal
case double
case float
case url
case array
case dictionary
case set
case unknownOptional
case unknownNonOptional
}
}

let name: String
Expand Down Expand Up @@ -40,3 +59,55 @@ extension Object.Property {
) != nil
}
}

extension Object.Property.PropertyType {
init(string: String) {
let matched = Self.patterns.first {
string.contains($0.value)
}
if let matched {
self = matched.key
} else if string.contains(Self.optionalPattern) {
self = .unknownOptional
} else {
self = .unknownNonOptional
}
}

private static let patterns: [Self: Regex] = [
.array: /\[\w+\??\]\??$/,
.dictionary: /\[\w+ ?: ?\w+\??\]\??$/,
.set: /Set\<\w+\??\>\??$/,
.uuid: /UUID\??$/,
.string: /String\??$/,
.date: /Date\??$/,
.timeInterval: /TimeInterval\??$/,
.bool: /Bool\??$/,
.int: /[Int|Int8|Int16|Int32|Int64|UInt]\??$/,
.decimal: /Decimal\??$/,
.double: /Double\??$/,
.float: /Float\??$/,
.url: /URL\??$/,
]
private static let optionalPattern: Regex = /.*\?$/

func defaultValue(forName name: String) -> String {
switch self {
case .uuid: return "UUID()"
case .string: return "\"\(name)\""
case .date: return "Date(timeIntervalSince1970: 1000)"
case .timeInterval: return Int.random(in: 100 ... 200).description
case .bool: return "false"
case .int: return Int.random(in: 0 ... 100).description
case .decimal: return "Decimal(\(Int.random(in: 0 ... 100))"
case .double: return Double.random(in: 0.0 ... 10.0).description
case .float: return Float.random(in: 0.0 ... 10.0).description
case .url: return "URL(string: \"https://foo.bar/\")"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns optional URL, I'd add ! to avoid errors after generating samples

case .array: return "[]"
case .dictionary: return "[:]"
case .set: return "Set()"
case .unknownOptional: return "nil"
case .unknownNonOptional: return "<#default value#" + ">"
}
}
}
22 changes: 12 additions & 10 deletions SampleGenerator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MACOSX_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -488,7 +488,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MACOSX_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand All @@ -503,17 +503,18 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = SampleGenerator/SampleGenerator.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Z3544C935Q;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SampleGenerator/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MACOSX_DEPLOYMENT_TARGET = 13.0;
PRODUCT_BUNDLE_IDENTIFIER = com.sftnhrd.SampleGenerator;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -526,17 +527,18 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = SampleGenerator/SampleGenerator.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Z3544C935Q;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SampleGenerator/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MACOSX_DEPLOYMENT_TARGET = 13.0;
PRODUCT_BUNDLE_IDENTIFIER = com.sftnhrd.SampleGenerator;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -549,7 +551,7 @@
CODE_SIGN_ENTITLEMENTS = Generator/Generator.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Z3544C935Q;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Generator/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -570,7 +572,7 @@
CODE_SIGN_ENTITLEMENTS = Generator/Generator.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Z3544C935Q;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Generator/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -591,7 +593,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Z3544C935Q;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = SampleGeneratorTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -611,7 +613,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = Z3544C935Q;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = SampleGeneratorTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
7 changes: 0 additions & 7 deletions SampleGeneratorTests/LineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,4 @@ class SampleGeneratorTests: XCTestCase {

XCTAssert(true)
}

func testClosurePropertyArgument() {
let property = Object.Property(name: "closure", type: "() -> Void")

let argument = property.argument
XCTAssertEqual(argument, " closure: @escaping () -> Void = <#default value#>")
}
}