Rust分布式应用开发框架holochain_cli_bundle的使用,holochain_cli_bundle提供去中心化应用打包与部署工具

hc_bundle

Project Forum Chat

License: Apache-2.0

用于从源工作目录和清单文件创建DNA、hApp和web-hApp捆绑文件的工具,反之亦然。 这个crate为hc CLI工具定义了三个独立的子命令,每种捆绑类型一个。 所有子命令都非常相似,具有几乎相同的标志和选项。

这个crate还为每个子命令定义了独立的二进制文件:hc-dnahc-apphc-web-app

hc apphc web-app非常相似,唯一的区别是增加了--recursive标志。 如果使用这个标志,它会首先尝试打包要包含在正在打包的捆绑包中的所有资源。 如果找不到指定的捆绑DNA或hApp资源,按照惯例它会在同一目录中查找DNA或hApp清单文件,并尝试使用指定的名称打包它。

示例代码

以下是一个完整的使用示例,展示如何使用holochain_cli_bundle来创建和部署去中心化应用:

// 首先安装holochain_cli_bundle
// cargo install holochain_cli_bundle

use std::path::PathBuf;
use std::process::Command;

fn main() {
    // 创建一个DNA bundle
    let dna_path = PathBuf::from("./my_dna");
    let dna_output = PathBuf::from("./my_dna.dna");
    
    Command::new("hc-dna")
        .arg("pack")
        .arg(dna_path)
        .arg("--output")
        .arg(dna_output)
        .status()
        .expect("Failed to pack DNA");

    // 创建一个hApp bundle
    let happ_path = PathBuf::from("./my_happ");
    let happ_output = PathBuf::from("./my_happ.happ");
    
    Command::new("hc-app")
        .arg("pack")
        .arg(happ_path)
        .arg("--output")
        .arg(happ_output)
        .status()
        .expect("Failed to pack hApp");

    // 创建一个web-hApp bundle
    let web_happ_path = PathBuf::from("./my_web_happ");
    let web_happ_output = PathBuf::from("./my_web_happ.webhapp");
    
    Command::new("hc-web-app")
        .arg("pack")
        .arg(web_happ_path)
        .arg("--output")
        .arg(web_happ_output)
        .arg("--recursive")  // 递归打包所有依赖
        .status()
        .expect("Failed to pack web-hApp");

    println!("All bundles created successfully!");
}

完整示例demo

以下是基于上述示例的扩展完整demo,展示如何在实际项目中使用hc_bundle:

// 导入所需模块
use std::path::PathBuf;
use std::process::Command;
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 检查并创建输出目录
    let output_dir = PathBuf::from("./bundles");
    if !output_dir.exists() {
        fs::create_dir(&output_dir)?;
    }

    // 1. 打包DNA
    let dna_source = PathBuf::from("./dna_source");
    let dna_output = output_dir.join("my_app.dna");
    
    if !Command::new("hc-dna")
        .arg("pack")
        .arg(&dna_source)
        .arg("--output")
        .arg(&dna_output)
        .status()?
        .success() {
            return Err("Failed to pack DNA".into());
        }

    // 2. 打包hApp (包含多个DNA)
    let happ_source = PathBuf::from("./happ_source");
    let happ_output = output_dir.join("my_app.happ");
    
    if !Command::new("hc-app")
        .arg("pack")
        .arg(&happ_source)
        .arg("--output")
        .arg(&happ_output)
        .status()?
        .success() {
            return Err("Failed to pack hApp".into());
        }

    // 3. 打包web-hApp (包含UI和hApp)
    let web_happ_source = PathBuf::from("./web_app");
    let web_happ_output = output_dir.join("my_web_app.webhapp");
    
    if !Command::new("hc-web-app")
        .arg("pack")
        .arg(&web_happ_source)
        .arg("--output")
        .arg(&web_happ_output)
        .arg("--recursive")  // 递归打包所有依赖
        .status()?
        .success() {
            return Err("Failed to pack web-hApp".into());
        }

    println!("Successfully created all bundles in {:?}", output_dir);
    Ok(())
}

// 测试用例
#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;
    
    #[test]
    fn test_hc_dna_pack() {
        let status = Command::new("hc-dna")
            .arg("--version")
            .status()
            .expect("hc-dna command failed to start");
        
        assert!(status.success(), "hc-dna command should be available");
    }
}

贡献

Holochain是一个开源项目。我们欢迎各种形式的参与,并积极努力扩大接受参与的范围。请参阅我们的贡献指南,了解我们在社区参与的一般做法和协议,以及关于代码格式化、测试实践、持续集成等的具体期望。

许可证

License: Apache-2.0

版权所有 © 2019 - 2024, Holochain Foundation

