Skip to content

Commit d91a2a4

Browse files
committed
Add comprehensive documentation and examples for STFilePath functionality
- Introduced new markdown files covering various aspects of STFilePath, including: - Core paths and sandbox usage - DownloadableFile async fetch/save pipeline - File and folder I/O operations - Function cookbook for practical coding examples - Hashing utilities using CryptoKit - iOS/macOS integrations for file handling - JSON Lines support for line-oriented file access - Metadata, permissions, extended attributes, and symlink handling - MMAP and Darwin-specific file operations - Search and backup functionalities - Watchers for file/folder/path event monitoring - Added a test support file for handling watcher events in unit tests.
1 parent 3e3fc12 commit d91a2a4

27 files changed

Lines changed: 1707 additions & 263 deletions

.agent/skills/stfilepath/README_SKILL.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

.agent/skills/stfilepath/SKILL.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
11
---
22
name: stfilepath
3-
description: "Developer-facing skill for working with the STFilePath Swift library. Use when you need to read, modify, build, test, or produce examples for STFilePath (repository contains Swift package, macOS/iOS support, file-watching, hashing, and DownloadableFile utilities)."
3+
description: "Developer-facing skill for working with the STFilePath Swift library. Use when you need to read, modify, build, test, or generate examples for any STFilePath feature (paths/files/folders, sandbox & containers, watchers, hashing, metadata/permissions/xattrs, mmap/Darwin, search/backup, JSON lines, compression, DownloadableFile, caches/UserDefaults, iOS/macOS integrations)."
44
---
55

66

77
# STFilePath Skill
88

9-
Concise developer-facing guidance and quick references for working with the STFilePath Swift package. Use this skill when you need to read, modify, explain, test, or produce examples that use STFilePath features (file operations, hashing, folder watching, DownloadableFile patterns).
9+
Developer-facing guidance for working with the STFilePath Swift package. Use this skill when you need to read, modify, explain, test, or produce examples across the full surface area of the library.
1010

1111
Trigger signals (when to use this skill)
1212
- User asks about STFilePath internals or how to implement/patch features in Sources/STFilePath
13-
- User asks for example code, README snippets, or sample apps using STFilePath APIs
13+
- User asks for example code, README snippets, scripts, or sample apps using STFilePath APIs
1414
- User requests CI/build/test guidance, or to add unit tests to Tests/STFilePathTests
15+
- User asks about any of: sandbox/container URLs, iOS document picker, macOS Finder integration, mmap, permissions/xattrs, JSON lines, caching, compression
1516

1617
Quick start
1718
- Build locally: `swift build`
1819
- Run tests: `swift test`
1920
- CI workflow: see `.github/workflows/swift.yml` for an example using macOS and swift build/test
20-
- Example snippet for README: use `scripts/example_usage.swift` as canonical sample code
21-
22-
What this skill provides
23-
- Quick start snippets for the most common tasks (create/read/write/move/delete, hashing, folder watcher)
24-
- Pointer files mapping core sources and where to add or change code (references/FILES.md)
25-
- Example code for basic and advanced patterns (references/EXAMPLES.md and scripts/example_usage.swift)
26-
- Short API quick reference to locate types and commonly-used methods (references/API_REFERENCE.md)
27-
- Troubleshooting guide for common runtime issues (references/TROUBLESHOOTING.md)
28-
- CI, packaging, and release notes (references/CI_AND_PACKAGING.md)
29-
- Test suggestions and where to add them (references/TESTS.md)
21+
- Example snippets: see `references/EXAMPLES.md` (API-accurate snippets) and `scripts/` (longer, script-like examples)
22+
23+
Capability map (open the referenced file for details)
24+
- “函数教程/直接写代码”:`references/FUNCTION_COOKBOOK.md`
25+
- Core types & path model: `references/API_REFERENCE.md`
26+
- File/folder operations & streaming: `references/FILE_IO.md`
27+
- Sandbox & containers (document/library/cache/app group/iCloud): `references/CORE_PATHS_AND_SANDBOX.md`
28+
- Watchers (file/folder/path, backends): `references/WATCHERS.md`
29+
- Hashing (CryptoKit): `references/HASHING.md`
30+
- Metadata/permissions/xattrs/symlinks/security-scoped: `references/METADATA_PERMISSIONS_XATTR_LINKS.md`
31+
- MMAP & Darwin low-level APIs: `references/MMAP_AND_DARWIN.md`
32+
- Search & backup: `references/SEARCH_AND_BACKUP.md`
33+
- JSON Lines: `references/JSON_LINES.md`
34+
- Compression: `references/COMPRESSION.md`
35+
- DownloadableFile (DFAnyFile/DFFileMap/DFCurrentValueFile): `references/DOWNLOADABLEFILE.md`
36+
- Caching & UserDefaults helpers: `references/CACHING_AND_USERDEFAULTS.md`
37+
- Platform integrations (iOS/macOS): `references/INTEGRATIONS_IOS_MACOS.md`
38+
- Repo map (where to change code/tests): `references/FILES.md`
39+
- Tests guidance (existing tests + adding new): `references/TESTS.md`
40+
- Troubleshooting: `references/TROUBLESHOOTING.md`
41+
- Skill packaging notes: `references/CI_AND_PACKAGING.md`
3042

