-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
24 changed files
with
2,502 additions
and
97 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
common --enable_bzlmod |
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 @@ | ||
7.3.0 |
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,7 @@ DerivedData | |
Tests/Fixtures/.build/ | ||
|
||
# VSCode | ||
.vscode/* | ||
.vscode/* | ||
|
||
# Bazel | ||
bazel-* |
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,12 @@ | ||
module( | ||
name = "periphery", | ||
version = "0.0.0", | ||
compatibility_level = 1, | ||
) | ||
|
||
bazel_dep(name = "rules_swift", version = "2.1.1") | ||
bazel_dep(name = "rules_apple", version = "3.8.0") | ||
bazel_dep(name = "bazel_skylib", version = "1.7.1") | ||
|
||
generated = use_extension("//bazel:extensions.bzl", "generated") | ||
use_repo(generated, "periphery_generated") |
Large diffs are not rendered by default.
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,160 @@ | ||
import Foundation | ||
import Shared | ||
import SystemPackage | ||
|
||
public class BazelProjectDriver: ProjectDriver { | ||
public static var isSupported: Bool { | ||
FilePath("MODULE.bazel").exists || FilePath("WORKSPACE").exists | ||
} | ||
|
||
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) | ||
} | ||
|
||
private static let topLevelKinds = [ | ||
// rules_apple, iOS | ||
"ios_app_clip", | ||
"ios_application", | ||
"ios_extension", | ||
"ios_imessage_application", | ||
"ios_imessage_extension", | ||
"ios_sticker_pack_extension", | ||
"ios_ui_test", | ||
"ios_unit_test", | ||
|
||
// rules_apple, tvOS | ||
"tvos_application", | ||
"tvos_extension", | ||
"tvos_ui_test", | ||
"tvos_unit_test", | ||
|
||
// rules_apple, watchOS | ||
"watchos_application", | ||
"watchos_extension", | ||
"watchos_ui_test", | ||
"watchos_unit_test", | ||
|
||
// rules_apple, visionOS | ||
"visionos_application", | ||
"visionos_ui_test", | ||
"visionos_unit_test", | ||
|
||
// rules_apple, macOS | ||
"macos_application", | ||
"macos_command_line_application", | ||
"macos_extension", | ||
"macos_kernel_extension", | ||
"macos_quick_look_plugin", | ||
"macos_spotlight_importer", | ||
"macos_xpc_service", | ||
"macos_ui_test", | ||
"macos_unit_test", | ||
|
||
// rules_swift | ||
"swift_binary", | ||
"swift_test", | ||
"swift_compiler_plugin", | ||
] | ||
|
||
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", query], stderr: false) | ||
.split(separator: "\n") | ||
.map { "\"@@\($0)\"" } | ||
} | ||
|
||
private var query: String { | ||
let query = """ | ||
filter( | ||
'^//.*', | ||
kind( | ||
'(\(Self.topLevelKinds.joined(separator: "|"))) rule', | ||
deps(//...) | ||
) | ||
) | ||
""" | ||
|
||
if let pattern = configuration.bazelFilter { | ||
return "filter('\(pattern)', \(query))" | ||
} | ||
|
||
return query | ||
} | ||
} |
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
Oops, something went wrong.