Skip to content

Commit

Permalink
Warn and detect edition of generated files (#445)
Browse files Browse the repository at this point in the history
If the detection happens to have false positive, it will be removed in
favor of keeping the warning only.

Fixes #444
  • Loading branch information
ia0 committed Apr 25, 2024
1 parent fe57cd1 commit 27ae8a6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/rust/blink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
//! The board must possess at least one LED and one timer. The applet indefinitely cycles through
//! all LEDs in order with a period of 1 second and blinks them for 500 milli-seconds.

// DO NOT EDIT MANUALLY:
// - Edit book/src/applet/prelude/led.rs instead.
// - Then use ./scripts/sync.sh to generate this file.

#![no_std]
wasefire::applet!();

Expand Down
4 changes: 4 additions & 0 deletions examples/rust/button/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
//!
//! The applet prints the button state to the debug output on any button event.

// DO NOT EDIT MANUALLY:
// - Edit book/src/applet/prelude/button1.rs instead.
// - Then use ./scripts/sync.sh to generate this file.

#![no_std]
wasefire::applet!();

Expand Down
4 changes: 4 additions & 0 deletions examples/rust/button_abort/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
//! If the button is released before the timer runs out, then the LED will blink until next button
//! press.

// DO NOT EDIT MANUALLY:
// - Edit book/src/applet/prelude/timer.rs instead.
// - Then use ./scripts/sync.sh to generate this file.

#![no_std]
wasefire::applet!();

Expand Down
4 changes: 4 additions & 0 deletions examples/rust/led/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
//! is toggled. After a button is released, it becomes associated with the next LED (wrapping back
//! to the first LED after the last LED).

// DO NOT EDIT MANUALLY:
// - Edit book/src/applet/prelude/button2.rs instead.
// - Then use ./scripts/sync.sh to generate this file.

#![no_std]
wasefire::applet!();

Expand Down
4 changes: 4 additions & 0 deletions examples/rust/memory_game/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
//! serial. It essentially shows a random base32 string (the length representing the level in the
//! game) for 3 seconds and gives 7 seconds to type it back.

// DO NOT EDIT MANUALLY:
// - Edit book/src/applet/prelude/usb.rs instead.
// - Then use ./scripts/sync.sh to generate this file.

#![no_std]
wasefire::applet!();

Expand Down
4 changes: 4 additions & 0 deletions examples/rust/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
//! The applet additionally relies on USB serial. It describes its own usage when connecting on the
//! USB serial.

// DO NOT EDIT MANUALLY:
// - Edit book/src/applet/prelude/store.rs instead.
// - Then use ./scripts/sync.sh to generate this file.

#![no_std]
wasefire::applet!();

Expand Down
2 changes: 2 additions & 0 deletions scripts/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

x() { ( set -x; "$@"; ); }
i() { _log '1;36' Info "$*"; }
w() { _log '1;33' Warn "$*"; }
t() { _log '1;33' Todo "$*"; }
d() { _log '1;32' Done "$*"; exit 0; }
e() { _log '1;31' Error "$*"; exit 1; }

Expand Down
20 changes: 19 additions & 1 deletion scripts/sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@ set -e
cargo xtask update-apis

book_example() {
sed '/ANCHOR/d' book/src/applet/prelude/$1.rs > examples/rust/$2/src/lib.rs
local src=book/src/applet/prelude/$1.rs
local dst=examples/rust/$2/src/lib.rs
# We only check that the destination is newer by more than one second, because when cloning the
# repository or switching branches, it may happen that the destination is slightly newer.
if [ $(stat -c%Y $dst) -gt $(($(stat -c%Y $src) + 1)) ]; then
t "Update $src instead of $dst"
e "$dst seems to have been manually modified"
fi
# Besides removing all anchors, we insert a warning before the #![no_std] line, which all examples
# should have near the beginning of the file.
sed '/ANCHOR/d;/^#!\[no_std\]$/{i \'"
// DO NOT EDIT MANUALLY:\\
// - Edit $src instead.\\
// - Then use ./scripts/sync.sh to generate this file.\\
}" $src > $dst
# Now that the destination has been updated, it is newer than the source. So we touch the source
# to preserve the invariant that the destination is never newer than the source.
touch $src
}

book_example led blink
Expand Down

0 comments on commit 27ae8a6

Please sign in to comment.