Skip to content

Commit 3069e7a

Browse files
authored
feat: add a script that automatically update function indexing when using the FunctionOrder::ByIndex option (#7)
* chore: move auto index script to a script folder * docs: add documentation for the index script * feat: add help for the index script * chore: clippy fix
1 parent 2218d9c commit 3069e7a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

scripts/rhai-autodocs-indexer.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
4+
if len(sys.argv) != 2:
5+
print("""usage: rhai-autodocs-indexer.py <path-to-source-file>
6+
7+
This script searches for any `# rhai-autodocs:index:` comment and
8+
automatically adds the index number following the order of the function
9+
in the source file.
10+
11+
See the `FunctionOrder::ByIndex` option from the rhai-autodocs crate.
12+
""")
13+
exit(1)
14+
15+
file = sys.argv[1]
16+
17+
oldfile = open(file, 'r')
18+
newfile = open(f"{file}.autodocs", 'w')
19+
20+
function_order = 1
21+
for line in oldfile:
22+
newline = ""
23+
if "# rhai-autodocs:index:" in line:
24+
newline = f" /// # rhai-autodocs:index:{function_order}\n"
25+
function_order += 1
26+
else:
27+
newline = line
28+
29+
newfile.write(newline)

src/options.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ pub enum FunctionOrder {
101101
/// #[rhai_fn(global)]
102102
/// pub fn my_function2() {}
103103
/// ```
104+
///
105+
/// Adding, removing or re-ordering your functions from your api can be a chore
106+
/// because you have to update all indexes by hand. Thankfully, you will found
107+
/// a python script in the `scripts` folder of the `rhai-autodocs` repository
108+
/// that will update the indexes by hand just for you.
109+
///
110+
/// The script generates a .autodocs file from your original source file,
111+
/// make sure to check that it did not mess with your source code using
112+
/// a diff tool.
104113
ByIndex,
105114
}
106115

@@ -130,7 +139,7 @@ impl FunctionOrder {
130139
{
131140
let index = index
132141
.parse::<usize>()
133-
.map_err(|err| AutodocsError::PreProcessing(format!("failed to parsed order metadata: {}", err.to_string())))?;
142+
.map_err(|err| AutodocsError::PreProcessing(format!("failed to parsed order metadata: {err}")))?;
134143

135144
if let Some(slot) = ordered.get_mut(index - 1) {
136145
*slot = (function.clone(), polymorphisms.clone());

0 commit comments

Comments
 (0)