本程序是自由软件:您可以根据许可证条款重新分发和/或修改它,许可证在LICENSE文件(Apache 2.0)中提供。本程序分发时希望它有用,但不提供任何担保;甚至没有适销性或针对特定用途的适用性的暗示担保。


1 回复

Holochain CLI Bundle - Rust分布式应用开发框架

介绍

holochain_cli_bundle 是一个用于 Holochain 分布式应用开发的 Rust 工具包,专门用于去中心化应用的打包和部署。它为开发者提供了将 Holochain 应用打包为可分发格式并部署到 Holochain 网络的能力。

Holochain 是一个用于构建完全分布式、点对点应用的框架,而 holochain_cli_bundle 则是其生态系统中的重要工具,简化了应用的打包和部署流程。

主要功能

  • 将 Holochain 应用打包为可分发格式
  • 支持应用部署到本地或网络环境
  • 提供应用打包的验证功能
  • 支持多种打包配置选项

安装方法

首先确保已安装 Rust 工具链,然后通过 cargo 安装:

cargo install holochain_cli_bundle

或者将其作为项目依赖添加到 Cargo.toml:

[dependencies]
holochain_cli_bundle = "0.1.0"  # 使用最新版本号

完整示例demo

下面是一个完整的 Holochain CLI Bundle 使用示例,展示了从打包到部署的完整流程:

use holochain_cli_bundle::{Bundle, BundleConfig, DeployOptions};
use std::path::PathBuf;
use anyhow::Context;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 1. 创建自定义配置
    let config = BundleConfig {
        compression: Some("zstd".to_string()),
        include: Some(vec!["**/*.wasm".to_string(), "**/*.json".to_string()]),
        exclude: Some(vec!["tests/**".to_string()]),
        metadata: Some(std::collections::HashMap::from([
            ("author".to_string(), "Alice".to_string()),
            ("version".to_string(), "1.0.0".to_string()),
        ])),
    };
    
    // 将配置写入文件
    let config_path = PathBuf::from("./bundle.config.json");
    config.write_to_file(&config_path)?;

    // 2. 使用配置打包应用
    println!("开始打包应用...");
    let bundle = Bundle::with_config("./my-happ", config)?;
    let bundled = bundle.pack().context("打包失败")?;
    let output_path = PathBuf::from("./dist/my-app.happ");
    bundled.write_to_file(&output_path)?;
    println!("应用已成功打包到: {}", output_path.display());

    // 3. 验证打包
    println!("验证打包文件...");
    let verify_bundle = Bundle::read_from_file(&output_path)?;
    verify_bundle.validate()?;
    println!("打包验证通过!");

    // 4. 部署应用
    println!("开始部署应用到测试网络...");
    let deploy_options = DeployOptions {
        network: Some("testnet".to_string()),
        admin_port: 12345,
        ..Default::default()
    };
    
    let deployment = verify_bundle.deploy(deploy_options).await?;
    println!("应用已成功部署! 部署ID: {}", deployment.id);

    Ok(())
}

对应的CLI操作

上述Rust代码对应的命令行操作如下:

# 创建配置文件
echo '{
  "compression": "zstd",
  "include": ["**/*.wasm", "**/*.json"],
  "exclude": ["tests/**"],
  "metadata": {
    "author": "Alice",
    "version": "1.0.0"
  }
}' > bundle.config.json

# 打包应用
holochain-cli-bundle pack ./my-happ -c ./bundle.config.json -o ./dist/my-app.happ

# 验证打包
holochain-cli-bundle validate ./dist/my-app.happ

# 部署应用
holochain-cli-bundle deploy ./dist/my-app.happ --network testnet --admin-port 12345

高级配置选项

BundleConfig 支持更多高级配置选项:

let advanced_config = BundleConfig {
    compression: Some("lz4".to_string()),  // 支持 "zstd", "lz4", "none"
    include: Some(vec![
        "**/*.wasm".to_string(),
        "**/*.zome".to_string(),
        "dna.yaml".to_string()
    ]),
    exclude: Some(vec![
        "node_modules/**".to_string(),
        ".git/**".to_string()
    ]),
    metadata: Some(std::collections::HashMap::from([
        ("description".to_string(), "My Awesome Holochain App".to_string()),
        ("license".to_string(), "MIT".to_string()),
        ("repository".to_string(), "github.com/username/repo".to_string()),
    ])),
    // 其他高级选项...
};

注意事项

  1. 确保你的 Holochain 应用已经完成开发和本地测试
  2. 打包前验证所有依赖项是否正确安装
  3. 部署到生产网络前,先在测试网络验证应用行为
  4. 注意网络配置和端口设置,确保与目标环境兼容

holochain_cli_bundle 为 Holochain 应用提供了便捷的打包和部署解决方案,使得分布式应用的发布流程更加标准化和自动化。

回到顶部