Skip to content

Commit

Permalink
Allow Info.plist customisation
Browse files Browse the repository at this point in the history
  • Loading branch information
stackotter committed Feb 3, 2022
1 parent 333f456 commit cb2cad9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 70 deletions.
18 changes: 16 additions & 2 deletions Sources/swift-bundler/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ struct Configuration: Codable {
var buildNumber: Int
var category: String
var minOSVersion: String

var extraInfoPlistEntries: [String: Any] = [:]

enum CodingKeys: String, CodingKey {
case target, bundleIdentifier, versionString, buildNumber, category, minOSVersion
}

static func load(_ directory: URL) -> Configuration {
do {
let data = try Data(contentsOf: directory.appendingPathComponent("Bundle.json"))
return try JSONDecoder().decode(Configuration.self, from: data)
var configuration = try JSONDecoder().decode(Configuration.self, from: data)

// Load the `extraInfoPlistEntries` property if present
let json = try JSONSerialization.jsonObject(with: data)
if let json = json as? [String: Any], let extraEntries = json["extraInfoPlistEntries"] as? [String: Any] {
configuration.extraInfoPlistEntries = extraEntries
}

return configuration
} catch {
terminate("Failed to load config from Bundle.json. Please make sure that the current directory is setup for swift-bundler correctly; \(error)")
}
}
}
}
21 changes: 11 additions & 10 deletions Sources/swift-bundler/Subcommands/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ extension Bundler {
// Create Info.plist
updateProgress("Creating Info.plist", 0.7)
let infoPlistFile = appContents.appendingPathComponent("Info.plist")
let infoPlist = createAppInfoPlist(
appName: target,
bundleIdentifier: config.bundleIdentifier,
versionString: config.versionString,
buildNumber: config.buildNumber,
category: config.category,
minOSVersion: config.minOSVersion)
do {
try infoPlist.write(to: infoPlistFile, atomically: false, encoding: .utf8)
let infoPlist = try createAppInfoPlist(
appName: target,
bundleIdentifier: config.bundleIdentifier,
versionString: config.versionString,
buildNumber: config.buildNumber,
category: config.category,
minOSVersion: config.minOSVersion,
extraEntries: config.extraInfoPlistEntries)
try infoPlist.write(to: infoPlistFile)
} catch {
terminate("Failed to create Info.plist at '\(infoPlistFile.path)'; \(error)")
}
Expand Down Expand Up @@ -240,9 +241,9 @@ extension Bundler {
let infoPlistFile = bundleContents.appendingPathComponent("Info.plist")
if !FileManager.default.itemExists(at: infoPlistFile, withType: .file) {
log.info("Creating Info.plist for \(bundle.lastPathComponent)")
let infoPlist = createBundleInfoPlist(bundleIdentifier: bundleIdentifier, bundleName: bundleName, minOSVersion: config.minOSVersion)
do {
try infoPlist.write(to: infoPlistFile, atomically: false, encoding: .utf8)
let infoPlist = try createBundleInfoPlist(bundleIdentifier: bundleIdentifier, bundleName: bundleName, minOSVersion: config.minOSVersion)
try infoPlist.write(to: infoPlistFile)
} catch {
terminate("Failed to create Info.plist for '\(bundle.lastPathComponent)'; \(error)")
}
Expand Down
90 changes: 32 additions & 58 deletions Sources/swift-bundler/Utils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,40 @@ rm -R \(iconSetPath)
}
}

func createAppInfoPlist(appName: String, bundleIdentifier: String, versionString: String, buildNumber: Int, category: String, minOSVersion: String) -> String {
return """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>\(appName)</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIconName</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>\(bundleIdentifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>\(appName)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>\(versionString)</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>\(buildNumber)</string>
<key>LSApplicationCategoryType</key>
<string>\(category)</string>
<key>LSMinimumSystemVersion</key>
<string>\(minOSVersion)</string>
</dict>
</plist>
"""
func createAppInfoPlist(appName: String, bundleIdentifier: String, versionString: String, buildNumber: Int, category: String, minOSVersion: String, extraEntries: [String: Any]) throws -> Data {
var entries: [String: Any] = [
"CFBundleExecutable": appName,
"CFBundleIconFile": "AppIcon",
"CFBundleIconName": "AppIcon",
"CFBundleIdentifier": bundleIdentifier,
"CFBundleInfoDictionaryVersion": "6.0",
"CFBundleName": appName,
"CFBundlePackageType": "APPL",
"CFBundleShortVersionString": versionString,
"CFBundleSupportedPlatforms": ["MacOSX"],
"CFBundleVersion": "\(buildNumber)",
"LSApplicationCategoryType": category,
"LSMinimumSystemVersion": minOSVersion,
]

for (key, value) in extraEntries {
entries[key] = value
}

return try PropertyListSerialization.data(fromPropertyList: entries, format: .xml, options: 0)
}

func createBundleInfoPlist(bundleIdentifier: String, bundleName: String, minOSVersion: String) -> String {
return """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>\(bundleIdentifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>\(bundleName)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>LSMinimumSystemVersion</key>
<string>\(minOSVersion)</string>
</dict>
</plist>
"""
func createBundleInfoPlist(bundleIdentifier: String, bundleName: String, minOSVersion: String) throws -> Data {
let entries: [String: Any] = [
"CFBundleIdentifier": bundleIdentifier,
"CFBundleInfoDictionaryVersion": "6.0",
"CFBundleName": bundleName,
"CFBundlePackageType": "BNDL",
"CFBundleSupportedPlatforms": ["MacOSX"],
"LSMinimumSystemVersion": minOSVersion,
]

return try PropertyListSerialization.data(fromPropertyList: entries, format: .xml, options: 0)
}

func terminate(_ message: String) -> Never {
Expand Down

0 comments on commit cb2cad9

Please sign in to comment.