-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathPackage.swift
More file actions
166 lines (142 loc) · 6.06 KB
/
Copy pathPackage.swift
File metadata and controls
166 lines (142 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// swift-tools-version:5.9
// MIT License
//
// Copyright (c) 2023-present Poing Studios
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import PackageDescription
import Foundation
struct SPMPackageInfo {
let url: String
let version: String
let products: [String]
}
func parseGDIPFile(at path: String) -> [SPMPackageInfo] {
let fileURL = URL(fileURLWithPath: path)
guard let content = try? String(contentsOf: fileURL, encoding: .utf8) else {
return []
}
guard let spmStart = content.range(of: "spm_packages") else { return [] }
let afterSpm = String(content[spmStart.upperBound...])
let sectionPattern = #"\n\s*\[[a-zA-Z]"#
let nextSection: String
if let sectionRegex = try? NSRegularExpression(pattern: sectionPattern),
let sectionMatch = sectionRegex.firstMatch(in: afterSpm, range: NSRange(afterSpm.startIndex..., in: afterSpm)) {
nextSection = String(afterSpm[afterSpm.startIndex..<Range(sectionMatch.range, in: afterSpm)!.lowerBound])
} else {
nextSection = afterSpm
}
let dictPattern = #"\{([^}]+)\}"#
guard let dictRegex = try? NSRegularExpression(pattern: dictPattern) else { return [] }
let dictRange = NSRange(nextSection.startIndex..., in: nextSection)
let dictMatches = dictRegex.matches(in: nextSection, options: [], range: dictRange)
var packages: [SPMPackageInfo] = []
for dictMatch in dictMatches {
let dictContent = String(nextSection[Range(dictMatch.range(at: 1), in: nextSection)!])
let url = extractValue(from: dictContent, key: "url")
let version = extractValue(from: dictContent, key: "version")
let products = extractArray(from: dictContent, key: "products")
if !url.isEmpty && !version.isEmpty {
packages.append(SPMPackageInfo(url: url, version: version, products: products))
}
}
return packages
}
func extractValue(from text: String, key: String) -> String {
let pattern = #""\#(key)"\s*:\s*"([^"]+)""#
guard let regex = try? NSRegularExpression(pattern: pattern) else { return "" }
let range = NSRange(text.startIndex..., in: text)
guard let match = regex.firstMatch(in: text, options: [], range: range) else { return "" }
return String(text[Range(match.range(at: 1), in: text)!])
}
func extractArray(from text: String, key: String) -> [String] {
let pattern = #""\#(key)"\s*:\s*\[([^\]]*)\]"#
guard let regex = try? NSRegularExpression(pattern: pattern) else { return [] }
let range = NSRange(text.startIndex..., in: text)
guard let match = regex.firstMatch(in: text, options: [], range: range) else { return [] }
let arrayContent = String(text[Range(match.range(at: 1), in: text)!])
return arrayContent.components(separatedBy: ",")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines).trimmingCharacters(in: CharacterSet(charactersIn: "\"")) }
.filter { !$0.isEmpty }
}
var packageDependencies: [Package.Dependency] = []
var targetDependencies: [Target.Dependency] = []
var seenURLs = Set<String>()
var seenProducts = Set<String>()
let fileManager = FileManager.default
let packagePath = URL(fileURLWithPath: #file).deletingLastPathComponent().path
let searchPaths = [
"\(packagePath)/src/ads/config",
"\(packagePath)/src/mediation"
]
var gdipFiles: [String] = []
for searchPath in searchPaths {
if let enumerator = fileManager.enumerator(atPath: searchPath) {
for case let file as String in enumerator {
if file.hasSuffix(".gdip") {
gdipFiles.append("\(searchPath)/\(file)")
}
}
}
}
if gdipFiles.isEmpty {
print("PoingAdMob: WARNING - No .gdip files found in \(searchPaths)")
}
for gdipFile in gdipFiles {
print("PoingAdMob: Parsing \(gdipFile)")
let spmPackages = parseGDIPFile(at: gdipFile)
for package in spmPackages {
print("PoingAdMob: Found package \(package.url) v\(package.version) → \(package.products)")
if !seenURLs.contains(package.url) {
packageDependencies.append(.package(url: package.url, exact: Version(stringLiteral: package.version)))
seenURLs.insert(package.url)
}
let packageName = package.url.components(separatedBy: "/").last?
.replacingOccurrences(of: ".git", with: "") ?? ""
for product in package.products {
if !seenProducts.contains(product) {
targetDependencies.append(.product(name: product, package: packageName))
seenProducts.insert(product)
}
}
}
}
let package = Package(
name: "PoingGodotAdMobDeps",
platforms: [.iOS(.v14)],
products: [
.library(
name: "PoingGodotAdMobDeps",
targets: ["PoingGodotAdMobDeps"]),
],
dependencies: packageDependencies,
targets: [
.target(
name: "PoingGodotAdMobDeps",
dependencies: targetDependencies,
path: "scripts/spm",
exclude: [
"../../godot",
"../../bin",
"../../.build"
]
)
]
)
// cache_bust: 1