mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 16:57:58 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -18,15 +18,20 @@
|
||||
use crate::{write_file_if_changed, CargoCommandVersioned};
|
||||
|
||||
use std::{
|
||||
fs, path::{Path, PathBuf}, borrow::ToOwned, process, env, collections::HashSet,
|
||||
hash::{Hash, Hasher}, ops::Deref,
|
||||
borrow::ToOwned,
|
||||
collections::HashSet,
|
||||
env, fs,
|
||||
hash::{Hash, Hasher},
|
||||
ops::Deref,
|
||||
path::{Path, PathBuf},
|
||||
process,
|
||||
};
|
||||
|
||||
use toml::value::Table;
|
||||
|
||||
use build_helper::rerun_if_changed;
|
||||
|
||||
use cargo_metadata::{MetadataCommand, Metadata};
|
||||
use cargo_metadata::{Metadata, MetadataCommand};
|
||||
|
||||
use walkdir::WalkDir;
|
||||
|
||||
@@ -114,19 +119,16 @@ pub(crate) fn create_and_compile(
|
||||
);
|
||||
|
||||
build_project(&project, default_rustflags, cargo_cmd);
|
||||
let (wasm_binary, wasm_binary_compressed, bloaty) = compact_wasm_file(
|
||||
&project,
|
||||
project_cargo_toml,
|
||||
wasm_binary_name,
|
||||
);
|
||||
let (wasm_binary, wasm_binary_compressed, bloaty) =
|
||||
compact_wasm_file(&project, project_cargo_toml, wasm_binary_name);
|
||||
|
||||
wasm_binary.as_ref().map(|wasm_binary|
|
||||
copy_wasm_to_target_directory(project_cargo_toml, wasm_binary)
|
||||
);
|
||||
wasm_binary
|
||||
.as_ref()
|
||||
.map(|wasm_binary| copy_wasm_to_target_directory(project_cargo_toml, wasm_binary));
|
||||
|
||||
wasm_binary_compressed.as_ref().map(|wasm_binary_compressed|
|
||||
wasm_binary_compressed.as_ref().map(|wasm_binary_compressed| {
|
||||
copy_wasm_to_target_directory(project_cargo_toml, wasm_binary_compressed)
|
||||
);
|
||||
});
|
||||
|
||||
generate_rerun_if_changed_instructions(project_cargo_toml, &project, &wasm_workspace);
|
||||
|
||||
@@ -144,17 +146,17 @@ fn find_cargo_lock(cargo_manifest: &Path) -> Option<PathBuf> {
|
||||
}
|
||||
|
||||
if !path.pop() {
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(path) = find_impl(build_helper::out_dir()) {
|
||||
return Some(path);
|
||||
return Some(path)
|
||||
}
|
||||
|
||||
if let Some(path) = find_impl(cargo_manifest.to_path_buf()) {
|
||||
return Some(path);
|
||||
return Some(path)
|
||||
}
|
||||
|
||||
build_helper::warning!(
|
||||
@@ -169,15 +171,20 @@ fn find_cargo_lock(cargo_manifest: &Path) -> Option<PathBuf> {
|
||||
/// Extract the crate name from the given `Cargo.toml`.
|
||||
fn get_crate_name(cargo_manifest: &Path) -> String {
|
||||
let cargo_toml: Table = toml::from_str(
|
||||
&fs::read_to_string(cargo_manifest).expect("File exists as checked before; qed")
|
||||
).expect("Cargo manifest is a valid toml file; qed");
|
||||
&fs::read_to_string(cargo_manifest).expect("File exists as checked before; qed"),
|
||||
)
|
||||
.expect("Cargo manifest is a valid toml file; qed");
|
||||
|
||||
let package = cargo_toml
|
||||
.get("package")
|
||||
.and_then(|t| t.as_table())
|
||||
.expect("`package` key exists in valid `Cargo.toml`; qed");
|
||||
|
||||
package.get("name").and_then(|p| p.as_str()).map(ToOwned::to_owned).expect("Package name exists; qed")
|
||||
package
|
||||
.get("name")
|
||||
.and_then(|p| p.as_str())
|
||||
.map(ToOwned::to_owned)
|
||||
.expect("Package name exists; qed")
|
||||
}
|
||||
|
||||
/// Returns the name for the wasm binary.
|
||||
@@ -192,9 +199,10 @@ fn get_wasm_workspace_root() -> PathBuf {
|
||||
loop {
|
||||
match out_dir.parent() {
|
||||
Some(parent) if out_dir.ends_with("build") => return parent.to_path_buf(),
|
||||
_ => if !out_dir.pop() {
|
||||
break;
|
||||
}
|
||||
_ =>
|
||||
if !out_dir.pop() {
|
||||
break
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,10 +218,10 @@ fn create_project_cargo_toml(
|
||||
enabled_features: impl Iterator<Item = String>,
|
||||
) {
|
||||
let mut workspace_toml: Table = toml::from_str(
|
||||
&fs::read_to_string(
|
||||
workspace_root_path.join("Cargo.toml"),
|
||||
).expect("Workspace root `Cargo.toml` exists; qed")
|
||||
).expect("Workspace root `Cargo.toml` is a valid toml file; qed");
|
||||
&fs::read_to_string(workspace_root_path.join("Cargo.toml"))
|
||||
.expect("Workspace root `Cargo.toml` exists; qed"),
|
||||
)
|
||||
.expect("Workspace root `Cargo.toml` is a valid toml file; qed");
|
||||
|
||||
let mut wasm_workspace_toml = Table::new();
|
||||
|
||||
@@ -232,25 +240,25 @@ fn create_project_cargo_toml(
|
||||
wasm_workspace_toml.insert("profile".into(), profile.into());
|
||||
|
||||
// Add patch section from the project root `Cargo.toml`
|
||||
while let Some(mut patch) = workspace_toml.remove("patch")
|
||||
.and_then(|p| p.try_into::<Table>().ok()) {
|
||||
while let Some(mut patch) =
|
||||
workspace_toml.remove("patch").and_then(|p| p.try_into::<Table>().ok())
|
||||
{
|
||||
// Iterate over all patches and make the patch path absolute from the workspace root path.
|
||||
patch.iter_mut()
|
||||
.filter_map(|p|
|
||||
patch
|
||||
.iter_mut()
|
||||
.filter_map(|p| {
|
||||
p.1.as_table_mut().map(|t| t.iter_mut().filter_map(|t| t.1.as_table_mut()))
|
||||
)
|
||||
})
|
||||
.flatten()
|
||||
.for_each(|p|
|
||||
p.iter_mut()
|
||||
.filter(|(k, _)| k == &"path")
|
||||
.for_each(|(_, v)| {
|
||||
if let Some(path) = v.as_str().map(PathBuf::from) {
|
||||
if path.is_relative() {
|
||||
*v = workspace_root_path.join(path).display().to_string().into();
|
||||
}
|
||||
.for_each(|p| {
|
||||
p.iter_mut().filter(|(k, _)| k == &"path").for_each(|(_, v)| {
|
||||
if let Some(path) = v.as_str().map(PathBuf::from) {
|
||||
if path.is_relative() {
|
||||
*v = workspace_root_path.join(path).display().to_string().into();
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
wasm_workspace_toml.insert("patch".into(), patch.into());
|
||||
}
|
||||
@@ -296,7 +304,8 @@ fn find_package_by_manifest_path<'a>(
|
||||
manifest_path: &Path,
|
||||
crate_metadata: &'a cargo_metadata::Metadata,
|
||||
) -> &'a cargo_metadata::Package {
|
||||
crate_metadata.packages
|
||||
crate_metadata
|
||||
.packages
|
||||
.iter()
|
||||
.find(|p| p.manifest_path == manifest_path)
|
||||
.expect("Wasm project exists in its own metadata; qed")
|
||||
@@ -309,18 +318,19 @@ fn project_enabled_features(
|
||||
) -> Vec<String> {
|
||||
let package = find_package_by_manifest_path(cargo_manifest, crate_metadata);
|
||||
|
||||
let mut enabled_features = package.features.keys()
|
||||
let mut enabled_features = package
|
||||
.features
|
||||
.keys()
|
||||
.filter(|f| {
|
||||
let mut feature_env = f.replace("-", "_");
|
||||
feature_env.make_ascii_uppercase();
|
||||
|
||||
// We don't want to enable the `std`/`default` feature for the wasm build and
|
||||
// we need to check if the feature is enabled by checking the env variable.
|
||||
*f != "std"
|
||||
&& *f != "default"
|
||||
&& env::var(format!("CARGO_FEATURE_{}", feature_env))
|
||||
.map(|v| v == "1")
|
||||
.unwrap_or_default()
|
||||
*f != "std" &&
|
||||
*f != "default" && env::var(format!("CARGO_FEATURE_{}", feature_env))
|
||||
.map(|v| v == "1")
|
||||
.unwrap_or_default()
|
||||
})
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
@@ -418,7 +428,8 @@ fn build_project(project: &Path, default_rustflags: &str, cargo_cmd: CargoComman
|
||||
env::var(crate::WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default(),
|
||||
);
|
||||
|
||||
build_cmd.args(&["rustc", "--target=wasm32-unknown-unknown"])
|
||||
build_cmd
|
||||
.args(&["rustc", "--target=wasm32-unknown-unknown"])
|
||||
.arg(format!("--manifest-path={}", manifest_path.display()))
|
||||
.env("RUSTFLAGS", rustflags)
|
||||
// Unset the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir exclusive).
|
||||
@@ -456,17 +467,16 @@ fn compact_wasm_file(
|
||||
let is_release_build = is_release_build();
|
||||
let target = if is_release_build { "release" } else { "debug" };
|
||||
let default_wasm_binary_name = get_wasm_binary_name(cargo_manifest);
|
||||
let wasm_file = project.join("target/wasm32-unknown-unknown")
|
||||
let wasm_file = project
|
||||
.join("target/wasm32-unknown-unknown")
|
||||
.join(target)
|
||||
.join(format!("{}.wasm", default_wasm_binary_name));
|
||||
|
||||
let wasm_compact_file = if is_release_build {
|
||||
let wasm_compact_file = project.join(
|
||||
format!(
|
||||
"{}.compact.wasm",
|
||||
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone()),
|
||||
)
|
||||
);
|
||||
let wasm_compact_file = project.join(format!(
|
||||
"{}.compact.wasm",
|
||||
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone()),
|
||||
));
|
||||
wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file)
|
||||
.expect("Failed to compact generated WASM binary.");
|
||||
Some(WasmBinary(wasm_compact_file))
|
||||
@@ -474,24 +484,19 @@ fn compact_wasm_file(
|
||||
None
|
||||
};
|
||||
|
||||
let wasm_compact_compressed_file = wasm_compact_file.as_ref()
|
||||
.and_then(|compact_binary| {
|
||||
let file_name = wasm_binary_name.clone()
|
||||
.unwrap_or_else(|| default_wasm_binary_name.clone());
|
||||
let wasm_compact_compressed_file = wasm_compact_file.as_ref().and_then(|compact_binary| {
|
||||
let file_name =
|
||||
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone());
|
||||
|
||||
let wasm_compact_compressed_file = project.join(
|
||||
format!(
|
||||
"{}.compact.compressed.wasm",
|
||||
file_name,
|
||||
)
|
||||
);
|
||||
let wasm_compact_compressed_file =
|
||||
project.join(format!("{}.compact.compressed.wasm", file_name,));
|
||||
|
||||
if compress_wasm(&compact_binary.0, &wasm_compact_compressed_file) {
|
||||
Some(WasmBinary(wasm_compact_compressed_file))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
if compress_wasm(&compact_binary.0, &wasm_compact_compressed_file) {
|
||||
Some(WasmBinary(wasm_compact_compressed_file))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
let bloaty_file_name = if let Some(name) = wasm_binary_name {
|
||||
format!("{}.wasm", name)
|
||||
@@ -502,24 +507,14 @@ fn compact_wasm_file(
|
||||
let bloaty_file = project.join(bloaty_file_name);
|
||||
fs::copy(wasm_file, &bloaty_file).expect("Copying the bloaty file to the project dir.");
|
||||
|
||||
(
|
||||
wasm_compact_file,
|
||||
wasm_compact_compressed_file,
|
||||
WasmBinaryBloaty(bloaty_file),
|
||||
)
|
||||
(wasm_compact_file, wasm_compact_compressed_file, WasmBinaryBloaty(bloaty_file))
|
||||
}
|
||||
|
||||
fn compress_wasm(
|
||||
wasm_binary_path: &Path,
|
||||
compressed_binary_out_path: &Path,
|
||||
) -> bool {
|
||||
fn compress_wasm(wasm_binary_path: &Path, compressed_binary_out_path: &Path) -> bool {
|
||||
use sp_maybe_compressed_blob::CODE_BLOB_BOMB_LIMIT;
|
||||
|
||||
let data = fs::read(wasm_binary_path).expect("Failed to read WASM binary");
|
||||
if let Some(compressed) = sp_maybe_compressed_blob::compress(
|
||||
&data,
|
||||
CODE_BLOB_BOMB_LIMIT,
|
||||
) {
|
||||
if let Some(compressed) = sp_maybe_compressed_blob::compress(&data, CODE_BLOB_BOMB_LIMIT) {
|
||||
fs::write(compressed_binary_out_path, &compressed[..])
|
||||
.expect("Failed to write WASM binary");
|
||||
|
||||
@@ -590,7 +585,8 @@ fn generate_rerun_if_changed_instructions(
|
||||
.exec()
|
||||
.expect("`cargo metadata` can not fail!");
|
||||
|
||||
let package = metadata.packages
|
||||
let package = metadata
|
||||
.packages
|
||||
.iter()
|
||||
.find(|p| p.manifest_path == cargo_manifest)
|
||||
.expect("The crate package is contained in its own metadata; qed");
|
||||
@@ -603,12 +599,11 @@ fn generate_rerun_if_changed_instructions(
|
||||
packages.insert(DeduplicatePackage::from(package));
|
||||
|
||||
while let Some(dependency) = dependencies.pop() {
|
||||
let path_or_git_dep = dependency.source
|
||||
.as_ref()
|
||||
.map(|s| s.starts_with("git+"))
|
||||
.unwrap_or(true);
|
||||
let path_or_git_dep =
|
||||
dependency.source.as_ref().map(|s| s.starts_with("git+")).unwrap_or(true);
|
||||
|
||||
let package = metadata.packages
|
||||
let package = metadata
|
||||
.packages
|
||||
.iter()
|
||||
.filter(|p| !p.manifest_path.starts_with(wasm_workspace))
|
||||
.find(|p| {
|
||||
@@ -649,9 +644,7 @@ fn package_rerun_if_changed(package: &DeduplicatePackage) {
|
||||
// Ignore this entry if it is a directory that contains a `Cargo.toml` that is not the
|
||||
// `Cargo.toml` related to the current package. This is done to ignore sub-crates of a crate.
|
||||
// If such a sub-crate is a dependency, it will be processed independently anyway.
|
||||
p.path() == manifest_path
|
||||
|| !p.path().is_dir()
|
||||
|| !p.path().join("Cargo.toml").exists()
|
||||
p.path() == manifest_path || !p.path().is_dir() || !p.path().join("Cargo.toml").exists()
|
||||
})
|
||||
.filter_map(|p| p.ok().map(|p| p.into_path()))
|
||||
.filter(|p| {
|
||||
@@ -681,5 +674,6 @@ fn copy_wasm_to_target_directory(cargo_manifest: &Path, wasm_binary: &WasmBinary
|
||||
fs::copy(
|
||||
wasm_binary.wasm_binary_path(),
|
||||
target_dir.join(format!("{}.wasm", get_wasm_binary_name(cargo_manifest))),
|
||||
).expect("Copies WASM binary to `WASM_TARGET_DIRECTORY`.");
|
||||
)
|
||||
.expect("Copies WASM binary to `WASM_TARGET_DIRECTORY`.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user