-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.nu
More file actions
461 lines (397 loc) · 11.7 KB
/
mod.nu
File metadata and controls
461 lines (397 loc) · 11.7 KB
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
# Root Module Aggregator with Lazy Loading and Dependency Resolution
#
# This module serves as the primary entry point for the entire module system,
# implementing a sophisticated dependency injection framework with topological
# sorting for correct initialization order.
export const MODULE_REGISTRY = {
version: "1.0.0"
modules: {
core: {
path: "./core/mod.nu"
exports: ["functional", "types", "iterators"]
dependencies: []
lazy: false # Core modules loaded eagerly
}
fs: {
path: "./fs/mod.nu"
exports: ["traversal", "atomic", "watchers"]
dependencies: ["core"]
lazy: true
}
data: {
path: "./data/mod.nu"
exports: ["transforms", "validators", "parsers"]
dependencies: ["core"]
lazy: true
}
net: {
path: "./net/mod.nu"
exports: ["http", "websocket", "dns"]
dependencies: ["core", "data"]
lazy: true
}
sys: {
path: "./sys/mod.nu"
exports: ["process", "signals", "resources"]
dependencies: ["core"]
lazy: true
}
git: {
path: "./git/mod.nu"
exports: ["status", "workflow", "hooks"]
dependencies: ["core", "fs"]
lazy: true
}
dev: {
path: "./dev/mod.nu"
exports: ["repl", "debug", "profile"]
dependencies: ["core", "sys"]
lazy: true
}
}
}
# Initialize module system with dependency resolution
export def --env init [] {
print "Initializing nushell module system..."
# Set up module environment
$env.NU_MODULES = {
loaded: {}
cache: {}
config: (load-module-config)
initialized: (date now)
}
# Load core modules eagerly
for module in ($MODULE_REGISTRY.modules | transpose key value) {
if not $module.value.lazy {
load-module $module.key
}
}
print "Module system initialized successfully"
}
# Topological sort for dependency resolution
def topological-sort [modules: record]: nothing -> list<string> {
mut sorted = []
mut visited = {}
mut temp_mark = {}
def visit [node: string, modules: record, visited: record, temp_mark: record]: nothing -> list<string> {
if $node in $temp_mark and ($temp_mark | get $node) {
error make { msg: $"Circular dependency detected at module: ($node)" }
}
if $node in $visited and ($visited | get $node) {
return []
}
$temp_mark = ($temp_mark | upsert $node true)
let deps = ($modules | get $node | get dependencies)
mut result = []
for dep in $deps {
$result = ($result | append (visit $dep $modules $visited $temp_mark))
}
$temp_mark = ($temp_mark | upsert $node false)
$visited = ($visited | upsert $node true)
$result | append $node
}
for module in ($modules | columns) {
if not ($module in $visited and ($visited | get $module)) {
$sorted = ($sorted | append (visit $module $modules $visited $temp_mark))
}
}
$sorted | uniq
}
# Dynamic module loader with caching
def load-module [name: string]: nothing -> any {
# Check if already loaded
if $name in $env.NU_MODULES.loaded {
return ($env.NU_MODULES.loaded | get $name)
}
# Get module metadata
let module_info = ($MODULE_REGISTRY.modules | get $name)
# Load dependencies first
for dep in $module_info.dependencies {
load-module $dep
}
# Load the module
let module_path = (
$env.FILE_PWD
| path join $module_info.path
| path expand
)
if not ($module_path | path exists) {
error make {
msg: $"Module not found: ($name)"
label: { text: $"Expected at: ($module_path)" }
}
}
# Import and cache
use $module_path *
$env.NU_MODULES.loaded = (
$env.NU_MODULES.loaded
| upsert $name {
path: $module_path
loaded: (date now)
exports: $module_info.exports
}
)
print $"Loaded module: ($name)"
}
# Load configuration from various sources
def load-module-config []: nothing -> record {
let config_paths = [
($env.HOME | path join ".config/nushell-modules/config.nu")
($env.FILE_PWD | path join "config/modules.nu")
"./modules.config.nu"
]
let config_path = (
$config_paths
| where ($it | path exists)
| first -n
)
if $config_path != null {
source $config_path
$env.MODULE_CONFIG
} else {
# Default configuration
{
lazy_loading: true
cache_enabled: true
cache_ttl: 1hr
auto_reload: false
debug: false
}
}
}
# Re-export commonly used functions from core modules
export use ./core/functional.nu [
compose
pipe
curry
partial
memoize
Y
fmap
lazy-seq
]
export use ./fs/traversal.nu [
bfs-traverse
dfs-traverse
find-duplicates
tree
atomic-write
sync-dirs
]
# Lazy module proxy generators
export def fs [...args]: nothing -> any {
load-module "fs"
# Dispatch to fs module commands
if ($args | length) == 0 {
help modules | where name =~ "^fs"
} else {
let cmd = ($args | first)
let rest = ($args | skip 1)
nu -c $"use ($env.NU_MODULES.loaded.fs.path) *; ($cmd) ...($rest)"
}
}
export def data [...args]: nothing -> any {
load-module "data"
# Similar dispatch pattern
}
export def net [...args]: nothing -> any {
load-module "net"
# Similar dispatch pattern
}
export def sys [...args]: nothing -> any {
load-module "sys"
# Similar dispatch pattern
}
export def git [...args]: nothing -> any {
load-module "git"
# Similar dispatch pattern
}
export def dev [...args]: nothing -> any {
load-module "dev"
# Similar dispatch pattern
}
# Module management commands
export def "modules list" [
--loaded # Show only loaded modules
--available # Show all available modules
]: nothing -> table {
if $loaded {
$env.NU_MODULES.loaded
| transpose key value
| each { |m|
{
name: $m.key
path: $m.value.path
loaded: $m.value.loaded
exports: ($m.value.exports | str join ", ")
}
}
} else {
$MODULE_REGISTRY.modules
| transpose key value
| each { |m|
{
name: $m.key
path: $m.value.path
dependencies: ($m.value.dependencies | str join ", ")
lazy: $m.value.lazy
loaded: ($m.key in $env.NU_MODULES.loaded)
}
}
}
}
export def "modules reload" [
module?: string # Specific module to reload (all if not specified)
]: nothing -> nothing {
if $module != null {
# Unload specific module
$env.NU_MODULES.loaded = (
$env.NU_MODULES.loaded | reject $module
)
load-module $module
} else {
# Reload all modules
let loaded = ($env.NU_MODULES.loaded | columns)
$env.NU_MODULES.loaded = {}
for mod in $loaded {
load-module $mod
}
}
print "Modules reloaded successfully"
}
export def "modules test" [
--module: string # Test specific module (all if not specified)
--verbose # Verbose output
]: nothing -> table {
let modules_to_test = if $module != null {
[$module]
} else {
$MODULE_REGISTRY.modules | columns
}
let results = ($modules_to_test | each { |mod|
print $"Testing module: ($mod)..."
let module_path = (
$env.FILE_PWD
| path join ($MODULE_REGISTRY.modules | get $mod | get path)
)
let test_result = try {
nu -c $"use ($module_path) test; test"
| from nuon
} catch {
{ passed: 0, failed: 1, skipped: 0, error: $in }
}
{
module: $mod
passed: $test_result.passed
failed: $test_result.failed
skipped: $test_result.skipped
status: (if $test_result.failed == 0 { "✅" } else { "❌" })
}
})
# Summary
let total = {
modules: ($results | length)
passed: ($results | get passed | math sum)
failed: ($results | get failed | math sum)
skipped: ($results | get skipped | math sum)
}
print $"\nTest Summary:"
print $" Modules tested: ($total.modules)"
print $" Tests passed: ($total.passed)"
print $" Tests failed: ($total.failed)"
print $" Tests skipped: ($total.skipped)"
$results
}
export def "modules bench" [
module: string
function: string
--iterations: int = 100
]: nothing -> record {
load-module $module
let module_path = (
$env.FILE_PWD
| path join ($MODULE_REGISTRY.modules | get $module | get path)
)
print $"Benchmarking ($module)::($function)..."
# Warmup
for _ in 0..<10 {
nu -c $"use ($module_path) ($function); ($function)"
}
# Measure
let times = (0..<$iterations | each { |_|
let start = (date now)
nu -c $"use ($module_path) ($function); ($function)"
let end = (date now)
($end - $start) | into int
})
{
module: $module
function: $function
iterations: $iterations
mean: ($times | math avg)
median: ($times | math median)
stddev: ($times | math stddev)
min: ($times | math min)
max: ($times | math max)
}
}
# Module documentation generator
export def "modules docs" [
--output: path = "docs/modules/" # Output directory
--format: string = "markdown" # Output format
]: nothing -> nothing {
mkdir $output
for module in ($MODULE_REGISTRY.modules | transpose key value) {
let module_name = $module.key
let module_info = $module.value
let doc_path = ($output | path join $"($module_name).md")
let doc_content = $"# Module: ($module_name)
## Overview
Path: `($module_info.path)`
Dependencies: ($module_info.dependencies | str join ", ")
Lazy Loading: ($module_info.lazy)
## Exports
($module_info.exports | each { |exp| $"- ($exp)" } | str join "\n")
## Functions
"
$doc_content | save -f $doc_path
print $"Generated documentation for ($module_name)"
}
print $"Documentation generated in ($output)"
}
# Main entry point
export def main [
--init # Initialize module system
--list # List modules
--test # Run tests
--bench # Run benchmarks
--docs # Generate documentation
]: nothing -> any {
if $init {
init
} else if $list {
modules list
} else if $test {
modules test
} else if $bench {
print "Specify module and function: modules bench <module> <function>"
} else if $docs {
modules docs
} else {
print "Nushell Module System
Commands:
modules list - List all modules
modules reload - Reload modules
modules test - Run module tests
modules bench - Benchmark module functions
modules docs - Generate documentation
Modules:
fs - Filesystem operations
data - Data transformations
net - Network operations
sys - System interface
git - Git integrations
dev - Development tools
"
}
}