Skip to content

Commit 3faa71a

Browse files
Ensure fdev new generates a buildable project (#1752)
Co-authored-by: Ian Clarke <sanity@users.noreply.github.com>
1 parent 8859415 commit 3faa71a

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

crates/fdev/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ glob = "0.3"
2222
pico-args = "0.5"
2323
prettytable-rs = "0.10"
2424
rand = { workspace = true }
25-
serde = "1"
25+
serde = { version = "1", features = ["derive"] }
2626
serde_json = "1"
2727
serde_with = { workspace = true }
2828
semver = { workspace = true }

crates/fdev/src/new_package.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use std::{
2+
collections::BTreeMap,
23
env,
34
fs::{self, File},
4-
io::{Read, Write},
5+
io::Write,
56
path::{Path, PathBuf},
67
process::{Command, Stdio},
78
};
89

10+
use serde::{Deserialize, Serialize};
11+
912
use crate::{
1013
build::*,
1114
config::{ContractKind, NewPackageConfig},
@@ -68,6 +71,30 @@ fn create_regular_contract(cwd: &Path) -> anyhow::Result<()> {
6871
Ok(())
6972
}
7073

74+
/// Minimal Cargo.toml that only contains fields we need.
75+
#[derive(Debug, Deserialize, Serialize)]
76+
#[serde(rename_all = "kebab-case")]
77+
struct Manifest {
78+
package: toml::Value,
79+
dependencies: toml::Value,
80+
#[serde(default)]
81+
lib: Option<ManifestLib>,
82+
#[serde(default)]
83+
features: Option<BTreeMap<String, Vec<String>>>,
84+
// Catch-all to ensure we don't drop any fields.
85+
#[serde(default, flatten)]
86+
any: Option<BTreeMap<String, toml::Value>>,
87+
}
88+
89+
#[derive(Debug, Default, Deserialize, Serialize)]
90+
#[serde(rename_all = "kebab-case")]
91+
struct ManifestLib {
92+
name: Option<String>,
93+
path: Option<PathBuf>,
94+
crate_type: Option<Vec<String>>,
95+
required_features: Option<Vec<String>>,
96+
}
97+
7198
fn create_rust_crate(cwd: &Path, kind: ContractKind) -> anyhow::Result<()> {
7299
let (dest_path, cmd) = match kind {
73100
ContractKind::WebApp => (cwd.join("container"), &["new"]),
@@ -113,20 +140,26 @@ fn create_rust_crate(cwd: &Path, kind: ContractKind) -> anyhow::Result<()> {
113140

114141
// add any additional config keys
115142
// todo: improve error handling here, in case something fails would have to rollback any changes
116-
let mut cargo_file = File::open(dest_path.join("Cargo.toml"))?;
117-
let mut buf = vec![];
118-
cargo_file.read_to_end(&mut buf)?;
119-
let cargo_file_content = std::str::from_utf8(buf.as_slice()).expect("Found invalid cargo file");
120-
let mut cargo_def: toml::Value = toml::from_str(cargo_file_content)?;
121-
let lib_entry = toml::map::Map::from_iter([(
122-
"crate-type".into(),
123-
toml::Value::Array(vec![toml::Value::String("cdylib".into())]),
124-
)]);
125-
let root = cargo_def.as_table_mut().unwrap();
126-
root.insert("lib".into(), toml::Value::Table(lib_entry));
127-
std::mem::drop(cargo_file);
128-
let mut cargo_file = File::create(dest_path.join("Cargo.toml"))?;
129-
cargo_file.write_all(toml::to_string(&cargo_def)?.into_bytes().as_slice())?;
143+
let manifest_path = dest_path.join("Cargo.toml");
144+
let toml_str = fs::read_to_string(&manifest_path)?;
145+
let mut manifest: Manifest = toml::from_str(&toml_str)?;
146+
147+
manifest.lib = Some(ManifestLib {
148+
crate_type: Some(vec!["cdylib".into()]),
149+
..Default::default()
150+
});
151+
manifest.features = Some(
152+
[
153+
("default".into(), vec!["freenet-main-contract".into()]),
154+
("contract".into(), vec!["freenet-stdlib/contract".into()]),
155+
("freenet-main-contract".into(), vec![]),
156+
("trace".into(), vec!["freenet-stdlib/trace".into()]),
157+
]
158+
.into_iter()
159+
.collect(),
160+
);
161+
162+
fs::write(&manifest_path, toml::to_string(&manifest)?)?;
130163
Ok(())
131164
}
132165

modules/identity-management/Cargo.lock

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)