-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
536 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,8 @@ DerivedData | |
Tests/Fixtures/.build/ | ||
|
||
# VSCode | ||
.vscode/* | ||
.vscode/* | ||
|
||
# Bazel | ||
bazel-* | ||
/MODULE.bazel.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module( | ||
name = "periphery", | ||
version = "0.0.0", | ||
compatibility_level = 1, | ||
) | ||
|
||
bazel_dep(name = "rules_swift", version = "2.1.1") | ||
bazel_dep(name = "bazel_skylib", version = "1.7.1") | ||
|
||
generated = use_extension("//bazel:extensions.bzl", "generated") | ||
use_repo(generated, "periphery_generated") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
import SystemPackage | ||
|
||
public struct Bazel { | ||
public static var isSupported: Bool { | ||
FilePath("MODULE.bazel").exists || FilePath("WORKSPACE").exists | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import Foundation | ||
import Shared | ||
import Indexer | ||
import SystemPackage | ||
|
||
public class BazelProjectDriver: ProjectDriver { | ||
public static func build() throws -> Self { | ||
let configuration = Configuration.shared | ||
configuration.bazel = false // Generic project mode is used for the actual scan. | ||
configuration.reportExclude.append("**/bazel-out/**/*") | ||
return self.init(configuration: configuration) | ||
} | ||
|
||
// TODO: Others? tvos, watchos, etc | ||
private static let kinds = [ | ||
"apple_framework_packaging", | ||
"ios_unit_test", | ||
"ios_ui_test", | ||
"ios_application", | ||
// "swift_binary", | ||
// "swift_test" | ||
] | ||
|
||
private let configuration: Configuration | ||
private let shell: Shell | ||
private let logger: Logger | ||
private let fileManager: FileManager | ||
|
||
private let outputPath = FilePath("/var/tmp/periphery_bazel") | ||
|
||
private lazy var contextLogger: ContextualLogger = { | ||
logger.contextualized(with: "bazel") | ||
}() | ||
|
||
required init( | ||
configuration: Configuration = .shared, | ||
shell: Shell = .shared, | ||
logger: Logger = .init(), | ||
fileManager: FileManager = .default | ||
) { | ||
self.configuration = configuration | ||
self.shell = shell | ||
self.logger = logger | ||
self.fileManager = fileManager | ||
} | ||
|
||
public func build() throws { | ||
guard let executablePath = Bundle.main.executablePath else { | ||
fatalError("Expected executable path.") | ||
} | ||
|
||
try fileManager.createDirectory(at: outputPath.url, withIntermediateDirectories: true) | ||
|
||
let configPath = outputPath.appending("periphery.yml") | ||
try configuration.save(to: configPath) | ||
contextLogger.debug("Configuration written to \(configPath)") | ||
|
||
let buildPath = outputPath.appending("BUILD.bazel") | ||
let deps = try queryTargets().joined(separator: ",\n") | ||
let buildFileContents = """ | ||
load("@periphery//bazel/internal:scan.bzl", "scan") | ||
scan( | ||
name = "scan", | ||
testonly = True, | ||
config = "\(configPath)", | ||
periphery_binary = "\(executablePath)", | ||
visibility = [ | ||
"@periphery//bazel:package_group" | ||
], | ||
deps = [ | ||
\(deps) | ||
], | ||
) | ||
""" | ||
|
||
try buildFileContents.write(to: buildPath.url, atomically: true, encoding: .utf8) | ||
contextLogger.debug("Build file written to \(buildPath)") | ||
|
||
if configuration.outputFormat.supportsAuxiliaryOutput { | ||
let asterisk = colorize("*", .boldGreen) | ||
logger.info("\(asterisk) Building...") | ||
} | ||
|
||
let status = try shell.execStatus([ | ||
"bazel", | ||
"run", | ||
"--ui_event_filters=-info,-debug,-warning", | ||
"@periphery//bazel:scan"] | ||
) | ||
|
||
// The actual scan is performed by Bazel. | ||
exit(status) | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func queryTargets() throws -> [String] { | ||
try shell | ||
.exec([ | ||
"bazel", | ||
"query", | ||
"--noshow_progress", | ||
"--ui_event_filters=-info,-debug,-warning", | ||
query | ||
]) | ||
.split(separator: "\n") | ||
.map { "\"@@\($0)\"" } | ||
} | ||
|
||
private var query: String { | ||
// TODO: Make configurable. | ||
// TODO: Add option to filter labels. | ||
let target = ["//..."] | ||
let depsExpr = target.map { "deps(\($0))" }.joined(separator: " union ") | ||
let kindsExpr = "kind('(\(Self.kinds.joined(separator: "|"))) rule', \(depsExpr))" | ||
let filterExpr = "filter('^//.*', \(kindsExpr))" | ||
return filterExpr | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.