Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples #3813

Merged
merged 5 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/publish-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches: [master]
paths:
- 'ci/**'
- 'tools/build-examples/**'
- 'examples/**'

jobs:
Expand Down Expand Up @@ -33,7 +33,7 @@ jobs:
version: 'latest'

- name: Build examples
run: ./ci/build-examples.sh
run: cargo run -p build-examples -b build-examples

- name: Deploy to Firebase
uses: siku2/action-hosting-deploy@v1
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 0 additions & 43 deletions ci/build-examples.sh

This file was deleted.

2 changes: 2 additions & 0 deletions examples/async_clock/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/boids/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/communication_child_to_parent/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/communication_grandchild_with_grandparent/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/communication_grandparent_to_grandchild/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/communication_parent_to_child/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/contexts/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/counter/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/counter_functional/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/dyn_create_destroy_apps/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/file_upload/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/function_memory_game/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/function_router/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/function_todomvc/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/futures/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/game_of_life/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/immutable/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/inner_html/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
3 changes: 3 additions & 0 deletions examples/js_callback/Trunk.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[tools]
wasm_opt = "version_122"

[[hooks]]
stage = "pre_build"
command = "rustc"
Expand Down
2 changes: 2 additions & 0 deletions examples/keyed_list/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/mount_point/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/nested_list/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/node_refs/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/password_strength/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/portals/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/router/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/suspense/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/timer/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/timer_functional/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/todomvc/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/two_apps/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/web_worker_fib/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/web_worker_prime/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 2 additions & 0 deletions examples/webgl/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
wasm_opt = "version_122"
2 changes: 1 addition & 1 deletion packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![doc(html_logo_url = "https://yew.rs/img/logo.png")]
#![cfg_attr(documenting, feature(doc_cfg))]
#![cfg_attr(documenting, feature(doc_auto_cfg))]
#![cfg_attr(nightly_yew, feature(fn_traits, async_closure, unboxed_closures))]
#![cfg_attr(nightly_yew, feature(fn_traits, unboxed_closures))]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this emits warning cuz async_closure is stablized


//! # Yew Framework - API Documentation
//!
Expand Down
11 changes: 11 additions & 0 deletions tools/build-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "build-examples"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
reqwest = { version = "0.12.12", features = ["blocking", "json"] }
regex = "1.11.1"
toml = "0.8.20"
serde = { version = "1.0.218", features = ["derive"] }
123 changes: 123 additions & 0 deletions tools/build-examples/src/bin/update-wasm-opt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use build_examples::{get_latest_wasm_opt_version, is_wasm_opt_outdated, NO_TRUNK_EXAMPLES};
use regex::Regex;

fn main() -> ExitCode {
// Must be run from root of the repo
let examples_dir = Path::new("examples");
if !examples_dir.exists() {
eprintln!(
"examples directory not found. Make sure you're running from the root of the repo."
);
return ExitCode::from(1);
}

let latest_wasm_opt = get_latest_wasm_opt_version();
let mut outdated_example_paths = Vec::new();
let mut outdated_examples = Vec::new();

// Get all entries in the examples directory
let entries = fs::read_dir(examples_dir).expect("Failed to read examples directory");

for entry in entries {
let entry = entry.expect("Failed to read directory entry");
let path = entry.path();

// Skip if not a directory
if !path.is_dir() {
continue;
}

let example = path
.file_name()
.expect("Failed to get directory name")
.to_string_lossy()
.to_string();

// Skip ssr examples as they don't need trunk
if NO_TRUNK_EXAMPLES.contains(&example.as_str()) {
continue;
}

// Check Trunk.toml for wasm_opt version and collect outdated examples
if is_wasm_opt_outdated(&path, &latest_wasm_opt) {
outdated_examples.push(example);
outdated_example_paths.push(path);
}
}

if outdated_examples.is_empty() {
println!(
"All examples are up-to-date with the latest wasm_opt version: {}",
latest_wasm_opt
);
return ExitCode::from(0);
}

println!(
"Found {} examples with outdated or missing wasm_opt configuration:",
outdated_examples.len()
);
for example in &outdated_examples {
println!(" - {}", example);
}
println!("Latest wasm_opt version is: {}", latest_wasm_opt);
println!("Updating all examples...");

let updated_count = update_all_examples(&outdated_example_paths, &latest_wasm_opt);
println!(
"Updated {} example configurations to use {}",
updated_count, latest_wasm_opt
);

ExitCode::from(0)
}

pub fn update_all_examples(outdated_paths: &[PathBuf], latest_version: &str) -> usize {
let mut updated_count = 0;

let re = Regex::new(r#"(?m)^\[tools\]\s*\nwasm_opt\s*=\s*"(version_\d+)""#).unwrap();
for path in outdated_paths {
let trunk_toml_path = path.join("Trunk.toml");

let content = fs::read_to_string(&trunk_toml_path).unwrap_or_default();

let updated_content = if re.is_match(&content) {
// Replace existing wasm_opt version
re.replace(&content, |_: &regex::Captures| {
format!(
r#"[tools]
wasm_opt = "{}""#,
latest_version
)
})
.to_string()
} else {
// Add wasm_opt configuration
if content.is_empty() {
format!(
r#"[tools]
wasm_opt = "{}""#,
latest_version
)
} else {
format!(
"{}\n\n[tools]\nwasm_opt = \"{}\"",
content.trim(),
latest_version
)
}
};

if let Err(e) = fs::write(&trunk_toml_path, updated_content) {
println!("Failed to update {}: {}", trunk_toml_path.display(), e);
} else {
updated_count += 1;
}
}

updated_count
}
62 changes: 62 additions & 0 deletions tools/build-examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::fs;
use std::path::Path;

use serde::Deserialize;
use toml::Table;

/// Examples that don't use Trunk for building
pub const NO_TRUNK_EXAMPLES: [&str; 3] = ["simple_ssr", "ssr_router", "wasi_ssr_module"];

#[derive(Deserialize)]
struct GitHubRelease {
tag_name: String,
}

pub fn get_latest_wasm_opt_version() -> String {
let url = "https://api.github.com/repos/WebAssembly/binaryen/releases/latest";
let client = reqwest::blocking::Client::new();

// github api requires a user agent
// https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api?apiVersion=2022-11-28#user-agent-required
let req_builder = client.get(url).header("User-Agent", "yew-wasm-opt-checker");

// Send the request
let res = req_builder
.send()
.expect("Failed to send request to GitHub API");

if !res.status().is_success() {
// Get more details about the error
let status = res.status();
let error_text = res
.text()
.unwrap_or_else(|_| "Could not read error response".to_string());

panic!(
"GitHub API request failed with status: {}. Details: {}",
status, error_text
);
}

let release: GitHubRelease = res.json().expect("Failed to parse GitHub API response");
release.tag_name
}

pub fn is_wasm_opt_outdated(path: &Path, latest_version: &str) -> bool {
let trunk_toml_path = path.join("Trunk.toml");

if !trunk_toml_path.exists() {
return true;
}

let content = match fs::read_to_string(&trunk_toml_path) {
Ok(content) => content,
Err(_) => return true,
};

// Check if wasm_opt is configured and up-to-date
let table: Table = toml::from_str(&content).unwrap();
let tools = table.get("tools").unwrap().as_table().unwrap();
let wasm_opt = tools.get("wasm_opt").unwrap().as_str().unwrap();
wasm_opt != latest_version
}
Loading
Loading