Skip to content

Commit

Permalink
Merge pull request #2 from gregcotten/fix-macOS-generator
Browse files Browse the repository at this point in the history
Make generation work on macOS
  • Loading branch information
david-swift committed Feb 27, 2024
2 parents 461e9f4 + 414cb95 commit 8f29298
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import PackageDescription
let package = Package(
name: "Adwaita",
platforms: [
.macOS(.v10_15)
.macOS(.v13)
],
products: [
.library(
Expand Down
41 changes: 35 additions & 6 deletions Sources/Generation/Generation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = 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 = getGirDir()
path.replace(GenerationConfiguration.includeDir, with: girDir)
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"]
Expand All @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions Sources/Generation/GenerationConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ 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)"

Check failure on line 20 in Sources/Generation/GenerationConfiguration.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
/// The directory containing the GIR files.
static let gtkGirFilePath = "\(gtkIncludeDir)/../share/gir-1.0/"

/// The directory containing the GIR files.
static let girFilePath = "\(includeDir)/../share/gir-1.0/"
static let adwGirFilePath = "\(adwIncludeDir)/../share/gir-1.0/"

/// Exclude properties of buttons.
static var excludeButtons: [String] {
["action-target", "related-action"]
}

/// 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/"
Expand Down

0 comments on commit 8f29298

Please sign in to comment.