From b5834f16e17e27a10b49d5cb72ca7a78986e47fe Mon Sep 17 00:00:00 2001 From: Nikita Moiseev Date: Sun, 19 Jan 2020 12:39:40 +0300 Subject: [PATCH 1/6] refactoring: more clean code for inputFiles() function at the cost of multi grep --- Sources/WeaverCommand/Configuration.swift | 37 ++++++++++++----------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Sources/WeaverCommand/Configuration.swift b/Sources/WeaverCommand/Configuration.swift index 21222785..042c937f 100644 --- a/Sources/WeaverCommand/Configuration.swift +++ b/Sources/WeaverCommand/Configuration.swift @@ -195,33 +195,36 @@ extension Configuration { func inputPaths() throws -> [Path] { var inputPaths = Set() - - let inputDirectories = Set(inputPathStrings - .lazy - .map { self.projectPath + $0 } - .filter { $0.exists && $0.isDirectory } - .map { $0.absolute().string }) - - if inputDirectories.isEmpty == false { - let grepArguments = ["-lR", "-e", Configuration.annotationRegex, "-e", Configuration.propertyWrapperRegex] + Array(inputDirectories) - inputPaths.formUnion(try shellOut(to: "grep", arguments: grepArguments) - .split(separator: "\n") - .lazy - .map { Path(String($0)) } - .filter { $0.extension == "swift" }) - } - inputPaths.formUnion(inputPathStrings + inputPaths.formUnion(try inputPathStrings .lazy .map { self.projectPath + $0 } + .flatMap { $0.isFile ? [$0] : try self.recursiveFilesByPattren(fromDirectory: $0) } .filter { $0.exists && $0.isFile && $0.extension == "swift" }) inputPaths.subtract(try ignoredPathStrings .lazy .map { self.projectPath + $0 } - .flatMap { $0.isFile ? [$0] : recursive ? try $0.recursiveChildren() : try $0.children() } + .flatMap { $0.isFile ? [$0] : try self.files(fromDirectory: $0) } .filter { $0.extension == "swift" }) return inputPaths.sorted() } + + private func recursiveFilesByPattren(fromDirectory directory: Path) throws -> [Path] { + let grepArguments = [ + "-lR", + "-e", Configuration.annotationRegex, + "-e", Configuration.propertyWrapperRegex, + directory.absolute().string + ] + return try shellOut(to: "grep", arguments: grepArguments) + .split(separator: "\n") + .lazy + .map { Path(String($0)) } + } + + private func files(fromDirectory directory: Path) throws -> [Path] { + recursive ? try directory.recursiveChildren() : try directory.children() + } } From fd64c61634827d1e835109151ddcf9027baee028 Mon Sep 17 00:00:00 2001 From: Nikita Moiseev Date: Sun, 19 Jan 2020 12:51:05 +0300 Subject: [PATCH 2/6] fix throws exception if no files match the pattern --- Sources/WeaverCommand/Configuration.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/WeaverCommand/Configuration.swift b/Sources/WeaverCommand/Configuration.swift index 042c937f..519b5855 100644 --- a/Sources/WeaverCommand/Configuration.swift +++ b/Sources/WeaverCommand/Configuration.swift @@ -218,7 +218,13 @@ extension Configuration { "-e", Configuration.propertyWrapperRegex, directory.absolute().string ] - return try shellOut(to: "grep", arguments: grepArguments) + // if there are no files matching the pattern + // command grep return exit 1, and ShellOut throws an exception with empty message + guard let grepResult = try? shellOut(to: "grep", arguments: grepArguments) else { + return [] + } + + return grepResult .split(separator: "\n") .lazy .map { Path(String($0)) } From 638a62637ddfa52e2a88bba4237142f6caaaf5b9 Mon Sep 17 00:00:00 2001 From: Nikita Moiseev Date: Sun, 19 Jan 2020 12:51:32 +0300 Subject: [PATCH 3/6] update packages --- Package.resolved | 10 +++++----- Sources/WeaverCodeGen/Lexer.swift | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Package.resolved b/Package.resolved index ca145118..fb4b4e16 100644 --- a/Package.resolved +++ b/Package.resolved @@ -42,7 +42,7 @@ "repositoryURL": "https://github.com/scribd/Meta.git", "state": { "branch": "master", - "revision": "94685cb2252a7eb5caa0f74a440f5d130595733b", + "revision": "a955d8dfbaaf6416922967d190580ea5a4571288", "version": null } }, @@ -51,8 +51,8 @@ "repositoryURL": "https://github.com/Quick/Nimble.git", "state": { "branch": null, - "revision": "6abeb3f5c03beba2b9e4dbe20886e773b5b629b6", - "version": "8.0.4" + "revision": "b02b00b30b6353632aa4a5fb6124f8147f7140c0", + "version": "8.0.5" } }, { @@ -96,8 +96,8 @@ "repositoryURL": "https://github.com/jpsim/SourceKitten.git", "state": { "branch": null, - "revision": "356551fc513eb12ed779bb369f79cf86a3a01599", - "version": "0.27.0" + "revision": "77a4dbbb477a8110eb8765e3c44c70fb4929098f", + "version": "0.29.0" } }, { diff --git a/Sources/WeaverCodeGen/Lexer.swift b/Sources/WeaverCodeGen/Lexer.swift index b03f19a2..2152e142 100644 --- a/Sources/WeaverCodeGen/Lexer.swift +++ b/Sources/WeaverCodeGen/Lexer.swift @@ -135,7 +135,7 @@ private extension Lexer { return nil } - guard let nextLine = findNextLine(after: currentLine, containing: syntaxToken.offset) else { + guard let nextLine = findNextLine(after: currentLine, containing: syntaxToken.offset.value) else { return nil } currentLine = nextLine @@ -144,8 +144,8 @@ private extension Lexer { do { guard let token = try TokenBuilder.makeAnnotationToken(string: content, - offset: syntaxToken.offset, - length: syntaxToken.length, + offset: syntaxToken.offset.value, + length: syntaxToken.length.value, line: currentLine) else { return nil } From a1dee4691949cba93c131bfa692fc3adee31514a Mon Sep 17 00:00:00 2001 From: Nikita Moiseev Date: Sat, 1 Feb 2020 19:48:55 +0300 Subject: [PATCH 4/6] remove excess lazy --- Sources/WeaverCommand/Configuration.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/WeaverCommand/Configuration.swift b/Sources/WeaverCommand/Configuration.swift index 519b5855..81b922c6 100644 --- a/Sources/WeaverCommand/Configuration.swift +++ b/Sources/WeaverCommand/Configuration.swift @@ -226,7 +226,6 @@ extension Configuration { return grepResult .split(separator: "\n") - .lazy .map { Path(String($0)) } } From aec640c17660440da6eb430742ede2e3ab40baa6 Mon Sep 17 00:00:00 2001 From: Nikita Moiseev Date: Sat, 1 Feb 2020 19:49:16 +0300 Subject: [PATCH 5/6] remove excess try --- Sources/WeaverCommand/Configuration.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/WeaverCommand/Configuration.swift b/Sources/WeaverCommand/Configuration.swift index 81b922c6..d6affcf4 100644 --- a/Sources/WeaverCommand/Configuration.swift +++ b/Sources/WeaverCommand/Configuration.swift @@ -196,22 +196,22 @@ extension Configuration { func inputPaths() throws -> [Path] { var inputPaths = Set() - inputPaths.formUnion(try inputPathStrings + inputPaths.formUnion(inputPathStrings .lazy .map { self.projectPath + $0 } - .flatMap { $0.isFile ? [$0] : try self.recursiveFilesByPattren(fromDirectory: $0) } + .flatMap { $0.isFile ? [$0] : self.recursiveFilesByPattern(fromDirectory: $0) } .filter { $0.exists && $0.isFile && $0.extension == "swift" }) inputPaths.subtract(try ignoredPathStrings .lazy .map { self.projectPath + $0 } - .flatMap { $0.isFile ? [$0] : try self.files(fromDirectory: $0) } + .flatMap { $0.isFile ? [$0] : try files(fromDirectory: $0) } .filter { $0.extension == "swift" }) return inputPaths.sorted() } - private func recursiveFilesByPattren(fromDirectory directory: Path) throws -> [Path] { + private func recursiveFilesByPattern(fromDirectory directory: Path) -> [Path] { let grepArguments = [ "-lR", "-e", Configuration.annotationRegex, From acdc6df90bd3029983e853d46320a5b1e343125d Mon Sep 17 00:00:00 2001 From: Nikita Moiseev Date: Mon, 24 Feb 2020 14:38:08 +0300 Subject: [PATCH 6/6] rename files to paths --- Sources/WeaverCommand/Configuration.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/WeaverCommand/Configuration.swift b/Sources/WeaverCommand/Configuration.swift index d6affcf4..e6ec359b 100644 --- a/Sources/WeaverCommand/Configuration.swift +++ b/Sources/WeaverCommand/Configuration.swift @@ -199,19 +199,19 @@ extension Configuration { inputPaths.formUnion(inputPathStrings .lazy .map { self.projectPath + $0 } - .flatMap { $0.isFile ? [$0] : self.recursiveFilesByPattern(fromDirectory: $0) } + .flatMap { $0.isFile ? [$0] : self.recursivePathsByPattern(fromDirectory: $0) } .filter { $0.exists && $0.isFile && $0.extension == "swift" }) inputPaths.subtract(try ignoredPathStrings .lazy .map { self.projectPath + $0 } - .flatMap { $0.isFile ? [$0] : try files(fromDirectory: $0) } + .flatMap { $0.isFile ? [$0] : try paths(fromDirectory: $0) } .filter { $0.extension == "swift" }) return inputPaths.sorted() } - private func recursiveFilesByPattern(fromDirectory directory: Path) -> [Path] { + private func recursivePathsByPattern(fromDirectory directory: Path) -> [Path] { let grepArguments = [ "-lR", "-e", Configuration.annotationRegex, @@ -229,7 +229,7 @@ extension Configuration { .map { Path(String($0)) } } - private func files(fromDirectory directory: Path) throws -> [Path] { + private func paths(fromDirectory directory: Path) throws -> [Path] { recursive ? try directory.recursiveChildren() : try directory.children() } }