diff --git a/Core/Package.swift b/Core/Package.swift index 675abea4..c9c78209 100644 --- a/Core/Package.swift +++ b/Core/Package.swift @@ -3,18 +3,11 @@ import PackageDescription -let gitTag: String = "\"" + (Context.gitInformation?.currentTag ?? "unknown") + "\"" -let gitCommit: String = "\"" + (Context.gitInformation?.currentCommit ?? "unknown") + "\"" - let package = Package( name: "Core", platforms: [.macOS(.v13)], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. - .library( - name: "Metadata", - targets: ["Metadata"] - ), .library( name: "Core", targets: ["Core"] @@ -24,24 +17,23 @@ let package = Package( .package(url: "https://github.com/azooKey/AzooKeyKanaKanjiConverter", from: "0.8.0", traits: ["Zenzai"]), ], targets: [ - .target( - name: "Metadata", - cSettings: [ - .define( - "GIT_TAG", to: gitTag - ), - .define( - "GIT_COMMIT", to: gitCommit - ), - ] + .executableTarget( + name: "git-info-generator", + ), + .plugin( + name: "GitInfoPlugin", + capability: .buildTool(), + dependencies: [.target(name: "git-info-generator")] ), .target( name: "Core", dependencies: [ - .target(name: "Metadata"), .product(name: "SwiftUtils", package: "AzooKeyKanaKanjiConverter"), .product(name: "KanaKanjiConverterModuleWithDefaultDictionary", package: "AzooKeyKanaKanjiConverter"), ], + plugins: [ + .plugin(name: "GitInfoPlugin") + ], ), .testTarget( name: "CoreTests", diff --git a/Core/Plugins/GitInfoPlugin/plugin.swift b/Core/Plugins/GitInfoPlugin/plugin.swift new file mode 100644 index 00000000..396978a4 --- /dev/null +++ b/Core/Plugins/GitInfoPlugin/plugin.swift @@ -0,0 +1,20 @@ +import PackagePlugin +import Foundation + +@main +struct GitInfoPlugin: BuildToolPlugin { + func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { + let tool = try context.tool(named: "git-info-generator") + let outputFile = context.pluginWorkDirectoryURL.appending(path: "GitInfo.swift") + print(outputFile) + + return [ + .buildCommand( + displayName: "Generate Git Info", + executable: tool.url, + arguments: [outputFile.path()], + outputFiles: [outputFile] + ) + ] + } +} diff --git a/Core/Sources/Core/Metadata/Git.swift b/Core/Sources/Core/Metadata/Git.swift index 04f5b102..cdea2e14 100644 --- a/Core/Sources/Core/Metadata/Git.swift +++ b/Core/Sources/Core/Metadata/Git.swift @@ -1,20 +1,8 @@ -import Metadata - public struct PackageMetadata { public static var gitTag: String? { - let gitTag = String(validatingCString: git_tag_string()) - return if gitTag == "unknown" { - nil - } else { - gitTag - } + gitTagFromPlugin } public static var gitCommit: String? { - let gitCommit = String(validatingCString: git_commit_string()) - return if gitCommit == "unknown" { - nil - } else { - gitCommit - } + gitCommitFromPlugin } } diff --git a/Core/Sources/Metadata/git.c b/Core/Sources/Metadata/git.c deleted file mode 100644 index 2d8775ce..00000000 --- a/Core/Sources/Metadata/git.c +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef GIT_TAG -#define GIT_TAG "unknown" -#endif - -#ifndef GIT_COMMIT -#define GIT_COMMIT "unknown" -#endif - -const char* git_tag_string(void) { - return GIT_TAG; -} - -const char* git_commit_string(void) { - return GIT_COMMIT; -} diff --git a/Core/Sources/Metadata/include/git.h b/Core/Sources/Metadata/include/git.h deleted file mode 100644 index 6f72c3de..00000000 --- a/Core/Sources/Metadata/include/git.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef GIT_H -#define GIT_H - -const char* git_tag_string(void); -const char* git_commit_string(void); - -#endif diff --git a/Core/Sources/git-info-generator/main.swift b/Core/Sources/git-info-generator/main.swift new file mode 100644 index 00000000..d92e0e3d --- /dev/null +++ b/Core/Sources/git-info-generator/main.swift @@ -0,0 +1,35 @@ +import Foundation + +var tag = try? shell("git tag --points-at HEAD") +var commit = try? shell("git rev-parse HEAD") +if tag?.isEmpty == true { + tag = nil +} +if commit?.isEmpty == true { + commit = nil +} + +let outputPath = CommandLine.arguments[1] +let contents = """ +// This file is auto-generated. + +let gitTagFromPlugin: String? = \(tag as String?) +let gitCommitFromPlugin: String? = \(commit as String?) +""" + +try contents.write(toFile: outputPath, atomically: true, encoding: .utf8) + +@discardableResult +func shell(_ command: String) throws -> String { + let process = Process() + let pipe = Pipe() + + process.standardOutput = pipe + process.executableURL = URL(fileURLWithPath: "/bin/bash") + process.arguments = ["-c", command] + + try process.run() + let data = pipe.fileHandleForReading.readDataToEndOfFile() + + return String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines) +} diff --git a/exportOptions.plist b/exportOptions.plist new file mode 100644 index 00000000..d1f14877 --- /dev/null +++ b/exportOptions.plist @@ -0,0 +1,17 @@ + + + + + method + developer-id + signingStyle + automatic + destination + export + stripSwiftSymbols + + compileBitcode + + + \ No newline at end of file diff --git a/pkgbuild.sh b/pkgbuild.sh index f5a7b4cc..034e5b7f 100755 --- a/pkgbuild.sh +++ b/pkgbuild.sh @@ -1,12 +1,60 @@ -set -e +set -ex +PROJECT_NAME="azooKeyMac" +SCHEME="azooKeyMac" +CONFIGURATION="Release" +ARCHIVE_PATH="./build/archive.xcarchive" +EXPORT_PATH="./build/export" +EXPORT_OPTIONS_PLIST="./exportOptions.plist" + +# 1. Clean Build +rm -rf ./build +mkdir -p ./build +rm -rf ./Core/.build +rm -f ./Core/Package.resolved + +# 2. Archive +# Note: use `"$(mktemp -d)` to avoid conflicts with previous builds +xcodebuild \ + -project "${PROJECT_NAME}.xcodeproj" \ + -scheme "${SCHEME}" \ + clean archive \ + -configuration "${CONFIGURATION}" \ + -archivePath "${ARCHIVE_PATH}" \ + -derivedDataPath "$(mktemp -d)" \ + -clonedSourcePackagesDirPath $(mktemp -d) \ + -allowProvisioningUpdates \ + -destination "generic/platform=macOS" + +# 3. Export +xcodebuild -exportArchive \ + -archivePath "${ARCHIVE_PATH}" \ + -exportPath "${EXPORT_PATH}" \ + -exportOptionsPlist "${EXPORT_OPTIONS_PLIST}" \ + -allowProvisioningUpdates + +# 4. Notarize .app +APP_PATH="${EXPORT_PATH}/${PROJECT_NAME}.app" +APP_ZIP="${PROJECT_NAME}.zip" + +# Zip the .app for notarization +ditto -c -k --sequesterRsrc --keepParent "${APP_PATH}" "${APP_ZIP}" + +# Submit the .app (zip) for notarization +xcrun notarytool submit "${APP_ZIP}" --keychain-profile "Notarytool" --wait + +# Staple the notarization ticket to the .app +xcrun stapler staple "${APP_PATH}" + +# Remove the temporary zip +rm "${APP_ZIP}" # Suppose we have build/azooKeyMac.app # Use this script to create a plist package for distribution # pkgbuild --analyze --root ./build/ pkg.plist # Create a temporary package -pkgbuild --root ./build/ \ +pkgbuild --root ${EXPORT_PATH} \ --component-plist pkg.plist --identifier dev.ensan.inputmethod.azooKeyMac \ --version 0 \ --install-location /Library/Input\ Methods \