3143
Progressive disclosure (what to open when)
32-
- Look at references/FILES.md first to locate the implementation file you need to change.
33-
- Open references/API_REFERENCE.md when you need method signatures and short usage snippets.
34-
- Open references/EXAMPLES.md for copy-pasteable examples (basic + advanced).
35-
- Open references/TROUBLESHOOTING.md when encountering runtime issues (watcher permissions, file locks, path errors).
44+
- If you’re changing behavior: open `references/FILES.md` first to locate the implementation + existing tests.
45+
- If you’re writing usage/docs: open `references/EXAMPLES.md` first; then jump to the matching deep-dive reference file.
46+
- If you’re debugging runtime issues: open `references/TROUBLESHOOTING.md`.
47+
- If you need a quick symbol search: use `rg` with keywords listed at the top of each reference file.
3648

3749
Minimal expectations and conventions
3850
- This repo is a Swift Package (Package.swift present) and targets macOS/iOS.
@@ -44,7 +56,7 @@ Safety and change guidance
4456
- For platform-specific behavior (macOS vs iOS), include platform guards and run tests on macOS CI.
4557

4658
Packaging the skill
47-
- To package this skill as a distributable .skill, bundle the skills/stfilepath directory. Tell me if you want me to produce that .skill archive.
59+
- To package this skill as a distributable `.skill`, zip the `.agent/skills/stfilepath` directory (see `references/CI_AND_PACKAGING.md`).
4860

4961
Contacts and maintenance
5062
- When renaming or moving files, update references/FILES.md.

