-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathmain.rs
More file actions
436 lines (379 loc) · 11.2 KB
/
Copy pathmain.rs
File metadata and controls
436 lines (379 loc) · 11.2 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use clap::Parser;
use clap::Subcommand;
use deno_doc::DocParser;
use deno_doc::DocParserOptions;
use deno_doc::DocPrinter;
use deno_doc::ParseOutput;
use deno_doc::diff;
use deno_doc::find_nodes_by_name_recursively;
use deno_doc::html::GenerateCtx;
use deno_doc::html::HrefResolver;
use deno_doc::html::UrlResolveKind;
use deno_doc::html::UsageComposer;
use deno_doc::html::UsageComposerEntry;
use deno_doc::node::DeclarationDef;
use deno_graph::BuildOptions;
use deno_graph::GraphKind;
use deno_graph::ModuleGraph;
use deno_graph::ModuleSpecifier;
use deno_graph::ast::CapturingModuleAnalyzer;
use deno_graph::source::LoadFuture;
use deno_graph::source::LoadResponse;
use deno_graph::source::Loader;
use futures::executor::block_on;
use futures::future;
use indexmap::IndexMap;
use std::env::current_dir;
use std::path::PathBuf;
use std::rc::Rc;
#[derive(Parser)]
#[command(name = "ddoc")]
#[command(about = "Generate documentation for Deno/TypeScript modules")]
#[command(version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Generate documentation for source files
Doc {
/// Source files to document
#[arg(required = true)]
files: Vec<PathBuf>,
/// Output format: json
#[arg(long, conflicts_with = "html")]
json: bool,
/// Treat input files as JSON ParseOutput instead of source files
#[arg(long)]
json_input: bool,
/// Generate HTML documentation
#[arg(long, requires = "output")]
html: bool,
/// Output directory for HTML documentation
#[arg(short, long)]
output: Option<PathBuf>,
/// Package name for HTML documentation
#[arg(long)]
name: Option<String>,
/// Main entrypoint for HTML documentation
#[arg(long)]
main_entrypoint: Option<PathBuf>,
/// Filter documentation by name
#[arg(short, long, conflicts_with = "html")]
filter: Option<String>,
/// Include private items
#[arg(long)]
private: bool,
},
/// Compare documentation between two versions
///
/// Usage: ddoc diff <OLD_FILES>... -- <NEW_FILES>...
Diff {
/// Old source files (before --)
#[arg(required = true)]
old_files: Vec<PathBuf>,
/// New source files (after --)
#[arg(last = true, required = true)]
new_files: Vec<PathBuf>,
/// Include private items
#[arg(long)]
private: bool,
/// Treat input files as JSON ParseOutput instead of source files
#[arg(long)]
json_input: bool,
},
}
struct SourceFileLoader;
impl Loader for SourceFileLoader {
fn load(
&self,
specifier: &ModuleSpecifier,
_options: deno_graph::source::LoadOptions,
) -> LoadFuture {
let result = if specifier.scheme() == "file" {
let path = specifier.to_file_path().unwrap();
std::fs::read(path)
.map(|content| {
Some(LoadResponse::Module {
specifier: specifier.clone(),
mtime: None,
maybe_headers: None,
content: content.into(),
})
})
.map_err(|err| {
deno_graph::source::LoadError::Other(std::sync::Arc::new(err))
})
} else {
Ok(None)
};
Box::pin(future::ready(result))
}
}
fn path_to_specifier(path: &PathBuf) -> ModuleSpecifier {
let cwd = current_dir().unwrap();
let absolute = if path.is_absolute() {
path.clone()
} else {
cwd.join(path)
};
ModuleSpecifier::from_file_path(absolute).unwrap()
}
async fn parse_sources(
source_files: Vec<ModuleSpecifier>,
private: bool,
) -> anyhow::Result<ParseOutput> {
let loader = SourceFileLoader;
let analyzer = CapturingModuleAnalyzer::default();
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(
source_files.clone(),
Vec::new(),
&loader,
BuildOptions {
module_analyzer: &analyzer,
..Default::default()
},
)
.await;
let mut source_files = source_files;
source_files.sort();
let parser = DocParser::new(
&graph,
&analyzer,
&source_files,
DocParserOptions {
diagnostics: false,
private,
},
)?;
Ok(parser.parse()?)
}
async fn run() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Doc {
files,
json,
json_input,
html,
output,
name,
main_entrypoint,
filter,
private,
} => {
let doc_nodes_by_url = if json_input {
assert_eq!(files.len(), 1);
serde_json::from_reader(std::fs::File::open(&files[0])?)?
} else {
let source_files = files.iter().map(path_to_specifier).collect();
parse_sources(source_files, private).await?
};
if html {
let output_dir = output.unwrap();
let main_entrypoint = main_entrypoint.map(|p| path_to_specifier(&p));
generate_docs_directory(
name,
output_dir,
main_entrypoint,
doc_nodes_by_url,
)?;
return Ok(());
}
let mut merged_doc = deno_doc::Document::default();
for doc in doc_nodes_by_url.into_values() {
if merged_doc.module_doc.is_empty() {
merged_doc.module_doc = doc.module_doc;
}
merged_doc.symbols.extend(doc.symbols);
}
merged_doc.symbols.retain(|doc_node| {
!matches!(doc_node.declarations[0].def, DeclarationDef::Import(..))
});
if let Some(filter) = filter {
merged_doc.symbols =
find_nodes_by_name_recursively(merged_doc.symbols, &filter);
}
if json {
serde_json::to_writer_pretty(std::io::stdout(), &merged_doc)?;
println!();
} else {
let result = DocPrinter::new(&merged_doc, true, false);
println!("{result}");
}
}
Commands::Diff {
old_files,
new_files,
private,
json_input,
} => {
let (old, new) = if json_input {
assert_eq!(old_files.len(), 1);
assert_eq!(new_files.len(), 1);
let old_docs: ParseOutput =
serde_json::from_reader(std::fs::File::open(&old_files[0])?)?;
let new_docs: ParseOutput =
serde_json::from_reader(std::fs::File::open(&new_files[0])?)?;
(old_docs, new_docs)
} else {
let old_specifiers =
old_files.iter().map(path_to_specifier).collect::<Vec<_>>();
let new_specifiers =
new_files.iter().map(path_to_specifier).collect::<Vec<_>>();
let old_docs = parse_sources(old_specifiers.clone(), private).await?;
let new_docs = parse_sources(new_specifiers.clone(), private).await?;
// Match modules by position: old[0] -> new[0], old[1] -> new[1], etc.
// This allows comparing renamed modules.
let old_by_original = old_specifiers
.iter()
.zip(old_docs.into_iter())
.map(|(orig, (_, nodes))| (orig.clone(), nodes))
.collect::<IndexMap<_, _>>();
let new_by_original = new_specifiers
.iter()
.zip(new_docs.into_iter())
.map(|(orig, (_, nodes))| (orig.clone(), nodes))
.collect::<IndexMap<_, _>>();
// Create normalized maps using new specifiers as canonical keys
let mut old_normalized = IndexMap::new();
let mut new_normalized = IndexMap::new();
for (i, new_spec) in new_specifiers.iter().enumerate() {
if let Some(old_spec) = old_specifiers.get(i)
&& let Some(old_nodes) = old_by_original.get(old_spec)
{
old_normalized.insert(new_spec.clone(), old_nodes.clone());
}
if let Some(new_nodes) = new_by_original.get(new_spec) {
new_normalized.insert(new_spec.clone(), new_nodes.clone());
}
}
// Handle extra old modules (removed)
for old_spec in old_specifiers.iter().skip(new_specifiers.len()) {
if let Some(old_nodes) = old_by_original.get(old_spec) {
old_normalized.insert(old_spec.clone(), old_nodes.clone());
}
}
(old_normalized, new_normalized)
};
let doc_diff = diff::DocDiff::diff(&old, &new);
serde_json::to_writer_pretty(std::io::stdout(), &doc_diff)?;
println!();
}
}
Ok(())
}
fn main() {
let future = async move {
if let Err(err) = run().await {
eprintln!("{}", err);
std::process::exit(1);
}
};
block_on(future);
}
struct EmptyResolver;
impl HrefResolver for EmptyResolver {
fn resolve_path(
&self,
current: UrlResolveKind,
target: UrlResolveKind,
) -> String {
deno_doc::html::href_path_resolve(current, target)
}
fn resolve_global_symbol(&self, _symbol: &[String]) -> Option<String> {
None
}
fn resolve_import_href(
&self,
_symbol: &[String],
_src: &str,
) -> Option<String> {
None
}
fn resolve_source(&self, location: &deno_doc::Location) -> Option<String> {
Some(location.filename.to_string())
}
fn resolve_external_jsdoc_module(
&self,
_module: &str,
_symbol: Option<&str>,
) -> Option<(String, String)> {
None
}
}
impl UsageComposer for EmptyResolver {
fn is_single_mode(&self) -> bool {
true
}
fn compose(
&self,
current_resolve: UrlResolveKind,
usage_to_md: deno_doc::html::UsageToMd,
) -> IndexMap<UsageComposerEntry, String> {
current_resolve
.get_file()
.map(|current_file| {
IndexMap::from([(
UsageComposerEntry {
name: "".to_string(),
icon: None,
},
usage_to_md(current_file.specifier.as_str(), None),
)])
})
.unwrap_or_default()
}
}
fn generate_docs_directory(
package_name: Option<String>,
output_dir: PathBuf,
main_entrypoint: Option<ModuleSpecifier>,
doc_nodes_by_url: IndexMap<ModuleSpecifier, deno_doc::Document>,
) -> Result<(), anyhow::Error> {
let cwd = current_dir().unwrap();
let output_dir_resolved = cwd.join(output_dir);
let mut index_map = IndexMap::new();
if let Some(main_entrypoint) = main_entrypoint.as_ref() {
index_map.insert(main_entrypoint.clone(), String::from("."));
}
let options = deno_doc::html::GenerateOptions {
package_name,
main_entrypoint,
href_resolver: Rc::new(EmptyResolver),
usage_composer: Some(Rc::new(EmptyResolver)),
rewrite_map: Some(index_map),
category_docs: None,
disable_search: false,
symbol_redirect_map: None,
default_symbol_map: None,
markdown_renderer: deno_doc::html::comrak::create_renderer(
None, None, None,
),
markdown_stripper: Rc::new(deno_doc::html::comrak::strip),
head_inject: Some(Rc::new(|root| {
format!(
r#"<link rel="stylesheet" href="{root}{}">"#,
deno_doc::html::comrak::COMRAK_STYLESHEET_FILENAME
)
})),
id_prefix: None,
diff_only: false,
};
let ctx = GenerateCtx::create_basic(options, doc_nodes_by_url, None)?;
let html = deno_doc::html::generate(ctx)?;
let path = &output_dir_resolved;
let _ = std::fs::remove_dir_all(path);
std::fs::create_dir(path)?;
for (name, content) in html {
let this_path = path.join(name);
let prefix = this_path.parent().unwrap();
std::fs::create_dir_all(prefix).unwrap();
std::fs::write(this_path, content).unwrap();
}
Ok(())
}