From 32579f457ae25ea610cc760766dbb725bc2fcde7 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 20 Aug 2020 11:19:17 -0700 Subject: [PATCH] Add support for params files for file path arguments This allows you to pass `@path/to/file` which will be read line by line for the list of files to lint / analyze. This is useful if you want to pass a massive list of files which will either exceed arg max, or break most unix tools if you use script input files + env vars. --- CHANGELOG.md | 3 ++- .../swiftlint/Helpers/LintableFilesVisitor.swift | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b644b07cd..b9814e441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ #### Enhancements -* None. +* Add support for params files for file paths. + [keith](https://github.com/keith) #### Bug Fixes diff --git a/Source/swiftlint/Helpers/LintableFilesVisitor.swift b/Source/swiftlint/Helpers/LintableFilesVisitor.swift index f536fc560..71cf15753 100644 --- a/Source/swiftlint/Helpers/LintableFilesVisitor.swift +++ b/Source/swiftlint/Helpers/LintableFilesVisitor.swift @@ -28,6 +28,16 @@ enum LintOrAnalyzeModeWithCompilerArguments { case analyze(allCompilerInvocations: CompilerInvocations) } +private func resolveParamsFiles(args: [String]) -> [String] { + return args.reduce(into: []) { allArgs, arg in + if arg.hasPrefix("@"), let contents = try? String(contentsOfFile: String(arg.dropFirst())) { + allArgs += resolveParamsFiles(args: contents.split(separator: "\n").map(String.init)) + } else if !arg.isEmpty { + allArgs.append(arg) + } + } +} + struct LintableFilesVisitor { let paths: [String] let action: String @@ -44,7 +54,7 @@ struct LintableFilesVisitor { init(paths: [String], action: String, useSTDIN: Bool, quiet: Bool, useScriptInputFiles: Bool, forceExclude: Bool, cache: LinterCache?, parallel: Bool, allowZeroLintableFiles: Bool, block: @escaping (CollectedLinter) -> Void) { - self.paths = paths + self.paths = resolveParamsFiles(args: paths) self.action = action self.useSTDIN = useSTDIN self.quiet = quiet @@ -61,7 +71,7 @@ struct LintableFilesVisitor { useScriptInputFiles: Bool, forceExclude: Bool, cache: LinterCache?, compilerInvocations: CompilerInvocations?, allowZeroLintableFiles: Bool, block: @escaping (CollectedLinter) -> Void) { - self.paths = paths + self.paths = resolveParamsFiles(args: paths) self.action = action self.useSTDIN = useSTDIN self.quiet = quiet