Skip to content

Commit

Permalink
SDK-411 SDK-422 Add start and build commands (#25)
Browse files Browse the repository at this point in the history
* Update dfinity for "Hello, {name}!" Wasm

* Update actorscript

* Extract copying assets for use in multiple places

* Make executable assets available in shell.nix

* Add start command

* Use nix store path for assets

* Add conversion from serde_cbor & serde_json errors

* Add build command, examples dir
  • Loading branch information
paulyoung authored Sep 6, 2019
1 parent 0bd79a3 commit 8437d96
Show file tree
Hide file tree
Showing 21 changed files with 627 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
result*

# Building
build/
target/

# IDEs
Expand Down
95 changes: 94 additions & 1 deletion dfx/Cargo.lock

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

25 changes: 17 additions & 8 deletions dfx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
[package]
name = "dfx"
version = "0.1.0"
version = "0.2.0"
authors = ["DFINITY Team"]
edition = "2018"
build = "assets/build.rs"

[[bin]]
name = "dfx"
path = "src/main.rs"

[build-dependencies]
tar = "0.4.26"
flate2 = "1.0.11"

[dependencies]
clap = "2.33.0"
futures = "0.1"
reqwest = "0.9"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
serde_cbor = "0.10"
serde_repr = "0.1"
console = "0.7.7"
flate2 = "1.0.11"
futures = "0.1.28"
reqwest = "0.9.20"
serde = "1.0"
serde_bytes = "0.11.2"
serde_cbor = "0.10.1"
serde_repr = "0.1.5"
serde_json = "1.0.40"
tar = "0.4.26"
tokio = "0.1"

[dev-dependencies]
env_logger = "0.6"
mockito = "0.20"
mockito = "0.20.0"
9 changes: 9 additions & 0 deletions dfx/assets/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
= Assets

The `files/` directory contains all the files of a new project. This is tar gzipped at build time
and injected into the binary.

The following strings are replaced:

- `{project_name}` => the project name.
- `{dfx_version}` => the DFX version used to create the project.
53 changes: 53 additions & 0 deletions dfx/assets/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use flate2::write::GzEncoder;
use flate2::Compression;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

fn add_assets(f: &mut File, path: &str) -> () {
let out_dir = env::var("OUT_DIR").unwrap();
let tgz_path = Path::new(&out_dir).join(format!("{}.tgz", path));
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();

f.write_all(
format!(
"
pub fn assets() -> Result<Archive<GzDecoder<Cursor<Vec<u8>>>>> {{
let mut v = Vec::new();
v.extend_from_slice(include_bytes!(\"{path}.tgz\"));
let tar = GzDecoder::new(std::io::Cursor::new(v));
let archive = Archive::new(tar);
Ok(archive)
}}
",
path = path
)
.as_bytes(),
)
.unwrap();
}

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let loader_path = Path::new(&out_dir).join("load_assets.rs");
let mut f = File::create(&loader_path).unwrap();

f.write_all(
b"
use flate2::read::GzDecoder;
use std::io::{Cursor, Result};
use std::vec::Vec;
use tar::Archive;
",
)
.unwrap();

let path = env::var("DFX_ASSETS").unwrap();
add_assets(&mut f, &path);
}
5 changes: 5 additions & 0 deletions dfx/examples/hello/app/canisters/hello/main.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
actor {
public func main(name : Text) : async Text {
return "Hello, " # name # "!";
};
};
7 changes: 7 additions & 0 deletions dfx/examples/hello/dfinity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"canisters": {
"hello": {
"main": "app/canisters/hello/main.as"
}
}
}
18 changes: 10 additions & 8 deletions dfx/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
, moreutils
, cargo-graph
, graphviz
, dfinity
, actorscript
, dfinity
, runCommand
}:

let
Expand All @@ -38,6 +39,7 @@ let
in lib.concatMapStringsSep "\n" timestamp lines;

src = lib.sourceFilesByRegex (lib.gitOnlySource ./.) [
"^assets/.*$"
".*\.rs$"
".*Cargo\.toml$"
".*Cargo\.lock$"
Expand Down Expand Up @@ -122,13 +124,13 @@ naersk.buildPackage src
# derivation (the deps-only build has name "${name}-deps").
lib.optionalAttrs (oldAttrs.name == name)
{
preBuild = ''
mkdir -p dfx_assets/{bin,rts}
cp ${dfinity.rust-workspace}/bin/{dfinity,nodemanager} dfx_assets/bin
cp ${actorscript.asc}/bin/asc dfx_assets/bin
cp ${actorscript.as-ide}/bin/as-ide dfx_assets/bin
cp ${actorscript.didc}/bin/didc dfx_assets/bin
cp ${actorscript.rts}/rts/as-rts.wasm dfx_assets/rts
DFX_ASSETS = runCommand "dfx-assets" {} ''
mkdir -p $out
cp ${dfinity.rust-workspace}/bin/{client,nodemanager} $out
cp ${actorscript.asc}/bin/asc $out
cp ${actorscript.as-ide}/bin/as-ide $out
cp ${actorscript.didc}/bin/didc $out
cp ${actorscript.rts}/rts/as-rts.wasm $out
'';

postDoc = ''
Expand Down
5 changes: 4 additions & 1 deletion dfx/shell.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{ pkgs ? import ../. {} }:

let dfx = pkgs.dfinity-sdk.dfx; in

pkgs.mkCiShell {
name = "dfinity-sdk-dfx-env";
inputsFrom = [
pkgs.dfinity-sdk.dfx
dfx
];
DFX_ASSETS = dfx.DFX_ASSETS;
}
Loading

0 comments on commit 8437d96

Please sign in to comment.