@@ -12,7 +12,6 @@ import (
1212 "time"
1313
1414 "github.com/1414C/sqac/common"
15- "github.com/markbates/pkger"
1615)
1716
1817// Info is used to hold name-value-pairs for Entity definitions
@@ -115,8 +114,6 @@ const (
115114
116115// CreateModelFile generates a model file for the Entity using the user-defined model.json file in conjunction
117116// with the model.gotmpl text/template. Returns the fully-qualified file-name / error.
118- // pkger is used to bundle the .gotmpl files into the binary. Pkger implements the File interface, so the
119- // file handling is a little more pedantic than it would be with ioutil.
120117func (ent * Entity ) CreateModelFile (tDir string , ef embed.FS ) (fName string , err error ) {
121118
122119 // new part
@@ -373,48 +370,34 @@ func (ent *Entity) CreateModelExtensionPointsFile(tDir string, ef embed.FS) (fNa
373370// =============================================================================================
374371// static generation functions
375372// =============================================================================================
376- // GenerateStaticTemplates reads the ./static folder and uses Glob
377- // to execute each template in-turn. Returns the fully-qualified
378- // file-names or an error. Processes .gotmpl files into .go files.
379- func (s * Static ) GenerateStaticTemplates () (fNames []string , err error ) {
373+ // GenerateStaticTemplates reads the ./static folder and executes each template in-turn.
374+ // Returns the fully-qualified file-names or an error. Processes .gotmpl files into .go files.
375+ func (s * Static ) GenerateStaticTemplates (ef embed.FS ) (fNames []string , err error ) {
380376
381- // begin of new part
382- tmlFiles , err := glob (s .SrcDir , "*" + ".gotmpl" )
377+ // rewrite to use embed
378+ dirEntries , err := ef . ReadDir (s .SrcDir )
383379 if err != nil {
380+ log .Printf ("failed to read %s: got: %s" , s .SrcDir , err .Error ())
384381 return nil , err
385382 }
386- // end of new part
387-
388- for _ , f := range tmlFiles {
389383
390- // start of new part
391- fi , err := pkger .Stat (f )
392- if err != nil {
393- log .Printf ("Stat: %v\n " , err )
394- return nil , err
395- }
384+ for _ , dirEntry := range dirEntries {
396385
397- tf , err := pkger .Open (f )
398- if err != nil {
399- log .Printf ("Open: %v\n " , err )
400- return nil , err
386+ if dirEntry .IsDir () {
387+ continue
401388 }
402- defer tf .Close ()
403389
404- // read the template source from pkger
405- buf := make ([]byte , fi .Size ())
406- _ , err = tf .Read (buf )
390+ tf , err := ef .ReadFile (s .SrcDir + "/" + dirEntry .Name ())
407391 if err != nil {
408- log .Printf ("Unable to read template: %s %v \n " , f , err )
392+ log .Printf ("failed to read %s: got: %s " , s . SrcDir + "/" + dirEntry . Name () , err . Error () )
409393 return nil , err
410394 }
411395
412- st := template .Must (template .New ("Static template" ).Parse (string (buf )))
396+ st := template .Must (template .New ("Static template" ).Parse (string (tf )))
413397 if st == nil {
414398 log .Printf ("Parse error: %v\n " , err )
415399 return nil , err
416400 }
417- // end of new part
418401
419402 // create the file-path if required
420403 _ , err = os .Stat (s .DstDir )
@@ -423,7 +406,8 @@ func (s *Static) GenerateStaticTemplates() (fNames []string, err error) {
423406 }
424407
425408 // create the static source file
426- fileName := filepath .Base (f )
409+ // fileName := filepath.Base(f)
410+ fileName := filepath .Base (dirEntry .Name ())
427411 fileName = strings .TrimSuffix (fileName , "tmpl" )
428412 f , err := os .Create (s .DstDir + "/" + fileName )
429413 if err != nil {
@@ -459,14 +443,6 @@ func (s *Static) GenerateGoMod(ef embed.FS) error {
459443 if err != nil {
460444 return err
461445 }
462- // end of new part
463-
464- // check the controller file path and create if required
465- // tDir = tDir + "/controllers"
466- // _, err = os.Stat(tDir)
467- // if err != nil {
468- // os.Mkdir(tDir, 0755)
469- // }
470446
471447 // create the go.mod file
472448 // tfDir := tDir + "go.mod"
@@ -1365,37 +1341,16 @@ func superCleanString(s string) string {
13651341 return s
13661342}
13671343
1368- // prepareTemplate is used to read a template source from the pkger
1369- // or local location, create a *Template and return it to the caller.
1344+ // prepareTemplate is used to read a template source from the embedded
1345+ // location, create a *Template and return it to the caller.
13701346func prepareTemplate (templateName string , ef embed.FS ) (* template.Template , error ) {
1371- // stat for .gotmpl file size
1372- //fi, err := pkger.Stat(templateName)
1373- //if err != nil {
1374- // log.Printf("Stat: %v\n", err)
1375- // return nil, err
1376- //}
13771347
13781348 tf , err := ef .ReadFile (templateName )
13791349 if err != nil {
13801350 log .Printf ("ReadFile: %v\n " , err )
13811351 return nil , err
13821352 }
13831353
1384- // tf, err := ef.Open(templateName)
1385- // if err != nil {
1386- // log.Printf("Open: %v\n", err)
1387- // return nil, err
1388- // }
1389- // defer tf.Close()
1390-
1391- // read the template source from pkger
1392- // buf := make([]byte, fi.Size())
1393- // _, err = tf.Read(buf)
1394- // if err != nil {
1395- // log.Printf("Unable to read template %s\n", templateName)
1396- // return nil, err
1397- // }
1398-
13991354 // create the template
14001355 // t := template.Must(template.New("Entity model template").Parse(string(buf)))
14011356 t := template .Must (template .New ("Entity model template" ).Parse (string (tf )))
@@ -1406,40 +1361,6 @@ func prepareTemplate(templateName string, ef embed.FS) (*template.Template, erro
14061361 return t , nil
14071362}
14081363
1409- // glob is used to read files matching the pattern string
1410- // parameter from the directory specified by the dir parameter.
1411- // There is a slight argument for making this walk the tree, but
1412- // the needs of jiffy are mostly served by reading single directories.
1413- // The pkger.Walk(...) function would be a place to start.
1414- func glob (dir , pattern string ) ([]string , error ) {
1415- m := make ([]string , 0 )
1416- fi , err := pkger .Stat (dir )
1417- if err != nil {
1418- return nil , err
1419- }
1420- if ! fi .IsDir () {
1421- return nil , err
1422- }
1423- d , err := pkger .Open (dir )
1424- if err != nil {
1425- return nil , err
1426- }
1427- defer d .Close ()
1428-
1429- names , _ := d .Readdir (- 1 )
1430-
1431- for _ , n := range names {
1432- matched , err := filepath .Match (pattern , n .Name ())
1433- if err != nil {
1434- return m , err
1435- }
1436- if matched {
1437- m = append (m , dir + "/" + n .Name ())
1438- }
1439- }
1440- return m , nil
1441- }
1442-
14431364// ExecuteGoTools runs gofmt -w and goimports on the specified file
14441365func ExecuteGoTools (fileName string ) error {
14451366
0 commit comments