Skip to content

Commit 084b188

Browse files
authored
Merge pull request #132 from happynik/fix-grep-error-for-empty-dependencies
Fix grep error for empty dependencies
2 parents 393ad7e + acdc6df commit 084b188

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

Package.resolved

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/WeaverCodeGen/Lexer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private extension Lexer {
135135
return nil
136136
}
137137

138-
guard let nextLine = findNextLine(after: currentLine, containing: syntaxToken.offset) else {
138+
guard let nextLine = findNextLine(after: currentLine, containing: syntaxToken.offset.value) else {
139139
return nil
140140
}
141141
currentLine = nextLine
@@ -144,8 +144,8 @@ private extension Lexer {
144144

145145
do {
146146
guard let token = try TokenBuilder.makeAnnotationToken(string: content,
147-
offset: syntaxToken.offset,
148-
length: syntaxToken.length,
147+
offset: syntaxToken.offset.value,
148+
length: syntaxToken.length.value,
149149
line: currentLine) else {
150150
return nil
151151
}

Sources/WeaverCommand/Configuration.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,33 +195,41 @@ extension Configuration {
195195

196196
func inputPaths() throws -> [Path] {
197197
var inputPaths = Set<Path>()
198-
199-
let inputDirectories = Set(inputPathStrings
200-
.lazy
201-
.map { self.projectPath + $0 }
202-
.filter { $0.exists && $0.isDirectory }
203-
.map { $0.absolute().string })
204-
205-
if inputDirectories.isEmpty == false {
206-
let grepArguments = ["-lR", "-e", Configuration.annotationRegex, "-e", Configuration.propertyWrapperRegex] + Array(inputDirectories)
207-
inputPaths.formUnion(try shellOut(to: "grep", arguments: grepArguments)
208-
.split(separator: "\n")
209-
.lazy
210-
.map { Path(String($0)) }
211-
.filter { $0.extension == "swift" })
212-
}
213198

214199
inputPaths.formUnion(inputPathStrings
215200
.lazy
216201
.map { self.projectPath + $0 }
202+
.flatMap { $0.isFile ? [$0] : self.recursivePathsByPattern(fromDirectory: $0) }
217203
.filter { $0.exists && $0.isFile && $0.extension == "swift" })
218204

219205
inputPaths.subtract(try ignoredPathStrings
220206
.lazy
221207
.map { self.projectPath + $0 }
222-
.flatMap { $0.isFile ? [$0] : recursive ? try $0.recursiveChildren() : try $0.children() }
208+
.flatMap { $0.isFile ? [$0] : try paths(fromDirectory: $0) }
223209
.filter { $0.extension == "swift" })
224210

225211
return inputPaths.sorted()
226212
}
213+
214+
private func recursivePathsByPattern(fromDirectory directory: Path) -> [Path] {
215+
let grepArguments = [
216+
"-lR",
217+
"-e", Configuration.annotationRegex,
218+
"-e", Configuration.propertyWrapperRegex,
219+
directory.absolute().string
220+
]
221+
// if there are no files matching the pattern
222+
// command grep return exit 1, and ShellOut throws an exception with empty message
223+
guard let grepResult = try? shellOut(to: "grep", arguments: grepArguments) else {
224+
return []
225+
}
226+
227+
return grepResult
228+
.split(separator: "\n")
229+
.map { Path(String($0)) }
230+
}
231+
232+
private func paths(fromDirectory directory: Path) throws -> [Path] {
233+
recursive ? try directory.recursiveChildren() : try directory.children()
234+
}
227235
}

0 commit comments

Comments
 (0)