Skip to content
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
28 changes: 18 additions & 10 deletions assets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
, bootstrap-js ? import ./nix/agent-js/bootstrap-js.nix { inherit pkgs; }
, distributed-canisters ? import ./distributed-canisters.nix { inherit pkgs; }
}:
let
looseBinaryCache = pkgs.runCommandNoCCLocal "loose-binary-cache" {} ''
mkdir -p $out

cp ${pkgs.dfinity.ic-replica}/bin/replica $out
cp ${pkgs.dfinity.ic-starter}/bin/ic-starter $out
cp -R ${pkgs.motoko.base-src} $out/base
cp ${pkgs.motoko.mo-doc}/bin/mo-doc $out
cp ${pkgs.motoko.mo-ide}/bin/mo-ide $out
cp ${pkgs.motoko.moc}/bin/moc $out

# Install bootstrap
mkdir $out/bootstrap
cp -R ${bootstrap-js.dist}/* $out/bootstrap/
'';
in
pkgs.runCommandNoCCLocal "assets" {} ''
mkdir -p $out

cp ${pkgs.dfinity.ic-replica}/bin/replica $out
cp ${pkgs.dfinity.ic-starter}/bin/ic-starter $out
cp -R ${pkgs.motoko.base-src} $out/base
cp ${pkgs.motoko.mo-doc}/bin/mo-doc $out
cp ${pkgs.motoko.mo-ide}/bin/mo-ide $out
cp ${pkgs.motoko.moc}/bin/moc $out
tar -czf $out/binary_cache.tgz -C ${looseBinaryCache}/ .

# Install bootstrap
mkdir $out/bootstrap
cp -R ${bootstrap-js.dist}/* $out/bootstrap/
tar -czf $out/assetstorage_canister.tgz -C ${distributed-canisters}/assetstorage/ .

cp -R ${distributed-canisters} $out/canisters
''
59 changes: 29 additions & 30 deletions src/dfx/assets/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,37 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

fn add_assets(fn_name: &str, f: &mut File, path: &str) {
fn add_asset_archive(fn_name: &str, f: &mut File) {
let filename_tgz = format!("{}.tgz", fn_name);

let path = env::var("DFX_ASSETS").expect("Cannot find DFX_ASSETS");
let prebuilt_file = Path::new(&path).join(&filename_tgz);

let out_dir = env::var("OUT_DIR").unwrap();
let tgz_path = Path::new(&out_dir).join(&filename_tgz);

if tgz_path.exists() {
// This avoids PermissionDenied errors.
std::fs::remove_file(&tgz_path).unwrap();
}
std::fs::copy(prebuilt_file, tgz_path).unwrap();

write_archive_accessor(fn_name, f);
}

fn add_assets_from_directory(fn_name: &str, f: &mut File, path: &str) {
let out_dir = env::var("OUT_DIR").unwrap();
let tgz_path = Path::new(&out_dir).join(format!("{}.tgz", fn_name));

let tar_gz = File::create(&tgz_path).unwrap();
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all("", path).unwrap();

write_archive_accessor(fn_name, f);
}

fn write_archive_accessor(fn_name: &str, f: &mut File) {
f.write_all(
format!(
"
Expand All @@ -32,28 +55,6 @@ fn add_assets(fn_name: &str, f: &mut File, path: &str) {
.unwrap();
}

fn add_optional_assets(fn_name: &str, f: &mut File, path: &str) {
if Path::new(path).exists() {
add_assets(fn_name, f, path);
} else {
f.write_all(
format!(
"
pub fn {fn_name}() -> Result<Archive<GzDecoder<Cursor<Vec<u8>>>>> {{
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
\"{fn_name} is not available during this build phase.\",
))
}}
",
fn_name = fn_name,
)
.as_bytes(),
)
.unwrap();
}
}

fn get_git_hash() -> Option<String> {
use std::process::Command;

Expand Down Expand Up @@ -82,13 +83,11 @@ fn main() {
)
.unwrap();

let path = env::var("DFX_ASSETS").expect("Cannot find DFX_ASSETS");
add_assets("binary_cache", &mut f, &path);
let assetstorage_canister_path = path + &String::from("/canisters/assetstorage");
add_optional_assets("assetstorage_canister", &mut f, &assetstorage_canister_path);
add_assets("language_bindings", &mut f, "assets/language_bindings");
add_assets("new_project_files", &mut f, "assets/new_project_files");
add_assets(
add_asset_archive("binary_cache", &mut f);
add_asset_archive("assetstorage_canister", &mut f);
add_assets_from_directory("language_bindings", &mut f, "assets/language_bindings");
add_assets_from_directory("new_project_files", &mut f, "assets/new_project_files");
add_assets_from_directory(
"new_project_node_files",
&mut f,
"assets/new_project_node_files",
Expand Down