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

Extract lib for invoking contract build #787

Merged
merged 38 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
2f29737
WIP extract build crate
ascjones Oct 17, 2022
16017f7
WIP extract build crate
ascjones Oct 17, 2022
82d7da0
build crate builds
ascjones Oct 17, 2022
47d0267
Put new template back, move generate-metadata template
ascjones Oct 17, 2022
5f4deac
WIP fixing up errors in exe project
ascjones Oct 17, 2022
478f298
Remove original build tests to build crate
ascjones Oct 18, 2022
1c20a5b
WIP move template to own crate
ascjones Oct 18, 2022
7bb9a98
Merge branch 'master' into aj/build-lib
ascjones Nov 28, 2022
80da3e2
Cargo.lock
ascjones Nov 28, 2022
ff2d087
Fix up after merge, continue with build lib separation
ascjones Nov 28, 2022
6d4c452
Continue extraction of `contract-template` crate
ascjones Nov 28, 2022
7571c51
Complete move of new impl to its own crate
ascjones Nov 28, 2022
4c309a5
Restore BuildSteps delete test-utils crate
ascjones Nov 28, 2022
cf87f72
Add missing wabt dependency
ascjones Nov 28, 2022
e9fe924
Get contract-build lib compiling
ascjones Nov 28, 2022
d54fe36
Fmt
ascjones Nov 28, 2022
197fcc9
Rename new from template method
ascjones Nov 28, 2022
6849dbc
Import contract-build crate into cargo-contract
ascjones Nov 28, 2022
53670fa
Recombine templates crate back into build crate
ascjones Nov 28, 2022
3c109e1
Workspace check success
ascjones Nov 28, 2022
d65ec5a
Fix some warnings
ascjones Nov 28, 2022
eceb586
Fix more warnings
ascjones Nov 28, 2022
1cf07d1
Fix remaining warnings
ascjones Nov 28, 2022
f9793f4
Merge branch 'master' into aj/build-lib
ascjones Nov 28, 2022
bb0f314
Move test tracing subscriber init to where it is used
ascjones Nov 29, 2022
d910ba8
Fix final errors with sharing test code
ascjones Nov 29, 2022
43eff59
Fmt
ascjones Nov 29, 2022
d45b62a
Add README and license
ascjones Nov 29, 2022
070190a
Sync serde dependencies
ascjones Nov 29, 2022
046c775
Relax serde requirement on crates
ascjones Nov 29, 2022
e634859
Merge branch 'master' into aj/build-lib
ascjones Nov 30, 2022
939a23d
Restore skip_wasm_validation step
ascjones Nov 30, 2022
5212504
Fmt
ascjones Nov 30, 2022
d009829
Add missing which dependency for windows
ascjones Nov 30, 2022
578524e
Add README to module docs, runs code example as doc test
ascjones Dec 1, 2022
1791ef2
Fmt
ascjones Dec 1, 2022
7528dc4
Renamed transcode dependency for consistency
ascjones Dec 1, 2022
70d41ec
Fmt
ascjones Dec 1, 2022
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
77 changes: 32 additions & 45 deletions Cargo.lock

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

48 changes: 48 additions & 0 deletions crates/build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "contract-build"
version = "2.0.0-beta"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"

license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/paritytech/cargo-contract"
documentation = "https://docs.rs/contract-metadata"
homepage = "https://www.substrate.io/"
description = "Library for building ink! smart contracts"
keywords = ["wasm", "parity", "webassembly", "blockchain", "edsl"]
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE", "build.rs", "templates",]

[dependencies]
anyhow = "1.0.65"
blake2 = "0.10.4"
cargo_metadata = "0.15.0"
colored = "2.0.0"
clap = { version = "4.0.15", features = ["derive", "env"] }
heck = "0.4.0"
hex = "0.4.3"
impl-serde = "0.4.0"
rustc_version = "0.4.0"
scale = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
toml = "0.5.9"
tracing = "0.1.37"
parity-wasm = "0.45.0"
semver = { version = "1.0.14", features = ["serde"] }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1.0.86"
tempfile = "3.3.0"
url = { version = "2.3.1", features = ["serde"] }
wasm-opt = "0.110.1"
which = "4.3.0"
zip = { version = "0.6.3", default-features = false }

contract-metadata = { version = "2.0.0-beta", path = "../metadata" }

