Commit cd3a975e authored by zpc's avatar zpc

Restore inline selectors and compact release logs; allow closing idle panes

parent 12947a88
...@@ -19,9 +19,9 @@ ...@@ -19,9 +19,9 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>1.4.0</string> <string>1.4.1</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>10</string> <string>11</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>
<string>14.0</string> <string>14.0</string>
<key>LSUIElement</key> <key>LSUIElement</key>
......
...@@ -18,6 +18,9 @@ private struct SavedPane: Codable { ...@@ -18,6 +18,9 @@ private struct SavedPane: Codable {
@Observable @Observable
final class ReleasePaneState: Identifiable { final class ReleasePaneState: Identifiable {
enum State: String { case idle, queued, running, finished, skipped } enum State: String { case idle, queued, running, finished, skipped }
enum ConfigurationField { case project, configuration, server, module }
var expandedField: ConfigurationField?
var expandedConsole = false
let id: UUID let id: UUID
var projectID: UUID var projectID: UUID
...@@ -136,6 +139,7 @@ final class SplitReleaseModel { ...@@ -136,6 +139,7 @@ final class SplitReleaseModel {
private var batch = false private var batch = false
private var unread = false private var unread = false
private var runIDs = Set<UUID>() private var runIDs = Set<UUID>()
private var runResults: [UUID: ReleaseResult] = [:]
private var batchStarted: Date? private var batchStarted: Date?
private var completionTask: Task<Void, Never>? private var completionTask: Task<Void, Never>?
private(set) var completionDuration: Int? private(set) var completionDuration: Int?
...@@ -181,18 +185,18 @@ final class SplitReleaseModel { ...@@ -181,18 +185,18 @@ final class SplitReleaseModel {
var current: ReleasePaneState? { panes.first { $0.state == .running } } var current: ReleasePaneState? { panes.first { $0.state == .running } }
var progressPercentage: Int? { var progressPercentage: Int? {
let active = panes.filter { runIDs.contains($0.id) } let active = panes.filter { runIDs.contains($0.id) }
guard isRunning, !active.isEmpty else { return nil } guard isRunning, !runIDs.isEmpty else { return nil }
let completed = active.filter { $0.state == .finished }.count let completed = runResults.count
let partial = active.filter { $0.state == .running }.reduce(0) { let partial = active.filter { $0.state == .running }.reduce(0) {
$0 + ($1.phase?.estimatedPercentage ?? 0) $0 + ($1.phase?.estimatedPercentage ?? 0)
} }
return min(99, (completed * 100 + partial) / active.count) return min(99, (completed * 100 + partial) / runIDs.count)
} }
var indicator: ReleaseStatus? { var indicator: ReleaseStatus? {
if isRunning { return nil } if isRunning { return nil }
if completionTask != nil { return .succeeded } if completionTask != nil { return .succeeded }
if unread { if unread {
return panes.filter { runIDs.contains($0.id) }.compactMap(\.result).first(where: { return runResults.values.first(where: {
$0.status != .succeeded $0.status != .succeeded
})?.status })?.status
} }
...@@ -227,7 +231,7 @@ final class SplitReleaseModel { ...@@ -227,7 +231,7 @@ final class SplitReleaseModel {
} }
func remove(_ pane: ReleasePaneState) { func remove(_ pane: ReleasePaneState) {
guard !isRunning else { return } guard pane.state != .running && pane.state != .queued else { return }
pane.inventoryTask?.cancel() pane.inventoryTask?.cancel()
panes.removeAll { $0.id == pane.id } panes.removeAll { $0.id == pane.id }
save() save()
...@@ -320,6 +324,7 @@ final class SplitReleaseModel { ...@@ -320,6 +324,7 @@ final class SplitReleaseModel {
batch = together batch = together
batchStarted = Date() batchStarted = Date()
runIDs = Set(prepared.map { $0.pane.id }) runIDs = Set(prepared.map { $0.pane.id })
runResults.removeAll()
if together, prepared.count > 1 { if together, prepared.count > 1 {
let cityJobs = prepared.filter { let cityJobs = prepared.filter {
$0.project.type == .cityKeeper && $0.action == .standard $0.project.type == .cityKeeper && $0.action == .standard
...@@ -336,6 +341,8 @@ final class SplitReleaseModel { ...@@ -336,6 +341,8 @@ final class SplitReleaseModel {
} }
for job in prepared { for job in prepared {
let pane = job.pane let pane = job.pane
pane.expandedField = nil
pane.expandedConsole = false
pane.consoleDismissed = false pane.consoleDismissed = false
pane.viewingLog = false pane.viewingLog = false
pane.state = .queued pane.state = .queued
...@@ -408,6 +415,7 @@ final class SplitReleaseModel { ...@@ -408,6 +415,7 @@ final class SplitReleaseModel {
pane.log = control.liveOutput() pane.log = control.liveOutput()
pane.control = nil pane.control = nil
pane.result = result pane.result = result
runResults[pane.id] = result
pane.state = .finished pane.state = .finished
pane.phase = nil pane.phase = nil
pane.paused = false pane.paused = false
...@@ -426,9 +434,8 @@ final class SplitReleaseModel { ...@@ -426,9 +434,8 @@ final class SplitReleaseModel {
startAvailable() startAvailable()
} else { } else {
unread = job.action != .preflight unread = job.action != .preflight
let finished = panes.filter { runIDs.contains($0.id) }
if job.action != .preflight, if job.action != .preflight,
finished.allSatisfy({ $0.result?.status == .succeeded }) runIDs.allSatisfy({ runResults[$0]?.status == .succeeded })
{ {
let duration = Int(batchStarted?.distance(to: Date()) ?? 0) let duration = Int(batchStarted?.distance(to: Date()) ?? 0)
showCompletion(duration: duration) showCompletion(duration: duration)
......
...@@ -27,23 +27,50 @@ struct SplitReleasePanel: View { ...@@ -27,23 +27,50 @@ struct SplitReleasePanel: View {
} }
private var columnHeight: CGFloat { private var columnHeight: CGFloat {
let desired: CGFloat = let base: CGFloat = model.panes.count == 1 ? 460 : 500
model.panes.contains(where: \.showsConsole) ? 650 : (model.panes.count == 1 ? 440 : 490) let desired =
model.panes.map { pane -> CGFloat in
if pane.showsConsole { return pane.expandedConsole ? 650 : base }
guard let field = pane.expandedField else { return base }
let extra: CGFloat
switch field {
case .project:
extra = min(150, CGFloat(model.settings.projects.count) * 34 + 8) + 8
case .configuration:
let project = model.project(for: pane)
let count =
((try? project?.configurationFiles()) ?? project?.profiles.map(\.file) ?? [])
.count
extra = min(160, CGFloat(count) * 46 + 8) + 50
case .server:
extra = max(52, min(170, CGFloat(pane.servers.count) * 38 + 8)) + 8
case .module:
extra = max(44, min(220, CGFloat(pane.availableModules.count) * 31 + 8)) + 44
}
return min(650, base + extra)
}.max() ?? base
return min(desired, max(380, (NSScreen.main?.visibleFrame.height ?? 900) - 160)) return min(desired, max(380, (NSScreen.main?.visibleFrame.height ?? 900) - 160))
} }
var body: some View { var body: some View {
VStack(spacing: 0) { VStack(spacing: 0) {
HStack(spacing: 12) { HStack(spacing: 12) {
ZStack { if model.isRunning {
Circle().stroke(ReleaseColors.blue.opacity(0.25), lineWidth: 3) ZStack {
Circle().trim(from: 0, to: 0.76) Circle().stroke(ReleaseColors.blue.opacity(0.25), lineWidth: 3)
.stroke( Circle().trim(from: 0, to: 0.76)
ReleaseColors.blue, style: StrokeStyle(lineWidth: 3, lineCap: .round) .stroke(
) ReleaseColors.blue,
.rotationEffect(.degrees(-90)) style: StrokeStyle(lineWidth: 3, lineCap: .round)
)
.rotationEffect(.degrees(-90))
}
.frame(width: 32, height: 32)
} else {
Image(systemName: "shippingbox")
.font(.system(size: 26)).foregroundStyle(.white.opacity(0.9))
.frame(width: 32, height: 32)
} }
.frame(width: 32, height: 32)
Text("Java 发布").font(.system(size: 23, weight: .semibold)) Text("Java 发布").font(.system(size: 23, weight: .semibold))
Spacer() Spacer()
Button(action: model.add) { Button(action: model.add) {
...@@ -155,10 +182,6 @@ private struct ProjectReleaseColumn: View { ...@@ -155,10 +182,6 @@ private struct ProjectReleaseColumn: View {
@Bindable var pane: ReleasePaneState @Bindable var pane: ReleasePaneState
let height: CGFloat let height: CGFloat
@State private var configurationDraft: DeployProfile? @State private var configurationDraft: DeployProfile?
@State private var showsModules = false
@State private var showsConfiguration = false
@State private var showsProjects = false
@State private var showsServers = false
@FocusState private var moduleSearchFocused: Bool @FocusState private var moduleSearchFocused: Bool
private var project: ReleaseProject? { model.project(for: pane) } private var project: ReleaseProject? { model.project(for: pane) }
...@@ -187,7 +210,7 @@ private struct ProjectReleaseColumn: View { ...@@ -187,7 +210,7 @@ private struct ProjectReleaseColumn: View {
.padding(6) .padding(6)
} }
.buttonStyle(ReleaseButtonStyle()) .buttonStyle(ReleaseButtonStyle())
.disabled(!editable) .disabled(pane.state == .running || pane.state == .queued)
} }
} }
if pane.showsConsole { if pane.showsConsole {
...@@ -200,120 +223,93 @@ private struct ProjectReleaseColumn: View { ...@@ -200,120 +223,93 @@ private struct ProjectReleaseColumn: View {
.frame(height: height) .frame(height: height)
.background(pane.state == .running ? ReleaseColors.blue.opacity(0.09) : .clear) .background(pane.state == .running ? ReleaseColors.blue.opacity(0.09) : .clear)
.font(ReleaseTypography.body) .font(ReleaseTypography.body)
.popover(item: $configurationDraft) { draft in
ConfigurationEditor(profile: draft) { updated in
saveProfile(updated)
configurationDraft = nil
}
}
} }
private var configuration: some View { private var configuration: some View {
VStack(alignment: .leading, spacing: 16) { ScrollView(.vertical) {
VStack(spacing: 0) { VStack(alignment: .leading, spacing: 16) {
formRow("项目", symbol: "folder", value: project?.name ?? "选择项目") { VStack(spacing: 0) {
showsProjects = true formRow(.project, "项目", symbol: "folder", value: project?.name ?? "选择项目")
} if pane.expandedField == .project { projectChoices }
.popover(isPresented: $showsProjects) { if project?.type == .cityKeeper {
VStack(alignment: .leading, spacing: 12) { rowDivider
Text("选择项目").font(.headline) formRow(
ForEach( .configuration, "发布配置", symbol: "doc.text",
model.settings.projects.filter { candidate in value: profile?.title ?? "选择配置")
!model.panes.contains { if pane.expandedField == .configuration { configurationChoices }
$0.id != pane.id && $0.projectID == candidate.id rowDivider
} formRow(.server, "服务器", symbol: "server.rack", value: serverTitle)
} if pane.expandedField == .server { serverChoices }
) { candidate in
Button(candidate.name) {
showsProjects = false
model.replace(pane, with: candidate)
}.buttonStyle(ReleaseButtonStyle())
}
}.padding(18).frame(minWidth: 240, alignment: .leading)
.environment(\.colorScheme, .dark)
}
if project?.type == .cityKeeper {
rowDivider
formRow("发布配置", symbol: "doc.text", value: profile?.title ?? "选择配置") {
showsConfiguration = true
} }
.popover(isPresented: $showsConfiguration) { configurationControls }
rowDivider rowDivider
formRow("服务器", symbol: "server.rack", value: serverTitle) { formRow(.module, "模块", symbol: "square.stack", value: moduleTitle)
showsServers = true if pane.expandedField == .module { modulePicker }
} if project?.type == .cityKeeper {
.popover(isPresented: $showsServers) { rowDivider
ScrollView { HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 12) { Image(systemName: "hammer").foregroundStyle(ReleaseColors.muted).frame(
Text("选择服务器").font(.headline) width: 20)
ForEach(pane.servers, id: \.name) { target in Text("Maven").foregroundStyle(ReleaseColors.muted).frame(
Button(target.displayName) { width: 66, alignment: .leading)
pane.server = target.name Picker("Maven", selection: $pane.mavenCommand) {
model.save() Text("mvnd").tag(MavenCommand.mvnd)
showsServers = false Text("mvn").tag(MavenCommand.mvn)
}.buttonStyle(ReleaseButtonStyle()) }
} .labelsHidden().pickerStyle(.segmented)
if pane.servers.isEmpty { Text("未读取到服务器,请检查项目设置中的 hosts 文件。") } .onChange(of: pane.mavenCommand) { model.save() }
}.padding(18) }.padding(.horizontal, 14).frame(height: 48)
}.frame(width: 310, height: 240)
.environment(\.colorScheme, .dark)
} }
} }
rowDivider .background(ReleaseColors.surface, in: RoundedRectangle(cornerRadius: 14))
formRow("模块", symbol: "square.stack", value: moduleTitle) { showsModules = true } .clipShape(RoundedRectangle(cornerRadius: 14))
.popover(isPresented: $showsModules) { modulePicker } .overlay {
RoundedRectangle(cornerRadius: 14).strokeBorder(Color.white.opacity(0.13))
.allowsHitTesting(false)
}
.disabled(!editable)
if project?.type == .cityKeeper { if project?.type == .cityKeeper {
rowDivider Toggle("使用 VPC 镜像地址", isOn: $pane.useVPC)
HStack(spacing: 12) { .toggleStyle(.checkbox).disabled(!editable)
Image(systemName: "hammer").foregroundStyle(ReleaseColors.muted).frame( .onChange(of: pane.useVPC) { model.save() }
width: 20) }
Text("Maven").foregroundStyle(ReleaseColors.muted).frame( if let project, project.supportsVerifiedRelease(profile) {
width: 66, alignment: .leading) HStack {
Picker("Maven", selection: $pane.mavenCommand) { Toggle("验证发布", isOn: $pane.verified).toggleStyle(.checkbox)
Text("mvnd").tag(MavenCommand.mvnd) .onChange(of: pane.verified) { model.save() }
Text("mvn").tag(MavenCommand.mvn) Spacer()
Button("预检") { model.publish(pane, action: .preflight) }
.buttonStyle(ReleaseButtonStyle())
.disabled(pane.modules.isEmpty)
}.disabled(!editable)
}
publishButton(title: "发布 \(pane.selectedCount) 个模块")
if let message = pane.error {
Text(message).font(.caption).foregroundStyle(.red).lineLimit(3)
}
if pane.state == .skipped {
Text("前序项目失败,本项目未执行").foregroundStyle(.orange)
}
if let result = pane.result {
HStack(alignment: .top, spacing: 8) {
Image(
systemName: result.status == .succeeded
? "checkmark.circle.fill" : "exclamationmark.circle.fill"
)
.foregroundStyle(result.status == .succeeded ? .green : .red)
VStack(alignment: .leading, spacing: 4) {
Text("\(result.headline) · 耗时 \(result.duration) 秒")
Text(result.module).font(.caption).foregroundStyle(ReleaseColors.muted)
.lineLimit(1)
} }
.labelsHidden().pickerStyle(.segmented) Spacer(minLength: 0)
.onChange(of: pane.mavenCommand) { model.save() } Button("查看日志") { pane.showLog() }.buttonStyle(ReleaseButtonStyle())
}.padding(.horizontal, 14).frame(height: 48) }.font(ReleaseTypography.detail)
} else {
Text("选择模块后即可发布").font(ReleaseTypography.detail).foregroundStyle(
ReleaseColors.muted)
} }
} }.padding(.bottom, 4)
.background(ReleaseColors.surface, in: RoundedRectangle(cornerRadius: 14))
.overlay { RoundedRectangle(cornerRadius: 14).strokeBorder(Color.white.opacity(0.13)) }
.disabled(!editable)
if project?.type == .cityKeeper {
Toggle("使用 VPC 镜像地址", isOn: $pane.useVPC)
.toggleStyle(.checkbox).disabled(!editable)
.onChange(of: pane.useVPC) { model.save() }
}
publishButton(title: "发布 \(pane.selectedCount) 个模块")
if let message = pane.error {
Text(message).font(.caption).foregroundStyle(.red).lineLimit(3)
}
if pane.state == .skipped {
Text("前序项目失败,本项目未执行").foregroundStyle(.orange)
}
if let result = pane.result {
HStack(alignment: .top, spacing: 8) {
Image(
systemName: result.status == .succeeded
? "checkmark.circle.fill" : "exclamationmark.circle.fill"
)
.foregroundStyle(result.status == .succeeded ? .green : .red)
VStack(alignment: .leading, spacing: 4) {
Text("\(result.headline) · 耗时 \(result.duration) 秒")
Text(result.module).font(.caption).foregroundStyle(ReleaseColors.muted)
.lineLimit(1)
}
Spacer(minLength: 0)
Button("查看日志") { pane.showLog() }.buttonStyle(ReleaseButtonStyle())
}.font(ReleaseTypography.detail)
} else {
Text("选择模块后即可发布").font(ReleaseTypography.detail).foregroundStyle(
ReleaseColors.muted)
}
Spacer(minLength: 0)
} }
} }
...@@ -328,19 +324,26 @@ private struct ProjectReleaseColumn: View { ...@@ -328,19 +324,26 @@ private struct ProjectReleaseColumn: View {
Rectangle().fill(Color.white.opacity(0.12)).frame(height: 1).padding(.horizontal, 14) Rectangle().fill(Color.white.opacity(0.12)).frame(height: 1).padding(.horizontal, 14)
} }
private func formRow( private func formRow(
_ label: String, symbol: String, value: String, action: @escaping () -> Void _ field: ReleasePaneState.ConfigurationField, _ label: String, symbol: String, value: String
) -> some View { ) -> some View {
Button(action: action) { Button {
configurationDraft = nil
pane.expandedField = pane.expandedField == field ? nil : field
} label: {
HStack(spacing: 12) { HStack(spacing: 12) {
Image(systemName: symbol).foregroundStyle(ReleaseColors.muted).frame(width: 20) Image(systemName: symbol).foregroundStyle(ReleaseColors.muted).frame(width: 20)
Text(label).foregroundStyle(ReleaseColors.muted).frame( Text(label).foregroundStyle(ReleaseColors.muted).frame(
width: 66, alignment: .leading) width: 66, alignment: .leading)
Text(value).foregroundStyle(.white.opacity(0.9)).lineLimit(1).truncationMode(.tail) Text(value).foregroundStyle(.white.opacity(0.9)).lineLimit(1).truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
Image(systemName: "chevron.down").font(.system(size: 11)).foregroundStyle( Image(systemName: pane.expandedField == field ? "chevron.up" : "chevron.down").font(
.system(size: 11)
).foregroundStyle(
ReleaseColors.muted) ReleaseColors.muted)
}.padding(.horizontal, 14).frame(height: 50).contentShape(Rectangle()) }.padding(.horizontal, 14).frame(height: 50).contentShape(Rectangle())
}.buttonStyle(ReleaseButtonStyle()) }.buttonStyle(ReleaseButtonStyle(radius: 0))
.accessibilityLabel(label)
.accessibilityValue(value)
} }
private func publishButton(title: String) -> some View { private func publishButton(title: String) -> some View {
Button { Button {
...@@ -373,6 +376,15 @@ private struct ProjectReleaseColumn: View { ...@@ -373,6 +376,15 @@ private struct ProjectReleaseColumn: View {
Text("实时输出 · \(paneTitle)").font(.system(size: 13, weight: .medium)).lineLimit( Text("实时输出 · \(paneTitle)").font(.system(size: 13, weight: .medium)).lineLimit(
1) 1)
Spacer(minLength: 4) Spacer(minLength: 4)
Button {
pane.expandedConsole.toggle()
} label: {
Image(
systemName: pane.expandedConsole
? "arrow.down.right.and.arrow.up.left"
: "arrow.up.left.and.arrow.down.right")
}
.help(pane.expandedConsole ? "收起日志" : "展开日志")
if pane.state == .running { if pane.state == .running {
Button(pane.paused ? "继续" : "暂停") { model.togglePause(pane) }.disabled( Button(pane.paused ? "继续" : "暂停") { model.togglePause(pane) }.disabled(
pane.stopping) pane.stopping)
...@@ -410,12 +422,6 @@ private struct ProjectReleaseColumn: View { ...@@ -410,12 +422,6 @@ private struct ProjectReleaseColumn: View {
private var modulePicker: some View { private var modulePicker: some View {
VStack(alignment: .leading, spacing: 10) { VStack(alignment: .leading, spacing: 10) {
HStack {
Text("选择模块").font(.headline)
Spacer()
Button("完成") { showsModules = false }
}
TextField("输入模块名搜索…", text: $pane.search) TextField("输入模块名搜索…", text: $pane.search)
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
.focused($moduleSearchFocused) .focused($moduleSearchFocused)
...@@ -457,9 +463,7 @@ private struct ProjectReleaseColumn: View { ...@@ -457,9 +463,7 @@ private struct ProjectReleaseColumn: View {
ReleaseColors.terminal, in: RoundedRectangle(cornerRadius: 7)) ReleaseColors.terminal, in: RoundedRectangle(cornerRadius: 7))
} }
.padding(12) .padding([.horizontal, .bottom], 8)
.frame(width: 310)
.environment(\.colorScheme, .dark)
} }
private var configurationSummary: String { private var configurationSummary: String {
...@@ -469,85 +473,142 @@ private struct ProjectReleaseColumn: View { ...@@ -469,85 +473,142 @@ private struct ProjectReleaseColumn: View {
return "\(configuration) · \(destination) · \(pane.selectedCount) 个模块" return "\(configuration) · \(destination) · \(pane.selectedCount) 个模块"
} }
private var configurationControls: some View { private var projectChoices: some View {
VStack(alignment: .leading, spacing: 14) { let available = model.settings.projects.filter { candidate in
Text("发布选项").font(.headline) !model.panes.contains { $0.id != pane.id && $0.projectID == candidate.id }
if let project { }
if project.type == .cityKeeper { return ScrollView {
menuRow("发布配置", value: profile?.title ?? "选择配置文件") { VStack(spacing: 2) {
let files = ForEach(available) { candidate in
(try? project.configurationFiles()) choiceButton(candidate.name, selected: candidate.id == pane.projectID) {
?? project.profiles.map(\.file) pane.expandedField = nil
ForEach(files, id: \.self) { file in model.replace(pane, with: candidate)
Button(
project.profiles.first(where: { $0.file == file })?.title
?? file
) {
selectConfiguration(file, in: project)
}
}
Divider()
Button("浏览其他 YAML…") { browseConfiguration(in: project) }
} }
menuRow( }
"服务器", }.padding(4)
value: pane.servers.first(where: { $0.name == pane.server })? }
.displayName ?? "选择服务器" .frame(height: min(150, CGFloat(available.count) * 34 + 8))
) { .background(ReleaseColors.terminal, in: RoundedRectangle(cornerRadius: 7))
ForEach(pane.servers, id: \.name) { target in .padding([.horizontal, .bottom], 8)
Button(target.displayName) { }
pane.server = target.name
model.save() private var serverChoices: some View {
} ScrollView {
} VStack(spacing: 2) {
ForEach(pane.servers, id: \.name) { target in
choiceButton(target.displayName, selected: target.name == pane.server) {
pane.server = target.name
pane.expandedField = nil
model.save()
} }
menuRow("Maven", value: pane.mavenCommand.title) { }
ForEach([MavenCommand.mvnd, .mvn]) { command in if pane.servers.isEmpty {
Button(command.title) { Text("未读取到服务器,请检查项目设置中的 hosts 文件。")
pane.mavenCommand = command .font(.caption).foregroundStyle(ReleaseColors.muted).padding(10)
model.save() }
}.padding(4)
}
.frame(height: max(52, min(170, CGFloat(pane.servers.count) * 38 + 8)))
.background(ReleaseColors.terminal, in: RoundedRectangle(cornerRadius: 7))
.padding([.horizontal, .bottom], 8)
}
private func choiceButton(_ title: String, selected: Bool, action: @escaping () -> Void)
-> some View
{
Button(action: action) {
HStack(spacing: 8) {
Image(systemName: selected ? "largecircle.fill.circle" : "circle")
.foregroundStyle(selected ? ReleaseColors.blue : ReleaseColors.muted)
Text(title).foregroundStyle(.primary).fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
}.padding(.horizontal, 8).padding(.vertical, 7)
}
.buttonStyle(
ReleaseButtonStyle(
base: selected ? ReleaseColors.blue.opacity(0.12) : .clear, radius: 5)
)
.accessibilityAddTraits(selected ? .isSelected : [])
}
private var configurationChoices: some View {
let files = (try? project?.configurationFiles()) ?? project?.profiles.map(\.file) ?? []
return VStack(alignment: .leading, spacing: 6) {
if !files.isEmpty {
ScrollView {
VStack(spacing: 2) {
ForEach(files, id: \.self) { file in configurationChoice(file) }
}.padding(4)
}
.frame(height: min(160, CGFloat(files.count) * 46 + 8))
.background(ReleaseColors.terminal, in: RoundedRectangle(cornerRadius: 7))
}
if let draft = Binding($configurationDraft) {
VStack(alignment: .leading, spacing: 8) {
Text(draft.file.wrappedValue).font(.system(.caption, design: .monospaced))
.foregroundStyle(ReleaseColors.muted).lineLimit(1)
TextField("别名(可选)", text: draft.alias).textFieldStyle(.roundedBorder)
TextField("SSH 目标(可选)", text: draft.sshTarget).textFieldStyle(.roundedBorder)
HStack {
Spacer()
Button("取消") { configurationDraft = nil }
Button("保存") {
if let configurationDraft,
saveProfile(configurationDraft, select: false)
{
self.configurationDraft = nil
} }
} }
} }
HStack(spacing: 12) { }
Toggle("VPC", isOn: $pane.useVPC) .font(ReleaseTypography.detail).padding(8)
if project.supportsVerifiedRelease(profile) { .background(ReleaseColors.terminal, in: RoundedRectangle(cornerRadius: 7))
Toggle("验证发布", isOn: $pane.verified) }
Spacer(minLength: 0) Button {
Button("预检") { model.publish(pane, action: .preflight) } if let project { browseConfiguration(in: project) }
.buttonStyle(ReleaseButtonStyle()) } label: {
.disabled(!editable || pane.modules.isEmpty) Label("浏览其他 YAML…", systemImage: "folder")
} else { .foregroundStyle(ReleaseColors.blue).padding(8)
Spacer(minLength: 0) }.buttonStyle(ReleaseButtonStyle())
} }.padding([.horizontal, .bottom], 8)
if let profile { }
Button {
configurationDraft = profile private func configurationChoice(_ file: String) -> some View {
} label: { var option = project?.profiles.first(where: { $0.file == file }) ?? DeployProfile()
Image(systemName: "slider.horizontal.3") option.file = file
} let selected = profile?.file == file
.buttonStyle(ReleaseButtonStyle()) return HStack(spacing: 2) {
.help("编辑配置别名和 SSH 目标") Button {
.disabled(!editable) configurationDraft = nil
if let project { selectConfiguration(file, in: project) }
} label: {
HStack(spacing: 8) {
Image(systemName: selected ? "largecircle.fill.circle" : "circle")
.foregroundStyle(selected ? ReleaseColors.blue : ReleaseColors.muted)
VStack(alignment: .leading, spacing: 2) {
Text(option.title).lineLimit(1)
if option.title != file {
Text(file).font(.system(.caption, design: .monospaced))
.foregroundStyle(ReleaseColors.muted).lineLimit(1)
} }
} }
.toggleStyle(.checkbox) Spacer(minLength: 3)
.font(ReleaseTypography.detail) }.padding(.horizontal, 8).frame(height: 44).contentShape(Rectangle())
.disabled(!editable) }
.onChange(of: pane.useVPC) { model.save() } .buttonStyle(
.onChange(of: pane.verified) { model.save() } ReleaseButtonStyle(
} base: selected ? ReleaseColors.blue.opacity(0.12) : .clear, radius: 5)
if project.type != .cityKeeper { )
Text("自定义项目使用项目设置中的发布脚本。") .accessibilityAddTraits(selected ? .isSelected : [])
.foregroundStyle(ReleaseColors.muted) Button {
} configurationDraft = option
} label: {
Image(systemName: "pencil").foregroundStyle(ReleaseColors.blue)
.frame(width: 27, height: 27)
} }
Button("完成") { showsConfiguration = false } .buttonStyle(ReleaseButtonStyle())
.frame(maxWidth: .infinity, alignment: .trailing) .accessibilityLabel("编辑 \(file) 的别名和选项")
} }
.padding(18)
.frame(width: 360)
.environment(\.colorScheme, .dark)
} }
private var paneTitle: String { private var paneTitle: String {
...@@ -561,35 +622,10 @@ private struct ProjectReleaseColumn: View { ...@@ -561,35 +622,10 @@ private struct ProjectReleaseColumn: View {
} }
} }
private func menuRow<Content: View>(
_ label: String, value: String,
@ViewBuilder content: () -> Content
) -> some View {
HStack(spacing: 8) {
Text(label).foregroundStyle(ReleaseColors.muted)
.frame(width: 60, alignment: .leading)
Menu(content: content) {
HStack {
Text(value).lineLimit(1)
Spacer(minLength: 3)
Image(systemName: "chevron.down").font(.system(size: 10))
}
.padding(.horizontal, 11)
.frame(height: 36)
.frame(maxWidth: .infinity)
.background(ReleaseColors.surface, in: RoundedRectangle(cornerRadius: 7))
.overlay { RoundedRectangle(cornerRadius: 7).strokeBorder(ReleaseColors.border) }
}
.menuStyle(.borderlessButton)
.menuIndicator(.hidden)
.frame(maxWidth: .infinity)
.disabled(!editable)
}
}
private func selectConfiguration(_ file: String, in project: ReleaseProject) { private func selectConfiguration(_ file: String, in project: ReleaseProject) {
if let existing = project.profiles.first(where: { $0.file == file }) { if let existing = project.profiles.first(where: { $0.file == file }) {
pane.profileID = existing.id pane.profileID = existing.id
pane.verified = false
model.save() model.save()
return return
} }
...@@ -610,8 +646,9 @@ private struct ProjectReleaseColumn: View { ...@@ -610,8 +646,9 @@ private struct ProjectReleaseColumn: View {
selectConfiguration(name, in: project) selectConfiguration(name, in: project)
} }
private func saveProfile(_ updated: DeployProfile) { @discardableResult
guard var project else { return } private func saveProfile(_ updated: DeployProfile, select: Bool = true) -> Bool {
guard var project else { return false }
do { do {
try project.validate(updated) try project.validate(updated)
if let index = project.profiles.firstIndex(where: { $0.id == updated.id }) { if let index = project.profiles.firstIndex(where: { $0.id == updated.id }) {
...@@ -620,27 +657,15 @@ private struct ProjectReleaseColumn: View { ...@@ -620,27 +657,15 @@ private struct ProjectReleaseColumn: View {
project.profiles.append(updated) project.profiles.append(updated)
} }
try model.settings.save(project) try model.settings.save(project)
pane.profileID = updated.id if select {
pane.profileID = updated.id
pane.verified = false
}
model.save() model.save()
return true
} catch { } catch {
pane.error = error.localizedDescription pane.error = error.localizedDescription
return false
} }
} }
} }
private struct ConfigurationEditor: View {
@State var profile: DeployProfile
let save: (DeployProfile) -> Void
var body: some View {
VStack(alignment: .leading, spacing: 9) {
Text(profile.file).font(.system(size: 12, design: .monospaced))
TextField("别名", text: $profile.alias)
TextField("SSH 目标(可选)", text: $profile.sshTarget)
Button("保存") { save(profile) }
}
.padding(15)
.frame(width: 280)
.environment(\.colorScheme, .dark)
}
}
...@@ -23,6 +23,7 @@ final class ReleaseRunnerTests: XCTestCase { ...@@ -23,6 +23,7 @@ final class ReleaseRunnerTests: XCTestCase {
beta.name = "Beta" beta.name = "Beta"
try settings.save(alpha) try settings.save(alpha)
try settings.save(beta) try settings.save(beta)
settings.projectID = alpha.id
let model = SplitReleaseModel(settings: settings, storage: storage) let model = SplitReleaseModel(settings: settings, storage: storage)
model.add() model.add()
XCTAssertEqual(model.panes.count, 2) XCTAssertEqual(model.panes.count, 2)
...@@ -60,6 +61,68 @@ final class ReleaseRunnerTests: XCTestCase { ...@@ -60,6 +61,68 @@ final class ReleaseRunnerTests: XCTestCase {
XCTAssertTrue(restored.panes.contains { $0.log.contains("first task completed") }) XCTAssertTrue(restored.panes.contains { $0.log.contains("first task completed") })
} }
@MainActor
func testIdlePaneCanCloseWhileAnotherReleaseRuns() async throws {
let first = try Fixture()
let second = try Fixture()
defer { first.remove(); second.remove() }
try "sleep 0.4\nprintf 'active release finished\\n'\n".write(
to: first.local.appending(path: "release.sh"), atomically: true, encoding: .utf8)
let storage = first.base.appending(path: "close-state")
let settings = AppModel(storage: storage)
var alpha = first.project; alpha.name = "Alpha"
var beta = second.project; beta.name = "Beta"
try settings.save(alpha); try settings.save(beta)
settings.projectID = alpha.id
let model = SplitReleaseModel(settings: settings, storage: storage)
model.add()
let active = model.panes[0]
let idle = model.panes[1]
model.publish(active)
model.remove(active)
XCTAssertEqual(model.panes.count, 2)
model.remove(idle)
XCTAssertEqual(model.panes.count, 1)
XCTAssertTrue(model.isRunning)
for _ in 0..<200 where model.isRunning { try await Task.sleep(for: .milliseconds(10)) }
XCTAssertEqual(active.result?.status, .succeeded)
XCTAssertTrue(active.log.contains("active release finished"))
XCTAssertEqual(model.indicator, .succeeded)
}
@MainActor
func testFinishedPaneCanCloseWithoutLosingBatchOutcome() async throws {
let first = try Fixture()
let second = try Fixture()
defer { first.remove(); second.remove() }
try "printf 'first finished\\n'\n".write(
to: first.local.appending(path: "release.sh"), atomically: true, encoding: .utf8)
try "sleep 0.4\nprintf 'second finished\\n'\n".write(
to: second.local.appending(path: "release.sh"), atomically: true, encoding: .utf8)
let storage = first.base.appending(path: "batch-close-state")
let settings = AppModel(storage: storage)
var alpha = first.project; alpha.name = "Alpha"
var beta = second.project; beta.name = "Beta"
try settings.save(alpha); try settings.save(beta)
settings.projectID = alpha.id
let model = SplitReleaseModel(settings: settings, storage: storage)
model.add()
let finished = model.panes[0]
let next = model.panes[1]
model.publishAll()
model.remove(next)
XCTAssertEqual(model.panes.count, 2)
for _ in 0..<200 where finished.state != .finished { try await Task.sleep(for: .milliseconds(10)) }
XCTAssertEqual(finished.result?.status, .succeeded)
XCTAssertTrue(model.isRunning)
model.remove(finished)
XCTAssertEqual(model.panes.count, 1)
XCTAssertGreaterThanOrEqual(try XCTUnwrap(model.progressPercentage), 50)
for _ in 0..<200 where model.isRunning { try await Task.sleep(for: .milliseconds(10)) }
XCTAssertEqual(next.result?.status, .succeeded)
XCTAssertEqual(model.indicator, .succeeded)
}
@MainActor @MainActor
func testFailedPaneKeepsConsoleUntilReturnedAndRetryResetsIt() async throws { func testFailedPaneKeepsConsoleUntilReturnedAndRetryResetsIt() async throws {
let fixture = try Fixture() let fixture = try Fixture()
...@@ -128,6 +191,7 @@ final class ReleaseRunnerTests: XCTestCase { ...@@ -128,6 +191,7 @@ final class ReleaseRunnerTests: XCTestCase {
beta.profiles = [profile] beta.profiles = [profile]
try settings.save(alpha) try settings.save(alpha)
try settings.save(beta) try settings.save(beta)
settings.projectID = alpha.id
let model = SplitReleaseModel(settings: settings, storage: storage) let model = SplitReleaseModel(settings: settings, storage: storage)
model.add() model.add()
for _ in 0..<100 where model.panes.contains(where: { $0.servers.isEmpty }) { for _ in 0..<100 where model.panes.contains(where: { $0.servers.isEmpty }) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment