Skip to content

Commit

Permalink
fix issue #67
Browse files Browse the repository at this point in the history
  • Loading branch information
weezy20 committed Mar 20, 2024
1 parent 4176187 commit f5ff852
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/engines/pallet_engine/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ impl Dependency {
Self {
name: format!("pallet-parachain-template"),
features: vec![Features::RuntimeBenchmarks, Features::TryRuntime, Features::Std],
// TODO hardcode for now
path: (
// TODO hardcode for now
// The reason is, `pop new pallet` places a new pallet on $(workspace_root)/pallets
std::path::Path::new("../pallets/pallet-parachain-template").to_path_buf(),
semver::Version::new(1, 0, 0),
).into(),
Expand Down
44 changes: 30 additions & 14 deletions src/engines/pallet_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,55 @@ use toml_edit::DocumentMut;
pub fn execute(pallet: AddPallet, runtime_path: PathBuf) -> anyhow::Result<()> {
let mut pe = PalletEngine::new(&runtime_path)?;
// Todo: Add option to source from cli
let dep = TomlEditor::infer(&runtime_path);
let dep = TomlEditor::infer(&runtime_path)?;
// Check if workspace has uncommitted changes, if yes, abort
if !is_git_repo_with_commits(dep.runtime.parent().unwrap().parent().unwrap()) {
if !is_git_repo_with_commits(&dep.workspace)? {
cliclack::log::error(format!("Workspace -> {}", dep.workspace.display()));
bail!("Workspace has uncommitted changes, aborting pallet addition");
}
let steps = step_builder(pallet)?;
run_steps(pe, dep, steps)
}
#[derive(Default)]
struct TomlEditor {
// workspace: PathBuf,
/// Path to workspace toml file
workspace: PathBuf,
/// Path to runtime toml file
runtime: PathBuf,
/// Path to node toml file
node: PathBuf,
}
impl TomlEditor {
fn new(runtime: &Path, node: &Path) -> Self {
Self { runtime: runtime.to_path_buf(), node: node.to_path_buf() }
fn new(workspace: &Path, runtime: &Path, node: &Path) -> Self {
Self {
workspace: workspace.to_path_buf(),
runtime: runtime.to_path_buf(),
node: node.to_path_buf(),
}
}
/// Infer a given runtime and node manifest paths from the given runtime path
/// runtime -> ../Cargo.toml
/// node -> ../node/Cargo.toml
fn infer(runtime_path: &Path) -> Self {
let runtime_manifest = runtime_path.parent().unwrap().parent().unwrap().join("Cargo.toml");
let node_manifest = runtime_path
/// runtime -> ../Cargo.toml
/// node -> ../../node/Cargo.toml
/// workspace -> ../../Cargo.toml
fn infer(runtime_path: &Path) -> anyhow::Result<Self> {
let runtime_path = fs::canonicalize(runtime_path).with_context(|| {
format!("Failed to resolve {} into absolute path", runtime_path.display())
})?;
let workspace_manifest = runtime_path
.parent() // src
.unwrap()
.parent() // runtime
.unwrap()
.parent() // workspace
.unwrap()
.join("node/Cargo.toml");
.parent()
.unwrap(); // workspace
let runtime_manifest = workspace_manifest.join("runtime/Cargo.toml");
let node_manifest = workspace_manifest.join("node/Cargo.toml");
// println!("{} {}", runtime_manifest.display(), node_manifest.display());
Self { runtime: runtime_manifest, node: node_manifest }
Ok(Self {
workspace: workspace_manifest.to_path_buf(),
runtime: runtime_manifest,
node: node_manifest,
})
}
/// Inject a dependency into the node manifest
fn inject_node(&self, dep: Dependency) -> anyhow::Result<()> {
Expand Down
16 changes: 8 additions & 8 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ pub(crate) fn resolve_pallet_path(path: Option<String>) -> PathBuf {
}
}

pub(crate) fn is_git_repo_with_commits(repo_path: &Path) -> bool {
pub(crate) fn is_git_repo_with_commits(repo_path: &Path) -> anyhow::Result<bool> {
match Repository::open(repo_path) {
Ok(repo) => {
let mut status_opts = git2::StatusOptions::new();
status_opts.include_untracked(true);
let statuses = repo.statuses(Some(&mut status_opts)).unwrap();
status_opts.include_untracked(false);
let statuses = repo.statuses(Some(&mut status_opts))?;

// Check if there are no changes to commit
if !statuses.iter().any(|s| s.status() != git2::Status::CURRENT) {
Ok(if !statuses.iter().any(|s| s.status() != git2::Status::CURRENT) {
true
} else {
println!("Repository has uncommitted changes.");
log::warning(format!("Repository has uncommitted changes."));
false
}
})
},
Err(e) => {
eprintln!("Failed to open repository: {}", e);
false
log::error(format!("Failed to open repository: {}", e));
Err(e.into())
},
}
}

0 comments on commit f5ff852

Please sign in to comment.