-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add components count and statistics time to templates report
- Loading branch information
Your Name
committed
Nov 28, 2024
1 parent
a1155d4
commit d591887
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func main() { | ||
templatesDir := "../templates" | ||
stats := make(map[string]int) | ||
totalCount := 0 | ||
|
||
err := filepath.Walk(templatesDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() || strings.HasSuffix(strings.ToLower(info.Name()), ".md") { | ||
return nil | ||
} | ||
|
||
relPath, err := filepath.Rel(templatesDir, filepath.Dir(path)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if relPath == "." { | ||
stats["root"]++ | ||
} else { | ||
stats[relPath]++ | ||
} | ||
totalCount++ | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalf("Error walking templates directory: %v", err) | ||
} | ||
|
||
var report strings.Builder | ||
report.WriteString("# Templates Statistics\n\n") | ||
report.WriteString(fmt.Sprintf("Statistics Time: **%s**\n\n", time.Now().Format("2006-01-02 15:04:05"))) | ||
report.WriteString(fmt.Sprintf("Total POC count: **%d**\n", totalCount)) | ||
report.WriteString(fmt.Sprintf("Total Components: **%d**\n\n", len(stats))) | ||
report.WriteString("## POC Distribution\n\n") | ||
|
||
// 对目录名进行排序 | ||
var dirs []string | ||
for dir := range stats { | ||
dirs = append(dirs, dir) | ||
} | ||
sort.Strings(dirs) | ||
|
||
for _, dir := range dirs { | ||
report.WriteString(fmt.Sprintf("- %s: %d\n", dir, stats[dir])) | ||
} | ||
report.WriteString("\n") | ||
|
||
readmePath := filepath.Join(templatesDir, "README.md") | ||
existingContent := "" | ||
if data, err := os.ReadFile(readmePath); err == nil { | ||
existingContent = string(data) | ||
} | ||
|
||
if strings.Contains(existingContent, "# Templates Statistics") { | ||
parts := strings.Split(existingContent, "# Templates Statistics") | ||
existingContent = parts[0] | ||
} | ||
|
||
finalContent := strings.TrimSpace(existingContent) + "\n\n" + report.String() | ||
|
||
err = os.WriteFile(readmePath, []byte(finalContent), 0644) | ||
if err != nil { | ||
log.Fatalf("Error writing to README.md: %v", err) | ||
} | ||
|
||
fmt.Printf("Statistics updated in %s\n", readmePath) | ||
} |