@@ -528,7 +528,11 @@ func Generate(location string, envs []string, verbose bool) error {
528
528
return nil
529
529
}
530
530
531
- func ModAddLocalModule (module * ExtensionModule , target string ) error {
531
+ func ModAddLocalModule (
532
+ module * TenPackageModule ,
533
+ target string ,
534
+ moduleType string ,
535
+ ) error {
532
536
// go mod edit -replace <mod>=<full path>
533
537
//
534
538
// Note that the module must be replaced with the full path, as the module
@@ -540,8 +544,9 @@ func ModAddLocalModule(module *ExtensionModule, target string) error {
540
544
"edit" ,
541
545
"-replace" ,
542
546
fmt .Sprintf (
543
- "%s=./ten_packages/extension /%s" ,
547
+ "%s=./ten_packages/%s /%s" ,
544
548
module .module ,
549
+ moduleType ,
545
550
path .Base (module .location ),
546
551
),
547
552
},
@@ -613,11 +618,12 @@ func (b *BuildOption) Valid() error {
613
618
// -------------- builder ----------------
614
619
615
620
type AppBuilder struct {
616
- pkgName string
617
- acquiredGoVersion string
618
- options * BuildOption
619
- cachedEnv map [string ]string
620
- extensions []* ExtensionModule
621
+ pkgName string
622
+ acquiredGoVersion string
623
+ options * BuildOption
624
+ cachedEnv map [string ]string
625
+ extensionPackageModules []* TenPackageModule
626
+ systemPackageModules []* TenPackageModule
621
627
}
622
628
623
629
func NewAppBuilder (
@@ -626,18 +632,19 @@ func NewAppBuilder(
626
632
options * BuildOption ,
627
633
) * AppBuilder {
628
634
return & AppBuilder {
629
- pkgName : pkgName ,
630
- acquiredGoVersion : goVersion ,
631
- options : options ,
632
- cachedEnv : make (map [string ]string ),
633
- extensions : make ([]* ExtensionModule , 0 ),
635
+ pkgName : pkgName ,
636
+ acquiredGoVersion : goVersion ,
637
+ options : options ,
638
+ cachedEnv : make (map [string ]string ),
639
+ extensionPackageModules : make ([]* TenPackageModule , 0 ),
640
+ systemPackageModules : make ([]* TenPackageModule , 0 ),
634
641
}
635
642
}
636
643
637
644
// runTidyAndGenerate executes 'go mod tidy' and 'go generate' on GO app and all
638
645
// GO extensions.
639
646
func (ab * AppBuilder ) runTidyAndGenerate (envs []string ) error {
640
- for _ , ext := range ab .extensions {
647
+ for _ , ext := range ab .extensionPackageModules {
641
648
if err := ModTidy (ext .location , envs , ab .options .Verbose ); err != nil {
642
649
return err
643
650
}
@@ -690,9 +697,16 @@ func (ab *AppBuilder) Build() error {
690
697
return fmt .Errorf ("precheck failed. Root cause: \n \t %w" , err )
691
698
}
692
699
693
- if err := ab .autoDetectExtensions (); err != nil {
700
+ if err := ab .autoDetectExtensionPackageModules (); err != nil {
694
701
return fmt .Errorf (
695
- "auto detect extensions failed. Root cause: \n \t %w" ,
702
+ "auto detect extension packages failed. Root cause: \n \t %w" ,
703
+ err ,
704
+ )
705
+ }
706
+
707
+ if err := ab .autoDetectSystemPackageModules (); err != nil {
708
+ return fmt .Errorf (
709
+ "auto detect system packages failed. Root cause: \n \t %w" ,
696
710
err ,
697
711
)
698
712
}
@@ -713,7 +727,7 @@ func (ab *AppBuilder) Build() error {
713
727
return err
714
728
}
715
729
716
- if err := ab .requireExtensionModules (); err != nil {
730
+ if err := ab .requireExtensionAndSystemPackageModules (); err != nil {
717
731
return err
718
732
}
719
733
@@ -967,21 +981,69 @@ func (ab *AppBuilder) buildExecEnvs() []string {
967
981
968
982
// -------------- extension --------------
969
983
970
- type ExtensionModule struct {
984
+ type TenPackageModule struct {
971
985
// The module name in go.mod
972
986
module string
973
987
974
988
location string
975
989
}
976
990
977
- func (em * ExtensionModule ) String () string {
978
- return fmt .Sprintf ("%s @ %s" , em .module , em .location )
991
+ func (tpm * TenPackageModule ) String () string {
992
+ return fmt .Sprintf ("%s @ %s" , tpm .module , tpm .location )
979
993
}
980
994
981
- func LoadExtensionModule (
995
+ func LoadSystemPackageModule (
982
996
location string ,
983
997
verbose bool ,
984
- ) (* ExtensionModule , error ) {
998
+ ) (* TenPackageModule , error ) {
999
+ // Check if the folder contains a manifest.json to determine if it is a
1000
+ // system package.
1001
+ if ! IsFilePresent (path .Join (location , "manifest.json" )) {
1002
+ if verbose {
1003
+ log .Printf (
1004
+ "%s is not a system package, no manifest.json.\n " ,
1005
+ location ,
1006
+ )
1007
+ }
1008
+
1009
+ return nil , nil
1010
+ }
1011
+
1012
+ // Check if the folder contains a go.mod to determine if it is a GO system
1013
+ // package.
1014
+ modFile := path .Join (location , "go.mod" )
1015
+ if ! IsFilePresent (modFile ) {
1016
+ if verbose {
1017
+ log .Printf ("%s is not a GO system package, no go.mod" , location )
1018
+ }
1019
+
1020
+ return nil , nil
1021
+ }
1022
+
1023
+ bytes , err := os .ReadFile (modFile )
1024
+ if err != nil {
1025
+ return nil , fmt .Errorf (
1026
+ "%s system package is invalid. \n \t %w" ,
1027
+ location ,
1028
+ err ,
1029
+ )
1030
+ }
1031
+
1032
+ module := ModulePath (bytes )
1033
+ if len (module ) == 0 {
1034
+ return nil , fmt .Errorf ("no mod is detected in %s/go.mod" , location )
1035
+ }
1036
+
1037
+ return & TenPackageModule {
1038
+ module : module ,
1039
+ location : location ,
1040
+ }, nil
1041
+ }
1042
+
1043
+ func LoadExtensionPackageModule (
1044
+ location string ,
1045
+ verbose bool ,
1046
+ ) (* TenPackageModule , error ) {
985
1047
// Check if the folder contains a manifest.json to determine if it is an
986
1048
// extension.
987
1049
if ! IsFilePresent (path .Join (location , "manifest.json" )) {
@@ -1024,13 +1086,58 @@ func LoadExtensionModule(
1024
1086
return nil , fmt .Errorf ("no mod is detected in %s/go.mod" , location )
1025
1087
}
1026
1088
1027
- return & ExtensionModule {
1089
+ return & TenPackageModule {
1028
1090
module : module ,
1029
1091
location : location ,
1030
1092
}, nil
1031
1093
}
1032
1094
1033
- func (ab * AppBuilder ) autoDetectExtensions () error {
1095
+ func (ab * AppBuilder ) autoDetectSystemPackageModules () error {
1096
+ sysBaseDir := path .Join (ab .options .AppDir , "ten_packages/system" )
1097
+ if ! IsDirPresent (sysBaseDir ) {
1098
+ if ab .options .Verbose {
1099
+ log .Println (
1100
+ "The base directory [ten_packages/system] is absent, no system packages." ,
1101
+ )
1102
+ }
1103
+
1104
+ return nil
1105
+ }
1106
+
1107
+ entries , err := os .ReadDir (sysBaseDir )
1108
+ if err != nil {
1109
+ return err
1110
+ }
1111
+
1112
+ // Check if the system package is a GO module.
1113
+ for _ , entry := range entries {
1114
+ if entry .IsDir () {
1115
+ sysModFile := path .Join (sysBaseDir , entry .Name (), "go.mod" )
1116
+ if ! IsFilePresent (sysModFile ) {
1117
+ continue
1118
+ }
1119
+
1120
+ sysModDir := path .Join (sysBaseDir , entry .Name ())
1121
+ sysMod , err := LoadSystemPackageModule (
1122
+ sysModDir ,
1123
+ ab .options .Verbose ,
1124
+ )
1125
+ if err != nil {
1126
+ return err
1127
+ }
1128
+
1129
+ if sysMod == nil {
1130
+ continue
1131
+ }
1132
+
1133
+ ab .systemPackageModules = append (ab .systemPackageModules , sysMod )
1134
+ }
1135
+ }
1136
+
1137
+ return nil
1138
+ }
1139
+
1140
+ func (ab * AppBuilder ) autoDetectExtensionPackageModules () error {
1034
1141
extBaseDir := path .Join (ab .options .AppDir , "ten_packages/extension" )
1035
1142
if ! IsDirPresent (extBaseDir ) {
1036
1143
if ab .options .Verbose {
@@ -1136,7 +1243,7 @@ func (ab *AppBuilder) autoDetectExtensions() error {
1136
1243
}
1137
1244
1138
1245
extDir := path .Join (extBaseDir , entry .Name ())
1139
- ext , err := LoadExtensionModule (extDir , ab .options .Verbose )
1246
+ ext , err := LoadExtensionPackageModule (extDir , ab .options .Verbose )
1140
1247
if err != nil {
1141
1248
return err
1142
1249
}
@@ -1156,12 +1263,12 @@ func (ab *AppBuilder) autoDetectExtensions() error {
1156
1263
}
1157
1264
1158
1265
uniqueModules [ext .module ] = ext .location
1159
- ab .extensions = append (ab .extensions , ext )
1266
+ ab .extensionPackageModules = append (ab .extensionPackageModules , ext )
1160
1267
}
1161
1268
1162
- if ab .options .Verbose && len (ab .extensions ) > 0 {
1269
+ if ab .options .Verbose && len (ab .extensionPackageModules ) > 0 {
1163
1270
log .Println ("Go Extensions are detected:" )
1164
- for _ , ext := range ab .extensions {
1271
+ for _ , ext := range ab .extensionPackageModules {
1165
1272
log .Printf ("\t %s\n " , ext )
1166
1273
}
1167
1274
}
@@ -1241,7 +1348,7 @@ func (ab *AppBuilder) generateAutoImportFile() error {
1241
1348
}
1242
1349
}
1243
1350
1244
- if len (ab .extensions ) == 0 {
1351
+ if len (ab .extensionPackageModules ) == 0 {
1245
1352
log .Println (
1246
1353
"No extension is detected, no need to generate import file." ,
1247
1354
)
@@ -1259,7 +1366,7 @@ func (ab *AppBuilder) generateAutoImportFile() error {
1259
1366
_ , _ = f .WriteString ("// Code generated by app builder. DO NOT EDIT.\n \n " )
1260
1367
_ , _ = f .WriteString (fmt .Sprintf ("package %s\n \n " , ab .pkgName ))
1261
1368
1262
- for _ , ext := range ab .extensions {
1369
+ for _ , ext := range ab .extensionPackageModules {
1263
1370
_ , _ = f .WriteString (fmt .Sprintf ("import _ \" %s\" \n " , ext .module ))
1264
1371
}
1265
1372
@@ -1270,10 +1377,11 @@ func (ab *AppBuilder) generateAutoImportFile() error {
1270
1377
return nil
1271
1378
}
1272
1379
1273
- func (ab * AppBuilder ) requireExtensionModules () error {
1274
- if len (ab .extensions ) == 0 {
1380
+ func (ab * AppBuilder ) requireExtensionAndSystemPackageModules () error {
1381
+ if len (ab .extensionPackageModules ) == 0 &&
1382
+ len (ab .systemPackageModules ) == 0 {
1275
1383
log .Println (
1276
- "No extension is detected, no need to require extension modules." ,
1384
+ "No extension or system package is detected, no need to require extension and system modules." ,
1277
1385
)
1278
1386
1279
1387
return nil
@@ -1299,9 +1407,9 @@ func (ab *AppBuilder) requireExtensionModules() error {
1299
1407
}
1300
1408
}
1301
1409
1302
- // Add GO extensions as the module of the app.
1303
- for _ , ext := range ab .extensions {
1304
- if err := ModAddLocalModule (ext , ab .options .AppDir ); err != nil {
1410
+ // Add GO extension packages as the module of the app.
1411
+ for _ , ext := range ab .extensionPackageModules {
1412
+ if err := ModAddLocalModule (ext , ab .options .AppDir , "extension" ); err != nil {
1305
1413
return fmt .Errorf (
1306
1414
"Failed to add %s as go module of the app. \n \t %w" ,
1307
1415
ext .location ,
@@ -1310,6 +1418,17 @@ func (ab *AppBuilder) requireExtensionModules() error {
1310
1418
}
1311
1419
}
1312
1420
1421
+ // Add GO system packages as the module of the app.
1422
+ for _ , sys := range ab .systemPackageModules {
1423
+ if err := ModAddLocalModule (sys , ab .options .AppDir , "system" ); err != nil {
1424
+ return fmt .Errorf (
1425
+ "Failed to add %s as go module of the app. \n \t %w" ,
1426
+ sys .location ,
1427
+ err ,
1428
+ )
1429
+ }
1430
+ }
1431
+
1313
1432
if ab .options .Verbose {
1314
1433
log .Println ("Add extension modules to app successfully." )
1315
1434
}
0 commit comments