2024-06-20 08:55:55 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2024-06-20 09:16:58 +03:00
|
|
|
import ArgumentParser
|
2024-06-20 08:50:12 +03:00
|
|
|
import Foundation
|
|
|
|
|
2024-06-20 09:16:58 +03:00
|
|
|
struct MacCrafter: ParsableCommand {
|
|
|
|
static let configuration = CommandConfiguration(
|
2024-06-20 09:59:11 +03:00
|
|
|
abstract: "A tool to easily build a fully-functional Nextcloud Desktop Client for macOS."
|
2024-06-20 09:16:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
enum MacCrafterError: Error {
|
|
|
|
case failedEnumeration(String)
|
2024-06-20 13:20:26 +03:00
|
|
|
case environmentError(String)
|
2024-06-22 11:44:31 +03:00
|
|
|
case gitError(String)
|
|
|
|
case craftError(String)
|
2024-06-20 09:16:58 +03:00
|
|
|
}
|
|
|
|
|
2024-06-20 09:58:14 +03:00
|
|
|
@Argument(help: "Path to the root directory of the Nextcloud Desktop Client git repository.")
|
|
|
|
var repoRootDir = "\(FileManager.default.currentDirectoryPath)/../../.."
|
|
|
|
|
|
|
|
@Option(name: [.short, .long], help: "Code signing identity for desktop client and libs.")
|
|
|
|
var codeSignIdentity: String?
|
|
|
|
|
2024-07-16 10:53:43 +03:00
|
|
|
@Option(name: [.short, .long], help: "Path for build files to be written.")
|
2024-06-20 09:58:14 +03:00
|
|
|
var buildPath = "\(FileManager.default.currentDirectoryPath)/build"
|
|
|
|
|
2024-07-16 10:54:44 +03:00
|
|
|
@Option(name: [.short, .long], help: "Path for the final product to be put.")
|
|
|
|
var productPath = "\(FileManager.default.currentDirectoryPath)/product"
|
|
|
|
|
2024-07-16 10:51:00 +03:00
|
|
|
@Option(name: [.short, .long], help: "Architecture.")
|
|
|
|
var arch = "arm64"
|
|
|
|
|
2024-06-20 09:58:14 +03:00
|
|
|
@Option(name: [.long], help: "Brew installation script URL.")
|
|
|
|
var brewInstallShUrl = "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
|
|
|
|
|
|
|
|
@Option(name: [.long], help: "CraftMaster git url.")
|
|
|
|
var craftMasterGitUrl = "https://invent.kde.org/packaging/craftmaster.git"
|
|
|
|
|
2024-06-22 10:59:11 +03:00
|
|
|
@Option(name: [.long], help: "Nextcloud Desktop Client craft blueprint git url.")
|
2024-06-20 09:58:14 +03:00
|
|
|
var clientBlueprintsGitUrl = "https://github.com/nextcloud/desktop-client-blueprints.git"
|
2024-06-20 09:27:25 +03:00
|
|
|
|
2024-06-22 10:59:11 +03:00
|
|
|
@Option(name: [.long], help: "Nextcloud Desktop Client craft blueprint name.")
|
|
|
|
var craftBlueprintName = "nextcloud-client"
|
|
|
|
|
2024-06-20 13:09:48 +03:00
|
|
|
@Option(name: [.long], help: "Build type (e.g. Release, RelWithDebInfo, MinSizeRel, Debug).")
|
|
|
|
var buildType = "RelWithDebInfo"
|
|
|
|
|
2024-06-20 13:11:33 +03:00
|
|
|
@Option(name: [.long], help: "The application's branded name.")
|
|
|
|
var appName = "Nextcloud"
|
|
|
|
|
2024-06-21 15:16:37 +03:00
|
|
|
@Option(name: [.long], help: "Sparkle download URL.")
|
|
|
|
var sparkleDownloadUrl =
|
|
|
|
"https://github.com/sparkle-project/Sparkle/releases/download/1.27.3/Sparkle-1.27.3.tar.xz"
|
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
@Option(name: [.long], help: "Git clone command; include options such as depth.")
|
|
|
|
var gitCloneCommand = "git clone --depth=1"
|
|
|
|
|
|
|
|
@Flag(help: "Reconfigure KDE Craft.")
|
|
|
|
var reconfigureCraft = false
|
|
|
|
|
2024-06-24 14:41:28 +03:00
|
|
|
@Flag(help: "Run build offline (i.e. do not update craft)")
|
|
|
|
var offline = false
|
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
@Flag(help: "Build test suite.")
|
|
|
|
var buildTests = false
|
|
|
|
|
|
|
|
@Flag(name: [.long], help: "Do not build App Bundle.")
|
|
|
|
var disableAppBundle = false
|
2024-06-22 11:49:55 +03:00
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
@Flag(help: "Build File Provider Module.")
|
2024-06-22 11:49:55 +03:00
|
|
|
var buildFileProviderModule = false
|
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
@Flag(help: "Build without Sparkle auto-updater.")
|
|
|
|
var disableAutoUpdater = false
|
2024-06-22 11:41:40 +03:00
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
@Flag(help: "Run a full rebuild.")
|
2024-06-22 12:29:21 +03:00
|
|
|
var fullRebuild = false
|
|
|
|
|
2024-06-20 09:16:58 +03:00
|
|
|
mutating func run() throws {
|
|
|
|
print("Configuring build tooling.")
|
|
|
|
|
2024-06-20 13:30:57 +03:00
|
|
|
if codeSignIdentity != nil {
|
2024-06-20 13:20:26 +03:00
|
|
|
guard commandExists("codesign") else {
|
|
|
|
throw MacCrafterError.environmentError("codesign not found, cannot proceed.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 09:25:56 +03:00
|
|
|
try installIfMissing("git", "xcode-select --install")
|
|
|
|
try installIfMissing(
|
2024-06-20 09:16:58 +03:00
|
|
|
"brew",
|
2024-06-20 10:59:10 +03:00
|
|
|
"curl -fsSL \(brewInstallShUrl) | /bin/bash",
|
2024-06-20 09:16:58 +03:00
|
|
|
installCommandEnv: ["NONINTERACTIVE": "1"]
|
|
|
|
)
|
2024-06-20 09:25:56 +03:00
|
|
|
try installIfMissing("inkscape", "brew install inkscape")
|
|
|
|
try installIfMissing("python3", "brew install pyenv && pyenv install 3.12.4")
|
2024-06-20 09:16:58 +03:00
|
|
|
|
|
|
|
print("Build tooling configured.")
|
2024-06-20 08:50:12 +03:00
|
|
|
|
2024-06-20 09:16:58 +03:00
|
|
|
let fm = FileManager.default
|
2024-06-20 13:46:44 +03:00
|
|
|
let craftMasterDir = "\(buildPath)/craftmaster"
|
2024-06-20 09:16:58 +03:00
|
|
|
let craftMasterIni = "\(repoRootDir)/craftmaster.ini"
|
|
|
|
let craftMasterPy = "\(craftMasterDir)/CraftMaster.py"
|
2024-07-16 10:51:00 +03:00
|
|
|
let craftTarget = arch == "arm64" ? "macos-clang-arm64" : "macos-64-clang"
|
2024-06-20 13:46:44 +03:00
|
|
|
let craftCommand =
|
2024-06-20 09:43:27 +03:00
|
|
|
"python3 \(craftMasterPy) --config \(craftMasterIni) --target \(craftTarget) -c"
|
2024-06-20 09:16:58 +03:00
|
|
|
|
2024-06-22 11:41:22 +03:00
|
|
|
if !fm.fileExists(atPath: craftMasterDir) || reconfigureCraft {
|
2024-06-20 13:46:44 +03:00
|
|
|
print("Configuring KDE Craft.")
|
|
|
|
|
|
|
|
if fm.fileExists(atPath: craftMasterDir) {
|
|
|
|
print("KDE Craft is already cloned.")
|
|
|
|
} else {
|
|
|
|
print("Cloning KDE Craft...")
|
2024-06-22 11:44:31 +03:00
|
|
|
guard shell("\(gitCloneCommand) \(craftMasterGitUrl) \(craftMasterDir)") == 0 else {
|
|
|
|
throw MacCrafterError.gitError("Error cloning craftmaster.")
|
|
|
|
}
|
2024-06-20 13:46:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
print("Configuring Nextcloud Desktop Client blueprints for KDE Craft...")
|
2024-06-22 11:44:31 +03:00
|
|
|
guard shell("\(craftCommand) --add-blueprint-repository \(clientBlueprintsGitUrl)") == 0 else {
|
|
|
|
throw MacCrafterError.craftError("Error adding blueprint repository.")
|
|
|
|
}
|
2024-06-20 13:46:44 +03:00
|
|
|
|
|
|
|
print("Crafting KDE Craft...")
|
2024-06-22 11:44:31 +03:00
|
|
|
guard shell("\(craftCommand) craft") == 0 else {
|
|
|
|
throw MacCrafterError.craftError("Error crafting KDE Craft.")
|
|
|
|
}
|
2024-06-20 09:16:58 +03:00
|
|
|
|
2024-06-20 13:46:44 +03:00
|
|
|
print("Crafting Nextcloud Desktop Client dependencies...")
|
2024-06-22 11:44:31 +03:00
|
|
|
guard shell("\(craftCommand) --install-deps \(craftBlueprintName)") == 0 else {
|
|
|
|
throw MacCrafterError.craftError("Error installing dependencies.")
|
|
|
|
}
|
2024-06-20 13:46:44 +03:00
|
|
|
}
|
2024-06-20 09:16:58 +03:00
|
|
|
|
2024-06-22 11:49:55 +03:00
|
|
|
var craftOptions = [
|
|
|
|
"\(craftBlueprintName).srcDir=\(repoRootDir)",
|
2024-07-16 10:51:00 +03:00
|
|
|
"\(craftBlueprintName).osxArchs=\(arch)",
|
2024-06-23 12:45:52 +03:00
|
|
|
"\(craftBlueprintName).buildTests=\(buildTests ? "True" : "False")",
|
2024-06-24 14:01:19 +03:00
|
|
|
"\(craftBlueprintName).buildMacOSBundle=\(disableAppBundle ? "False" : "True")",
|
2024-06-22 11:49:55 +03:00
|
|
|
"\(craftBlueprintName).buildFileProviderModule=\(buildFileProviderModule ? "True" : "False")"
|
|
|
|
]
|
2024-06-21 15:16:37 +03:00
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
if !disableAutoUpdater {
|
2024-06-21 15:16:37 +03:00
|
|
|
print("Configuring Sparkle auto-updater.")
|
|
|
|
|
|
|
|
let fm = FileManager.default
|
|
|
|
guard fm.fileExists(atPath: "\(buildPath)/Sparkle.tar.xz") ||
|
|
|
|
shell("wget \(sparkleDownloadUrl) -O \(buildPath)/Sparkle.tar.xz") == 0
|
|
|
|
else {
|
|
|
|
throw MacCrafterError.environmentError("Error downloading sparkle.")
|
|
|
|
}
|
|
|
|
|
|
|
|
guard fm.fileExists(atPath: "\(buildPath)/Sparkle.framework") ||
|
|
|
|
shell("tar -xvf \(buildPath)/Sparkle.tar.xz -C \(buildPath)") == 0
|
|
|
|
else {
|
|
|
|
throw MacCrafterError.environmentError("Error unpacking sparkle.")
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:01:22 +03:00
|
|
|
craftOptions.append(
|
|
|
|
"\(craftBlueprintName).sparkleLibPath=\(buildPath)/Sparkle.framework"
|
|
|
|
)
|
2024-06-21 15:16:37 +03:00
|
|
|
}
|
|
|
|
|
2024-06-20 14:33:50 +03:00
|
|
|
print("Crafting Nextcloud Desktop Client...")
|
2024-06-21 15:16:37 +03:00
|
|
|
|
2024-06-22 11:01:22 +03:00
|
|
|
let allOptionsString = craftOptions.map({ "--options \"\($0)\"" }).joined(separator: " ")
|
2024-06-22 17:55:17 +03:00
|
|
|
|
|
|
|
let clientBuildDir = "\(buildPath)/\(craftTarget)/build/\(craftBlueprintName)"
|
|
|
|
if fullRebuild {
|
|
|
|
do {
|
|
|
|
try fm.removeItem(atPath: clientBuildDir)
|
|
|
|
} catch let error {
|
|
|
|
print("WARNING! Error removing build directory: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 14:01:19 +03:00
|
|
|
let buildMode = fullRebuild ? "-i" : disableAppBundle ? "compile" : "--compile --install"
|
2024-06-24 14:41:28 +03:00
|
|
|
let offlineMode = offline ? "--offline" : ""
|
2024-06-22 11:44:31 +03:00
|
|
|
guard shell(
|
2024-06-24 14:41:28 +03:00
|
|
|
"\(craftCommand) --buildtype \(buildType) \(buildMode) \(offlineMode) \(allOptionsString) \(craftBlueprintName)"
|
2024-06-22 11:44:31 +03:00
|
|
|
) == 0 else {
|
|
|
|
throw MacCrafterError.craftError("Error crafting Nextcloud Desktop Client.")
|
|
|
|
}
|
2024-06-20 09:58:14 +03:00
|
|
|
|
2024-06-20 14:33:50 +03:00
|
|
|
guard let codeSignIdentity else {
|
|
|
|
print("Crafted Nextcloud Desktop Client. Not codesigned.")
|
|
|
|
return
|
|
|
|
}
|
2024-06-20 09:58:14 +03:00
|
|
|
|
2024-06-20 14:33:50 +03:00
|
|
|
print("Code-signing Nextcloud Desktop Client libraries and frameworks...")
|
2024-06-22 17:55:17 +03:00
|
|
|
let clientAppDir = "\(clientBuildDir)/image-\(buildType)-master/\(appName).app"
|
2024-06-20 14:38:33 +03:00
|
|
|
try codesignClientAppBundle(at: clientAppDir, withCodeSignIdentity: codeSignIdentity)
|
2024-06-20 14:33:50 +03:00
|
|
|
|
2024-07-16 10:54:44 +03:00
|
|
|
print("Placing Nextcloud Desktop Client in product directory...")
|
|
|
|
try fm.createDirectory(atPath: productPath, withIntermediateDirectories: true, attributes: nil)
|
2024-08-05 14:00:21 +03:00
|
|
|
try fm.copyItem(atPath: clientAppDir, toPath: "\(productPath)/\(appName).app")
|
2024-07-16 10:54:44 +03:00
|
|
|
|
2024-06-20 14:33:50 +03:00
|
|
|
print("Done!")
|
2024-06-20 09:16:58 +03:00
|
|
|
}
|
|
|
|
}
|
2024-06-20 08:50:12 +03:00
|
|
|
|
2024-06-20 09:16:58 +03:00
|
|
|
MacCrafter.main()
|