Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: default to delete orphaned files, with -keepOrphanedFiles argument flag to disable feature #325

Merged
merged 9 commits into from
Dec 17, 2023
36 changes: 34 additions & 2 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type Arguments struct {
GenerateSourceMapVisualisations bool
IncludeVersion bool
IncludeTimestamp bool
// PPROFPort is the port to run the pprof server on.
PPROFPort int
PPROFPort int // PPROFPort is the port to run the pprof server on.
YiumPotato marked this conversation as resolved.
Show resolved Hide resolved
KeepOrphanedFiles bool
}

var defaultWorkerCount = runtime.NumCPU()
Expand Down Expand Up @@ -125,6 +125,38 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
p = proxy.New(args.ProxyPort, target)
}

if !args.KeepOrphanedFiles {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right place to do this work, because it means walking through the directory tree twice which is relatively expensive to do.

If we can combine the deletion of orphaned files with the generation, I think that would be better. Also, I'm wondering if the deletion of the orphaned file should be a warning or some other level to make it stand out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% right, I was on something. Passed the argument to the processFile function (would that be the way?), checked it and used logWarning for the message.

// By default deletes all generated orphaned _templ.go files
err = filepath.WalkDir(args.Path, func(fileName string, info os.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() && shouldSkipDir(fileName) {
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(fileName, "_templ.go") {
// lets make sure the generated file is orphaned
// by checking if the corresponding .templ file exists
templFileName := strings.TrimSuffix(fileName, "_templ.go") + ".templ"
if _, err := os.Stat(templFileName); err == nil {
// the .templ file exists, so we don't delete the generated file
return nil
}
if err = os.Remove(fileName); err != nil {
return fmt.Errorf("failed to remove file: %w", err)
}
logSuccess(w, "Deleted orphaned file %q in %s\n", fileName, time.Since(start))
}
return nil
})
if err != nil {
return fmt.Errorf("failed to delete generated files: %w", err)
}
}

fmt.Fprintln(w, "Processing path:", args.Path)
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = time.Millisecond * 500
Expand Down
4 changes: 4 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Args:
Number of workers to use when generating code. (default runtime.NumCPUs)
-pprof
Port to run the pprof server on.
-keep-orphaned-files
Keeps orphaned generated templ files. (default false)
-help
Print help and exit.

Expand Down Expand Up @@ -120,6 +122,7 @@ func generateCmd(w io.Writer, args []string) (code int) {
proxyPortFlag := cmd.Int("proxyport", 7331, "")
workerCountFlag := cmd.Int("w", runtime.NumCPU(), "")
pprofPortFlag := cmd.Int("pprof", 0, "")
keepOrphanedFilesFlag := cmd.Bool("keep-orphaned-files", false, "")
helpFlag := cmd.Bool("help", false, "")
err := cmd.Parse(args)
if err != nil || *helpFlag {
Expand All @@ -138,6 +141,7 @@ func generateCmd(w io.Writer, args []string) (code int) {
IncludeVersion: *includeVersionFlag,
IncludeTimestamp: *includeTimestampFlag,
PPROFPort: *pprofPortFlag,
KeepOrphanedFiles: *keepOrphanedFilesFlag,
})
if err != nil {
color.New(color.FgRed).Fprint(w, "(✗) ")
Expand Down