Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support offline env variable in wasm builder
  • Loading branch information
skunert committed Jun 22, 2022
commit d2bc87817021336fdd2ef631b81a4e79d005822b
3 changes: 3 additions & 0 deletions utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub use builder::{WasmBuilder, WasmBuilderSelectProject};
/// Environment variable that tells us to skip building the wasm binary.
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";

/// Environment variable that tells us whether we should avoid network requests
const OFFLINE: &str = "CARGO_NET_OFFLINE";

/// Environment variable to force a certain build type when building the wasm binary.
/// Expects "debug", "release" or "production" as value.
///
Expand Down
27 changes: 20 additions & 7 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{write_file_if_changed, CargoCommandVersioned};
use crate::{write_file_if_changed, CargoCommandVersioned, OFFLINE};

use build_helper::rerun_if_changed;
use cargo_metadata::{CargoOpt, Metadata, MetadataCommand};
Expand Down Expand Up @@ -88,12 +88,12 @@ fn crate_metadata(cargo_manifest: &Path) -> Metadata {
cargo_manifest.to_path_buf()
};

let crate_metadata = MetadataCommand::new()
.manifest_path(cargo_manifest)
.features(CargoOpt::AllFeatures)
let mut crate_metadata = create_metadata_command(cargo_manifest);
crate_metadata.features(CargoOpt::AllFeatures);

let crate_metadata = crate_metadata
.exec()
.expect("`cargo metadata` can not fail on project `Cargo.toml`; qed");

// If the `Cargo.lock` didn't exist, we need to remove it after
// calling `cargo metadata`. This is required to ensure that we don't change
// the build directory outside of the `target` folder. Commands like
Expand Down Expand Up @@ -631,6 +631,10 @@ fn build_project(
build_cmd.arg("--profile");
build_cmd.arg(profile.name());

if env::var(OFFLINE).is_ok() {
build_cmd.arg("--offline");
}

println!("{}", colorize_info_message("Information that should be included in a bug report."));
println!("{} {:?}", colorize_info_message("Executing build command:"), build_cmd);
println!("{} {}", colorize_info_message("Using rustc version:"), cargo_cmd.rustc_version());
Expand Down Expand Up @@ -751,6 +755,16 @@ impl<'a> Deref for DeduplicatePackage<'a> {
}
}

fn create_metadata_command(path: impl Into<PathBuf>) -> MetadataCommand {
let mut metadata_command = MetadataCommand::new();
metadata_command.manifest_path(path);

if env::var(OFFLINE).is_ok() {
metadata_command.other_options(vec!["--offline".to_owned()]);
}
metadata_command
}

/// Generate the `rerun-if-changed` instructions for cargo to make sure that the WASM binary is
/// rebuilt when needed.
fn generate_rerun_if_changed_instructions(
Expand All @@ -765,8 +779,7 @@ fn generate_rerun_if_changed_instructions(
rerun_if_changed(cargo_lock);
}

let metadata = MetadataCommand::new()
.manifest_path(project_folder.join("Cargo.toml"))
let metadata = create_metadata_command(project_folder.join("Cargo.toml"))
.exec()
.expect("`cargo metadata` can not fail!");

Expand Down