.agent/skills/stfilepath/SKILL_zh-CN.md

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,86 @@
1-
STFilePath - API Quick Reference (short)
2-
3-
STFile (file-level operations)
4-
- create(with: Data) -> create file with contents
5-
- read() -> Data
6-
- append(data: Data)
7-
- delete()
8-
- move(to: STFile)
9-
- hash(with: HashAlgorithm) -> String
10-
- watcher() -> STFileWatcher
11-
12-
STFolder (folder-level operations)
13-
- folder(name: String) -> STFolder (path builder)
14-
- file(name: String) -> STFile (path builder)
15-
- create() -> create folder
16-
- delete()
17-
- watcher(options:) -> STFolderWatcher
18-
19-
STPath / STPathProtocol
20-
- path string properties, checks for isExists, isFolderExists, isFileExists
21-
- watcher() -> STPathWatcher (generic)
22-
- openingProcesses() -> [STProcessInfo] (macOS only)
23-
24-
STProcessInfo
25-
- pid: Int32
26-
- name: String
27-
- command: String
28-
29-
Watcher Classes (STFileWatcher, STFolderWatcher, STPathWatcher)
30-
- streamMonitoring() / stream() -> AsyncThrowingStream of events
31-
- stopMonitoring() / stop() -> terminates the stream
32-
33-
DownloadableFile / DFAnyFile
34-
- .codable(Type.self) to transform to Codable handling
35-
- fetch() async -> Type
36-
- save(_:) async to persist transformed content
37-
38-
Where to find implementations
39-
- Sources/STFilePath/STFile.swift
40-
- Sources/STFilePath/STFolder+Folder.swift
41-
- Sources/STFilePath/STFolderWatcher.swift
42-
- Sources/STFilePath/STFileWatcher.swift
43-
- Sources/STFilePath/STPathWatcher.swift
44-
- Sources/STFilePath/STPathProtocol.swift
45-
- Sources/STFilePath/DownloadableFile/*
46-
47-
Note: This reference is intentionally concise. For method signatures and exact parameter types, open the source files listed above.
1+
STFilePath - API Quick Reference (accurate, terse)
2+
3+
If you need deeper coverage by area, open the matching reference file:
4+
- File/folder IO: `references/FILE_IO.md`
5+
- Sandbox/containers: `references/CORE_PATHS_AND_SANDBOX.md`
6+
- Watchers: `references/WATCHERS.md`
7+
- Hashing: `references/HASHING.md`
8+
- Metadata/permissions/xattrs/symlinks: `references/METADATA_PERMISSIONS_XATTR_LINKS.md`
9+
- MMAP & Darwin: `references/MMAP_AND_DARWIN.md`
10+
- DownloadableFile: `references/DOWNLOADABLEFILE.md`
11+
- Search/backup: `references/SEARCH_AND_BACKUP.md`
12+
- JSON Lines: `references/JSON_LINES.md`
13+
- Integrations: `references/INTEGRATIONS_IOS_MACOS.md`
14+
15+
Use cases
16+
1) 我想“直接写代码”而不是翻源码:先看 `references/FUNCTION_COOKBOOK.md`
17+
2) 我想快速复制粘贴:看 `references/EXAMPLES.md`
18+
3) 我想改库实现/补测试:先看 `references/FILES.md`(定位实现与测试文件)
19+
20+
Search hints
21+
- Core protocol: `rg -n "protocol STPathProtocol" Sources/STFilePath`
22+
- Core types: `rg -n "struct STPath|struct STFile|struct STFolder" Sources/STFilePath`
23+
- Watchers: `rg -n "Watcher|ST(Folder|File|Path)Watcher|WatcherBackend" Sources/STFilePath`
24+
- DownloadableFile: `rg -n "protocol DownloadableFile|DFAnyFile|DFFileMap|DFCurrentValueFile" Sources/STFilePath`
25+
26+
Core types
27+
- `STPathProtocol`: shared URL-based API for paths (exists/type/relativePath, attributes, permission, etc.). Implementation: `Sources/STFilePath/STPathProtocol.swift`
28+
- `STPath`: type-erased path (file/folder/notExist), can expose `referenceType` as `STFilePathReferenceType`. Implementation: `Sources/STFilePath/STPath.swift`
29+
- `STFile`: file path + file ops. Implementation: `Sources/STFilePath/STFile.swift`
30+
- `STFolder`: folder path + folder ops. Implementation: `Sources/STFilePath/STFolder+Folder.swift`
31+
32+
Path classification & reference
33+
- `STFilePathItemType`: `.file/.folder/.notExist`. Implementation: `Sources/STFilePath/STFilePathItemType.swift`
34+
- `STFilePathReferenceType`: `.file(STFile)` / `.folder(STFolder)`. Implementation: `Sources/STFilePath/STFilePathReferenceType.swift`
35+
36+
Watchers (AsyncThrowingStream-based)
37+
- `STFileWatcher(file: STFile).stream() -> AsyncThrowingStream<STPathChanged, Error>` and `.stop()`. Implementation: `Sources/STFilePath/STFileWatcher.swift`
38+
- `STFolderWatcher(folder: STFolder, options:).streamMonitoring() -> AsyncThrowingStream<STFolderWatcher.Changed, Error>` and `.stopMonitoring()`. Implementation: `Sources/STFilePath/STFolderWatcher.swift`
39+
- `STPathWatcher(path: STPath).stream() -> AsyncThrowingStream<STPathChanged, Error>` and `.stop()`. Implementation: `Sources/STFilePath/STPathWatcher.swift`
40+
- Backends:
41+
- macOS folder/path: FSEvents (`Sources/STFilePath/macos/FSEventsWatcher.swift`)
42+
- file + non-macOS: DispatchSource (`Sources/STFilePath/DispatchSourceWatcher.swift`)
43+
- common event model: `Sources/STFilePath/WatcherBackend.swift` (`STPathChanged`, `STPathChangeKind`)
44+
45+
Hashing (CryptoKit)
46+
- `STHasherKind`: `.sha256/.sha384/.sha512/.md5` (md5 uses `Insecure.MD5`). Implementation: `Sources/STFilePath/STFile+CryptoKit.swift`
47+
- `STFile.hash(with kind: STHasherKind) -> String` (hex digest). Implementation: `Sources/STFilePath/STFile+CryptoKit.swift`
48+
49+
Metadata / permissions / xattrs / symlinks
50+
- Attributes wrapper: `STPathAttributes` via `STPathProtocol.attributes`. Implementation: `Sources/STFilePath/STPathAttributes.swift`
51+
- Convenience permissions:
52+
- `STPathProtocol.permission -> STPathPermission` (exists/readable/writable/executable/deletable). Implementation: `Sources/STFilePath/STPathProtocol.swift`, `Sources/STFilePath/STPathPermission.swift`
53+
- POSIX permissions helpers: `STPathProtocol.permissions()` / `set(permissions:)` and `STPathPermission.Posix`. Implementation: `Sources/STFilePath/STPath+Metadata.swift`, `Sources/STFilePath/STPathPermission.swift`
54+
- Extended attributes (Darwin only): `STPathProtocol.extendedAttributes` with `set/value/remove/list`. Implementation: `Sources/STFilePath/STPath+Metadata.swift`
55+
- Symlink helpers: `isSymbolicLink`, `createSymbolicLink(to:)`, `destinationOfSymbolicLink()`. Implementation: `Sources/STFilePath/STPath+Link.swift`
56+
- Security-scoped resource helpers (Apple platforms): `startAccessingSecurityScopedResource()` / `stopAccessingSecurityScopedResource()`. Implementation: `Sources/STFilePath/STPathProtocol.swift`
57+
58+
Search & enumeration
59+
- `STFolder.files(...) / folders(...) / subFilePaths(...) / allSubFilePaths(...)`. Implementation: `Sources/STFilePath/STFolder+Search.swift`
60+
- `STFolder.SearchPredicate` and custom filters. Implementation: `Sources/STFilePath/STFolder+Search.swift`
61+
- Recursive file scanning with filters: `STFolder.files(matching:in:)`. Implementation: `Sources/STFilePath/STFolder+Search.swift`
62+
63+
Backup
64+
- `STFolder.backup(options:) -> STFolderBackup`, `connect()`, `monitoring() async`, `stopMonitoring()`. Implementation: `Sources/STFilePath/STFolderBackup.swift`
65+
66+
JSON Lines
67+
- `STFile.lineFile -> STLineFile`, `STLineFile.lines()` / `lines(as:)`, `STLineFile.NewLineWriter.append(...)`. Implementation: `Sources/STFilePath/STJSONLines.swift`
68+
69+
DownloadableFile
70+
- `DownloadableFile` protocol: `fetch()` / `save(_:) async`. Implementation: `Sources/STFilePath/DownloadableFile/DownloadableFile.swift`
71+
- `DFAnyFile<Model>` type erasure and `DFAnyFile(file: STFile)` when `Model == Data`. Implementation: `Sources/STFilePath/DownloadableFile/DFAnyFile.swift`
72+
- `DFFileMap<File, To>` mapping via `map(fetch:save:)` and helpers like `.codable(...)`, `.compression(...)`. Implementation: `Sources/STFilePath/DownloadableFile/DFFileMap.swift`, `Sources/STFilePath/DownloadableFile/DownloadableFile.swift`
73+
- `DFCurrentValueFile`: debounced-ish “last write wins” wrapper that saves on `value` set. Implementation: `Sources/STFilePath/DownloadableFile/DFAnyFile.swift`
74+
- Convenience: `STFile.toDFAnyFile() -> DFAnyFile<Data>`. Implementation: `Sources/STFilePath/DownloadableFile/DFAnyFile.swift`
75+
76+
Compression (if `Compression` module available)
77+
- `STComparator.compress/decompress(_:algorithm:)`. Implementation: `Sources/STFilePath/Compression/STComparator.swift`
78+
- `DownloadableFile.compression(_:)` for `Model == Data`. Implementation: `Sources/STFilePath/DownloadableFile/DownloadableFile.swift`
79+
80+
MMAP & Darwin file APIs (Darwin only)
81+
- `STFile.withMmap(...) { STFileMMAP in ... }`, `STFile.setSize(_:)`. Implementation: `Sources/STFilePath/STFile+MMAP.swift`
82+
- `STFile.system -> STFileSystem` (`open/stat/truncate/sync/...`). Implementation: `Sources/STFilePath/STFile+Darwin.swift`
83+
84+
Integrations
85+
- iOS: `STDocumentPicker` (UIDocumentPicker) and `STPathQuickLookController` (QuickLook). Implementation: `Sources/STFilePath/ios/*`
86+
- macOS: Finder helpers (`showInFinder`, `selectInFinder`) and associated apps (`associatedApplications`, `open(with:)`). Implementation: `Sources/STFilePath/macos/*`
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Caching (STKVCache) and UserDefaults helpers (STUserDefaults)
2+
3+
Search hints
4+
- Cache: `rg -n "final class STKVCache" Sources/STFilePath/Surroundings/STKVCache.swift`
5+
- UserDefaults wrapper: `rg -n "propertyWrapper\\s+public struct STUserDefaults" Sources/STFilePath/Surroundings/STUserDefaults.swift`
6+
7+
Use cases
8+
1) 解析结果缓存(避免重复 JSON decode)
9+
```swift
10+
import Foundation
11+
import STFilePath
12+
13+
let cache = STKVCache<String, Any>()
14+
// 业务建议:定义明确的 Value 类型(这里仅展示用法)
15+
```
16+
17+
2) Codable cache 落盘(重启恢复)
18+
```swift
19+
import Foundation
20+
import STFilePath
21+
22+
let file = STFile("/tmp/cache.json")
23+
try file.createIfNotExists(with: Data("[]".utf8))
24+
25+
let cache = STKVCache<String, Int>()
26+
cache["a"] = 1
27+
try cache.saveToDisk(with: file)
28+
29+
let restored = try STKVCache<String, Int>.decode(from: file)
30+
_ = restored["a"]
31+
```
32+
33+
3) 用户偏好(UserDefaults property wrapper)
34+
```swift
35+
import STFilePath
36+
37+
struct Settings {
38+
@STUserDefaults("enabled", default: false) var enabled: Bool
39+
@STUserDefaults("recentIDs", default: []) var recentIDs: [Int]
40+
}
41+
```
42+
43+
STKVCache
44+
- In-memory cache built on `NSCache` with optional expiration.
45+
- Codable support when `Key` and `Value` are Codable:
46+
- Can encode/decode the cache contents.
47+
- Can persist to disk using `saveToDisk(with:)` / `decode(from:)` with an `STFile`.
48+
49+
Example (basic)
50+
51+
```swift
52+
import STFilePath
53+
54+
let cache = STKVCache<String, Int>()
55+
cache.insert(1, forKey: "a", lifeTime: 60)
56+
let v = cache.value(of: "a")
57+
```
58+
59+
Example (persist to disk)
60+
61+
```swift
62+
import Foundation
63+
import STFilePath
64+
65+
let file = STFile("/tmp/cache.json")
66+
try file.createIfNotExists(with: Data("[]".utf8))
67+
68+
let cache = STKVCache<String, Int>()
69+
cache["a"] = 1
70+
try cache.saveToDisk(with: file)
71+
72+
let restored = try STKVCache<String, Int>.decode(from: file)
73+
```
74+
75+
STUserDefaults
76+
- `@STUserDefaults(key, default: ...)` property wrapper with typed values.
77+
- Supported out of the box:
78+
- `String`, `Int`, `Bool`, `Float`, `Double`, `URL`, `UUID`, `Data`
79+
- `Optional<Wrapped>` where `Wrapped: STUserDefaultsValue`
80+
- `Array<Element>` where `Element: Codable` (stored as binary plist)
81+
82+
Example
83+
84+
```swift
85+
import STFilePath
86+
87+
struct Settings {
88+
@STUserDefaults("enabled", default: false) var enabled: Bool
89+
@STUserDefaults("recentIDs", default: []) var recentIDs: [Int]
90+
}
91+
```
92+
93+
Where to change behavior
94+
- Cache: `Sources/STFilePath/Surroundings/STKVCache.swift`
95+
- UserDefaults wrapper: `Sources/STFilePath/Surroundings/STUserDefaults.swift`

.agent/skills/stfilepath/references/CI_AND_PACKAGING.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
CI, build, and packaging notes
22

3+
Use cases
4+
1) 本地验证(构建 + 测试)
5+
```bash
6+
swift build
7+
swift test
8+
```
9+
10+
2) 只跑 watcher 相关测试(定位 flake/hang)
11+
```bash
12+
swift test --filter STFilePathTests.STFolderWatcherTests --filter STFilePathTests.STWatcherTests
13+
```
14+
15+
3) 打包 skill(生成 `stfilepath.skill`
16+
```bash
17+
zip -r stfilepath.skill .agent/skills/stfilepath
18+
```
19+
320
Build & Test
421
- Local: `swift build` and `swift test`
522
- CI: .github/workflows/swift.yml currently runs `swift build` and `swift test --enable-code-coverage` on macos-latest with Swift 6.0 in the matrix
623

724
Packaging the skill
8-
- To distribute this skill as a .skill archive, create a zip of the skills/stfilepath directory and rename to `stfilepath.skill`.
25+
- To distribute this skill as a `.skill` archive, create a zip of the `.agent/skills/stfilepath` directory and rename to `stfilepath.skill`.
926
- Ensure SKILL.md frontmatter is present and valid (name + description). The packaging can be automated with a small script:
1027

1128
```bash
12-
zip -r stfilepath.skill skills/stfilepath
29+
zip -r stfilepath.skill .agent/skills/stfilepath
1330
```
1431

1532
Publishing

0 commit comments

Comments
 (0)