Skip to content

Commit a9d7793

Browse files
committed
tasks: implement execIfModified
1 parent bb89368 commit a9d7793

File tree

19 files changed

+2186
-133
lines changed

19 files changed

+2186
-133
lines changed

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"http-client-tls",
1010
"nix-conf-parser",
1111
"xtask",
12+
"devenv-cache-core",
1213
]
1314

1415
[workspace.package]
@@ -23,6 +24,7 @@ devenv = { path = "devenv" }
2324
devenv-eval-cache = { path = "devenv-eval-cache" }
2425
devenv-run-tests = { path = "devenv-run-tests" }
2526
devenv-tasks = { path = "devenv-tasks" }
27+
devenv-cache-core = { path = "devenv-cache-core" }
2628
http-client-tls = { path = "http-client-tls" }
2729
nix-conf-parser = { path = "nix-conf-parser" }
2830
xtask = { path = "xtask" }
@@ -83,6 +85,7 @@ which = "7.0.2"
8385
whoami = "1.5.1"
8486
xdg = "2.5.2"
8587
tokio-tar = "0.3.1"
88+
walkdir = "2.3"
8689

8790
# The version of rustls must match the version used by reqwest to set up rustls-platform-verifier.
8891
# If you encounter an error, lock these versions down.

devenv-cache-core/Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "devenv-cache-core"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Core utilities for file tracking and caching in devenv"
6+
license = "MIT"
7+
8+
[dependencies]
9+
# Database
10+
sqlx = { workspace = true, features = [
11+
"runtime-tokio",
12+
"tls-rustls",
13+
"sqlite",
14+
"migrate",
15+
"macros",
16+
] }
17+
18+
# Error handling
19+
thiserror.workspace = true
20+
miette.workspace = true
21+
eyre.workspace = true
22+
23+
# Hashing
24+
blake3.workspace = true
25+
26+
# File operations
27+
walkdir.workspace = true
28+
29+
# Async runtime
30+
tokio = { workspace = true, features = ["fs", "macros", "time"] }
31+
32+
# Logging
33+
tracing.workspace = true
34+
serde_json.workspace = true
35+
36+
[dev-dependencies]
37+
tempfile.workspace = true
38+
tokio = { workspace = true, features = ["rt", "macros"] }

devenv-cache-core/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# devenv-cache-core
2+
3+
A shared library for file tracking and caching in devenv.
4+
5+
## Overview
6+
7+
This crate provides common functionality used by both the task cache and eval cache implementations:
8+
9+
- File content hashing and modification detection
10+
- SQLite database connection and management
11+
- Time utilities for SystemTime ⟷ Unix timestamp conversion
12+
- Common error types
13+
14+
## Components
15+
16+
### Database Module (`db.rs`)
17+
18+
Provides a configurable SQLite database connection manager with optimized settings:
19+
20+
```rust
21+
// Create a database connection
22+
let config = DbConfig {
23+
path: PathBuf::from("/path/to/cache.db"),
24+
..Default::default()
25+
};
26+
let db = Database::new(config).await?;
27+
28+
// Initialize schema
29+
db.init_schema(MY_SCHEMA).await?;
30+
```
31+
32+
### File Tracking (`file.rs`)
33+
34+
Tracks file modifications using both modification time and content hashing:
35+
36+
```rust
37+
// Track a file
38+
let file = TrackedFile::new(path)?;
39+
40+
// Check if modified
41+
if file.is_modified()? {
42+
// File changed since last check
43+
}
44+
45+
// Update tracked state
46+
file.update()?;
47+
```
48+
49+
### Time Utilities (`time.rs`)
50+
51+
Convert between SystemTime and Unix timestamps:
52+
53+
```rust
54+
let unix_time = system_time_to_unix_seconds(SystemTime::now());
55+
let system_time = system_time_from_unix_seconds(unix_time);
56+
```
57+
58+
### Error Handling (`error.rs`)
59+
60+
Common error types with thiserror and miette integration:
61+
62+
```rust
63+
#[derive(Error, Diagnostic, Debug)]
64+
pub enum CacheError {
65+
#[error("Database error: {0}")]
66+
Database(#[from] sqlx::Error),
67+
68+
#[error("I/O error: {0}")]
69+
Io(#[from] std::io::Error),
70+
71+
// Other error variants...
72+
}
73+
```
74+
75+
## Integration Strategy
76+
77+
### For Task Cache
78+
79+
The task cache can use this shared library for file tracking and database operations, while keeping its own schema and business logic:
80+
81+
1. Replace direct SQLite usage with the `Database` type
82+
2. Replace file monitoring with `TrackedFile`
83+
3. Use shared error types and time utilities
84+
85+
### For Eval Cache
86+
87+
Similarly, the eval cache can leverage the shared library while maintaining its specific file input tracking:
88+
89+
1. Use the shared `Database` type for connection management
90+
2. Leverage `TrackedFile` for file state tracking
91+
3. Keep specialized command caching logic
92+
93+
## Benefits
94+
95+
- **Reduced Code Duplication**: Core functionality defined once
96+
- **Consistent Behavior**: Same file change detection logic across caches
97+
- **Simplified Maintenance**: Bug fixes in one place
98+
- **Better Abstraction**: Clear separation of concerns

0 commit comments

Comments
 (0)