Skip to content

Commit

Permalink
Support switch between debug and release (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored Jan 1, 2025
1 parent b9d4688 commit 52fa22f
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ jobs:

- name: Build
run: |
# Don't log key on any CI artifact.
cmake -B build -G Ninja \
-DCMAKE_Swift_COMPILER=`which swiftc` \
-DVERBOSE_LOGGING=OFF \
-DCMAKE_OSX_ARCHITECTURES=${{ matrix.arch }} \
-DCMAKE_BUILD_TYPE=${{ matrix.type }}
cmake --build build
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ build
assets/en.lproj/Localizable.strings
assets/po/base.pot
meta.swift
debug.swift
*~
6 changes: 5 additions & 1 deletion assets/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ INSTALL_DIR="/Library/Input Methods"
APP_DIR="$INSTALL_DIR/Fcitx5.app"
RESOURCES_DIR="$APP_DIR/Contents/Resources"

rm -rf "$APP_DIR/Contents/*"
# Don't remove files that must exist (which will be overwritten) as that will put Fcitx5 in a registered-but-not-listed state.
rm -rf "$APP_DIR"/Contents/{bin,lib,share,Resources}
if ls "$APP_DIR"/Contents/MacOS/Fcitx5.*; then # Debug symbols
rm -rf "$APP_DIR"/Contents/MacOS/Fcitx5.*
fi
tar xjvf "$tar_ball" -C "$INSTALL_DIR"
rm -f "$tar_ball"
xattr -dr com.apple.quarantine "$APP_DIR"
Expand Down
Binary file modified assets/zh-Hans.lproj/Localizable.strings
Binary file not shown.
9 changes: 4 additions & 5 deletions logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
set(LOGGING_SRCS logging.swift)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(IS_DEBUG true)
list(APPEND LOGGING_SRCS debug.swift)
else()
set(IS_DEBUG false)
list(APPEND LOGGING_SRCS release.swift)
endif()
configure_file(debug.swift.in ${CMAKE_CURRENT_SOURCE_DIR}/debug.swift @ONLY)

add_library(Logging STATIC logging.swift debug.swift)
add_library(Logging STATIC ${LOGGING_SRCS})
1 change: 1 addition & 0 deletions logging/debug.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public let isDebug = true
1 change: 0 additions & 1 deletion logging/debug.swift.in

This file was deleted.

1 change: 1 addition & 0 deletions logging/release.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public let isDebug = false
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ add_library(Fcitx5Objs STATIC
config/config.cpp
)

option(VERBOSE_LOGGING "Enable verbose logging" ON)
if(VERBOSE_LOGGING)
target_compile_definitions(Fcitx5Objs PRIVATE VERBOSE_LOGGING)
endif()

target_link_libraries(Fcitx5Objs
SwiftyJSON
SwiftFrontend
Expand Down
65 changes: 56 additions & 9 deletions src/config/about.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct AboutView: View {
@State private var showCheckFailed = false
@State private var showDownloadFailed = false
@State private var showInstallFailed = false
@State private var showSwitchToDebug = false
@State private var confirmUninstall = false
@State private var removeUserData = false
@State private var uninstalling = false
Expand All @@ -77,7 +78,13 @@ struct AboutView: View {
.font(.title)

Spacer().frame(height: gapSize)
Text(arch)

HStack {
Text(arch)
if isDebug {
Text("Debug")
}
}

Spacer().frame(height: gapSize)
urlButton(String(commit.prefix(7)), sourceRepo + "/commit/" + commit)
Expand Down Expand Up @@ -110,7 +117,7 @@ struct AboutView: View {
if viewModel.state == .notChecked {
checkUpdate()
} else if viewModel.state == .available {
update()
update(debug: isDebug)
}
} label: {
if viewModel.state == .notChecked || viewModel.state == .upToDate {
Expand Down Expand Up @@ -139,7 +146,7 @@ struct AboutView: View {
VStack {
Text("Update available")
Button {
update()
update(debug: isDebug)
} label: {
Text("Update now")
}.buttonStyle(.borderedProminent)
Expand All @@ -151,11 +158,50 @@ struct AboutView: View {
}.padding()
}

if isDebug {
Button {
update(debug: false)
} label: {
Text("Switch to Release")
}.disabled(
viewModel.state == .downloading || viewModel.state == .installing
)
} else {
Button {
showSwitchToDebug = true
} label: {
Text("Switch to Debug")
}.disabled(
viewModel.state == .downloading || viewModel.state == .installing
).sheet(
isPresented: $showSwitchToDebug
) {
VStack {
Text("Switch to debug only if Fctix5 crashes and you want to help debug.")
HStack {
Button {
showSwitchToDebug = false
} label: {
Text("Cancel")
}
Button {
update(debug: true)
showSwitchToDebug = false
} label: {
Text("OK")
}.buttonStyle(.borderedProminent)
}
}.padding()
}
}

Button {
confirmUninstall = true
} label: {
Text("Uninstall")
}.sheet(
}.disabled(
viewModel.state == .downloading || viewModel.state == .installing
).sheet(
isPresented: $confirmUninstall
) {
VStack {
Expand Down Expand Up @@ -272,15 +318,16 @@ struct AboutView: View {
}.resume()
}

func update() {
func update(debug: Bool) {
viewModel.state = .downloading
checkPluginUpdate({ success, nativePlugins, dataPlugins in
let updater = Updater(main: true, nativePlugins: nativePlugins, dataPlugins: dataPlugins)
let updater = Updater(
main: true, debug: debug, nativePlugins: nativePlugins, dataPlugins: dataPlugins)
updater.update(
// Install plugin in a best-effort manner. No need to check plugin status.
onFinish: { result, _, _ in
if result {
install()
install(debug: debug)
} else {
viewModel.state = .available
showDownloadFailed = true
Expand All @@ -292,7 +339,7 @@ struct AboutView: View {
})
}

func install() {
func install(debug: Bool) {
viewModel.state = .installing
let conditions = NSMutableDictionary()
conditions.setValue("com.apple.keylayout.ABC", forKey: kTISPropertyInputSourceID as String)
Expand All @@ -303,7 +350,7 @@ struct AboutView: View {
TISSelectInputSource(inputSource)
}
}
let path = cacheDir.appendingPathComponent(mainFileName).localPath()
let path = cacheDir.appendingPathComponent(debug ? mainDebugFileName : mainFileName).localPath()
// Necessary to put it in background, otherwise sudo UI will hang if it has been canceled once.
DispatchQueue.global().async {
if !sudo("update", path, updateLog) {
Expand Down
14 changes: 9 additions & 5 deletions src/config/installer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Logging

let mainFileName = "Fcitx5-\(arch).tar.bz2"
private let mainAddress = "\(sourceRepo)/releases/download/latest/\(mainFileName)"
let mainDebugFileName = "Fcitx5-\(arch)-debug.tar.bz2"
let pluginBaseAddress =
"https://github.com/fcitx-contrib/fcitx5-plugins/releases/download/macos/"

Expand Down Expand Up @@ -56,12 +56,14 @@ private func extractPlugin(_ plugin: String, native: Bool) -> Bool {
}

class Updater {
private var main: Bool
private var nativePlugins: [String]
private var dataPlugins: [String]
private let main: Bool
private let debug: Bool
private let nativePlugins: [String]
private let dataPlugins: [String]

init(main: Bool, nativePlugins: [String], dataPlugins: [String]) {
init(main: Bool, debug: Bool, nativePlugins: [String], dataPlugins: [String]) {
self.main = main
self.debug = debug
self.nativePlugins = nativePlugins
self.dataPlugins = dataPlugins
}
Expand All @@ -70,6 +72,8 @@ class Updater {
onFinish: @escaping (Bool, [String: Bool], [String: Bool]) -> Void,
onProgress: ((Double) -> Void)? = nil
) {
let mainAddress =
"\(sourceRepo)/releases/download/latest/\(self.debug ? mainDebugFileName : mainFileName)"
let downloader = Downloader(
nativePlugins.map({ getAddress($0, native: true) })
+ dataPlugins.map({ getAddress($0, native: false) }) + (main ? [mainAddress] : [])
Expand Down
3 changes: 2 additions & 1 deletion src/config/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ struct PluginView: View {
let selectedPlugins = selectedAvailable
selectedAvailable.removeAll()

let updater = Updater(main: false, nativePlugins: nativeAvailable, dataPlugins: dataAvailable)
let updater = Updater(
main: false, debug: false, nativePlugins: nativeAvailable, dataPlugins: dataAvailable)
updater.update(
onFinish: { _, nativeResults, dataResults in
processing = false
Expand Down
14 changes: 7 additions & 7 deletions src/fcitx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Fcitx &Fcitx::shared() {
}

Fcitx::Fcitx() {
setupLog(true);
setupLog();
setupEnv();
}

Expand Down Expand Up @@ -99,15 +99,15 @@ void Fcitx::teardown() {
instance_.reset();
}

void Fcitx::setupLog(bool verbose) {
void Fcitx::setupLog() {
static native_streambuf log_streambuf;
static std::ostream stream(&log_streambuf);
fcitx::Log::setLogStream(stream);
if (verbose) {
fcitx::Log::setLogRule("*=5,notimedate");
} else {
fcitx::Log::setLogRule("notimedate");
}
#ifdef VERBOSE_LOGGING
fcitx::Log::setLogRule("*=5,notimedate");
#else
fcitx::Log::setLogRule("*=4,notimedate");
#endif
}

void Fcitx::setupEnv() {
Expand Down
2 changes: 1 addition & 1 deletion src/fcitx.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Fcitx final {
fcitx::MacosFrontend *frontend();

private:
void setupLog(bool verbose);
void setupLog();
void setupEnv();
void setupInstance();

Expand Down

0 comments on commit 52fa22f

Please sign in to comment.