-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_lesson.ps1
More file actions
103 lines (92 loc) · 4.02 KB
/
build_lesson.ps1
File metadata and controls
103 lines (92 loc) · 4.02 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
# This script generates a summary of a Rust project, including the project structure
# and the contents of all .rs and .toml files, excluding 'target' and hidden directories.
# The output is written to lesson-00-minimum.md, with a .txt copy created if needed.
# Run this script from the root of the Rust project using PowerShell.
# Define the output file
$outputFile = "lesson-01-standard.md"
# Function to generate a directory tree
function Get-DirectoryTree {
param (
[string]$Path = '.',
[string]$Prefix = ''
)
# Get all items (files and directories) excluding 'target' and hidden items
$items = Get-ChildItem -Path $Path | Where-Object { $_.Name -ne 'target' -and $_.Name -notlike '.*' }
$count = $items.Count
for ($i = 0; $i -lt $count; $i++) {
$item = $items[$i]
$last = $i -eq ($count - 1)
# Use ASCII characters for tree structure
$itemPrefix = if ($last) { '\-- ' } else { '+-- ' }
Write-Output ($Prefix + $itemPrefix + $item.Name)
if ($item.PSIsContainer) {
if ($last) {
$newPrefix = $Prefix + ' '
} else {
$newPrefix = $Prefix + '| '
}
Get-DirectoryTree -Path $item.FullName -Prefix $newPrefix
}
}
}
# Check if README.md exists and initialize the file with its content
$readmePath = "README.md"
if (Test-Path $readmePath) {
# Start with README.md content
Get-Content $readmePath | Set-Content -Path $outputFile
Write-Output "" | Add-Content -Path $outputFile
Write-Output "---" | Add-Content -Path $outputFile
Write-Output "" | Add-Content -Path $outputFile
} else {
# Initialize empty file if no README
"" | Set-Content -Path $outputFile
}
# Add the Lesson 00: Minimum section
Write-Output "# Lesson 01: Standard" | Add-Content -Path $outputFile
Write-Output "" | Add-Content -Path $outputFile
Write-Output "## Project Structure" | Add-Content -Path $outputFile
Write-Output "" | Add-Content -Path $outputFile
Write-Output '```' | Add-Content -Path $outputFile
Write-Output "." | Add-Content -Path $outputFile
Get-DirectoryTree -Path '.' -Prefix '' | Add-Content -Path $outputFile
Write-Output '```' | Add-Content -Path $outputFile
Write-Output "" | Add-Content -Path $outputFile
# Find all Cargo.toml files to identify Rust project directories
$cargoTomls = Get-ChildItem -Recurse -Filter Cargo.toml -File
# Process each Rust project
foreach ($cargoToml in $cargoTomls) {
$projectDir = $cargoToml.Directory.FullName
# Find all *.toml and *.rs files, excluding those in 'target' or hidden directories
$files = Get-ChildItem -Path $projectDir -Recurse -File -Include *.toml,*.rs | Where-Object {
$_.FullName -notmatch '\\target\\' -and $_.FullName -notmatch '\\\.[^\\]+\\'
}
foreach ($file in $files) {
# Get the relative path for consistency with typical script outputs
$relativePath = Resolve-Path -Path $file.FullName -Relative
# Read the file content as a single string
$fileContent = Get-Content $file.FullName -Raw
# Ensure the content ends with a newline
if (-not $fileContent.EndsWith("`n")) {
$fileContent += "`n"
}
# Create the code block
$codeBlockOpen = switch ($file.Extension.ToLower()) {
'.rs' { '```rust' }
'.toml' { '```toml' }
default { '```text' }
}
# Add markdown header and code block to the file
Add-Content -Path $outputFile -Value "## $relativePath"
Add-Content -Path $outputFile -Value ""
Add-Content -Path $outputFile -Value $codeBlockOpen
Add-Content -Path $outputFile -Value "`n"
Add-Content -Path $outputFile -Value $fileContent
Add-Content -Path $outputFile -Value '```'
Add-Content -Path $outputFile -Value ""
}
}
# If the output file does not end with .txt, create a copy with .txt appended
if (-not $outputFile.EndsWith('.txt')) {
$txtFile = "..\$outputFile.txt"
Copy-Item -Path $outputFile -Destination $txtFile
}