Skip to content

Commit

Permalink
fix: generated files not written correctly (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbinz authored Aug 27, 2024
1 parent 85aac93 commit 4757cc4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Generate(inPath, outPath string, verbose, dry, check bool) error {
}

if verbose {
if err := out.Dry(outPath); err != nil {
if err := out.Dry(absDir); err != nil {
return err
}
}
Expand All @@ -81,5 +81,5 @@ func Generate(inPath, outPath string, verbose, dry, check bool) error {
return nil
}

return out.Flush(outPath)
return out.Flush(absDir)
}
53 changes: 29 additions & 24 deletions core/util/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ func (fs *FS) Flush(dir string) error {
return fmt.Errorf("failed to create dir %s: %w", dir, err)
}

dirs := make(map[string]struct{})
subDirs := make(map[string]struct{})

for path := range fs.writes {
dirs[filepath.Dir(filepath.Join(dir, path))] = struct{}{}
subDirs[filepath.Dir(filepath.Join(dir, path))] = struct{}{}
}

for path := range fs.writer {
dirs[filepath.Dir(filepath.Join(dir, path))] = struct{}{}
subDirs[filepath.Dir(filepath.Join(dir, path))] = struct{}{}
}

for _, dir := range maps.Keys(dirs) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create dir %s: %w", dir, err)
for _, subDir := range maps.Keys(subDirs) {
if err := os.MkdirAll(subDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create dir %s: %w", subDir, err)
}
}

Expand Down Expand Up @@ -101,40 +101,45 @@ func (fs *FS) Dry(dir string) error {
changes[file] = &valTrue
}

err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if _, err := os.Stat(dir); os.IsExist(err) {
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil // TODO: mark directories that are removed?
}
if d.IsDir() {
return nil // TODO: mark directories that are removed?
}

path = strings.TrimPrefix(path, dir+string(os.PathSeparator))
path = strings.TrimPrefix(path, dir+string(os.PathSeparator))

if slices.Contains(allFiles, path) {
changes[path] = nil
return nil
}
if slices.Contains(allFiles, path) {
changes[path] = nil
return nil
}

changes[path] = &valFalse
changes[path] = &valFalse

allFiles = append(allFiles, path)
allFiles = append(allFiles, path)

return nil
})
if err != nil {
return fmt.Errorf("failed to walk dir %s: %w", dir, err)
return nil
})
if err != nil {
return fmt.Errorf("failed to walk dir %s: %w", dir, err)
}
}

slices.Sort(allFiles)

for _, file := range allFiles {
switch changes[file] {

case nil:
fmt.Println("× " + filepath.Join(dir, file)) // TODO: only if content changed? (±)

case &valTrue:
fmt.Println("+ " + filepath.Join(dir, file))

case &valFalse:
fmt.Println("- " + filepath.Join(dir, file))
}
Expand Down
2 changes: 1 addition & 1 deletion core/util/gomod/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
pkgSOM = "github.com/go-surreal/som"
pkgSDBC = "github.com/go-surreal/sdbc"

requiredSOMVersion = "v0.6.0"
requiredSOMVersion = "v0.6.1"

requiredSDBCVersion = "v0.6.1" // v.0.7.0 already uses go 1.23
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22

toolchain go1.22.6

retract [v0.1.0, v0.5.0] // only the latest version is supported for now
retract [v0.1.0, v0.6.0] // only the latest version is supported for now

require (
github.com/dave/jennifer v1.7.0
Expand Down

0 comments on commit 4757cc4

Please sign in to comment.