Skip to content

Commit

Permalink
list all subdirectories on index
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Oct 5, 2024
1 parent 0d563bc commit 88c73f0
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 75 deletions.
30 changes: 9 additions & 21 deletions misc/stdlib_diff/packagediff.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package main

import (
"errors"
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
)
Expand Down Expand Up @@ -158,27 +155,18 @@ func (p *PackageDiffChecker) hasSameNumberOfFiles() bool {
// listDirFiles returns a list of file names in the specified directory.
func listDirFiles(dirPath string) ([]string, error) {
fileNames := make([]string, 0)
err := filepath.WalkDir(dirPath, func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
// Avoid returning an error if the file does not exists on source or destination.
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
dirEntries, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}

for _, dirEntry := range dirEntries {
// Only list .go and .gno files
if !strings.Contains(path, ".go") && !strings.Contains(path, ".gno") {
return nil
}

if dirEntry != nil && !dirEntry.IsDir() {
filenameWithSubfolder := strings.TrimPrefix(path, dirPath+"/")
fileNames = append(fileNames, filenameWithSubfolder)
if !strings.Contains(dirEntry.Name(), ".go") && !strings.Contains(dirEntry.Name(), ".gno") {
continue
}

return nil
})
fileNames = append(fileNames, dirEntry.Name())
}

return fileNames, err
}
167 changes: 114 additions & 53 deletions misc/stdlib_diff/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
_ "embed"
"fmt"
"html/template"
"io/fs"
"os"
"path/filepath"
"strings"
)

