| 1 |
# This is a script to analyze the dependencies of this project. |
| 2 |
# It is a replacement of / complement to |
| 3 |
# |
| 4 |
# - cargo tree (used by this script) |
| 5 |
# - cargo deps |
| 6 |
# - cargo deny |
| 7 |
# |
| 8 |
# The idea is that by calling all_dep_info, you get a table of all dependencies |
| 9 |
# in Cargo.lock, with a few additional columns based on some other tools. |
| 10 |
# Currently, these tools are |
| 11 |
# |
| 12 |
# - cargo tree |
| 13 |
# - the crates.io API |
| 14 |
# |
| 15 |
# The most useful columns in the table are: |
| 16 |
# |
| 17 |
# - `name`: the name of the crate. |
| 18 |
# - `version`: the version of the crate. |
| 19 |
# - `num_versions`: the number of versions in Cargo.lock. |
| 20 |
# - `normal_dep`: whether the crate is a normal dependency. |
| 21 |
# - `build_dep`: whether the crate is a build dependency. |
| 22 |
# - `dev_dep`: whether the crate is a dev dependency. |
| 23 |
# - `organization`: the GitHub/GitLab organization or user of the repository of the crate. |
| 24 |
# - `repository_name`: the name of the repository the crate is in. The format is "{owner}/{repo}". |
| 25 |
# - `dependencies`: direct dependencies of the crate (in the format of Cargo.lock). |
| 26 |
# |
| 27 |
# To use this script, start Nushell (tested only on version 0.82.0), import the library and |
| 28 |
# call `all_dep_info`: |
| 29 |
# |
| 30 |
# ``` |
| 31 |
# > nu |
| 32 |
# > use util/deps.nu |
| 33 |
# > let dep = (deps all_dep_info) |
| 34 |
# ``` |
| 35 |
# |
| 36 |
# Then you can perform analysis. For example, to group the dependencies by organization: |
| 37 |
# |
| 38 |
# ``` |
| 39 |
# > $dep | group-by organization |
| 40 |
# ``` |
| 41 |
# |
| 42 |
# Or to find all crates with multiple versions (like cargo deny): |
| 43 |
# ``` |
| 44 |
# > $dep | where num_versions > 1 |
| 45 |
# ``` |
| 46 |
# |
| 47 |
# Ideas to expand this: |
| 48 |
# |
| 49 |
# - Figure out the whole dependency graph |
| 50 |
# - Figure out which platforms and which features enable which crates |
| 51 |
# - Figure out which utils require which crates |
| 52 |
# - Count the number of crates on different platforms |
| 53 |
# - Add license information |
| 54 |
# - Add functions to perform common analyses |
| 55 |
# - Add info from cargo bloat |
| 56 |
# - Add MSRV info |
| 57 |
# - Add up-to-date info (the necessary info is there, it just needs to be derived) |
| 58 |
# - Check the number of owners/contributors |
| 59 |
# - Make a webpage to more easily explore the data |
| 60 |
|
| 61 |
# Read the names output by cargo tree |
| 62 |
export def read_tree_names [edge_kind: string, features: list<string>]: any -> table<> { |
| 63 |
cargo tree --edges $edge_kind --features ($features | str join ",") |
| 64 |
| parse -r "(?P<name>[a-zA-Z0-9_-]+) v(?P<version>[0-9.]+)" |
| 65 |
} |
| 66 |
|
| 67 |
# Read the crates.io info for a list of crates names |
| 68 |
def read_crates_io [names: list<string>] -> any -> table<> { |
| 69 |
let total = $names | length |
| 70 |
$names | enumerate | par-each {|name| |
| 71 |
print $"($name.index)/($total): ($name.item)" |
| 72 |
http get $"https://crates.io/api/v1/crates/($name.item)" | get crate |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
# Add column for a dependency type |
| 77 |
def add_dep_type [dep_type: string, features: list<string>]: table<> -> table<> { |
| 78 |
let input_table = $in |
| 79 |
let table = read_tree_names $dep_type $features |
| 80 |
$input_table | insert $"($dep_type)_dep" {|outer| |
| 81 |
$table | any {|inner| |
| 82 |
$inner.name == $outer.name and $inner.version == $outer.version |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
export def all_dep_info [] { |
| 88 |
let features = [unix, feat_selinux] |
| 89 |
|
| 90 |
let lock = open Cargo.lock | from toml | get package |
| 91 |
|
| 92 |
$lock |
| 93 |
# Add number of versions |
| 94 |
| join ($lock | group-by name | transpose | update column1 { length } | rename name num_versions) name |
| 95 |
# Add dependency types |
| 96 |
| add_dep_type normal $features |
| 97 |
| add_dep_type build $features |
| 98 |
| add_dep_type dev $features |
| 99 |
| insert used {|x| $x.normal_dep or $x.build_dep or $x.dev_dep} |
| 100 |
# Add crates.io info |
| 101 |
| join (read_crates_io ($lock.name | uniq)) name |
| 102 |
# Add GH org or user info |
| 103 |
# The organization is an indicator that crates should be treated as one dependency. |
| 104 |
# However, there are also unrelated projects by a single organization, so it's not |
| 105 |
# clear. |
| 106 |
| insert organization {|x| |
| 107 |
let repository = $x.repository? |
| 108 |
if ($repository == null) { "" } else { |
| 109 |
$repository | url parse | get path | path split | get 1 |
| 110 |
} |
| 111 |
} |
| 112 |
# Add repository (truncate everything after repo name) |
| 113 |
# If we get a url like |
| 114 |
# https://github.com/uutils/coreutils/tree/src/uu/ls |
| 115 |
# we only keep |
| 116 |
# uutils/coreutils |
| 117 |
# The idea is that crates in the same repo definitely belong to the same project and should |
| 118 |
# be treated as one dependency. |
| 119 |
| insert repository_name {|x| |
| 120 |
let repository = $x.repository? |
| 121 |
if ($repository == null) { '' } else { |
| 122 |
$repository |
| 123 |
| url parse |
| 124 |
| get path |
| 125 |
| path split |
| 126 |
| select 1 2 |
| 127 |
| path join |
| 128 |
} |
| 129 |
} |
| 130 |
} |