1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use cargo::{
core::{
registry::{LockedPatchDependency, PackageRegistry},
Dependency, EitherManifest, SourceId, Verbosity
},
util::{important_paths::find_root_manifest_for_wd, toml::read_manifest},
Config as CargoConfig
};
use clap::Clap;
use std::{borrow::Cow, env, fs::File, io::Read, path::PathBuf};
mod input;
mod output;
#[derive(Clap)]
enum Subcommand {
Doc2readme(Args)
}
#[derive(Clap)]
struct Args {
#[clap(long)]
manifest_path: Option<PathBuf>,
#[clap(short, long, default_value = "README.md")]
out: PathBuf,
#[clap(short, long, default_value = "README.j2")]
template: PathBuf
}
#[derive(Clap)]
struct CmdLine {
#[clap(subcommand)]
cmd: Subcommand
}
fn main() {
let args = match env::args().nth(1) {
Some(subcmd) if subcmd == "doc2readme" => match CmdLine::parse().cmd {
Subcommand::Doc2readme(args) => args
},
_ => Args::parse()
};
let manifest_path = match args.manifest_path {
Some(path) if path.is_relative() => env::current_dir().unwrap().join(path),
Some(path) => path,
None => find_root_manifest_for_wd(&env::current_dir().unwrap()).expect("Unable to find Cargo.toml")
};
let cargo_cfg = CargoConfig::default().expect("Failed to initialize cargo");
let src_id = SourceId::for_path(&manifest_path).expect("Failed to obtain source id");
let manifest = match read_manifest(&manifest_path, src_id, &cargo_cfg).expect("Failed to read Cargo.toml") {
(EitherManifest::Real(manifest), _) => manifest,
(EitherManifest::Virtual(_), _) => panic!("What on earth is a virtual manifest?")
};
match env::var("RUST_LOG") {
Ok(log) if log == "debug" => cargo_cfg.shell().set_verbosity(Verbosity::Verbose),
_ => cargo_cfg.shell().set_verbosity(Verbosity::Normal)
}
let targets = manifest.targets();
let target = targets
.iter()
.find(|target| target.is_lib())
.or_else(|| {
targets
.iter()
.find(|target| target.is_bin() && target.name() == manifest.name().as_str())
})
.or_else(|| targets.iter().find(|target| target.is_bin()))
.expect("Failed to find a library or binary target");
let _guard = cargo_cfg
.acquire_package_cache_lock()
.expect("Failed to aquire package cache lock");
let mut registry = PackageRegistry::new(&cargo_cfg).expect("Failed to initialize crate registry");
for (url, deps) in manifest.patch() {
let deps: Vec<(&Dependency, Option<LockedPatchDependency>)> = deps.iter().map(|dep| (dep, None)).collect();
registry.patch(url, &deps).expect("Failed to apply patches");
}
registry.lock_patches();
let template: Cow<'static, str> = if args.template.exists() {
let mut buf = String::new();
File::open(args.template)
.expect("Failed to open template")
.read_to_string(&mut buf)
.expect("Failed to read template");
buf.into()
} else {
include_str!("README.j2").into()
};
init_git_transports(&cargo_cfg);
let file = target.src_path().path().expect("Target does not have a source file");
cargo_cfg.shell().status("Reading", file.display()).ok();
let input_file = input::read_file(&manifest, &mut registry, file).expect("Unable to read file");
cargo_cfg
.shell()
.verbose(|shell| shell.status("Processing", format!("{:?}", input_file)))
.ok();
if input_file.scope.has_glob_use {
cargo_cfg.shell().warn("Your code contains glob use statements (e.g. `use std::io::prelude::*;`). Those can lead to incomplete link generation.").ok();
}
let out = if args.out.is_relative() {
env::current_dir().unwrap().join(args.out)
} else {
args.out
};
cargo_cfg.shell().status("Writing", out.display()).ok();
let mut out = File::create(out).expect("Unable to create output file");
output::emit(input_file, &template, &mut out).expect("Unable to write output file");
cargo_cfg.release_package_cache_lock();
}
fn init_git_transports(config: &CargoConfig) {
match cargo::ops::needs_custom_http_transport(config) {
Ok(true) => {},
_ => return
}
let handle = match cargo::ops::http_handle(config) {
Ok(handle) => handle,
Err(..) => return
};
unsafe {
git2_curl::register(handle);
}
}