Merge pull request #7173 from nextcloud/feature/mac-crafter-package

Add packaging capability to Mac Crafter
This commit is contained in:
Claudio Cambra 2024-09-23 16:31:22 +08:00 committed by GitHub
commit 59501ac524
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 224 additions and 5 deletions

View file

@ -0,0 +1,17 @@
/*
* Copyright (C) 2024 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
func archToCraftTarget(_ arch: String) -> String {
return arch == "arm64" ? "macos-clang-arm64" : "macos-64-clang"
}

View file

@ -0,0 +1,122 @@
/*
* Copyright (C) 2024 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
import Foundation
enum PackagingError: Error {
case projectNameSettingError(String)
case packageBuildError(String)
case packageSigningError(String)
case packageNotarisationError(String)
case packageSparkleBuildError(String)
case packageSparkleSignError(String)
}
/// NOTE: Requires Packages utility. http://s.sudre.free.fr/Software/Packages/about.html
fileprivate func buildPackage(appName: String, buildWorkPath: String, productPath: String) throws -> String {
let packageFile = "\(appName).pkg"
let pkgprojPath = "\(buildWorkPath)/admin/osx/macosx.pkgproj"
guard shell("packagesutil --file \(pkgprojPath) set project name \(appName)") == 0 else {
throw PackagingError.projectNameSettingError("Could not set project name in pkgproj!")
}
guard shell("packagesbuild -v --build-folder \(productPath) -F \(productPath) \(pkgprojPath)") == 0 else {
throw PackagingError.packageBuildError("Error building pkg file!")
}
return "\(productPath)/\(packageFile)"
}
fileprivate func signPackage(packagePath: String, packageSigningId: String) throws {
let packagePathNew = "\(packagePath).new"
guard shell("productsign --timestamp --sign '\(packageSigningId)' \(packagePath) \(packagePathNew)") == 0 else {
throw PackagingError.packageSigningError("Could not sign pkg file!")
}
let fm = FileManager.default
try fm.removeItem(atPath: packagePath)
try fm.moveItem(atPath: packagePathNew, toPath: packagePath)
}
fileprivate func notarisePackage(
packagePath: String, appleId: String, applePassword: String, appleTeamId: String
) throws {
guard shell("xcrun notarytool submit \(packagePath) --apple-id \(appleId) --password \(applePassword) --team-id \(appleTeamId) --wait") == 0 else {
throw PackagingError.packageNotarisationError("Failure when notarising package!")
}
guard shell("xcrun stapler staple \(packagePath)") == 0 else {
throw PackagingError.packageNotarisationError("Could not staple notarisation on package!")
}
}
fileprivate func buildSparklePackage(packagePath: String, buildPath: String) throws -> String {
let sparkleTbzPath = "\(packagePath).tbz"
guard shell("tar cf \(sparkleTbzPath) \(packagePath)") == 0 else {
throw PackagingError.packageSparkleBuildError("Could not create Sparkle package tbz!")
}
return sparkleTbzPath
}
fileprivate func signSparklePackage(sparkleTbzPath: String, buildPath: String, signKey: String) throws {
guard shell("\(buildPath)/bin/sign_update -s \(signKey) \(sparkleTbzPath)") == 0 else {
throw PackagingError.packageSparkleSignError("Could not sign Sparkle package tbz!")
}
}
func packageAppBundle(
productPath: String,
buildPath: String,
craftTarget: String,
craftBlueprintName: String,
appName: String,
packageSigningId: String?,
appleId: String?,
applePassword: String?,
appleTeamId: String?,
sparklePackageSignKey: String?
) throws {
print("Creating pkg file for client…")
let buildWorkPath = "\(buildPath)/\(craftTarget)/build/\(craftBlueprintName)/work/build"
let packagePath = try buildPackage(
appName: appName,
buildWorkPath: buildWorkPath,
productPath: productPath
)
if let packageSigningId {
print("Signing pkg with \(packageSigningId)")
try signPackage(packagePath: packagePath, packageSigningId: packageSigningId)
if let appleId, let applePassword, let appleTeamId {
print("Notarising pkg with Apple ID \(appleId)")
try notarisePackage(
packagePath: packagePath,
appleId: appleId,
applePassword: applePassword,
appleTeamId: appleTeamId
)
}
}
print("Creating Sparkle TBZ file…")
let sparklePackagePath =
try buildSparklePackage(packagePath: packagePath, buildPath: buildPath)
if let sparklePackageSignKey {
print("Signing Sparkle TBZ file…")
try signSparklePackage(
sparkleTbzPath: sparklePackagePath,
buildPath: buildPath,
signKey: sparklePackageSignKey
)
}
}

View file

@ -65,6 +65,21 @@ struct Build: ParsableCommand {
@Option(name: [.long], help: "Git clone command; include options such as depth.") @Option(name: [.long], help: "Git clone command; include options such as depth.")
var gitCloneCommand = "git clone --depth=1" var gitCloneCommand = "git clone --depth=1"
@Option(name: [.long], help: "Apple ID, used for notarisation.")
var appleId: String?
@Option(name: [.long], help: "Apple ID password, used for notarisation.")
var applePassword: String?
@Option(name: [.long], help: "Apple Team ID, used for notarisation.")
var appleTeamId: String?
@Option(name: [.long], help: "Apple package signing ID.")
var packageSigningId: String?
@Option(name: [.long], help: "Sparkle package signing key.")
var sparklePackageSignKey: String?
@Flag(help: "Reconfigure KDE Craft.") @Flag(help: "Reconfigure KDE Craft.")
var reconfigureCraft = false var reconfigureCraft = false
@ -86,6 +101,9 @@ struct Build: ParsableCommand {
@Flag(help: "Run a full rebuild.") @Flag(help: "Run a full rebuild.")
var fullRebuild = false var fullRebuild = false
@Flag(help: "Create an installer package.")
var package = false
mutating func run() throws { mutating func run() throws {
print("Configuring build tooling.") print("Configuring build tooling.")
@ -110,7 +128,7 @@ struct Build: ParsableCommand {
let craftMasterDir = "\(buildPath)/craftmaster" let craftMasterDir = "\(buildPath)/craftmaster"
let craftMasterIni = "\(repoRootDir)/craftmaster.ini" let craftMasterIni = "\(repoRootDir)/craftmaster.ini"
let craftMasterPy = "\(craftMasterDir)/CraftMaster.py" let craftMasterPy = "\(craftMasterDir)/CraftMaster.py"
let craftTarget = arch == "arm64" ? "macos-clang-arm64" : "macos-64-clang" let craftTarget = archToCraftTarget(arch)
let craftCommand = let craftCommand =
"python3 \(craftMasterPy) --config \(craftMasterIni) --target \(craftTarget) -c" "python3 \(craftMasterPy) --config \(craftMasterIni) --target \(craftTarget) -c"
@ -171,7 +189,7 @@ struct Build: ParsableCommand {
) )
} }
print("Crafting Nextcloud Desktop Client...") print("Crafting \(appName) Desktop Client...")
let allOptionsString = craftOptions.map({ "--options \"\($0)\"" }).joined(separator: " ") let allOptionsString = craftOptions.map({ "--options \"\($0)\"" }).joined(separator: " ")
@ -209,6 +227,21 @@ struct Build: ParsableCommand {
} }
try fm.copyItem(atPath: clientAppDir, toPath: "\(productPath)/\(appName).app") try fm.copyItem(atPath: clientAppDir, toPath: "\(productPath)/\(appName).app")
if package {
try packageAppBundle(
productPath: productPath,
buildPath: buildPath,
craftTarget: craftTarget,
craftBlueprintName: craftBlueprintName,
appName: appName,
packageSigningId: packageSigningId,
appleId: appleId,
applePassword: applePassword,
appleTeamId: appleTeamId,
sparklePackageSignKey: sparklePackageSignKey
)
}
print("Done!") print("Done!")
} }
} }
@ -227,14 +260,61 @@ struct Codesign: ParsableCommand {
} }
} }
struct Package: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Packaging script for the client.")
@Option(name: [.short, .long], help: "Architecture.")
var arch = "arm64"
@Option(name: [.short, .long], help: "Path for build files to be written.")
var buildPath = "\(FileManager.default.currentDirectoryPath)/build"
@Option(name: [.short, .long], help: "Path for the final product to be put.")
var productPath = "\(FileManager.default.currentDirectoryPath)/product"
@Option(name: [.long], help: "Nextcloud Desktop Client craft blueprint name.")
var craftBlueprintName = "nextcloud-client"
@Option(name: [.long], help: "The application's branded name.")
var appName = "Nextcloud"
@Option(name: [.long], help: "Apple ID, used for notarisation.")
var appleId: String?
@Option(name: [.long], help: "Apple ID password, used for notarisation.")
var applePassword: String?
@Option(name: [.long], help: "Apple Team ID, used for notarisation.")
var appleTeamId: String?
@Option(name: [.long], help: "Apple package signing ID.")
var packageSigningId: String?
@Option(name: [.long], help: "Sparkle package signing key.")
var sparklePackageSignKey: String?
mutating func run() throws {
try packageAppBundle(
productPath: productPath,
buildPath: buildPath,
craftTarget: archToCraftTarget(arch),
craftBlueprintName: craftBlueprintName,
appName: appName,
packageSigningId: packageSigningId,
appleId: appleId,
applePassword: applePassword,
appleTeamId: appleTeamId,
sparklePackageSignKey: sparklePackageSignKey
)
}
}
struct MacCrafter: ParsableCommand { struct MacCrafter: ParsableCommand {
static let configuration = CommandConfiguration( static let configuration = CommandConfiguration(
abstract: "A tool to easily build a fully-functional Nextcloud Desktop Client for macOS.", abstract: "A tool to easily build a fully-functional Nextcloud Desktop Client for macOS.",
subcommands: [Build.self, Codesign.self], subcommands: [Build.self, Codesign.self, Package.self],
defaultSubcommand: Build.self defaultSubcommand: Build.self
) )
} }
MacCrafter.main() MacCrafter.main()