Skip to content

Commit

Permalink
Merge pull request #128 from kcmvp/v1.0.19
Browse files Browse the repository at this point in the history
V1.0.19
  • Loading branch information
kcmvp authored Jan 15, 2025
2 parents 4b0df1b + 78a7a43 commit 3f24e93
Show file tree
Hide file tree
Showing 34 changed files with 662 additions and 321 deletions.
7 changes: 7 additions & 0 deletions buildtime/dummy/dummy.go
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")
}
25 changes: 25 additions & 0 deletions buildtime/go.mod
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
)
20 changes: 20 additions & 0 deletions buildtime/go.sum
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=
141 changes: 141 additions & 0 deletions buildtime/workspace.go
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
}
28 changes: 28 additions & 0 deletions buildtime/workspace_test.go
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"))
}
45 changes: 26 additions & 19 deletions cmd/gob/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,56 @@ module github.com/kcmvp/gob/cmd/gob
go 1.22.2

require (
github.com/creack/pty v1.1.21
github.com/fatih/color v1.17.0
github.com/google/yamlfmt v0.13.0
github.com/schollz/progressbar/v3 v3.14.4
github.com/creack/pty v1.1.24
github.com/fatih/color v1.18.0
github.com/google/yamlfmt v0.14.0
github.com/kcmvp/gob/core v0.0.0-20241211012851-4b0df1b15d73
github.com/olekukonko/tablewriter v0.0.5
github.com/samber/lo v1.47.0
github.com/samber/mo v1.13.0
github.com/schollz/progressbar/v3 v3.17.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/xlab/treeprint v1.2.0
golang.org/x/mod v0.18.0
golang.org/x/tools v0.22.0
github.com/tidwall/gjson v1.18.0
golang.org/x/mod v0.22.0
golang.org/x/tools v0.28.0
)

require (
github.com/bmatcuk/doublestar/v4 v4.6.0 // indirect
github.com/bmatcuk/doublestar/v4 v4.7.1 // indirect
github.com/braydonk/yaml v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/magiconair/properties v1.8.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/do/v2 v2.0.0-beta.7 // indirect
github.com/samber/go-type-to-string v1.7.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/exp v0.0.0-20241210194714-1829a127f884 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 3f24e93

Please sign in to comment.