Merge pull request #2 from gregcotten/fix-macOS-generator

Make generation work on macOS
This commit is contained in:
david-swift 2024-02-26 21:18:30 -08:00 committed by GitHub
commit 8f29298684
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 11 deletions

View File

@ -12,7 +12,7 @@ import PackageDescription
let package = Package(
name: "Adwaita",
platforms: [
.macOS(.v10_15)
.macOS(.v13)
],
products: [
.library(

View File

@ -23,8 +23,13 @@ struct Generation {
/// The main function.
func run() throws {
removeOldFiles()
guard let gtkDefinitions = getDefinitions(path: configuration.gtkGirFilePath),
let adwDefinitions = getDefinitions(path: configuration.adwGirFilePath) else {
guard let gtkDefinitions = getGtkDefinitions(path: configuration.gtkGirFilePath) else {
print("failed to get Gtk definitions")
return
}
guard let adwDefinitions = getAdwDefinitions(path: configuration.adwGirFilePath) else {
print("failed to get Adw definitions")
return
}
for `class` in gtkDefinitions.namespace.classes {
@ -66,17 +71,28 @@ struct Generation {
/// Get the definitions of a GIR file at a specified path.
/// - Parameter path: The path.
/// - Returns: The GIR data.
func getDefinitions(path: String) -> GIR? {
func getGtkDefinitions(path: String) -> GIR? {
var path = path
let girDir = getGirDir()
path.replace(GenerationConfiguration.includeDir, with: girDir)
let girDir = getGtkGirDir()
path.replace(GenerationConfiguration.gtkIncludeDir, with: girDir)
let data = (try? Data(contentsOf: .init(fileURLWithPath: path))) ?? .init()
return try? GIR.decodeGIR(data)
}
/// Get the definitions of a GIR file at a specified path.
/// - Parameter path: The path.
/// - Returns: The GIR data.
func getAdwDefinitions(path: String) -> GIR? {
var path = path
let girDir = getAdwGirDir()
path.replace(GenerationConfiguration.adwIncludeDir, with: girDir)
let data = (try? Data(contentsOf: .init(fileURLWithPath: path))) ?? .init()
return try? GIR.decodeGIR(data)
}
/// Get the directory of the gir files.
/// - Returns: The path.
func getGirDir() -> String {
func getGtkGirDir() -> String {
let process = Process()
process.executableURL = .init(fileURLWithPath: "/bin/bash")
process.arguments = ["-c", "pkg-config --variable=includedir gtk4"]
@ -87,6 +103,19 @@ struct Generation {
return .init(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
/// Get the directory of the gir files.
/// - Returns: The path.
func getAdwGirDir() -> String {
let process = Process()
process.executableURL = .init(fileURLWithPath: "/bin/bash")
process.arguments = ["-c", "pkg-config --variable=includedir libadwaita-1"]
let pipe = Pipe()
process.standardOutput = pipe
try? process.run()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
return .init(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
/// Create a file containing a class.
/// - Parameters:
/// - class: The class.

View File

@ -13,9 +13,16 @@ import Foundation
struct GenerationConfiguration {
/// The include directory.
static let includeDir = "$(pkg-config --variable=includedir gtk4)"
static let gtkIncludeDir = "$(pkg-config --variable=includedir gtk4)"
/// The include directory.
static let adwIncludeDir = "$(pkg-config --variable=includedir libadwaita-1)"
/// The directory containing the GIR files.
static let girFilePath = "\(includeDir)/../share/gir-1.0/"
static let gtkGirFilePath = "\(gtkIncludeDir)/../share/gir-1.0/"
/// The directory containing the GIR files.
static let adwGirFilePath = "\(adwIncludeDir)/../share/gir-1.0/"
/// Exclude properties of buttons.
static var excludeButtons: [String] {
@ -23,9 +30,9 @@ struct GenerationConfiguration {
}
/// The Gtk GIR file.
var gtkGirFilePath = girFilePath + "Gtk-4.0.gir"
var gtkGirFilePath = gtkGirFilePath + "Gtk-4.0.gir"
/// The Adw GIR file.
var adwGirFilePath = girFilePath + "Adw-1.gir"
var adwGirFilePath = adwGirFilePath + "Adw-1.gir"
/// The folder containing the generated Swift files.
var folder = "Sources/Adwaita/View/Generated/"