Added file scanner

This commit is contained in:
2023-10-30 16:17:41 +01:00
parent 5fe83543fd
commit 8b19989372
10 changed files with 178 additions and 71 deletions

63
src/build/Scan.go Normal file

@ -0,0 +1,63 @@
package build
import (
"os"
"path/filepath"
"strings"
"sync"
"git.akyoto.dev/cli/q/src/directory"
"git.akyoto.dev/cli/q/src/log"
)
// Scan scans the directory.
func Scan(path string) (<-chan *Function, <-chan error) {
functions := make(chan *Function, 16)
errors := make(chan error)
go func() {
scanDirectory(path, functions, errors)
close(functions)
close(errors)
}()
return functions, errors
}
// scanDirectory scans the directory without channel allocations.
func scanDirectory(path string, functions chan<- *Function, errors chan<- error) {
wg := sync.WaitGroup{}
directory.Walk(path, func(name string) {
if !strings.HasSuffix(name, ".q") {
return
}
fullPath := filepath.Join(path, name)
wg.Add(1)
go func() {
defer wg.Done()
err := scanFile(fullPath, functions)
if err != nil {
errors <- err
}
}()
})
wg.Wait()
}
// scanFile scans a single file.
func scanFile(path string, functions chan<- *Function) error {
log.Info.Println(path)
contents, err := os.ReadFile(path)
if err != nil {
return err
}
log.Info.Println(string(contents))
return nil
}