-
Notifications
You must be signed in to change notification settings - Fork 125
Extract lib for invoking contract build #787
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
Merged
Merged
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 16017f7
WIP extract build crate
ascjones 82d7da0
build crate builds
ascjones 47d0267
Put new template back, move generate-metadata template
ascjones 5f4deac
WIP fixing up errors in exe project
ascjones 478f298
Remove original build tests to build crate
ascjones 1c20a5b
WIP move template to own crate
ascjones 7bb9a98
Merge branch 'master' into aj/build-lib
ascjones 80da3e2
Cargo.lock
ascjones ff2d087
Fix up after merge, continue with build lib separation
ascjones 6d4c452
Continue extraction of `contract-template` crate
ascjones 7571c51
Complete move of new impl to its own crate
ascjones 4c309a5
Restore BuildSteps delete test-utils crate
ascjones cf87f72
Add missing wabt dependency
ascjones e9fe924
Get contract-build lib compiling
ascjones d54fe36
Fmt
ascjones 197fcc9
Rename new from template method
ascjones 6849dbc
Import contract-build crate into cargo-contract
ascjones 53670fa
Recombine templates crate back into build crate
ascjones 3c109e1
Workspace check success
ascjones d65ec5a
Fix some warnings
ascjones eceb586
Fix more warnings
ascjones 1cf07d1
Fix remaining warnings
ascjones f9793f4
Merge branch 'master' into aj/build-lib
ascjones bb0f314
Move test tracing subscriber init to where it is used
ascjones d910ba8
Fix final errors with sharing test code
ascjones 43eff59
Fmt
ascjones d45b62a
Add README and license
ascjones 070190a
Sync serde dependencies
ascjones 046c775
Relax serde requirement on crates
ascjones e634859
Merge branch 'master' into aj/build-lib
ascjones 939a23d
Restore skip_wasm_validation step
ascjones 5212504
Fmt
ascjones d009829
Add missing which dependency for windows
ascjones 578524e
Add README to module docs, runs code example as doc test
ascjones 1791ef2
Fmt
ascjones 7528dc4
Renamed transcode dependency for consistency
ascjones 70d41ec
Fmt
ascjones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <[email protected]>"] | ||
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../LICENSE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| ```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); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.