[build-dependencies]
anyhow = "1.0.65"
walkdir = "2.3.2"
zip = { version = "0.6.3", default-features = false }

[dev-dependencies]
pretty_assertions = "1.3.0"
wabt = "0.10.0"
1 change: 1 addition & 0 deletions crates/build/LICENSE
37 changes: 37 additions & 0 deletions crates/build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# contract-build

A crate for building [`ink!`](https://github.com/paritytech/ink) smart contracts. Used by
[`cargo-contract`](https://github.com/paritytech/cargo-contract).

## Usage
ascjones marked this conversation as resolved.
Show resolved Hide resolved

```rust
use contract_build::{
ManifestPath,
Verbosity,
BuildArtifacts,
BuildMode,
Network,
OptimizationPasses,
OutputType,
UnstableFlags,
};

let manifest_path = ManifestPath::new("my-contract/Cargo.toml").unwrap();

let args = contract_build::ExecuteArgs {
manifest_path,
verbosity: Verbosity::Default,
build_mode: BuildMode::Release,
network: Network::Online,
build_artifact: BuildArtifacts::All,
unstable_flags: UnstableFlags::default(),
optimization_passes: Some(OptimizationPasses::default()),
keep_debug_symbols: false,
lint: false,
output_type: OutputType::Json,
skip_wasm_validation: false,
};

contract_build::execute(args);
```
123 changes: 123 additions & 0 deletions crates/build/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2018-2022 Parity Technologies (UK) Ltd.
// This file is part of cargo-contract.
//
// cargo-contract is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cargo-contract is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use std::{
env,
ffi::OsStr,
fs::File,
io::{
prelude::*,
Write,
},
iter::Iterator,
path::{
Path,
PathBuf,
},
};

use anyhow::Result;
use walkdir::WalkDir;
use zip::{
write::FileOptions,
CompressionMethod,
ZipWriter,
};

const DEFAULT_UNIX_PERMISSIONS: u32 = 0o755;

fn main() {
let manifest_dir: PathBuf = env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR should be set by cargo")
.into();
let out_dir: PathBuf = env::var("OUT_DIR")
.expect("OUT_DIR should be set by cargo")
.into();
let res = zip_template(&manifest_dir, &out_dir);

match res {
Ok(()) => std::process::exit(0),
Err(err) => {
eprintln!("Encountered error: {:?}", err);
std::process::exit(1)
}
}
}

/// Creates a zip archive `template.zip` of the `new` project template in `out_dir`.
fn zip_template(manifest_dir: &Path, out_dir: &Path) -> Result<()> {
let template_dir = manifest_dir.join("templates").join("new");
let template_dst_file = out_dir.join("template.zip");
println!(
"Creating template zip: template_dir '{}', destination archive '{}'",
template_dir.display(),
template_dst_file.display()
);
zip_dir(&template_dir, &template_dst_file, CompressionMethod::Stored).map(|_| {
println!(
"Done: {} written to {}",
template_dir.display(),
template_dst_file.display()
);
})
}

/// Creates a zip archive at `dst_file` with the content of the `src_dir`.
fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result<()> {
if !src_dir.exists() {
anyhow::bail!("src_dir '{}' does not exist", src_dir.display());
}
if !src_dir.is_dir() {
anyhow::bail!("src_dir '{}' is not a directory", src_dir.display());
}

let file = File::create(dst_file)?;

let walkdir = WalkDir::new(src_dir);
let it = walkdir.into_iter().filter_map(|e| e.ok());

let mut zip = ZipWriter::new(file);
let options = FileOptions::default()
.compression_method(method)
.unix_permissions(DEFAULT_UNIX_PERMISSIONS);

let mut buffer = Vec::new();
for entry in it {
let path = entry.path();
let mut name = path.strip_prefix(src_dir)?.to_path_buf();

// `Cargo.toml` files cause the folder to excluded from `cargo package` so need to be renamed
if name.file_name() == Some(OsStr::new("_Cargo.toml")) {
name.set_file_name("Cargo.toml");
}

let file_path = name.as_os_str().to_string_lossy();

if path.is_file() {
zip.start_file(file_path, options)?;
let mut f = File::open(path)?;

f.read_to_end(&mut buffer)?;
zip.write_all(&buffer)?;
buffer.clear();
} else if !name.as_os_str().is_empty() {
zip.add_directory(file_path, options)?;
}
}
zip.finish()?;

Ok(())
}
Loading