MCPcopy Create free account
hub / github.com/Moosync/Moosync / install_extension

Method install_extension

src-tauri/extensions/src/lib.rs:141–208  ·  view source on GitHub ↗
(&self, ext_path: String)

Source from the content-addressed store, hash-verified

139
140 #[tracing::instrument(level = "debug", skip(self, ext_path))]
141 pub async fn install_extension(&self, ext_path: String) -> Result<()> {
142 tracing::debug!("ext path {}", ext_path);
143 let ext_path =
144 PathBuf::from_str(&ext_path).map_err(|e| MoosyncError::String(e.to_string()))?;
145
146 let tmp_dir = self
147 .tmp_dir
148 .join(format!("moosync_ext_{}", uuid::Uuid::new_v4()));
149
150 zip_extract(&ext_path, &tmp_dir)?;
151
152 let package_manifest: ExtensionManifest =
153 serde_json::from_slice(&fs::read(tmp_dir.join("package.json"))?)?;
154
155 if !package_manifest.moosync_extension {
156 return Err(MoosyncError::String(
157 "Extension is not a moosync extension".to_string(),
158 ));
159 }
160
161 let ext_extract_path = self.extensions_dir.join(package_manifest.name.clone());
162
163 match self.get_extension_version(ext_extract_path.clone()) {
164 Ok(version) => {
165 let old_version = self.get_ext_version(version)?;
166 let new_version = self.get_ext_version(package_manifest.version)?;
167
168 if new_version > old_version {
169 fs::remove_dir_all(ext_extract_path.clone())?;
170 } else {
171 return Err(MoosyncError::String(format!(
172 "Duplicate extension {}. Can not install",
173 package_manifest.name
174 )));
175 }
176 }
177 Err(_) => {
178 let _ = fs::remove_dir_all(ext_extract_path.clone());
179 }
180 }
181
182 let options = CopyOptions::default().overwrite(true);
183 let parent_dir = ext_extract_path.parent().unwrap();
184 tracing::debug!(
185 "Moving items from {:?} to {:?}",
186 tmp_dir.clone(),
187 parent_dir
188 );
189 if !parent_dir.exists() {
190 tracing::debug!("Creating dir {:?}", parent_dir);
191 fs::create_dir_all(parent_dir)?;
192 }
193 fs_extra::move_items(&[tmp_dir.clone()], parent_dir, &options)?;
194
195 tracing::debug!(
196 "Renaming {:?} to {:?}",
197 parent_dir.join(tmp_dir.file_name().unwrap()),
198 parent_dir.join(package_manifest.name.clone())

Callers 1

download_extensionMethod · 0.80

Calls 4

cloneMethod · 0.80
get_extension_versionMethod · 0.80
get_ext_versionMethod · 0.80
find_new_extensionsMethod · 0.80

Tested by

no test coverage detected