wsb 0.83.223988

Workspace - A tool suite for file operations, version management, and development workflow automation
Documentation
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
---
layout: default
title: Refactor - Code Refactoring Guide
---

# Refactor - Code Refactoring Guide

The `wsb refactor` tool is a string replacement engine designed for safe code refactoring and content modification across codebases.

## Overview

**Refac** (short for "refactor") provides string replacement capabilities with:
- **Pre-validation**: Tests all operations before execution
- **Collision Detection**: Prevents overwrites and conflicts
- **Multi-threading**: Parallel processing for large codebases
- **Language Awareness**: Handling of code structures
- **Safety Features**: Atomic operations with rollback capability

## Basic Usage

### Command Syntax
```bash
wsb refactor <directory> <old_string> <new_string> [OPTIONS]
```

### First Steps
```bash
# Refac always previews changes and asks for confirmation
wsb refactor ./src "OldClassName" "NewClassName" --verbose

# Apply with backup for safety
wsb refactor ./src "OldClassName" "NewClassName" --backup

# Target specific file types
wsb refactor ./src "old_api" "new_api" --include "*.rs" --include "*.toml"
```

## Core Features

### Pre-Operation Validation
Refac validates every operation before making changes:

```bash
# Validation process (automatic):
# 1. File accessibility check
# 2. Encoding validation
# 3. Permission verification
# 4. Collision detection
# 5. Path length validation
# 6. Disk space check
```

**Validation Output Example**:
```
INFO: Phase 1: Discovering files and directories...
INFO: Phase 2: Checking for naming collisions...
INFO: Phase 3: Validating all operations...
INFO: Validation passed: All operations can be performed safely.
```

### 🔍 Built-in Change Preview
Refac always shows changes before applying them and asks for confirmation:

```bash
# Basic operation (shows preview automatically)
wsb refactor . "oldname" "newname"

# Verbose output with detailed information
wsb refactor . "oldname" "newname" --verbose

# JSON output for scripting (still shows preview)
wsb refactor . "oldname" "newname" --format json
```

**Preview Output Example**:
```
=== CHANGE SUMMARY ===
Content modifications: 15 file(s)
File renames:         8 file(s)
Directory renames:    3 directory(ies)
Total changes:        26

FILES TO BE MODIFIED:
  src/main.rs: 3 occurrence(s)
  src/lib.rs: 1 occurrence(s)
  tests/integration.rs: 2 occurrence(s)

FILES TO BE RENAMED:
  oldname_config.toml → newname_config.toml
  src/oldname_module.rs → src/newname_module.rs
```

### ⚡ Multi-Threading and Performance
Optimized parallel processing for large datasets:

```bash
# Use multiple threads for faster processing
wsb refactor . "oldname" "newname" --threads 8

# Auto-detect optimal thread count
wsb refactor . "oldname" "newname" --threads 0

# Progress tracking for long operations
wsb refactor . "oldname" "newname" --progress always
```

**Performance Features**:
- **Parallel Content Processing**: Multi-threaded file content replacement
- **Streaming I/O**: Efficient handling of large files
- **Smart Filtering**: Pre-filter files to reduce processing overhead
- **Progress Tracking**: Visual progress bars with ETA estimates

## Operation Modes

### 🎯 Targeted Operations
Control exactly what gets modified:

```bash
# Only rename files and directories (skip content)
wsb refactor . "oldproject" "newproject" --names-only

# Only replace content (skip renaming)
wsb refactor . "old.api.com" "new.api.com" --content-only

# Only process files (skip directories)
wsb refactor . "oldname" "newname" --files-only

# Only process directories (skip files)
wsb refactor . "oldname" "newname" --dirs-only
```

### 📁 Pattern Filtering
Precise control over which files are processed:

```bash
# Include specific file types
wsb refactor . "oldname" "newname" \
  --include "*.rs" \
  --include "*.toml" \
  --include "*.md"

# Exclude unwanted areas
wsb refactor . "oldname" "newname" \
  --exclude "target/*" \
  --exclude "*.log" \
  --exclude ".git/*"

# Complex filtering
wsb refactor ./src "OldStruct" "NewStruct" \
  --include "*.rs" \
  --exclude "*/tests/*" \
  --exclude "*/examples/*"
```

### 🏗️ Directory Depth Control
Manage traversal depth for large projects:

```bash
# Limit to current directory only
wsb refactor . "oldname" "newname" --max-depth 1

# Search 3 levels deep
wsb refactor . "oldname" "newname" --max-depth 3

# Unlimited depth (default)
wsb refactor . "oldname" "newname" --max-depth 0
```

## Advanced Features

### 💾 Backup and Recovery
Safe modification with automatic backups:

```bash
# Create backups before modifying files
wsb refactor . "oldname" "newname" --backup

# Backups are created with .refac_backup extension
# Example: config.toml → config.toml.refac_backup
```

**Backup Features**:
- **Atomic Backup Creation**: Backups created before any modifications
- **Conflict Resolution**: Unique backup names if backups already exist
- **Selective Backup**: Only backs up files that will be modified
- **Manual Cleanup**: Backups remain until manually removed

