Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Interceptor/Automation/PluginScriptTree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation

/// Immediate-child projection of already validated package-relative source paths.
enum PluginScriptTree {
struct Entry: Equatable, Identifiable {
enum Kind: String { case directory, file }

let name: String
/// Full relative path: the directory prefix, or the exact source file name.
let path: String
let kind: Kind
var id: String { kind.rawValue + ":" + path }
}

/// Folders first, then files; each sorted exactly by name. Prefixes match whole path components.
static func entries(in files: [String], at directory: [String] = []) -> [Entry] {
var folders = Set<String>()
var leaves = Set<String>()
for file in files {
let components = file.split(separator: "/", omittingEmptySubsequences: false).map(String.init)
guard components.count > directory.count, components.starts(with: directory) else { continue }
if components.count == directory.count + 1 {
leaves.insert(file)
} else {
folders.insert(components[directory.count])
}
}
let prefix = directory.isEmpty ? "" : directory.joined(separator: "/") + "/"
let directories = folders.sorted().map { Entry(name: $0, path: prefix + $0, kind: .directory) }
let documents = leaves.map { Entry(name: String($0.dropFirst(prefix.count)), path: $0, kind: .file) }
.sorted { $0.name < $1.name }
return directories + documents
}
}
123 changes: 96 additions & 27 deletions Interceptor/Views/Automation/PluginBrowserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -573,40 +573,109 @@ struct PluginScriptFilesView: View {
let model: PluginBrowserModel
var package: PluginPackagePresentation?
var body: some View {
List {
if let package {
files(package)
} else {
PluginBrowserErrors(controller: model.controller, queryError: model.catalogReadError)
if !model.controller.isLoaded {
if let package {
PluginScriptDirectoryView(package: package, directory: [])
} else {
let controller = model.controller
let hasError = (controller.storeError ?? controller.installationError ?? controller.packageCatalogError
?? controller.actionError ?? model.catalogReadError) != nil
let installed = model.installedPackages()
let duplicateNames = Set(Dictionary(grouping: installed, by: { $0.package.manifest.name }).filter { $0.value.count > 1 }.keys)
List {
PluginBrowserErrors(controller: controller, queryError: model.catalogReadError)
if !controller.isLoaded {
ProgressView("AUTO_BROWSER_LOADING").accessibilityIdentifier("plugins.loading")
}
ForEach(model.installedPackages(), id: \.id) { revision in
Section(revision.package.manifest.name) { files(model.presentation(of: revision)) }
ForEach(installed, id: \.id) { revision in
let manifest = revision.package.manifest
let source = PluginBrowserFamilyID(revision).source
let summary = duplicateNames.contains(manifest.name)
? (source == "local" ? String(localized: "AUTO_BROWSER_LOCAL") : source) : (manifest.summary ?? "")
NavigationLink {
PluginScriptDirectoryView(package: model.presentation(of: revision), directory: [])
} label: {
PluginPackageRow(name: manifest.name, author: manifest.author, version: manifest.version,
summary: summary, iconSource: .bundled(revision.package), isInstalled: true)
}
.accessibilityIdentifier("plugins.scriptPackage." + manifest.id.uuidString + "." + revision.id)
}
if installed.isEmpty && controller.isLoaded && !hasError {
ContentUnavailableView {
Label("AUTO_BROWSER_NO_SCRIPTS", systemImage: "doc.text")
} description: {
Text("AUTO_BROWSER_ADD_TO_SEE_SCRIPTS")
}
.listRowSeparator(.hidden)
}
}
.listStyle(.plain)
.navigationTitle("AUTO_BROWSER_SCRIPTS")
.navigationBarTitleDisplayMode(.large)
.accessibilityIdentifier("plugins.scripts")
}
.listStyle(.plain)
.navigationTitle("AUTO_BROWSER_SCRIPTS")
.navigationBarTitleDisplayMode(package == nil ? .large : .inline)
.accessibilityIdentifier("plugins.scripts")
}

private func files(_ package: PluginPackagePresentation) -> some View {
ForEach(package.sourceFileNames, id: \.self) { file in
NavigationLink {
PluginFileDetailView(package: package, file: file)
} label: {
Label {
VStack(alignment: .leading, spacing: 3) {
Text(verbatim: (file as NSString).lastPathComponent)
if file.contains("/") {
Text(verbatim: file).font(.footnote).foregroundStyle(.secondary)
}
}
}

/// One directory level of a package's source tree; folders recurse into the same view.
struct PluginScriptDirectoryView: View {
let package: PluginPackagePresentation
let directory: [String]
@State private var openedFile: OpenedFile?
@State private var selection: String?

private struct OpenedFile: Identifiable {
let path: String
var id: String { path }
}

private func source(of file: String) -> String {
package.manifest.schemaVersion == 2 ? package.sourceFiles[file] ?? "" : package.source ?? ""
}

var body: some View {
let scope = package.manifest.id.uuidString
let entries = PluginScriptTree.entries(in: package.sourceFileNames, at: directory)
List {
if entries.isEmpty {
ContentUnavailableView("AUTO_BROWSER_NO_SCRIPTS", systemImage: "doc.text")
.listRowSeparator(.hidden)
}
Section {
ForEach(entries) { entry in
switch entry.kind {
case .directory:
NavigationLink {
PluginScriptDirectoryView(package: package, directory: directory + [entry.name])
} label: {
Label {
Text(verbatim: entry.name)
} icon: { Image(systemName: "folder").foregroundStyle(PluginBrowserColor.accent) }
}
.accessibilityIdentifier("plugins.scriptFolder." + scope + "." + entry.path)
case .file:
Button { openedFile = OpenedFile(path: entry.path) } label: {
Label {
Text(verbatim: entry.name).foregroundStyle(.primary)
} icon: { Image(systemName: "doc.text").foregroundStyle(PluginBrowserColor.accent) }
}
.accessibilityIdentifier("plugins.file." + scope + "." + entry.path)
}
} icon: { Image(systemName: "doc.text").foregroundStyle(PluginBrowserColor.accent) }
}
} header: {
if !directory.isEmpty {
Text(verbatim: package.manifest.name + "\n" + directory.joined(separator: "/")).textCase(nil)
}
}
.accessibilityIdentifier("plugins.file." + package.manifest.id.uuidString + "." + file)
}
.listStyle(.plain)
.navigationTitle(Text(verbatim: directory.last ?? package.manifest.name))
.navigationBarTitleDisplayMode(.inline)
.accessibilityIdentifier("plugins.scriptDirectory." + scope + "." + directory.joined(separator: "/"))
.fullScreenCover(item: $openedFile) { file in
PluginSourceReader(fileNames: [], selection: $selection, source: { _ in source(of: file.path) },
sourceIdentifier: "plugins.file.source", pickerIdentifier: "plugins.file.sourcePicker",
closeIdentifier: "plugins.file.closeSource", title: (file.path as NSString).lastPathComponent,
accentColor: PluginBrowserColor.accent) { openedFile = nil }
}
}
}
Expand Down
88 changes: 0 additions & 88 deletions Interceptor/Views/Automation/PluginFileDetailView.swift
Original file line number Diff line number Diff line change
@@ -1,94 +1,6 @@
import AutomationCore
import SwiftUI

/// File metadata only. The exact file's source remains hidden until Source Code is explicitly opened.
struct PluginFileDetailView: View {
let package: PluginPackagePresentation
let file: String
@State private var showSource = false
@State private var selection: String?

private var name: String {
(file as NSString).lastPathComponent
}

private var rules: [PluginBrowserRule] {
package.rulesByFile[file] ?? []
}

/// Hooks sharing the same traffic conditions belong to one visible target.
private var groupedRules: [[PluginBrowserRule]] {
rules.reduce(into: []) { groups, rule in
if let index = groups.firstIndex(where: { group in
guard let first = group.first else { return false }
return first.hosts == rule.hosts && first.methods == rule.methods
&& first.path == rule.path && first.status == rule.status
}) {
groups[index].append(rule)
} else {
groups.append([rule])
}
}
}

private var source: String {
package.manifest.schemaVersion == 2 ? package.sourceFiles[file] ?? "" : package.source ?? ""
}

var body: some View {
List {
Section {
ForEach(Array(groupedRules.enumerated()), id: \.offset) { index, group in
let rule = group[0]
VStack(alignment: .leading, spacing: 8) {
fact("AUTO_BROWSER_TARGET_HOSTS", rule.hosts.joined(separator: "\n"))
fact("AUTO_TARGET_METHODS", rule.methods?.joined(separator: ", ") ?? String(localized: "AUTO_RULE_ANY_METHOD"))
switch rule.path {
case let .exact(value)?: fact("AUTO_PATH_EXACT", value)
case let .prefix(value)?: fact("AUTO_PATH_PREFIX", value)
case nil: fact("AUTO_TARGET_PATH", String(localized: "AUTO_RULE_ANY_PATH"))
}
fact("AUTO_HOOK", Array(Set(group.map(\.hook))).sorted().joined(separator: "\n"))
if let status = rule.status {
fact("AUTO_TARGET_STATUS", "\(status.lowerBound)–\(status.upperBound)")
}
}
.accessibilityIdentifier("plugins.file.rule.\(index)")
}
} header: { Text("AUTO_TARGETS") }
Section {
Button { showSource = true } label: {
HStack {
Text("AUTO_BROWSER_SOURCE_CODE").foregroundStyle(.primary)
Spacer()
Image(systemName: "chevron.right").font(.footnote.weight(.semibold)).foregroundStyle(.tertiary)
}
.contentShape(Rectangle())
}
.accessibilityAddTraits(.isLink)
.accessibilityIdentifier("plugins.file.openSource")
}
}
.listStyle(.plain)
.navigationTitle(Text(verbatim: name))
.navigationBarTitleDisplayMode(.inline)
.accessibilityIdentifier("plugins.file.detail")
.fullScreenCover(isPresented: $showSource) {
PluginSourceReader(fileNames: [], selection: $selection, source: { _ in source },
sourceIdentifier: "plugins.file.source", pickerIdentifier: "plugins.file.sourcePicker",
closeIdentifier: "plugins.file.closeSource", title: name,
accentColor: PluginBrowserColor.accent) { showSource = false }
}
}

private func fact(_ title: LocalizedStringKey, _ value: String) -> some View {
VStack(alignment: .leading, spacing: 3) {
Text(title).font(.footnote).foregroundStyle(.secondary)
Text(verbatim: value).fixedSize(horizontal: false, vertical: true).textSelection(.enabled)
}
}
}

struct PluginManifestJSONView: View {
let package: PluginPackagePresentation
let done: () -> Void
Expand Down
Loading