@@ -20,7 +20,6 @@ import (
2020 "bytes"
2121 "context"
2222 "fmt"
23- "io/fs"
2423 "io/ioutil"
2524 "os"
2625 "path/filepath"
@@ -94,10 +93,15 @@ func (m *Manifest) renderTo(ctx context.Context, display ui.UI, destinationDir,
9493 return fmt .Errorf ("create destination: %w" , err )
9594 }
9695
97- if _ , err := CopyTree (filepath .Join (layoutDir , ContentDir ), destinationDir ); err != nil {
96+ tree , err := CopyTree (filepath .Join (layoutDir , ContentDir ), destinationDir )
97+ if err != nil {
9898 return fmt .Errorf ("copy content: %w" , err )
9999 }
100100
101+ if debug {
102+ spew .Dump (tree )
103+ }
104+
101105 // execute pre-generate
102106 for i , h := range m .Before {
103107 if ok , err := h .When .Ok (ctx , state ); err != nil {
@@ -113,48 +117,37 @@ func (m *Manifest) renderTo(ctx context.Context, display ui.UI, destinationDir,
113117 }
114118 }
115119
116- // render template
120+ // render template based on tree
117121 // rename files and dirs, empty entries removed
118- err := walk (destinationDir , func (dir string , d fs.DirEntry ) error {
119- renderedName , err := renderer .Render (d .Name ())
120- if err != nil {
121- return err
122- }
123- renderedName = strings .TrimSpace (renderedName )
124- oldPath := filepath .Join (dir , d .Name ())
125- newPath := filepath .Join (dir , renderedName )
126- if len (renderedName ) == 0 {
127- return os .RemoveAll (oldPath )
128- }
129- if oldPath == newPath {
130- return nil
131- }
132- return os .Rename (oldPath , newPath )
122+ err = tree .Render (func (node * FSTree ) (string , error ) {
123+ return renderer .Render (node .Name )
133124 })
134125 if err != nil {
135126 return fmt .Errorf ("render files names: %w" , err )
136127 }
128+
137129 // render file contents as template, except ignored
138130 ignoredFiles , err := m .filesToIgnore (destinationDir )
139131 if err != nil {
140132 return fmt .Errorf ("calculate which files to ignore: %w" , err )
141133 }
142- err = filepath . Walk ( destinationDir , func (path string , info fs. FileInfo , err error ) error {
143- if err != nil {
144- return err
134+ err = tree . Render ( func (node * FSTree ) ( string , error ) {
135+ if node . Dir {
136+ return node . Name , nil
145137 }
146- if info .IsDir () || ignoredFiles [path ] {
147- return nil
138+ path := node .Path ()
139+ if ignoredFiles [path ] {
140+ return node .Name , nil
148141 }
149142 templateData , err := ioutil .ReadFile (path )
150143 if err != nil {
151- return fmt .Errorf ("read content of %s: %w" , path , err )
144+ return node . Name , fmt .Errorf ("read content of %s: %w" , path , err )
152145 }
153146 data , err := renderer .Render (string (templateData ))
154147 if err != nil {
155- return fmt .Errorf ("render %s: %w" , path , err )
148+ return node . Name , fmt .Errorf ("render %s: %w" , path , err )
156149 }
157- return ioutil .WriteFile (path , []byte (data ), info . Mode () )
150+ return node . Name , ioutil .WriteFile (path , []byte (data ), 0755 )
158151 })
159152 if err != nil {
160153 return fmt .Errorf ("render: %w" , err )
0 commit comments