Skip to content

Commit

Permalink
Merge pull request #58 from xushiwei/q
Browse files Browse the repository at this point in the history
tool: gostdpkgs
  • Loading branch information
xushiwei authored Aug 1, 2024
2 parents b26a880 + b227763 commit b2e4978
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 32 deletions.
10 changes: 6 additions & 4 deletions chore/gopkgimps/gopkgimps.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package main

import (
"encoding/json"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -46,7 +45,10 @@ 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 {
if doc.ImportedBy == 0 {
break
}
fmt.Printf("- [ ] %s (Imported By: %d)\n", doc.Path, doc.ImportedBy)
}
}
65 changes: 65 additions & 0 deletions chore/gostdpkgs/gostdpkgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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/"
fis, err := os.ReadDir(dir)
check(err)
pkgs := collect(nil, fis, dir, "")
fmt.Println(strings.Join(pkgs, "\n"))
}

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 + "/"
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)
}
}
2 changes: 1 addition & 1 deletion fetcher/gopkg/_testdata/encoding/out.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "",
"path": "",
"importedBy": 14960
}
53 changes: 29 additions & 24 deletions fetcher/gopkg/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions fetcher/gopkg/gopkg_imps.gop
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b2e4978

Please sign in to comment.