### 🔒 Safety and Error Handling
Mission-critical safety features:

```bash
# Force operation without confirmation
wsb refactor . "oldname" "newname" --force

# Case-sensitive matching (default)
wsb refactor . "OldName" "NewName"

# Show detailed error information
wsb refactor . "oldname" "newname" --verbose
```

**Safety Guarantees**:
- **Collision Prevention**: Detects and prevents overwrite conflicts
- **Binary File Protection**: Automatically skips binary files for content operations
- **Atomic Operations**: Either all operations succeed or none are applied
- **Permission Respect**: Respects file system permissions and ownership

### 🌐 Cross-Platform Compatibility
Consistent behavior across all platforms:

```bash
# Works identically on Windows, macOS, and Linux
wsb refactor . "oldname" "newname"

# Handles platform-specific path separators
wsb refactor . "old\\path" "new/path"  # Windows
wsb refactor . "old/path" "new/path"   # Unix-like
```

**Platform Features**:
- **Path Normalization**: Automatic path separator handling
- **Case Sensitivity**: Respects filesystem case sensitivity settings
- **Permission Handling**: Platform-appropriate permission management
- **Character Encoding**: UTF-8 support with platform-specific handling

## Real-World Use Cases

### 🔄 API Migration
Migrate from old API to new API across entire codebase:

```bash
# 1. Review the migration (wsb refactor shows changes before applying)
wsb refactor ./src "old_api::Client" "new_api::Client" --verbose

# 2. Update import statements
wsb refactor ./src "use old_api" "use new_api" --content-only --include "*.rs"

# 3. Update function calls
wsb refactor ./src "old_api::connect" "new_api::connect" --content-only

# 4. Update configuration files
wsb refactor ./config "old_api_endpoint" "new_api_endpoint" --content-only --include "*.toml"
```

### 🏢 Project Rebranding
Rename a project throughout the codebase:

```bash
# 1. Update package names
wsb refactor . "oldproject" "newproject" --include "Cargo.toml" --include "package.json"

# 2. Update file names and directory structure
wsb refactor . "oldproject" "newproject" --names-only

# 3. Update content references
wsb refactor . "oldproject" "newproject" --content-only --exclude "target/*"

# 4. Update documentation
wsb refactor ./docs "OldProject" "NewProject" --include "*.md"
```

### 🏗️ Refactoring Code Structure
Reorganize and rename code components:

```bash
# 1. Rename a module throughout the codebase
wsb refactor ./src "user_service" "account_service" --include "*.rs"

# 2. Update struct names
wsb refactor ./src "UserData" "AccountData" --content-only --include "*.rs"

# 3. Update configuration keys
wsb refactor ./config "user_" "account_" --content-only --include "*.toml" --include "*.yaml"
```

### 🌍 Configuration Updates
Update configuration across multiple environments:

```bash
# Update API endpoints across all configs
wsb refactor ./config "api.old.com" "api.new.com" \
  --content-only \
  --include "*.toml" \
  --include "*.yaml" \
  --include "*.json" \
  --include "*.env"

# Update database connection strings
wsb refactor ./config "old_database" "new_database" \
  --content-only \
  --backup
```

## Output Formats

### 📊 Human-Readable Output
Default format with clear progress information:

```
=== REFAC TOOL ===
Root directory: /path/to/project
Old string: 'oldname'
New string: 'newname'
Mode: Full

Phase 1: Discovering files and directories...
Found 1,234 files and 56 directories to process

Phase 2: Checking for naming collisions...
No collisions detected

Phase 3: Validating all operations...
Validation passed: All operations can be performed safely.

=== CHANGE SUMMARY ===
Content modifications: 15 file(s)
File renames:         8 file(s)
Directory renames:    3 directory(ies)
Total changes:        26

Do you want to proceed? (y/N) y

Replacing content in files...
[████████████████████████████████████████] 15/15 files

Renaming files and directories...
[████████████████████████████████████████] 11/11 items

=== OPERATION COMPLETE ===
Operation completed successfully!
Total changes applied: 26
```

### 🤖 JSON Output
Machine-readable format for automation:

```bash
wsb refactor . "oldname" "newname" --format json
```

```json
{
  "summary": {
    "content_changes": 15,
    "file_renames": 8,
    "directory_renames": 3,
    "total_changes": 26
  },
  "operations": [
    {
      "type": "content_replace",
      "file": "src/main.rs",
      "occurrences": 3
    },
    {
      "type": "file_rename",
      "from": "oldname_config.toml",
      "to": "newname_config.toml"
    }
  ],
  "result": "success",
  "interactive": false,
  "execution_time_ms": 1234
}
```

### 📝 Plain Text Output
Minimal output for scripting:

```bash
wsb refactor . "oldname" "newname" --format plain
```

```
Content changes: 15
File renames: 8
Directory renames: 3
Total changes: 26
```

## Command Line Reference

### Essential Options
| Option | Short | Description |
|--------|-------|-------------|
| `--assume-yes` | `-y` | Skip confirmation prompts (non-interactive mode) |
| `--verbose` | `-v` | Show detailed output |
| `--backup` | `-b` | Create backup files before modification |

