cuenv 0.4.7

A direnv alternative that uses CUE files for environment configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package env

// Development environment configuration
env: {
	// Cargo configuration
	CARGO_TERM_COLOR: "always"
	RUST_BACKTRACE:   "1"
	CUENV_ENV:        "development"

	// Testing configuration
	RUST_TEST_THREADS: "4"
	GOMAXPROCS:        "4"

	// Build configuration
	CGO_ENABLED: "1"
}

// Development lifecycle hooks
hooks: {
	onEnter: [
		// Source nix flake development environment
		{
			flake: {
				// Use current directory's flake
				dir: "."
			}
		},
		// Then show welcome message
		{
			command: "echo"
			args: ["🦀 cuenv development environment activated (with nix toolchain)"]
		},
	]

	onExit: {
		command: "echo"
		args: ["👋 Leaving cuenv development environment"]
	}
}

// Comprehensive task definitions
tasks: {
	// === Code Quality Tasks ===

	"fmt": {
		description: "Format all code (Rust, Go, Nix, CUE, etc.)"
		command:     "treefmt"
		cache:       true
		inputs: ["src/**/*", "libcue-bridge/**/*", "*.nix", "**/*.cue", "**/*.md", "**/*.toml", "**/*.yaml", "**/*.yml"]
	}

	"fmt:check": {
		description: "Check all code formatting without making changes"
		command:     "treefmt --fail-on-change"
		inputs: ["src/**/*", "libcue-bridge/**/*", "*.nix", "**/*.cue", "**/*.md", "**/*.toml", "**/*.yaml", "**/*.yml"]
	}

	"fmt:rust": {
		description: "Format only Rust code"
		command:     "cargo fmt"
		cache:       true
		inputs: ["src/**/*.rs", "build.rs", "Cargo.toml"]
	}

	"fmt:rust:check": {
		description: "Check Rust code formatting"
		command:     "cargo fmt --check"
		inputs: ["src/**/*.rs", "build.rs", "Cargo.toml"]
	}

	"clippy": {
		description: "Run clippy lints with deny warnings"
		command:     "cargo clippy --all-targets --all-features -- -D warnings -A clippy::duplicate_mod -A clippy::uninlined_format_args -A clippy::too_many_arguments"
		cache:       true
		inputs: ["src/**/*.rs", "libcue-bridge/**/*", "build.rs", "Cargo.toml", "Cargo.lock"]
	}

	"clippy:fix": {
		description: "Apply clippy suggestions automatically"
		command:     "cargo clippy --fix --allow-dirty --allow-staged"
		cache:       false
		inputs: ["src/**/*.rs", "libcue-bridge/**/*", "build.rs", "Cargo.toml"]
	}

	// === Build Tasks ===

	"build": {
		description: "Build cuenv in release mode"
		command:     "cargo build --release"
		dependencies: ["fmt:rust:check"]
		cache: true
		inputs: ["src/**/*.rs", "libcue-bridge/**/*", "build.rs", "Cargo.toml", "Cargo.lock"]
		outputs: ["target/release/cuenv"]
	}

	"build:debug": {
		description: "Build cuenv in debug mode"
		command:     "cargo build"
		dependencies: ["fmt:rust:check"]
		cache: true
		inputs: ["src/**/*.rs", "libcue-bridge/**/*", "build.rs", "Cargo.toml", "Cargo.lock"]
		outputs: ["target/debug/cuenv"]
	}

	"build:check": {
		description: "Check that the code compiles without building"
		command:     "cargo check --all-targets --all-features"
		cache:       true
		inputs: ["src/**/*.rs", "libcue-bridge/**/*", "build.rs", "Cargo.toml", "Cargo.lock"]
	}

	// === Test Tasks ===

	"test": {
		description: "Run all tests with nextest"
		command:     "cargo nextest run"
		dependencies: ["build:check"]
		cache: true
		inputs: ["src/**/*.rs", "tests/**/*.rs", "libcue-bridge/**/*", "examples/**/*", "Cargo.toml", "Cargo.lock"]
	}

	"test:unit": {
		description: "Run only unit tests (lib and bins)"
		command:     "cargo nextest run --lib --bins -E 'not test(/concurrent|/thread_safe|/monitored_cache|/profiling|/tree_operations|/confidence|/sequential_pattern|/streaming|/prop_test_cache|/statistics|/parse_shell|/process_guard/)'"
		dependencies: ["build:check"]
		cache: true
		inputs: ["src/**/*.rs", "Cargo.toml", "Cargo.lock"]
	}

	"test:integration": {
		description: "Run integration tests"
		command:     "cargo nextest run --tests"
		dependencies: ["build:debug"]
		cache: true
		inputs: ["src/**/*.rs", "tests/**/*.rs", "libcue-bridge/**/*", "Cargo.toml", "Cargo.lock"]
	}

	"test:doc": {
		description: "Run documentation tests"
		command:     "cargo test --doc"
		dependencies: ["build:check"]
		cache: true
		inputs: ["src/**/*.rs", "Cargo.toml"]
	}

	"test:examples": {
		description: "Test example configurations"
		dependencies: ["build:debug"]
		cache: true
		inputs: ["examples/**/*", "scripts/test-examples.sh"]
		script: """
			if [ -f "./scripts/test-examples.sh" ]; then
				./scripts/test-examples.sh
			else
				echo "Error: test-examples.sh script not found"
				exit 1
			fi
			"""
	}

	"test:coverage": {
		description: "Generate test coverage report"
		command:     "cargo llvm-cov nextest --lcov --output-path lcov.info"
		dependencies: ["build:check"]
		cache: false
		inputs: ["src/**/*.rs", "tests/**/*.rs", "Cargo.toml", "Cargo.lock"]
		outputs: ["lcov.info"]
	}

	// === Quality Assurance Tasks ===

	"audit": {
		description: "Audit dependencies for security vulnerabilities"
		command:     "cargo audit"
		cache:       false
		inputs: ["Cargo.lock"]
	}

	"outdated": {
		description: "Check for outdated dependencies"
		command:     "cargo outdated"
		cache:       false
		inputs: ["Cargo.toml", "Cargo.lock"]
	}

	"doc": {
		description: "Generate and open API documentation"
		command:     "cargo doc --open --no-deps"
		cache:       true
		inputs: ["src/**/*.rs", "Cargo.toml"]
		outputs: ["target/doc/**/*"]
	}

	"doc:check": {
		description: "Check documentation for broken links and missing docs"
		command:     "cargo doc --no-deps"
		cache:       true
		inputs: ["src/**/*.rs", "Cargo.toml"]
	}

	// === Development Tasks ===

	"dev": {
		description: "Run development server with auto-reload"
		command:     "cargo watch -x run"
		cache:       false
	}

	"dev:check": {
		description: "Watch and check code continuously"
		command:     "cargo watch -x check"
		cache:       false
	}

	"dev:test": {
		description: "Watch and run tests continuously"
		command:     "cargo watch -x 'nextest run'"
		cache:       false
	}

	"clean": {
		description: "Clean all build artifacts"
		command:     "cargo clean"
		cache:       false
	}

	"clean:full": {
		description: "Clean build artifacts and Go cache"
		cache:       false
		script: """
			cargo clean
			if [ -d "libcue-bridge/vendor" ]; then
				rm -rf libcue-bridge/vendor
			fi
			if [ -n "$GOCACHE" ] && [ -d "$GOCACHE" ]; then
				go clean -cache
			fi
			echo "Full clean completed"
			"""
	}

	// === Comprehensive Check Tasks (replaces nix flake check) ===

	"check": {
		description: "Run all quality checks (replaces nix flake check)"
		dependencies: ["check:format", "check:lint", "check:build", "check:test"]
		cache: false
		script: """
			echo "✅ All quality checks passed!"
			echo ""
			echo "This task replaces 'nix flake check' and includes:"
			echo "  - Code formatting validation"
			echo "  - Clippy lints"
			echo "  - Build verification"
			echo "  - Unit and integration tests"
			echo "  - Example testing"
			echo "  - Documentation checks"
			"""
	}

	"check:format": {
		description: "Verify all code is properly formatted"
		dependencies: ["fmt:check"]
		cache: true
		script: """
			echo "✅ Code formatting verified"
			"""
	}

	"check:lint": {
		description: "Run all linting checks"
		dependencies: ["clippy"]
		cache: true
		script: """
			echo "✅ Linting checks passed"
			"""
	}

	"check:build": {
		description: "Verify the project builds successfully"
		dependencies: ["build:check", "doc:check"]
		cache: true
		script: """
			echo "✅ Build verification completed"
			"""
	}

	"check:test": {
		description: "Run comprehensive test suite"
		dependencies: ["test:unit", "test:integration", "test:doc", "test:examples"]
		cache: true
		script: """
			echo "✅ Comprehensive test suite completed"
			"""
	}

	"check:security": {
		description: "Run security and dependency checks"
		dependencies: ["audit"]
		cache: false
		script: """
			echo "✅ Security and dependency checks completed"
			"""
	}

	// === CI/CD Tasks ===

	"ci": {
		description: "Run CI pipeline locally"
		dependencies: ["check", "check:security"]
		cache: false
		script: """
			echo "🚀 Running full CI pipeline locally..."
			echo ""
			echo "All CI checks completed successfully!"
			echo "Ready for commit and push."
			"""
	}

	"pre-commit": {
		description: "Pre-commit validation (quick checks)"
		dependencies: ["fmt:rust:check", "clippy", "test:unit"]
		cache: true
		script: """
			echo "✅ Pre-commit checks passed!"
			"""
	}

	// === Version Management Tasks ===

	"version:show": {
		description: "Show current version from Cargo.toml"
		command:     "grep '^version = ' Cargo.toml | head -1 | cut -d'\"' -f2"
		cache:       false
	}

	"version:update": {
		description: "Update version (use with NEW_VERSION env var)"
		cache:       false
		script: """
			if [ -z "$NEW_VERSION" ]; then
				echo "Error: NEW_VERSION environment variable must be set"
				echo "Usage: NEW_VERSION=0.3.2 cuenv task version:update"
				exit 1
			fi

			# Validate version format
			if ! [[ "$NEW_VERSION" =~ ^[0-9]+\\.[0-9]+\\.[0-9]+$ ]]; then
				echo "Error: Version must be in format X.Y.Z (e.g., 0.3.2)"
				exit 1
			fi

			echo "Updating version to $NEW_VERSION..."
			sed -i "s/^version = \\".*\\"/version = \\"$NEW_VERSION\\"/" Cargo.toml
			cargo update -p cuenv

			echo "Version updated to $NEW_VERSION"
			echo ""
			echo "Next steps:"
			echo "1. Review changes: git diff"
			echo "2. Commit: git commit -am 'chore: bump version to $NEW_VERSION'"
			echo "3. Tag and push: git tag $NEW_VERSION && git push origin $NEW_VERSION"
			"""
	}

	"release:prepare": {
		description: "Prepare for release by running full validation"
		dependencies: ["ci"]
		cache: false
		script: """
			VERSION=$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2)
			echo "🚀 Preparing release for version $VERSION"
			echo ""

			# Check if Cargo.lock is up to date
			if ! cargo update --dry-run 2>&1 | grep -q "nothing to do"; then
				echo "Warning: Cargo.lock may need updating. Run 'cargo update -p cuenv'"
			fi

			echo "Release checklist:"
			echo "✅ All CI checks passed"
			echo "✅ Version in Cargo.toml: $VERSION"
			echo "✅ Dependencies up to date"
			echo ""
			echo "Ready to tag and release version $VERSION"
			echo ""
			echo "Next steps:"
			echo "1. git tag v$VERSION"
			echo "2. git push origin v$VERSION"
			"""
	}

	// === Utility Tasks ===

	"deps:update": {
		description: "Update all dependencies"
		cache:       false
		script: """
			echo "Updating Rust dependencies..."
			cargo update

			echo "Updating Go dependencies in libcue-bridge..."
			cd libcue-bridge
			go get -u ./...
			go mod tidy
			cd ..

			echo "Dependencies updated!"
			echo "Run 'cuenv task test' to verify everything still works."
			"""
	}

	"deps:tree": {
		description: "Show dependency tree"
		command:     "cargo tree"
		cache:       false
	}

	"size": {
		description: "Show binary size information"
		dependencies: ["build"]
		cache: false
		script: """
			echo "Binary sizes:"
			ls -lh target/release/cuenv 2>/dev/null || echo "Release binary not found - run 'cuenv task build' first"
			ls -lh target/debug/cuenv 2>/dev/null || echo "Debug binary not found - run 'cuenv task build:debug' first"
			"""
	}

	// === Nix Helper Tasks ===

	"nix:vendor:update": {
		description: "Recalculates and updates the Cargo vendor hash in flake.nix"
		cache:       false
		inputs: ["Cargo.lock"] // Depends on Cargo.lock, but also modifies flake.nix
		script: """
			#!/bin/bash
			# This script safely updates the cargoVendor hash in flake.nix by forcing
			# Nix to re-calculate it. This is the standard, most reliable method.
			set -euo pipefail

			FLAKE_FILE="flake.nix"
			DUMMY_HASH="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="

			# Preserve the original file content to restore on any script failure.
			ORIGINAL_CONTENT=$(cat "$FLAKE_FILE")

			# Cleanup function to restore the flake file if the script exits unexpectedly.
			cleanup() {
				if [ -n "${ORIGINAL_CONTENT-}" ]; then
					echo "$ORIGINAL_CONTENT" > "$FLAKE_FILE"
					echo "Restored original flake.nix due to an error."
				fi
			}
			trap cleanup EXIT

			echo "Calculating new vendor hash by probing Nix..."

			# Find the cargo vendor hash line to be replaced.
			HASH_LINE=$(grep 'name = "cuenv-cargo-vendor";' -A 1 "$FLAKE_FILE" | grep 'hash =')
			if [ -z "$HASH_LINE" ]; then
				echo "Error: Could not find the cargoVendor hash line in $FLAKE_FILE."
				exit 1 # trap will restore the file
			fi

			# Replace the real hash with a dummy value to force Nix to report the correct one.
			TEMP_CONTENT=$(echo "$ORIGINAL_CONTENT" | sed "s|$HASH_LINE|                hash = \"$DUMMY_HASH\";|")
			echo "$TEMP_CONTENT" > "$FLAKE_FILE"

			# Run Nix to get the correct hash. This is expected to fail with a mismatch error.
			NIX_OUTPUT=$(nix flake check --quiet 2>&1) || true

			# Check if we got the expected hash mismatch error.
			if echo "$NIX_OUTPUT" | grep -q "hash mismatch"; then
				CORRECT_HASH=$(echo "$NIX_OUTPUT" | grep "got:" | awk '{print $2}')

				if [ -n "$CORRECT_HASH" ]; then
					echo "New hash found: $CORRECT_HASH"
					# Create the final version of flake.nix with the correct hash.
					FINAL_CONTENT=$(echo "$ORIGINAL_CONTENT" | sed "s|$HASH_LINE|                hash = \"$CORRECT_HASH\";|")
					echo "$FINAL_CONTENT" > "$FLAKE_FILE"

					# Disable the exit trap because we have succeeded.
					trap - EXIT
					echo "✅ flake.nix has been updated successfully."
					echo "   Please review and commit the changes."
				else
					echo "Error: Hash mismatch detected, but could not extract the new hash." >&2
					echo "$NIX_OUTPUT" >&2
					exit 1 # trap will restore the file
				fi
			else
				echo "Error: Expected a hash mismatch, but a different error occurred." >&2
				echo "It's possible the hash was already correct, or another Nix error happened." >&2
				echo "$NIX_OUTPUT" >&2
				exit 1 # trap will restore the file
			fi
			"""
	}
}