Skip to content

Commit

Permalink
ditch using file-templates
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 11, 2024
1 parent 4e595f8 commit 6e73c80
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 80 deletions.
13 changes: 0 additions & 13 deletions Sources/EnumeratorMacro/Enumerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ public macro Enumerator(_ templates: StaticString...) = #externalMacro(
type: "EnumeratorMacroType"
)

@attached(member, names: arbitrary)
macro CreateSubtype() = #externalMacro(
module: "EnumeratorMacroImpl",
type: "EnumeratorMacroType"
)

@Enumerator("""
{{#cases}}
var is{{capitalized(name)}}: Bool {
Expand All @@ -27,10 +21,3 @@ enum TestEnum {
case b
case testCase(testValue: String)
}

@CreateSubtype
enum TestEnum2 {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}
70 changes: 14 additions & 56 deletions Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,31 @@ extension EnumeratorMacroType: MemberMacro {
throw MacroError.isNotEnum
}

let assignedMacroName = node.attributeName.description
var templateFromFile: String?
if assignedMacroName != "Enumerator" {
let fm = FileManager.default
var workingDirectory: String!
#if Xcode
let byDotBuild = #filePath.components(separatedBy: "/.build/")
if byDotBuild.count > 1 {
workingDirectory = byDotBuild[0]
} else {
let bySources = #filePath.components(separatedBy: "/Sources/")
if bySources.count > 1 {
workingDirectory = bySources[0]
}
}
if workingDirectory == nil {
throw MacroError.customNameIsEnteredForMacroButCannotFindWorkingDirectory(
name: assignedMacroName
)
}
#else
dir = fm.currentDirectoryPath
#endif
let path = workingDirectory + "/macro-templates/\(assignedMacroName).mustache"
if fm.fileExists(atPath: path) {
let data = try Data(contentsOf: .init(filePath: path))
templateFromFile = String(decoding: data, as: UTF8.self)
} else {
throw MacroError.customNameIsEnteredForMacroButFileDoesNotExist(
name: assignedMacroName,
path: path
)
}
}

let members = enumDecl.memberBlock.members
let caseDecls = members.compactMap { $0.decl.as(EnumCaseDeclSyntax.self) }
let elements = caseDecls.flatMap(\.elements)
let cases = try elements.map(EnumCase.init(from:))

func findTemplates() throws -> [String] {
if let templateFromFile {
return [templateFromFile]
}

guard let arguments = node.arguments else {
throw MacroError.macroDeclarationHasNoArguments
}
guard let exprList = arguments.as(LabeledExprListSyntax.self) else {
throw MacroError.unacceptableArguments
}
if exprList.isEmpty {
throw MacroError.expectedAtLeastOneArgument
}
let stringLiteralArguments = try exprList.compactMap { element in
guard let stringLiteral = element.expression.as(StringLiteralExprSyntax.self) else {
throw MacroError.allArgumentsMustBeStringLiterals(violation: element.description)
}
return stringLiteral
.segments
.formatted()
.description
guard let exprList = arguments.as(LabeledExprListSyntax.self) else {
throw MacroError.unacceptableArguments
}
if exprList.isEmpty {
throw MacroError.expectedAtLeastOneArgument
}
let templates = try exprList.compactMap { element in
guard let stringLiteral = element.expression.as(StringLiteralExprSyntax.self) else {
throw MacroError.allArgumentsMustBeStringLiterals(violation: element.description)
}
return stringLiteralArguments
return stringLiteral
.segments
.formatted()
.description
}

let rendered = try findTemplates().map { template in
let rendered = try templates.map { template in
try MustacheTemplate(
string: "{{%CONTENT_TYPE:TEXT}}\n" + template
).render([
Expand Down
6 changes: 0 additions & 6 deletions Sources/EnumeratorMacroImpl/MacroError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ enum MacroError: Error, CustomStringConvertible {
case expectedAtLeastOneArgument
case allArgumentsMustBeStringLiterals(violation: String)
case renderedSyntaxContainsErrors(String)
case customNameIsEnteredForMacroButCannotFindWorkingDirectory(name: String)
case customNameIsEnteredForMacroButFileDoesNotExist(name: String, path: String)

var description: String {
switch self {
Expand All @@ -25,10 +23,6 @@ enum MacroError: Error, CustomStringConvertible {
"All arguments must be string literals, but found: \(violation)"
case let .renderedSyntaxContainsErrors(syntax):
"A rendered syntax contains errors:\n\(syntax)"
case let .customNameIsEnteredForMacroButCannotFindWorkingDirectory(name):
"A custom name '\(name)' is entered for macro which indicates to look for a predefined mustache template, but the macro can't find your working directory"
case let .customNameIsEnteredForMacroButFileDoesNotExist(name, path):
"A custom name '\(name)' is entered for macro which indicates to look for a predefined mustache template, but no file seems to exist at path '\(path)'"
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions macro-templates/CreateSubtype.mustache

This file was deleted.

0 comments on commit 6e73c80

Please sign in to comment.