### Operation Modes
| Option | Description |
|--------|-------------|
| `--names-only` | Only rename files/directories, skip content |
| `--content-only` | Only replace content, skip renaming |
| `--files-only` | Only process files, skip directories |
| `--dirs-only` | Only process directories, skip files |

### Filtering Options
| Option | Description |
|--------|-------------|
| `--include <pattern>` | Include only files matching pattern |
| `--exclude <pattern>` | Exclude files matching pattern |
| `--max-depth <n>` | Maximum depth to search (0 = unlimited) |
| `--follow-symlinks` | Follow symbolic links |

### Performance Options
| Option | Short | Description |
|--------|-------|-------------|
| `--threads <n>` | `-j` | Number of threads (0 = auto) |
| `--progress <mode>` | | Progress display: auto, always, never |

### Output Options
| Option | Description |
|--------|-------------|
| `--format <format>` | Output format: human, json, plain |
| `--quiet` | Suppress non-essential output |

## Best Practices

### 🛡️ Safety First
1. **Review Changes Carefully**: Refac shows all changes before applying them
2. **Use Backups**: Enable `--backup` for important files
3. **Test on Copies**: Work on a copy of important directories
4. **Version Control**: Ensure files are committed before major refactoring
5. **Incremental Changes**: Make small, targeted changes rather than large ones

### ⚡ Performance Optimization
1. **Use Filters**: Limit scope with `--include` and `--exclude` patterns
2. **Adjust Threading**: Use `--threads` for optimal performance
3. **Limit Depth**: Use `--max-depth` for deep directory structures
4. **Target Specific Modes**: Use operation modes to limit processing scope

### 🎯 Effective Patterns
1. **Be Specific**: Use precise patterns to avoid unintended matches
2. **Case Sensitivity**: Be aware of case-sensitive matching behavior
3. **Escape Special Characters**: Quote strings with special characters
4. **Test Patterns**: Use verbose mode to verify pattern matching behavior

### 📋 Workflow Integration
1. **CI/CD Integration**: Use JSON output for automated workflows
2. **Script Integration**: Use exit codes for error handling
3. **Monitoring**: Use verbose output for debugging and auditing
4. **Documentation**: Document large refactoring operations for team awareness

## Troubleshooting

### Common Issues

**"Permission denied" errors**
```bash
# Check file permissions
ls -la affected_file.txt

# Run with appropriate permissions
sudo wsb refactor . "oldname" "newname"  # Use carefully
```

**"No changes found" when changes expected**
```bash
# Use verbose mode to see what's being processed
wsb refactor . "oldname" "newname" --verbose

# Check case sensitivity
wsb refactor . "OldName" "NewName"  # vs "oldname" "newname"

# Verify include/exclude patterns
wsb refactor . "oldname" "newname" --include "*.txt" --verbose
```

**"Naming collision detected"**
```bash
# Review the collision report (shown automatically)
wsb refactor . "oldname" "newname" --verbose

# Resolve conflicts manually before proceeding
mv conflicting_file.txt conflicting_file_backup.txt
wsb refactor . "oldname" "newname"
```

**Binary files not being processed**
```bash
# This is by design for safety
# Use verbose mode to see which files are skipped
wsb refactor . "oldname" "newname" --verbose --content-only
```

### Debug Mode
For detailed debugging information:

```bash
# Maximum verbosity (shows preview automatically)
wsb refactor . "oldname" "newname" --verbose --progress always

# Check specific file processing
wsb refactor specific_file.txt "oldname" "newname" --verbose
```

## Integration Examples

### With Git Workflows
```bash
# Safe refactoring workflow
git checkout -b refactor-api-names
git add .
git commit -m "Checkpoint before refactoring"

wsb refactor ./src "old_api" "new_api" --verbose
wsb refactor ./src "old_api" "new_api" --backup

git add .
git commit -m "Refactor API names from old_api to new_api"
```

### With Build Systems
```bash
# Update build configurations
wsb refactor . "old_target_name" "new_target_name" \
  --include "Makefile" \
  --include "*.cmake" \
  --include "*.toml" \
  --content-only
```

### With CI/CD Pipelines
```bash
#!/bin/bash
# Automated refactoring script
set -e

# Validate refactoring first (using assume-yes for non-interactive)
if wsb refactor ./src "$OLD_NAME" "$NEW_NAME" --assume-yes --format json > refac_plan.json; then
    echo "Refactoring plan validated"
    
    # Apply changes
    wsb refactor ./src "$OLD_NAME" "$NEW_NAME" --format json > refac_result.json
    
    # Verify success
    if [ $? -eq 0 ]; then
        echo "Refactoring completed successfully"
        exit 0
    else
        echo "Refactoring failed"
        exit 1
    fi
else
    echo "Refactoring validation failed"
    exit 1
fi
```

The `wsb refactor` tool provides comprehensive, safe, and efficient string replacement capabilities for any scale of refactoring operation, from small tweaks to large-scale codebase transformations.