1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::ManifestPath;
use anyhow::{
Context,
Result,
};
use cargo_metadata::{
Metadata as CargoMetadata,
MetadataCommand,
Package,
};
use semver::Version;
use serde_json::{
Map,
Value,
};
use std::{
fs,
path::PathBuf,
};
use toml::value;
use url::Url;
#[derive(Debug)]
pub struct CrateMetadata {
pub manifest_path: ManifestPath,
pub cargo_meta: cargo_metadata::Metadata,
pub contract_artifact_name: String,
pub root_package: Package,
pub original_wasm: PathBuf,
pub dest_wasm: PathBuf,
pub ink_version: Version,
pub documentation: Option<Url>,
pub homepage: Option<Url>,
pub user: Option<Map<String, Value>>,
pub target_directory: PathBuf,
}
impl CrateMetadata {
pub fn from_manifest_path(manifest_path: Option<&PathBuf>) -> Result<Self> {
let manifest_path = ManifestPath::try_from(manifest_path)?;
Self::collect(&manifest_path)
}
pub fn collect(manifest_path: &ManifestPath) -> Result<Self> {
let (metadata, root_package) = get_cargo_metadata(manifest_path)?;
let mut target_directory = metadata.target_directory.as_path().join("ink");
let package_name = root_package.name.replace('-', "_");
if let Some(lib_name) = &root_package
.targets
.iter()
.find(|target| target.kind.iter().any(|t| t == "lib"))
{
if lib_name.name != root_package.name {
use colored::Colorize;
eprintln!(
"{} the `name` field in the `[lib]` section of the `Cargo.toml`, \
is no longer used for the name of generated contract artifacts. \
The package name is used instead. Remove the `[lib] name` to \
stop this warning.",
"warning:".yellow().bold(),
);
}
}
let absolute_manifest_path = manifest_path.absolute_directory()?;
let absolute_workspace_root = metadata.workspace_root.canonicalize()?;
if absolute_manifest_path != absolute_workspace_root {
target_directory = target_directory.join(package_name.clone());
}
let mut original_wasm = target_directory.clone();
original_wasm.push("wasm32-unknown-unknown");
original_wasm.push("release");
original_wasm.push(package_name.clone());
original_wasm.set_extension("wasm");
let mut dest_wasm = target_directory.clone();
dest_wasm.push(package_name.clone());
dest_wasm.set_extension("wasm");
let ink_version = metadata
.packages
.iter()
.find_map(|package| {
if package.name == "ink" {
Some(
Version::parse(&package.version.to_string())
.expect("Invalid ink crate version string"),
)
} else {
None
}
})
.ok_or_else(|| anyhow::anyhow!("No 'ink' dependency found"))?;
let ExtraMetadata {
documentation,
homepage,
user,
} = get_cargo_toml_metadata(manifest_path)?;
let crate_metadata = CrateMetadata {
manifest_path: manifest_path.clone(),
cargo_meta: metadata,
root_package,
contract_artifact_name: package_name,
original_wasm: original_wasm.into(),
dest_wasm: dest_wasm.into(),
ink_version,
documentation,
homepage,
user,
target_directory: target_directory.into(),
};
Ok(crate_metadata)
}
pub fn metadata_path(&self) -> PathBuf {
let metadata_file = format!("{}.json", self.contract_artifact_name);
self.target_directory.join(metadata_file)
}
pub fn contract_bundle_path(&self) -> PathBuf {
let target_directory = self.target_directory.clone();
let fname_bundle = format!("{}.contract", self.contract_artifact_name);
target_directory.join(fname_bundle)
}
}
fn get_cargo_metadata(manifest_path: &ManifestPath) -> Result<(CargoMetadata, Package)> {
tracing::debug!(
"Fetching cargo metadata for {}",
manifest_path.as_ref().to_string_lossy()
);
let mut cmd = MetadataCommand::new();
let metadata = cmd
.manifest_path(manifest_path.as_ref())
.exec()
.with_context(|| {
format!(
"Error invoking `cargo metadata` for {}",
manifest_path.as_ref().display()
)
})?;
let root_package_id = metadata
.resolve
.as_ref()
.and_then(|resolve| resolve.root.as_ref())
.context("Cannot infer the root project id")?
.clone();
let root_package = metadata
.packages
.iter()
.find(|package| package.id == root_package_id)
.expect("The package is not found in the `cargo metadata` output")
.clone();
Ok((metadata, root_package))
}
struct ExtraMetadata {
documentation: Option<Url>,
homepage: Option<Url>,
user: Option<Map<String, Value>>,
}
fn get_cargo_toml_metadata(manifest_path: &ManifestPath) -> Result<ExtraMetadata> {
let toml = fs::read_to_string(manifest_path)?;
let toml: value::Table = toml::from_str(&toml)?;
let get_url = |field_name| -> Result<Option<Url>> {
toml.get("package")
.ok_or_else(|| anyhow::anyhow!("package section not found"))?
.get(field_name)
.and_then(|v| v.as_str())
.map(Url::parse)
.transpose()
.context(format!("{field_name} should be a valid URL"))
.map_err(Into::into)
};
let documentation = get_url("documentation")?;
let homepage = get_url("homepage")?;
let user = toml
.get("package")
.and_then(|v| v.get("metadata"))
.and_then(|v| v.get("contract"))
.and_then(|v| v.get("user"))
.and_then(|v| v.as_table())
.map(|v| {
serde_json::to_string(v).and_then(|json| serde_json::from_str(&json))
})
.transpose()?;
Ok(ExtraMetadata {
documentation,
homepage,
user,
})
}