|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +# Adds #[doc(cfg(...))] annotations to Rust docs, based on #[cfg(...)] in the source code. |
| 8 | + |
| 9 | +# Keep in sync with the same file in website repo. |
| 10 | + |
| 11 | +# Usage: |
| 12 | +# apply-doc-cfg.sh [--install-sd] [--rustfmt] |
| 13 | + |
| 14 | +set -e |
| 15 | + |
| 16 | + |
| 17 | +for arg in "$@"; do |
| 18 | + case "$arg" in |
| 19 | + --install-sd) |
| 20 | + installSd="true" |
| 21 | + ;; |
| 22 | + --rustfmt) |
| 23 | + rustfmt="true" |
| 24 | + ;; |
| 25 | + *) |
| 26 | + echo "Unknown argument: $arg" |
| 27 | + exit 1 |
| 28 | + ;; |
| 29 | + esac |
| 30 | +done |
| 31 | + |
| 32 | +SD_VERSION="1.0.0" |
| 33 | +PRE="DocCfg | " |
| 34 | + |
| 35 | +# For gdext, add feature/cfg annotations in docs. This needs nightly rustdoc + custom preprocessing. |
| 36 | +# Replace #[cfg(...)] with #[doc(cfg(...))], a nightly feature: https://doc.rust-lang.org/unstable-book/language-features/doc-cfg.html |
| 37 | +# Potential alternative: https://docs.rs/doc-cfg/latest/doc_cfg |
| 38 | +if [[ "$installSd" == "true" ]]; then |
| 39 | + # Install sd (modern sed). No point in waiting for eternal `cargo install` if we can fetch a prebuilt binary in 1s. |
| 40 | + echo "$PRE install sd (modern sed)..." |
| 41 | + curl -L https://github.com/chmln/sd/releases/download/v${SD_VERSION}/sd-v${SD_VERSION}-x86_64-unknown-linux-musl.tar.gz -o archive.tar.gz |
| 42 | + mkdir -p /tmp/tools |
| 43 | + tar -zxvf archive.tar.gz -C /tmp/tools --strip-components=1 |
| 44 | + sd=/tmp/tools/sd |
| 45 | +else |
| 46 | + sd=sd |
| 47 | +fi |
| 48 | + |
| 49 | +echo "$PRE preprocess docs..." |
| 50 | + |
| 51 | +# Enable feature in each lib.rs file. |
| 52 | +# Note: first command uses sed because it's easier, and only handful of files. |
| 53 | +find . -type f -name "lib.rs" -exec sed -i '1s/^/#![cfg_attr(published_docs, feature(doc_cfg))]\n/' {} + |
| 54 | + |
| 55 | +# Then do the actual replacements. |
| 56 | +# Could use \( -path "..." -o -path "..." \) to limit to certain paths. |
| 57 | +# Do NOT include './target/debug/build/*' because generated files cannot be modified -- rustdoc will rerun the generation. |
| 58 | +# This is done by directly emitting #[cfg_attr(published_docs, doc(cfg(...)))] in the godot-codegen crate, and that cfg is passed below. |
| 59 | +find . -type f -name '*.rs' \ |
| 60 | + \( -path './godot' -o -path './godot-*' \) \ |
| 61 | +| while read -r file; do |
| 62 | + # Replace #[cfg(...)] with #[doc(cfg(...))]. Do not insert a newline, in case the #[cfg] is commented-out. |
| 63 | + # shellcheck disable=SC2016 |
| 64 | + $sd '(\#\[(cfg\(.+?\))\])(\s*)([A-Za-z]|#\[)' '$1 #[cfg_attr(published_docs, doc($2))]$3$4' "$file" |
| 65 | + # $sd '(\#\[(cfg\(.+?\))\])\s*([A-Za-z]|#\[)' '$1 #[doc($2)]\n$3' "$file" |
| 66 | + # ^^^^^^^^^^^^^^^^^ require that #[cfg] is followed by an identifier or a #[ attribute start. |
| 67 | + # This avoids some usages of function-local #[cfg]s, although by far not all. Others generate warnings, which is fine. |
| 68 | +done |
| 69 | + |
| 70 | +if [[ "$rustfmt" == "true" ]]; then |
| 71 | + echo "$PRE Format code using rustfmt..." |
| 72 | + cargo fmt --all |
| 73 | +fi |
| 74 | + |
| 75 | +echo "$PRE Docs post-processed." |
0 commit comments