|
| 1 | +package rust |
| 2 | + |
| 3 | +import ( |
| 4 | + "opensca/internal/logs" |
| 5 | + "opensca/internal/srt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/BurntSushi/toml" |
| 10 | +) |
| 11 | + |
| 12 | +type cargoPkg struct { |
| 13 | + Name string `toml:"name"` |
| 14 | + Version string `toml:"version"` |
| 15 | + DepStr []string `toml:"dependencies"` |
| 16 | + Dependencies []struct { |
| 17 | + Name string |
| 18 | + Version string |
| 19 | + } `toml:"-"` |
| 20 | +} |
| 21 | + |
| 22 | +func parseCargoLock(dirRoot *srt.DirTree, depRoot *srt.DepTree, file *srt.FileData) []*srt.DepTree { |
| 23 | + cargo := struct { |
| 24 | + Pkgs []*cargoPkg `toml:"package"` |
| 25 | + }{} |
| 26 | + cdepMap := map[string]*cargoPkg{} |
| 27 | + depMap := map[string]*srt.DepTree{} |
| 28 | + directMap := map[string]*srt.DepTree{} |
| 29 | + if err := toml.Unmarshal(file.Data, &cargo); err != nil { |
| 30 | + logs.Warn(err) |
| 31 | + } |
| 32 | + for _, pkg := range cargo.Pkgs { |
| 33 | + dep := srt.NewDepTree(nil) |
| 34 | + dep.Name = pkg.Name |
| 35 | + dep.Version = srt.NewVersion(pkg.Version) |
| 36 | + pkg.Dependencies = make([]struct { |
| 37 | + Name string |
| 38 | + Version string |
| 39 | + }, len(pkg.DepStr)) |
| 40 | + for i, str := range pkg.DepStr { |
| 41 | + name, version := str, "" |
| 42 | + index := strings.Index(str, " ") |
| 43 | + if index > -1 { |
| 44 | + name, version = str[:index], str[index+1:] |
| 45 | + } |
| 46 | + pkg.Dependencies[i] = struct { |
| 47 | + Name string |
| 48 | + Version string |
| 49 | + }{Name: name, Version: version} |
| 50 | + } |
| 51 | + depMap[dep.Name] = dep |
| 52 | + directMap[dep.Name] = dep |
| 53 | + cdepMap[dep.Name] = pkg |
| 54 | + } |
| 55 | + // 找出未被依赖的作为直接依赖 |
| 56 | + for _, pkg := range cargo.Pkgs { |
| 57 | + for _, d := range pkg.Dependencies { |
| 58 | + delete(directMap, d.Name) |
| 59 | + } |
| 60 | + } |
| 61 | + directDeps := []*srt.DepTree{} |
| 62 | + for _, v := range directMap { |
| 63 | + directDeps = append(directDeps, v) |
| 64 | + } |
| 65 | + sort.Slice(directDeps, func(i, j int) bool { |
| 66 | + return directDeps[i].Name < directDeps[j].Name |
| 67 | + }) |
| 68 | + for _, d := range directDeps { |
| 69 | + d.Parent = depRoot |
| 70 | + depRoot.Children = append(depRoot.Children, d) |
| 71 | + } |
| 72 | + // 从顶层开始构建 |
| 73 | + q := make([]*srt.DepTree, len(directDeps)) |
| 74 | + copy(q, directDeps) |
| 75 | + exist := map[string]struct{}{} |
| 76 | + for len(q) > 0 { |
| 77 | + n := q[0] |
| 78 | + exist[n.Name] = struct{}{} |
| 79 | + if cdep, ok := cdepMap[n.Name]; ok { |
| 80 | + for _, d := range cdep.Dependencies { |
| 81 | + if _, ok := exist[d.Name]; !ok { |
| 82 | + exist[d.Name] = struct{}{} |
| 83 | + if sub, ok := depMap[d.Name]; ok { |
| 84 | + sub.Parent = n |
| 85 | + n.Children = append(n.Children, sub) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + q = append(q[1:], n.Children...) |
| 91 | + } |
| 92 | + return directDeps |
| 93 | +} |
0 commit comments