Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Adds abiity to pass DNA properties in hc package #1720

Merged
merged 8 commits into from
Oct 7, 2019
Merged
3 changes: 3 additions & 0 deletions CHANGELOG-UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

* Adds the `--properties`/`-p` flag to `hc package` which takes a stringifed JSON object to be inserted in the .dna.json under the properties field. This will alter the DNA hash and can therefore be used for fork DNAs from their source code. [#1720](https://github.com/holochain/holochain-rust/pull/1720)
* Adds publishing of headers again after rollback. Header publishing is now its own action rather than part of the `Publish` action that plays nicely with the testing framework. It also adds header entries to the author list so they are gossiped properly. [#1640](https://github.com/holochain/holochain-rust/pull/1640).

### Changed

* Updates to work with latest version of lib3h [#1737](https://github.com/holochain/holochain-rust/pull/1737)
Expand Down
35 changes: 23 additions & 12 deletions cli/src/cli/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Packager {
Packager { strip_meta }
}

pub fn package(strip_meta: bool, output: PathBuf) -> DefaultResult<()> {
pub fn package(strip_meta: bool, output: PathBuf, properties: Value) -> DefaultResult<()> {
// First, check whether they have `cargo` installed, since it will be needed for packaging
// TODO: in the future, don't check for this here, since other build tools and languages
// could be used
Expand All @@ -82,18 +82,25 @@ impl Packager {
return Ok(());
}

Packager::new(strip_meta).run(&output)
Packager::new(strip_meta).run(&output, properties)
}

fn run(&self, output: &PathBuf) -> DefaultResult<()> {
fn run(&self, output: &PathBuf, properties: Value) -> DefaultResult<()> {
let current_dir = std::env::current_dir()?;
let dir_obj_bundle = Value::from(self.bundle_recurse(&current_dir).map_err(|e| {
format_err!(
"Couldn't traverse DNA in directory {:?}: {}",
&current_dir,
e
)
})?);
let dir_obj_bundle = Value::from(
self.bundle_recurse(&current_dir)
.map(|mut val| {
val.insert("properties".to_string(), properties);
val
})
.map_err(|e| {
format_err!(
"Couldn't traverse DNA in directory {:?}: {}",
&current_dir,
e
)
})?,
);

let dna_str =
serde_json::to_string_pretty(&dir_obj_bundle).expect("failed to make pretty DNA");
Expand Down Expand Up @@ -300,8 +307,12 @@ impl Packager {
}
}

pub fn package(strip_meta: bool, output: PathBuf) -> DefaultResult<()> {
Packager::package(strip_meta, output)
pub fn package(
strip_meta: bool,
output: PathBuf,
properties: serde_json::Value,
) -> DefaultResult<()> {
Packager::package(strip_meta, output, properties)
}

pub fn unpack(path: &PathBuf, to: &PathBuf) -> DefaultResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn run(
conductor_config: Configuration,
) -> DefaultResult<()> {
if package {
cli::package(true, dna_path)?;
cli::package(true, dna_path, json!({}))?;
}

mount_conductor_from_config(conductor_config);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn test(
"Packaging".green().bold(),
&file_path
);
package(true, file_path)?;
package(true, file_path, json!({}))?;
}

// build tests
Expand Down
25 changes: 22 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod error;
mod util;

use crate::error::{HolochainError, HolochainResult};
use std::path::PathBuf;
use std::{path::PathBuf, str::FromStr};
use structopt::StructOpt;

#[derive(StructOpt)]
Expand All @@ -47,6 +47,8 @@ enum Cli {
strip_meta: bool,
#[structopt(long = "output", short = "o", parse(from_os_str))]
output: Option<PathBuf>,
#[structopt(long = "properties", short = "p")]
properties: Option<String>,
},
#[structopt(
name = "unpack",
Expand Down Expand Up @@ -205,13 +207,30 @@ fn run() -> HolochainResult<()> {
std::env::current_dir().map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
match args {
// If using default path, we'll create if necessary; otherwise, target dir must exist
Cli::Package { strip_meta, output } => {
Cli::Package {
strip_meta,
output,
properties: properties_string,
} => {
let output = if output.is_some() {
output.unwrap()
} else {
util::std_package_path(&project_path).map_err(HolochainError::Default)?
};
cli::package(strip_meta, output).map_err(HolochainError::Default)?

let properties = properties_string
.map(|s| {
serde_json::Value::from_str(&s)
}).unwrap_or_else(|| {
Ok(json!({}))
});

match properties {
Ok(properties) => cli::package(strip_meta, output, properties).map_err(HolochainError::Default)?,
Err(e) => return Err(HolochainError::Default(format_err!(
"Failed to parse properties argument as JSON: {:?}", e
)))
}
willemolding marked this conversation as resolved.
Show resolved Hide resolved
}

Cli::Unpack { path, to } => cli::unpack(&path, &to).map_err(HolochainError::Default)?,
Expand Down