Skip to content

Commit

Permalink
feat: add after-auto generation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzablocki committed Sep 26, 2021
1 parent 6ef751a commit 0f3f206
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update dependencies to fix build on Xcode 13 and support Swift 5.5 [#989](https://github.com/krzysztofzablocki/Sourcery/issues/989)
- Improves performance
- Skips hidden files / directories and doesn't step into packages
- added `after-auto:` generation mode to inline codegen

## 1.5.2
## Fixes
Expand Down
27 changes: 23 additions & 4 deletions Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,22 @@ extension Sourcery {
return MappedInlineAnnotations(range, filePath, inlineRanges[key]!, generatedBody, inlineIndentations[key] ?? "")
}

guard key.hasPrefix("auto:") else {

guard let autoRange = key.range(of: "auto:") else {
rangesToReplace.remove(range)
return nil
}
let autoTypeName = key.trimmingPrefix("auto:").components(separatedBy: ".").dropLast().joined(separator: ".")
let toInsert = "\n// sourcery:inline:\(key)\n\(generatedBody)// sourcery:end\n"

enum AutoType: String {
case after = "after-"
case normal = ""
}

let autoKey = key[..<autoRange.lowerBound]
let autoType = AutoType(rawValue: String(autoKey)) ?? .normal

let autoTypeName = key[autoRange.upperBound..<key.endIndex].components(separatedBy: ".").dropLast().joined(separator: ".")
var toInsert = "\n// sourcery:inline:\(key)\n\(generatedBody)// sourcery:end"

guard let definition = parsingResult.types.types.first(where: { $0.name == autoTypeName }),
let filePath = definition.path,
Expand All @@ -623,7 +633,16 @@ extension Sourcery {
}
let bodyEndRange = NSRange(location: NSMaxRange(bodyRange), length: 0)
let bodyEndLineRange = contents.bridge().lineRange(for: bodyEndRange)
let rangeInFile = NSRange(location: max(bodyRange.location, bodyEndLineRange.location), length: 0)
let rangeInFile: NSRange

switch autoType {
case .after:
rangeInFile = NSRange(location: max(bodyRange.location, bodyEndLineRange.location) + 1, length: 0)
case .normal:
rangeInFile = NSRange(location: max(bodyRange.location, bodyEndLineRange.location), length: 0)
toInsert += "\n"
}

return MappedInlineAnnotations(range, filePath, rangeInFile, toInsert, "")
}
.sorted { lhs, rhs in
Expand Down
23 changes: 23 additions & 0 deletions SourceryTests/SourcerySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,29 @@ class SourcerySpecTests: QuickSpec {
expect(result).to(equal(expectedResult))
}

it("insert generated code after the end of type body when using after-auto") {
update(code: "class Foo {}\nstruct Boo {}", in: sourcePath)

update(code: """
// sourcery:inline:after-auto:Foo.Inlined
var property = 2
// sourcery:end
""", in: templatePath)

expect { try Sourcery(watcherEnabled: false, cacheDisabled: true).processFiles(.sources(Paths(include: [sourcePath])), usingTemplates: Paths(include: [templatePath]), output: output) }.toNot(throwError())

let expectedResult = """
class Foo {}
// sourcery:inline:after-auto:Foo.Inlined
var property = 2
// sourcery:end
struct Boo {}
"""

let result = try? sourcePath.read(.utf8)
expect(result).to(equal(expectedResult))
}

it("insert generated code line before the end of type body") {
update(code: """
class Foo {
Expand Down
5 changes: 4 additions & 1 deletion guides/Writing templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ Sourcery will generate the template code and then perform replacement in your so

#### Automatic inline code generation

To avoid having to place the markup in your source files, you can use `inline:auto:{{ type.name }}.TemplateName`:
To avoid having to place the markup in your source files, you can use automatic generation e.g.
`inline:auto:{{ type.name }}.TemplateName`:

```swift
// in template:
Expand All @@ -216,6 +217,8 @@ class MyType {

The needed markup will be automatically added at the end of the type declaration body. After first parse Sourcery will work with generated code annotated with `inline:auto` the same way as annotated with `inline`, so you can even move these blocks of code anywhere in the same file.

If you want to insert code after the type declaration, use `after-auto:` instead of `auto:`

## Per file code generation

Sourcery supports generating code in a separate file per type, you just need to put `file` annotation in a template, e.g.
Expand Down

0 comments on commit 0f3f206

Please sign in to comment.