-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from kcmvp/v1.0.19
V1.0.19
- Loading branch information
Showing
34 changed files
with
662 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
fmt.Println("it's just for test") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module github.com/kcmvp/gob/buildtime | ||
|
||
go 1.22.2 | ||
|
||
require ( | ||
github.com/dominikbraun/graph v0.23.0 | ||
github.com/fatih/color v1.18.0 | ||
github.com/stretchr/testify v1.10.0 | ||
golang.org/x/mod v0.22.0 | ||
golang.org/x/tools v0.28.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/rogpeppe/go-internal v1.11.0 // indirect | ||
golang.org/x/sync v0.10.0 // indirect | ||
golang.org/x/sys v0.28.0 // indirect | ||
golang.org/x/text v0.21.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= | ||
github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucVPgCo= | ||
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= | ||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= | ||
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc= | ||
github.com/samber/mo v1.13.0 h1:LB1OwfJMju3a6FjghH+AIvzMG0ZPOzgTWj1qaHs1IQ4= | ||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= | ||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= | ||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= | ||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= | ||
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package buildtime | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"github.com/dominikbraun/graph" | ||
"github.com/fatih/color" | ||
"github.com/kcmvp/gob/common" | ||
"github.com/samber/lo" | ||
_ "github.com/samber/lo/parallel" | ||
"github.com/samber/mo" | ||
"go/types" | ||
"golang.org/x/mod/modfile" | ||
"golang.org/x/tools/go/packages" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ws workspace[string, *Module] | ||
) | ||
|
||
type workspace[K comparable, T any] struct { | ||
graph.Graph[K, T] | ||
dir2Path map[string]string | ||
} | ||
|
||
type Module struct { | ||
dir string | ||
mod *modfile.File | ||
ver string | ||
latestVer string | ||
dependencies []Module | ||
} | ||
|
||
func (module *Module) Path() string { | ||
return module.mod.Module.Mod.Path | ||
} | ||
|
||
func (module *Module) Dir() string { | ||
return module.dir | ||
} | ||
|
||
func (module *Module) MainFile() mo.Option[string] { | ||
cfg := &packages.Config{ | ||
Mode: packages.NeedName | packages.NeedTypes | packages.NeedFiles | packages.NeedSyntax, | ||
Dir: module.Dir(), | ||
} | ||
pkgs := mo.TupleToResult(packages.Load(cfg, "./...")).MustGet() | ||
files := lo.FilterMap(pkgs, func(pkg *packages.Package, _ int) (string, bool) { | ||
if pkg.Name != "main" { | ||
return "", false | ||
} | ||
scope := pkg.Types.Scope() | ||
for _, name := range scope.Names() { | ||
obj := scope.Lookup(name) | ||
if f, ok := obj.(*types.Func); ok { | ||
signature := f.Type().(*types.Signature) | ||
if f.Name() == "main" && signature.Params().Len() == 0 && signature.Results().Len() == 0 { | ||
return pkg.Fset.Position(obj.Pos()).Filename, true | ||
} | ||
} | ||
} | ||
return "", false | ||
}) | ||
return lo.If(len(files) == 0, mo.None[string]()).ElseF(func() mo.Option[string] { | ||
return mo.Some(files[0]) | ||
}) | ||
} | ||
|
||
// init initialize ws | ||
func init() { | ||
ws = workspace[string, *Module]{ | ||
Graph: graph.New[string, *Module](func(module *Module) string { | ||
return module.Path() | ||
}, graph.Directed(), graph.PreventCycles()), | ||
dir2Path: map[string]string{}, | ||
} | ||
rs := mo.TupleToResult(exec.Command("go", "list", "-m", "-f", "{{.Dir}}").CombinedOutput()) | ||
if rs.IsError() || len(rs.MustGet()) == 0 { | ||
log.Fatal(color.RedString("please execute command in ws or project root directory")) | ||
} | ||
scanner := bufio.NewScanner(bytes.NewBuffer(rs.MustGet())) | ||
for scanner.Scan() { | ||
dir := strings.TrimSpace(scanner.Text()) | ||
data := mo.TupleToResult(os.ReadFile(filepath.Join(dir, "go.mod"))).MustGet() | ||
mod := mo.TupleToResult(modfile.Parse("go.mod", data, nil)).MustGet() | ||
ws.AddVertex(&Module{dir: dir, mod: mod}) | ||
ws.dir2Path[dir] = mod.Module.Mod.Path | ||
} | ||
vertices, _ := ws.PredecessorMap() | ||
if len(vertices) > 1 { | ||
lo.ForEach(lo.Keys(vertices), func(path string, index int) { | ||
// build reference | ||
module := mo.TupleToResult(ws.Vertex(path)).MustGet() | ||
lo.ForEach(module.mod.Require, func(item *modfile.Require, index int) { | ||
if mo.TupleToResult(ws.Vertex(item.Mod.Path)).IsOk() { | ||
ws.AddEdge(path, item.Mod.Path, graph.EdgeAttribute("ref", "1")) | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func CurrentModule() mo.Result[*Module] { | ||
if path, ok := ws.dir2Path[common.CurrentDir()]; ok { | ||
return mo.Ok(mo.TupleToResult(ws.Vertex(path)).MustGet()) | ||
} | ||
return mo.Errf[*Module]("please execute the command in the ws or project root") | ||
} | ||
|
||
func Modules() []*Module { | ||
return lo.Map(lo.Values(ws.dir2Path), func(path string, index int) *Module { | ||
m, _ := ws.Vertex(path) | ||
return m | ||
}) | ||
} | ||
|
||
func RootDir() string { | ||
paths := lo.Keys(ws.dir2Path) | ||
rootSections := strings.FieldsFunc(paths[0], func(r rune) bool { return r == os.PathSeparator }) | ||
var root string | ||
lo.ForEachWhile(rootSections, func(section string, idx int) bool { | ||
tmp := strings.Join(rootSections[:idx+1], string(os.PathSeparator)) | ||
if !common.WindowsEnv() { | ||
tmp = fmt.Sprintf("%s%s", string(os.PathSeparator), tmp) | ||
} | ||
if lo.EveryBy(paths, func(path string) bool { | ||
return strings.HasPrefix(path, tmp) | ||
}) { | ||
root = tmp | ||
return true | ||
} | ||
return false | ||
}) | ||
return root | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package buildtime | ||
|
||
import ( | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWorkspace(t *testing.T) { | ||
root := RootDir() | ||
assert.NotNil(t, root) | ||
module := CurrentModule().MustGet() | ||
assert.Equal(t, "github.com/kcmvp/gob/buildtime", module.Path()) | ||
assert.True(t, strings.HasPrefix(module.Dir(), root)) | ||
modules := []string{"github.com/kcmvp/gob/buildtime", "github.com/kcmvp/gob/cmd/gob", | ||
"github.com/kcmvp/gob/common", "github.com/kcmvp/gob/dbo", "github.com/kcmvp/gob/runtime"} | ||
assert.ElementsMatch(t, modules, lo.Map(Modules(), func(m *Module, _ int) string { | ||
return m.Path() | ||
})) | ||
} | ||
|
||
func TestModule_MainFile(t *testing.T) { | ||
module := CurrentModule().MustGet() | ||
mf := module.MainFile() | ||
assert.True(t, mf.IsPresent()) | ||
assert.True(t, strings.HasSuffix(mf.MustGet(), "/buildtime/dummy/dummy.go")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.