@@ -12,7 +12,6 @@ import (
12
12
"time"
13
13
14
14
"github.com/1414C/sqac/common"
15
- "github.com/markbates/pkger"
16
15
)
17
16
18
17
// Info is used to hold name-value-pairs for Entity definitions
@@ -115,8 +114,6 @@ const (
115
114
116
115
// CreateModelFile generates a model file for the Entity using the user-defined model.json file in conjunction
117
116
// 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.
120
117
func (ent * Entity ) CreateModelFile (tDir string , ef embed.FS ) (fName string , err error ) {
121
118
122
119
// new part
@@ -373,48 +370,34 @@ func (ent *Entity) CreateModelExtensionPointsFile(tDir string, ef embed.FS) (fNa
373
370
// =============================================================================================
374
371
// static generation functions
375
372
// =============================================================================================
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 ) {
380
376
381
- // begin of new part
382
- tmlFiles , err := glob (s .SrcDir , "*" + ".gotmpl" )
377
+ // rewrite to use embed
378
+ dirEntries , err := ef . ReadDir (s .SrcDir )
383
379
if err != nil {
380
+ log .Printf ("failed to read %s: got: %s" , s .SrcDir , err .Error ())
384
381
return nil , err
385
382
}
386
- // end of new part
387
-
388
- for _ , f := range tmlFiles {
389
383
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 {
396
385
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
401
388
}
402
- defer tf .Close ()
403
389
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 ())
407
391
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 () )
409
393
return nil , err
410
394
}
411
395
412
- st := template .Must (template .New ("Static template" ).Parse (string (buf )))
396
+ st := template .Must (template .New ("Static template" ).Parse (string (tf )))
413
397
if st == nil {
414
398
log .Printf ("Parse error: %v\n " , err )
415
399
return nil , err
416
400
}
417
- // end of new part
418
401
419
402
// create the file-path if required
420
403
_ , err = os .Stat (s .DstDir )
@@ -423,7 +406,8 @@ func (s *Static) GenerateStaticTemplates() (fNames []string, err error) {
423
406
}
424
407
425
408
// create the static source file
426
- fileName := filepath .Base (f )
409
+ // fileName := filepath.Base(f)
410
+ fileName := filepath .Base (dirEntry .Name ())
427
411
fileName = strings .TrimSuffix (fileName , "tmpl" )
428
412
f , err := os .Create (s .DstDir + "/" + fileName )
429
413
if err != nil {
@@ -459,14 +443,6 @@ func (s *Static) GenerateGoMod(ef embed.FS) error {
459
443
if err != nil {
460
444
return err
461
445
}
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
- // }
470
446
471
447
// create the go.mod file
472
448
// tfDir := tDir + "go.mod"
@@ -1365,37 +1341,16 @@ func superCleanString(s string) string {
1365
1341
return s
1366
1342
}
1367
1343
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.
1370
1346
func 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
- //}
1377
1347
1378
1348
tf , err := ef .ReadFile (templateName )
1379
1349
if err != nil {
1380
1350
log .Printf ("ReadFile: %v\n " , err )
1381
1351
return nil , err
1382
1352
}
1383
1353
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
-
1399
1354
// create the template
1400
1355
// t := template.Must(template.New("Entity model template").Parse(string(buf)))
1401
1356
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
1406
1361
return t , nil
1407
1362
}
1408
1363
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
-
1443
1364
// ExecuteGoTools runs gofmt -w and goimports on the specified file
1444
1365
func ExecuteGoTools (fileName string ) error {
1445
1366
0 commit comments