var (
Expand Down Expand Up @@ -42,9 +45,10 @@ type IndexTemplate struct {
}

type LinkToReport struct {
PathToReport string
PackageName string
WasFound bool
PathToReport string
PackageName string
WasFound bool
Subdirectories []LinkToReport
}

// NewReportBuilder creates a new ReportBuilder instance with the specified
Expand Down Expand Up @@ -86,38 +90,28 @@ func (builder *ReportBuilder) Build() error {
}

for _, directory := range directories {
if directory.FoundInDest {
srcPackagePath := builder.SrcPath + "/" + directory.Path
dstPackagePath := builder.DstPath + "/" + directory.Path
packageChecker, err := NewPackageDiffChecker(srcPackagePath, dstPackagePath, builder.SrcIsGno)
if err != nil {
return fmt.Errorf("can't create new PackageDiffChecker: %w", err)
}

differences, err := packageChecker.Differences()
if err != nil {
return fmt.Errorf("can't compute differences: %w", err)
}

data := &PackageDiffTemplateData{
PackageName: directory.Path,
SrcFilesCount: len(packageChecker.SrcFiles),
SrcPackageLocation: srcPackagePath,
DstFileCount: len(packageChecker.DstFiles),
DstPackageLocation: dstPackagePath,
FilesDifferences: differences.FilesDifferences,
}

if err := builder.writePackageTemplate(data, directory.Path); err != nil {
if err := builder.ExecuteDiffTemplate(directory); err != nil {
return err
}
report := LinkToReport{
PathToReport: "./" + directory.Path + "/report.html",
PackageName: directory.Path,
WasFound: directory.FoundInDest,
Subdirectories: make([]LinkToReport, 0),
}
for _, subDirectory := range directory.Children {
if err := builder.ExecuteDiffTemplate(subDirectory); err != nil {
return err
}
report.Subdirectories = append(report.Subdirectories, LinkToReport{
PathToReport: "./" + subDirectory.Path + "/report.html",
PackageName: subDirectory.Path,
WasFound: subDirectory.FoundInDest,
})

}
indexTemplateData.Reports = append(indexTemplateData.Reports, report)

indexTemplateData.Reports = append(indexTemplateData.Reports, LinkToReport{
PathToReport: "./" + directory.Path + "/report.html",
PackageName: directory.Path,
WasFound: directory.FoundInDest,
})
}

if err := builder.writeIndexTemplate(indexTemplateData); err != nil {
Expand All @@ -127,45 +121,112 @@ func (builder *ReportBuilder) Build() error {
return nil
}

type Directory struct {
Path string
FoundInDest bool
}
func (builder *ReportBuilder) ExecuteDiffTemplate(directory *Directory) error {
if !directory.FoundInDest {
return nil
}

// listSrcDirectories retrieves a list of directories in the source path.
func (builder *ReportBuilder) listSrcDirectories() ([]Directory, error) {
dirEntries, err := os.ReadDir(builder.SrcPath)
srcPackagePath := builder.SrcPath + "/" + directory.Path
dstPackagePath := builder.DstPath + "/" + directory.Path
packageChecker, err := NewPackageDiffChecker(srcPackagePath, dstPackagePath, builder.SrcIsGno)
if err != nil {
return nil, err
return fmt.Errorf("can't create new PackageDiffChecker: %w", err)
}

destDirectories, err := builder.getSrcDirectories()
differences, err := packageChecker.Differences()
if err != nil {
return nil, err
return fmt.Errorf("can't compute differences: %w", err)
}

directories := make([]Directory, 0)
for _, dirEntry := range dirEntries {
if dirEntry.IsDir() {
directories = append(directories, Directory{FoundInDest: destDirectories[dirEntry.Name()], Path: dirEntry.Name()})
}
data := &PackageDiffTemplateData{
PackageName: directory.Path,
SrcFilesCount: len(packageChecker.SrcFiles),
SrcPackageLocation: srcPackagePath,
DstFileCount: len(packageChecker.DstFiles),
DstPackageLocation: dstPackagePath,
FilesDifferences: differences.FilesDifferences,
}
return directories, nil

return builder.writePackageTemplate(data, directory.Path)
}

type Directory struct {
Path string
FoundInDest bool
Children []*Directory
}

func (builder *ReportBuilder) getSrcDirectories() (map[string]bool, error) {
dirEntries, err := os.ReadDir(builder.DstPath)
// listSrcDirectories retrieves a list of directories in the source path.
func (builder *ReportBuilder) listSrcDirectories() ([]*Directory, error) {
destDirectories, err := builder.getDstDirectories()
if err != nil {
return nil, err
}

notfoundInDest := []string{}
directories := make(map[string]*Directory)
res := make([]*Directory, 0)
err = filepath.WalkDir(builder.SrcPath, func(path string, dirEntry fs.DirEntry, err error) error {
if path == builder.SrcPath {
return nil
}

folderName := strings.TrimPrefix(path, builder.SrcPath+"/")

// skip directories that are not found in the destination
for _, prefix := range notfoundInDest {
if strings.HasPrefix(folderName, prefix) {
return nil
}
}

if err != nil {
return err
}

if !dirEntry.IsDir() {
return nil
}

newDir := &Directory{
Path: folderName,
FoundInDest: destDirectories[folderName],
Children: make([]*Directory, 0),
}

if isRootFolder(folderName) {
directories[folderName] = newDir
res = append(res, newDir)
} else {
directory := directories[getRootFolder(folderName)]
directory.Children = append(directory.Children, newDir)
directories[getRootFolder(folderName)] = directory
}

if !destDirectories[dirEntry.Name()] {
notfoundInDest = append(notfoundInDest, folderName)
}
return nil
})

return res, err
}
func isRootFolder(path string) bool {
return !strings.Contains(path, "/")
}
func getRootFolder(path string) string {
return strings.Split(path, "/")[0]
}
func (builder *ReportBuilder) getDstDirectories() (map[string]bool, error) {
directories := make(map[string]bool)
for _, dirEntry := range dirEntries {
err := filepath.WalkDir(builder.DstPath, func(path string, dirEntry fs.DirEntry, err error) error {
if dirEntry.IsDir() {
directories[dirEntry.Name()] = true
folderName := strings.TrimPrefix(path, builder.DstPath+"/")
directories[folderName] = true
}
}
return directories, nil
return nil
})
return directories, err
}

// writeIndexTemplate generates and writes the index template with the given output paths.
Expand Down
23 changes: 22 additions & 1 deletion misc/stdlib_diff/templates/index_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,28 @@ <H1>List of packages processed</H1>
<ul style="margin-bottom: 2rem;">
{{- range .Reports}}
{{- if .WasFound}}
<li><a href="{{.PathToReport}}">{{.PackageName}}</a>
{{if .Subdirectories}}
<details>
<summary>{{.PackageName}}</summary>
<ul>
<li><a href="{{.PathToReport}}">{{.PackageName}}</a></li>
</ul>
{{- range .Subdirectories}}
{{- if .WasFound}}
<ul>
<li><a href="{{.PathToReport}}">{{.PackageName}}</a></li>
</ul>
{{- else}}
<ul>
<li style="display: flex;" ><p style="color: red;">{{.PackageName}}</p>: missing in go</a></li>
</ul>
{{- end}}
{{- end}}
</details>
{{- else}}
<a href="{{.PathToReport}}">{{.PackageName}}</a>

{{- end}}
{{- else}}
<li style="display: flex;" ><p style="color: red;">{{.PackageName}}</p>: missing in gno</a>
{{- end}}
Expand Down

0 comments on commit 88c73f0

Please sign in to comment.