Skip to content

Commit 52fa22f

Browse files
authored
Support switch between debug and release (#216)
1 parent b9d4688 commit 52fa22f

File tree

14 files changed

+93
-31
lines changed

14 files changed

+93
-31
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ jobs:
6868

6969
- name: Build
7070
run: |
71+
# Don't log key on any CI artifact.
7172
cmake -B build -G Ninja \
7273
-DCMAKE_Swift_COMPILER=`which swiftc` \
74+
-DVERBOSE_LOGGING=OFF \
7375
-DCMAKE_OSX_ARCHITECTURES=${{ matrix.arch }} \
7476
-DCMAKE_BUILD_TYPE=${{ matrix.type }}
7577
cmake --build build

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ build
88
assets/en.lproj/Localizable.strings
99
assets/po/base.pot
1010
meta.swift
11-
debug.swift
1211
*~

assets/update.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ INSTALL_DIR="/Library/Input Methods"
77
APP_DIR="$INSTALL_DIR/Fcitx5.app"
88
RESOURCES_DIR="$APP_DIR/Contents/Resources"
99

10-
rm -rf "$APP_DIR/Contents/*"
10+
# Don't remove files that must exist (which will be overwritten) as that will put Fcitx5 in a registered-but-not-listed state.
11+
rm -rf "$APP_DIR"/Contents/{bin,lib,share,Resources}
12+
if ls "$APP_DIR"/Contents/MacOS/Fcitx5.*; then # Debug symbols
13+
rm -rf "$APP_DIR"/Contents/MacOS/Fcitx5.*
14+
fi
1115
tar xjvf "$tar_ball" -C "$INSTALL_DIR"
1216
rm -f "$tar_ball"
1317
xattr -dr com.apple.quarantine "$APP_DIR"
702 Bytes
Binary file not shown.

logging/CMakeLists.txt

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
set(LOGGING_SRCS logging.swift)
12
if(CMAKE_BUILD_TYPE MATCHES "Debug")
2-
set(IS_DEBUG true)
3+
list(APPEND LOGGING_SRCS debug.swift)
34
else()
4-
set(IS_DEBUG false)
5+
list(APPEND LOGGING_SRCS release.swift)
56
endif()
6-
configure_file(debug.swift.in ${CMAKE_CURRENT_SOURCE_DIR}/debug.swift @ONLY)
7-
8-
add_library(Logging STATIC logging.swift debug.swift)
7+
add_library(Logging STATIC ${LOGGING_SRCS})

logging/debug.swift

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public let isDebug = true

logging/debug.swift.in

-1
This file was deleted.

logging/release.swift

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public let isDebug = false

src/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ add_library(Fcitx5Objs STATIC
1616
config/config.cpp
1717
)
1818

19+
option(VERBOSE_LOGGING "Enable verbose logging" ON)
20+
if(VERBOSE_LOGGING)
21+
target_compile_definitions(Fcitx5Objs PRIVATE VERBOSE_LOGGING)
22+
endif()
23+
1924
target_link_libraries(Fcitx5Objs
2025
SwiftyJSON
2126
SwiftFrontend

src/config/about.swift

+56-9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct AboutView: View {
5959
@State private var showCheckFailed = false
6060
@State private var showDownloadFailed = false
6161
@State private var showInstallFailed = false
62+
@State private var showSwitchToDebug = false
6263
@State private var confirmUninstall = false
6364
@State private var removeUserData = false
6465
@State private var uninstalling = false
@@ -77,7 +78,13 @@ struct AboutView: View {
7778
.font(.title)
7879

7980
Spacer().frame(height: gapSize)
80-
Text(arch)
81+
82+
HStack {
83+
Text(arch)
84+
if isDebug {
85+
Text("Debug")
86+
}
87+
}
8188

8289
Spacer().frame(height: gapSize)
8390
urlButton(String(commit.prefix(7)), sourceRepo + "/commit/" + commit)
@@ -110,7 +117,7 @@ struct AboutView: View {
110117
if viewModel.state == .notChecked {
111118
checkUpdate()
112119
} else if viewModel.state == .available {
113-
update()
120+
update(debug: isDebug)
114121
}
115122
} label: {
116123
if viewModel.state == .notChecked || viewModel.state == .upToDate {
@@ -139,7 +146,7 @@ struct AboutView: View {
139146
VStack {
140147
Text("Update available")
141148
Button {
142-
update()
149+
update(debug: isDebug)
143150
} label: {
144151
Text("Update now")
145152
}.buttonStyle(.borderedProminent)
@@ -151,11 +158,50 @@ struct AboutView: View {
151158
}.padding()
152159
}
153160

161+
if isDebug {
162+
Button {
163+
update(debug: false)
164+
} label: {
165+
Text("Switch to Release")
166+
}.disabled(
167+
viewModel.state == .downloading || viewModel.state == .installing
168+
)
169+
} else {
170+
Button {
171+
showSwitchToDebug = true
172+
} label: {
173+
Text("Switch to Debug")
174+
}.disabled(
175+
viewModel.state == .downloading || viewModel.state == .installing
176+
).sheet(
177+
isPresented: $showSwitchToDebug
178+
) {
179+
VStack {
180+
Text("Switch to debug only if Fctix5 crashes and you want to help debug.")
181+
HStack {
182+
Button {
183+
showSwitchToDebug = false
184+
} label: {
185+
Text("Cancel")
186+
}
187+
Button {
188+
update(debug: true)
189+
showSwitchToDebug = false
190+
} label: {
191+
Text("OK")
192+
}.buttonStyle(.borderedProminent)
193+
}
194+
}.padding()
195+
}
196+
}
197+
154198
Button {
155199
confirmUninstall = true
156200
} label: {
157201
Text("Uninstall")
158-
}.sheet(
202+
}.disabled(
203+
viewModel.state == .downloading || viewModel.state == .installing
204+
).sheet(
159205
isPresented: $confirmUninstall
160206
) {
161207
VStack {
@@ -272,15 +318,16 @@ struct AboutView: View {
272318
}.resume()
273319
}
274320

275-
func update() {
321+
func update(debug: Bool) {
276322
viewModel.state = .downloading
277323
checkPluginUpdate({ success, nativePlugins, dataPlugins in
278-
let updater = Updater(main: true, nativePlugins: nativePlugins, dataPlugins: dataPlugins)
324+
let updater = Updater(
325+
main: true, debug: debug, nativePlugins: nativePlugins, dataPlugins: dataPlugins)
279326
updater.update(
280327
// Install plugin in a best-effort manner. No need to check plugin status.
281328
onFinish: { result, _, _ in
282329
if result {
283-
install()
330+
install(debug: debug)
284331
} else {
285332
viewModel.state = .available
286333
showDownloadFailed = true
@@ -292,7 +339,7 @@ struct AboutView: View {
292339
})
293340
}
294341

295-
func install() {
342+
func install(debug: Bool) {
296343
viewModel.state = .installing
297344
let conditions = NSMutableDictionary()
298345
conditions.setValue("com.apple.keylayout.ABC", forKey: kTISPropertyInputSourceID as String)
@@ -303,7 +350,7 @@ struct AboutView: View {
303350
TISSelectInputSource(inputSource)
304351
}
305352
}
306-
let path = cacheDir.appendingPathComponent(mainFileName).localPath()
353+
let path = cacheDir.appendingPathComponent(debug ? mainDebugFileName : mainFileName).localPath()
307354
// Necessary to put it in background, otherwise sudo UI will hang if it has been canceled once.
308355
DispatchQueue.global().async {
309356
if !sudo("update", path, updateLog) {

src/config/installer.swift

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import Logging
33

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

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

5858
class Updater {
59-
private var main: Bool
60-
private var nativePlugins: [String]
61-
private var dataPlugins: [String]
59+
private let main: Bool
60+
private let debug: Bool
61+
private let nativePlugins: [String]
62+
private let dataPlugins: [String]
6263

63-
init(main: Bool, nativePlugins: [String], dataPlugins: [String]) {
64+
init(main: Bool, debug: Bool, nativePlugins: [String], dataPlugins: [String]) {
6465
self.main = main
66+
self.debug = debug
6567
self.nativePlugins = nativePlugins
6668
self.dataPlugins = dataPlugins
6769
}
@@ -70,6 +72,8 @@ class Updater {
7072
onFinish: @escaping (Bool, [String: Bool], [String: Bool]) -> Void,
7173
onProgress: ((Double) -> Void)? = nil
7274
) {
75+
let mainAddress =
76+
"\(sourceRepo)/releases/download/latest/\(self.debug ? mainDebugFileName : mainFileName)"
7377
let downloader = Downloader(
7478
nativePlugins.map({ getAddress($0, native: true) })
7579
+ dataPlugins.map({ getAddress($0, native: false) }) + (main ? [mainAddress] : [])

src/config/plugin.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ struct PluginView: View {
222222
let selectedPlugins = selectedAvailable
223223
selectedAvailable.removeAll()
224224

225-
let updater = Updater(main: false, nativePlugins: nativeAvailable, dataPlugins: dataAvailable)
225+
let updater = Updater(
226+
main: false, debug: false, nativePlugins: nativeAvailable, dataPlugins: dataAvailable)
226227
updater.update(
227228
onFinish: { _, nativeResults, dataResults in
228229
processing = false

src/fcitx.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Fcitx &Fcitx::shared() {
3838
}
3939

4040
Fcitx::Fcitx() {
41-
setupLog(true);
41+
setupLog();
4242
setupEnv();
4343
}
4444

@@ -99,15 +99,15 @@ void Fcitx::teardown() {
9999
instance_.reset();
100100
}
101101

102-
void Fcitx::setupLog(bool verbose) {
102+
void Fcitx::setupLog() {
103103
static native_streambuf log_streambuf;
104104
static std::ostream stream(&log_streambuf);
105105
fcitx::Log::setLogStream(stream);
106-
if (verbose) {
107-
fcitx::Log::setLogRule("*=5,notimedate");
108-
} else {
109-
fcitx::Log::setLogRule("notimedate");
110-
}
106+
#ifdef VERBOSE_LOGGING
107+
fcitx::Log::setLogRule("*=5,notimedate");
108+
#else
109+
fcitx::Log::setLogRule("*=4,notimedate");
110+
#endif
111111
}
112112

113113
void Fcitx::setupEnv() {

src/fcitx.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Fcitx final {
3030
fcitx::MacosFrontend *frontend();
3131

3232
private:
33-
void setupLog(bool verbose);
33+
void setupLog();
3434
void setupEnv();
3535
void setupInstance();
3636

0 commit comments

Comments
 (0)