From 3948c658648de03773889f194b7037c136f52055 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 2 Aug 2024 02:21:43 +0800 Subject: [PATCH 1/5] tool: gostdpkgs --- chore/gostdpkgs/gostdpkgs.go | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 chore/gostdpkgs/gostdpkgs.go diff --git a/chore/gostdpkgs/gostdpkgs.go b/chore/gostdpkgs/gostdpkgs.go new file mode 100644 index 0000000..3344bb1 --- /dev/null +++ b/chore/gostdpkgs/gostdpkgs.go @@ -0,0 +1,51 @@ +/* +Copyright 2024 The GoPlus Authors (goplus.org) +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "os" + "runtime" + "strings" +) + +func main() { + dir := runtime.GOROOT() + "/src/" + pkgs := collect(nil, dir, "") + fmt.Println(strings.Join(pkgs, "\n")) +} + +func collect(pkgs []string, dir, base string) []string { + fis, err := os.ReadDir(dir) + check(err) + for _, fi := range fis { + if !fi.IsDir() { + continue + } + if name := fi.Name(); name != "cmd" && name != "internal" && name != "vendor" && name != "testdata" { + nameSlash := name + "/" + pkgs = append(pkgs, base+name) + pkgs = collect(pkgs, dir+nameSlash, base+nameSlash) + } + } + return pkgs +} + +func check(err error) { + if err != nil { + panic(err) + } +} From 293a24979cd2bbac963c992ce97e0cf2fac198bb Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 2 Aug 2024 02:30:54 +0800 Subject: [PATCH 2/5] gostdpkgs: skip dir without goFiles --- chore/gostdpkgs/gostdpkgs.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/chore/gostdpkgs/gostdpkgs.go b/chore/gostdpkgs/gostdpkgs.go index 3344bb1..0490be7 100644 --- a/chore/gostdpkgs/gostdpkgs.go +++ b/chore/gostdpkgs/gostdpkgs.go @@ -24,26 +24,40 @@ import ( func main() { dir := runtime.GOROOT() + "/src/" - pkgs := collect(nil, dir, "") + fis, err := os.ReadDir(dir) + check(err) + pkgs := collect(nil, fis, dir, "") fmt.Println(strings.Join(pkgs, "\n")) } -func collect(pkgs []string, dir, base string) []string { - fis, err := os.ReadDir(dir) - check(err) +func collect(pkgs []string, fis []os.DirEntry, dir, base string) []string { for _, fi := range fis { if !fi.IsDir() { continue } if name := fi.Name(); name != "cmd" && name != "internal" && name != "vendor" && name != "testdata" { nameSlash := name + "/" - pkgs = append(pkgs, base+name) - pkgs = collect(pkgs, dir+nameSlash, base+nameSlash) + pkgDir := dir + nameSlash + pkgFis, err := os.ReadDir(pkgDir) + check(err) + if hasGoFiles(pkgFis) { + pkgs = append(pkgs, base+name) + } + pkgs = collect(pkgs, pkgFis, pkgDir, base+nameSlash) } } return pkgs } +func hasGoFiles(fis []os.DirEntry) bool { + for _, fi := range fis { + if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".go") { + return true + } + } + return false +} + func check(err error) { if err != nil { panic(err) From aae6671d2409c30c3fbac66c6b354295cbdf3dc1 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 2 Aug 2024 02:37:35 +0800 Subject: [PATCH 3/5] gopkgimps: don't use json --- chore/gopkgimps/gopkgimps.go | 7 ++--- fetcher/gopkg/gop_autogen.go | 53 ++++++++++++++++++++---------------- fetcher/gopkg/gopkg_imps.gop | 9 ++++-- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/chore/gopkgimps/gopkgimps.go b/chore/gopkgimps/gopkgimps.go index 9906088..2f163a7 100644 --- a/chore/gopkgimps/gopkgimps.go +++ b/chore/gopkgimps/gopkgimps.go @@ -16,7 +16,6 @@ limitations under the License. package main import ( - "encoding/json" "fmt" "log" "os" @@ -46,7 +45,7 @@ func main() { sort.Slice(docs, func(i, j int) bool { return docs[i].ImportedBy > docs[j].ImportedBy }) - enc := json.NewEncoder(os.Stdout) - enc.SetIndent("", " ") - enc.Encode(docs) + for _, doc := range docs { + fmt.Println(doc.Path, doc.ImportedBy) + } } diff --git a/fetcher/gopkg/gop_autogen.go b/fetcher/gopkg/gop_autogen.go index 7d8c98a..aa8be60 100644 --- a/fetcher/gopkg/gop_autogen.go +++ b/fetcher/gopkg/gop_autogen.go @@ -14,7 +14,7 @@ const GopPackage = "github.com/goplus/hdq" const _ = true type Result struct { - Name string `json:"name"` + Path string `json:"path"` ImportedBy int `json:"importedBy"` } //line fetcher/gopkg/gopkg_imps.gop:30:1 @@ -23,57 +23,62 @@ func New(input interface{}, doc hdq.NodeSet) Result { //line fetcher/gopkg/gopkg_imps.gop:32:1 const importedByPrefix = "Imported By:" //line fetcher/gopkg/gopkg_imps.gop:33:1 - name := input.(string) + path := input.(string) //line fetcher/gopkg/gopkg_imps.gop:34:1 a := doc.Any().A().Attribute__1("aria-label", func(v string) bool { //line fetcher/gopkg/gopkg_imps.gop:34:1 return strings.HasPrefix(v, importedByPrefix) }).One() //line fetcher/gopkg/gopkg_imps.gop:35:1 + if !a.Ok() { +//line fetcher/gopkg/gopkg_imps.gop:36:1 + return Result{path, 0} + } +//line fetcher/gopkg/gopkg_imps.gop:38:1 label := func() (_gop_ret string) { -//line fetcher/gopkg/gopkg_imps.gop:35:1 +//line fetcher/gopkg/gopkg_imps.gop:38:1 var _gop_err error -//line fetcher/gopkg/gopkg_imps.gop:35:1 +//line fetcher/gopkg/gopkg_imps.gop:38:1 _gop_ret, _gop_err = a.Attr__0("aria-label") -//line fetcher/gopkg/gopkg_imps.gop:35:1 +//line fetcher/gopkg/gopkg_imps.gop:38:1 if _gop_err != nil { -//line fetcher/gopkg/gopkg_imps.gop:35:1 - _gop_err = errors.NewFrame(_gop_err, "a.attr(\"aria-label\")", "fetcher/gopkg/gopkg_imps.gop", 35, "gopkg.New") -//line fetcher/gopkg/gopkg_imps.gop:35:1 +//line fetcher/gopkg/gopkg_imps.gop:38:1 + _gop_err = errors.NewFrame(_gop_err, "a.attr(\"aria-label\")", "fetcher/gopkg/gopkg_imps.gop", 38, "gopkg.New") +//line fetcher/gopkg/gopkg_imps.gop:38:1 panic(_gop_err) } -//line fetcher/gopkg/gopkg_imps.gop:35:1 +//line fetcher/gopkg/gopkg_imps.gop:38:1 return }() -//line fetcher/gopkg/gopkg_imps.gop:36:1 +//line fetcher/gopkg/gopkg_imps.gop:39:1 labelVal := strings.TrimSpace(label[len(importedByPrefix):]) -//line fetcher/gopkg/gopkg_imps.gop:37:1 +//line fetcher/gopkg/gopkg_imps.gop:40:1 importedBy := func() (_gop_ret int) { -//line fetcher/gopkg/gopkg_imps.gop:37:1 +//line fetcher/gopkg/gopkg_imps.gop:40:1 var _gop_err error -//line fetcher/gopkg/gopkg_imps.gop:37:1 +//line fetcher/gopkg/gopkg_imps.gop:40:1 _gop_ret, _gop_err = strconv.Atoi(strings.ReplaceAll(labelVal, ",", "")) -//line fetcher/gopkg/gopkg_imps.gop:37:1 +//line fetcher/gopkg/gopkg_imps.gop:40:1 if _gop_err != nil { -//line fetcher/gopkg/gopkg_imps.gop:37:1 - _gop_err = errors.NewFrame(_gop_err, "strings.replaceAll(labelVal, \",\", \"\").int", "fetcher/gopkg/gopkg_imps.gop", 37, "gopkg.New") -//line fetcher/gopkg/gopkg_imps.gop:37:1 +//line fetcher/gopkg/gopkg_imps.gop:40:1 + _gop_err = errors.NewFrame(_gop_err, "strings.replaceAll(labelVal, \",\", \"\").int", "fetcher/gopkg/gopkg_imps.gop", 40, "gopkg.New") +//line fetcher/gopkg/gopkg_imps.gop:40:1 panic(_gop_err) } -//line fetcher/gopkg/gopkg_imps.gop:37:1 +//line fetcher/gopkg/gopkg_imps.gop:40:1 return }() -//line fetcher/gopkg/gopkg_imps.gop:38:1 - return Result{name, importedBy} -} //line fetcher/gopkg/gopkg_imps.gop:41:1 + return Result{path, importedBy} +} +//line fetcher/gopkg/gopkg_imps.gop:44:1 // URL returns the input URL for the given name. func URL(name interface{}) string { -//line fetcher/gopkg/gopkg_imps.gop:43:1 +//line fetcher/gopkg/gopkg_imps.gop:46:1 return "https://pkg.go.dev/" + name.(string) } -//line fetcher/gopkg/gopkg_imps.gop:46:1 +//line fetcher/gopkg/gopkg_imps.gop:49:1 func init() { -//line fetcher/gopkg/gopkg_imps.gop:47:1 +//line fetcher/gopkg/gopkg_imps.gop:50:1 fetcher.Register("gopkg", New, URL) } diff --git a/fetcher/gopkg/gopkg_imps.gop b/fetcher/gopkg/gopkg_imps.gop index 84e8840..80de3ad 100644 --- a/fetcher/gopkg/gopkg_imps.gop +++ b/fetcher/gopkg/gopkg_imps.gop @@ -23,19 +23,22 @@ import ( ) type Result struct { - Name string `json:"name"` + Path string `json:"path"` ImportedBy int `json:"importedBy"` } // New creates a new Result from a html document. func New(input any, doc hdq.NodeSet) Result { const importedByPrefix = "Imported By:" - name := input.(string) + path := input.(string) a := doc.any.a.attribute("aria-label", v => strings.hasPrefix(v, importedByPrefix)).one + if !a.ok { + return {path, 0} + } label := a.attr("aria-label")! labelVal := strings.trimSpace(label[len(importedByPrefix):]) importedBy := strings.replaceAll(labelVal, ",", "").int! - return {name, importedBy} + return {path, importedBy} } // URL returns the input URL for the given name. From 86d2d825b00086e02dc9870063bcc8c2600e4d73 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 2 Aug 2024 02:39:53 +0800 Subject: [PATCH 4/5] x --- fetcher/gopkg/_testdata/encoding/out.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetcher/gopkg/_testdata/encoding/out.json b/fetcher/gopkg/_testdata/encoding/out.json index 4737abc..69add24 100644 --- a/fetcher/gopkg/_testdata/encoding/out.json +++ b/fetcher/gopkg/_testdata/encoding/out.json @@ -1,4 +1,4 @@ { - "name": "", + "path": "", "importedBy": 14960 } \ No newline at end of file From b227763df96c8225c3e6611f3227091bc9af7955 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 2 Aug 2024 03:18:15 +0800 Subject: [PATCH 5/5] gopkgimps: list and checkbox --- chore/gopkgimps/gopkgimps.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chore/gopkgimps/gopkgimps.go b/chore/gopkgimps/gopkgimps.go index 2f163a7..66b8f0a 100644 --- a/chore/gopkgimps/gopkgimps.go +++ b/chore/gopkgimps/gopkgimps.go @@ -46,6 +46,9 @@ func main() { return docs[i].ImportedBy > docs[j].ImportedBy }) for _, doc := range docs { - fmt.Println(doc.Path, doc.ImportedBy) + if doc.ImportedBy == 0 { + break + } + fmt.Printf("- [ ] %s (Imported By: %d)\n", doc.Path, doc.ImportedBy) } }