|
| 1 | +// This Source Code Form is subject to the terms of |
| 2 | +// the Mozilla Public License, v. 2.0. If a copy of the |
| 3 | +// MPL was not distributed with this file, You can |
| 4 | +// obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use std::fs; |
| 8 | + |
| 9 | +use miette::Diagnostic; |
| 10 | +use thiserror::Error; |
| 11 | + |
| 12 | +use crate::actions::{File as FileAction, Manifest, Transform as TransformAction}; |
| 13 | +use crate::repository::{ReadableRepository, RepositoryError, WritableRepository}; |
| 14 | +use crate::repository::file_backend::{FileBackend, Transaction}; |
| 15 | +use crate::transformer; |
| 16 | + |
| 17 | +/// Error type for high-level publishing operations |
| 18 | +#[derive(Debug, Error, Diagnostic)] |
| 19 | +pub enum PublisherError { |
| 20 | + #[error(transparent)] |
| 21 | + #[diagnostic(transparent)] |
| 22 | + Repository(#[from] RepositoryError), |
| 23 | + |
| 24 | + #[error(transparent)] |
| 25 | + #[diagnostic(transparent)] |
| 26 | + Transform(#[from] transformer::TransformError), |
| 27 | + |
| 28 | + #[error("I/O error: {0}")] |
| 29 | + #[diagnostic(code(ips::publisher_error::io), help("Check the path and permissions"))] |
| 30 | + Io(String), |
| 31 | + |
| 32 | + #[error("invalid root path: {0}")] |
| 33 | + #[diagnostic(code(ips::publisher_error::invalid_root_path), help("Ensure the directory exists and is readable"))] |
| 34 | + InvalidRoot(String), |
| 35 | +} |
| 36 | + |
| 37 | +pub type Result<T> = std::result::Result<T, PublisherError>; |
| 38 | + |
| 39 | +/// High-level Publisher client that keeps a repository handle and an open transaction. |
| 40 | +/// |
| 41 | +/// This is intended to simplify software build/publish flows: instantiate once with a |
| 42 | +/// repository path and publisher, then build/transform manifests and publish. |
| 43 | +pub struct PublisherClient { |
| 44 | + backend: FileBackend, |
| 45 | + publisher: String, |
| 46 | + tx: Option<Transaction>, |
| 47 | + transform_rules: Vec<transformer::TransformRule>, |
| 48 | +} |
| 49 | + |
| 50 | +impl PublisherClient { |
| 51 | + /// Open an existing repository located at `path` with a selected `publisher`. |
| 52 | + pub fn open<P: AsRef<Path>>(path: P, publisher: impl Into<String>) -> Result<Self> { |
| 53 | + let backend = FileBackend::open(path)?; |
| 54 | + Ok(Self { backend, publisher: publisher.into(), tx: None, transform_rules: Vec::new() }) |
| 55 | + } |
| 56 | + |
| 57 | + /// Open a transaction if not already open and return whether a new transaction was created. |
| 58 | + pub fn open_transaction(&mut self) -> Result<bool> { |
| 59 | + if self.tx.is_none() { |
| 60 | + let tx = self.backend.begin_transaction()?; |
| 61 | + self.tx = Some(tx); |
| 62 | + return Ok(true); |
| 63 | + } |
| 64 | + Ok(false) |
| 65 | + } |
| 66 | + |
| 67 | + /// Build a new Manifest from a directory tree. Paths in the manifest are relative to `root`. |
| 68 | + pub fn build_manifest_from_dir(&mut self, root: &Path) -> Result<Manifest> { |
| 69 | + if !root.exists() { |
| 70 | + return Err(PublisherError::InvalidRoot(root.display().to_string())); |
| 71 | + } |
| 72 | + let mut manifest = Manifest::new(); |
| 73 | + let root = root.canonicalize().map_err(|_| PublisherError::InvalidRoot(root.display().to_string()))?; |
| 74 | + |
| 75 | + let walker = walkdir::WalkDir::new(&root).into_iter().filter_map(|e| e.ok()); |
| 76 | + // Ensure a transaction is open |
| 77 | + if self.tx.is_none() { |
| 78 | + self.open_transaction()?; |
| 79 | + } |
| 80 | + let tx = self.tx.as_mut().expect("transaction must be open"); |
| 81 | + |
| 82 | + for entry in walker { |
| 83 | + let p = entry.path(); |
| 84 | + if p.is_file() { |
| 85 | + // Create a File action from the absolute path |
| 86 | + let mut f = FileAction::read_from_path(p).map_err(RepositoryError::from)?; |
| 87 | + // Set path to be relative to root |
| 88 | + let rel: PathBuf = p |
| 89 | + .strip_prefix(&root) |
| 90 | + .map_err(RepositoryError::from)? |
| 91 | + .to_path_buf(); |
| 92 | + f.path = rel.to_string_lossy().to_string(); |
| 93 | + // Add into manifest and stage via transaction |
| 94 | + manifest.add_file(f.clone()); |
| 95 | + tx.add_file(f, p)?; |
| 96 | + } |
| 97 | + } |
| 98 | + Ok(manifest) |
| 99 | + } |
| 100 | + |
| 101 | + /// Make a new empty manifest |
| 102 | + pub fn new_empty_manifest(&self) -> Manifest { |
| 103 | + Manifest::new() |
| 104 | + } |
| 105 | + |
| 106 | + /// Transform a manifest with a user-supplied rule function |
| 107 | + pub fn transform_manifest<F>(&self, mut manifest: Manifest, rule: F) -> Manifest |
| 108 | + where |
| 109 | + F: FnOnce(&mut Manifest), |
| 110 | + { |
| 111 | + rule(&mut manifest); |
| 112 | + manifest |
| 113 | + } |
| 114 | + |
| 115 | + /// Add a single AST transform rule |
| 116 | + pub fn add_transform_rule(&mut self, rule: transformer::TransformRule) { |
| 117 | + self.transform_rules.push(rule); |
| 118 | + } |
| 119 | + |
| 120 | + /// Add multiple AST transform rules |
| 121 | + pub fn add_transform_rules(&mut self, rules: Vec<transformer::TransformRule>) { |
| 122 | + self.transform_rules.extend(rules); |
| 123 | + } |
| 124 | + |
| 125 | + /// Clear all configured transform rules |
| 126 | + pub fn clear_transform_rules(&mut self) { |
| 127 | + self.transform_rules.clear(); |
| 128 | + } |
| 129 | + |
| 130 | + /// Load transform rules from raw text (returns number of rules added) |
| 131 | + pub fn load_transform_rules_from_text(&mut self, text: &str) -> Result<usize> { |
| 132 | + let rules = transformer::parse_rules_ast(text)?; |
| 133 | + let n = rules.len(); |
| 134 | + self.transform_rules.extend(rules); |
| 135 | + Ok(n) |
| 136 | + } |
| 137 | + |
| 138 | + /// Load transform rules from a file (returns number of rules added) |
| 139 | + pub fn load_transform_rules_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<usize> { |
| 140 | + let p = path.as_ref(); |
| 141 | + let content = fs::read_to_string(p).map_err(|e| PublisherError::Io(e.to_string()))?; |
| 142 | + self.load_transform_rules_from_text(&content) |
| 143 | + } |
| 144 | + |
| 145 | + /// Publish the given manifest. If no transaction is open, one will be opened. |
| 146 | + /// The transaction will be updated with the provided manifest and committed. |
| 147 | + /// If `rebuild_metadata` is true, repository metadata (catalog/index) will be rebuilt. |
| 148 | + pub fn publish(&mut self, mut manifest: Manifest, rebuild_metadata: bool) -> Result<()> { |
| 149 | + // Apply configured transform rules (if any) |
| 150 | + if !self.transform_rules.is_empty() { |
| 151 | + let rules: Vec<TransformAction> = self |
| 152 | + .transform_rules |
| 153 | + .clone() |
| 154 | + .into_iter() |
| 155 | + .map(Into::into) |
| 156 | + .collect(); |
| 157 | + transformer::apply(&mut manifest, &rules)?; |
| 158 | + } |
| 159 | + |
| 160 | + // Ensure transaction exists |
| 161 | + if self.tx.is_none() { |
| 162 | + self.open_transaction()?; |
| 163 | + } |
| 164 | + |
| 165 | + // Take ownership of the transaction, update and commit |
| 166 | + let mut tx = self.tx.take().expect("transaction must be open"); |
| 167 | + tx.set_publisher(&self.publisher); |
| 168 | + tx.update_manifest(manifest); |
| 169 | + tx.commit()?; |
| 170 | + // Optionally rebuild repo metadata for the publisher |
| 171 | + if rebuild_metadata { |
| 172 | + self.backend.rebuild(Some(&self.publisher), false, false)?; |
| 173 | + } |
| 174 | + Ok(()) |
| 175 | + } |
| 176 | +} |
0 commit comments