Skip to content

Commit

Permalink
remove RunMode flag & add WithExpand flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Jan 10, 2025
1 parent cb454a0 commit 9572e7f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
8 changes: 1 addition & 7 deletions cmd/llcppcfg/llcppcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ func main() {
name = flag.Arg(0)
}

fnToCfgExpandMode := func() llcppgcfg.RunMode {
if expand {
return llcppgcfg.ExpandMode
}
return llcppgcfg.NormalMode
}
exts := strings.Fields(extsString)
excludeSubdirs := []string{}
if len(excludes) > 0 {
Expand All @@ -54,7 +48,7 @@ func main() {
if sortByDep {
flag |= llcppgcfg.WithSort
}
buf, err := llcppgcfg.GenCfg(name, flag, fnToCfgExpandMode(), exts, excludeSubdirs)
buf, err := llcppgcfg.GenCfg(name, flag, exts, excludeSubdirs)
if err != nil {
log.Fatal(err)
}
Expand Down
15 changes: 4 additions & 11 deletions cmd/llcppcfg/llcppgcfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ import (
"github.com/goplus/llcppg/types"
)

type RunMode int

type FlagMode int

const (
NormalMode RunMode = iota
ExpandMode
)

const (
WithSort FlagMode = 1 << iota
WithNoTab
WithCpp
WithExpand
)

type emptyStringError struct {
Expand Down Expand Up @@ -298,21 +292,20 @@ func NormalizePackageName(name string) string {
return strings.Join(fields, "_")
}

func GenCfg(name string, flag FlagMode, expand RunMode, exts []string, excludeSubdirs []string) (*bytes.Buffer, error) {
func GenCfg(name string, flag FlagMode, exts []string, excludeSubdirs []string) (*bytes.Buffer, error) {
if len(name) == 0 {
return nil, newEmptyStringError("name")
}
cfg := NewLLCppConfig(name, flag)
switch expand {
case ExpandMode:
if flag&WithExpand != 0 {
if flag&WithSort != 0 {
cfg.CFlags, _ = ExpandName(name, "", "cflags")
cfg.Libs, _ = ExpandName(name, "", "libs")
sortIncludes(cfg.CFlags, cfg, exts, excludeSubdirs)
} else {
expandCFlagsAndLibs(name, cfg, "", exts, excludeSubdirs)
}
case NormalMode:
} else {
if flag&WithSort != 0 {
expandCFlags, _ := ExpandName(name, "", "cflags")
sortIncludes(expandCFlags, cfg, exts, excludeSubdirs)
Expand Down
24 changes: 8 additions & 16 deletions cmd/llcppcfg/llcppgcfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ func TestGenCfg(t *testing.T) {
type args struct {
name string
flag FlagMode
expand RunMode
exts []string
excludeSubdirs []string
}
Expand All @@ -843,7 +842,6 @@ func TestGenCfg(t *testing.T) {
args{
"libcjson",
WithSort,
NormalMode,
[]string{".h"},
[]string{},
},
Expand All @@ -855,7 +853,6 @@ func TestGenCfg(t *testing.T) {
args{
"bdw-gc",
WithSort,
NormalMode,
[]string{".h"},
[]string{},
},
Expand All @@ -867,7 +864,6 @@ func TestGenCfg(t *testing.T) {
args{
"libffi",
WithSort,
NormalMode,
[]string{".h"},
[]string{},
},
Expand All @@ -879,7 +875,6 @@ func TestGenCfg(t *testing.T) {
args{
"",
WithSort,
NormalMode,
[]string{".h"},
[]string{},
},
Expand All @@ -890,8 +885,7 @@ func TestGenCfg(t *testing.T) {
"expand",
args{
"libcjson",
WithSort,
ExpandMode,
WithSort | WithExpand,
[]string{".h"},
[]string{},
},
Expand All @@ -902,8 +896,7 @@ func TestGenCfg(t *testing.T) {
"expand_not_sort",
args{
"libcjson",
0,
ExpandMode,
WithExpand,
[]string{".h"},
[]string{},
},
Expand All @@ -915,7 +908,6 @@ func TestGenCfg(t *testing.T) {
args{
"libcjson",
0,
NormalMode,
[]string{".h"},
[]string{},
},
Expand All @@ -925,19 +917,19 @@ func TestGenCfg(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GenCfg(tt.args.name, tt.args.flag, tt.args.expand, tt.args.exts, tt.args.excludeSubdirs)
got, err := GenCfg(tt.args.name, tt.args.flag, tt.args.exts, tt.args.excludeSubdirs)
if (err != nil) != tt.wantErr {
t.Errorf("GenCfg() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.args.expand == NormalMode && tt.args.flag&WithSort != 0 {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GenCfg() = %v, want %v", got, tt.want)
}
} else {
if tt.args.flag&WithExpand != 0 {
if got.Len() <= 0 {
t.Errorf("GenCfg() = %v, want expaned", got)
}
} else {
if tt.args.flag&WithSort != 0 && !reflect.DeepEqual(got, tt.want) {
t.Errorf("GenCfg() = %v, want %v", got, tt.want)
}
}
})
}
Expand Down

0 comments on commit 9572e7f

Please sign in to comment.