Git support (#143)

This commit is contained in:
Gregory Schier
2025-02-07 07:59:48 -08:00
committed by GitHub
parent cffc7714c1
commit 1a7c27663a
111 changed files with 4264 additions and 372 deletions

View File

@@ -6,8 +6,8 @@ use tauri::{
};
mod commands;
mod error;
mod models;
pub mod error;
pub mod models;
mod sync;
mod watch;

View File

@@ -3,14 +3,14 @@ use crate::error::Result;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
use std::fs;
use std::path::Path;
use tokio::fs;
use ts_rs::TS;
use yaak_models::models::{
AnyModel, Environment, Folder, GrpcRequest, HttpRequest, WebsocketRequest, Workspace,
};
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export, export_to = "gen_models.ts")]
pub enum SyncModel {
@@ -23,12 +23,10 @@ pub enum SyncModel {
}
impl SyncModel {
pub async fn from_file(file_path: &Path) -> Result<Option<(SyncModel, Vec<u8>, String)>> {
let content = match fs::read(file_path).await {
Ok(c) => c,
Err(_) => return Ok(None),
};
pub fn from_bytes(
content: Vec<u8>,
file_path: &Path,
) -> Result<Option<(SyncModel, Vec<u8>, String)>> {
let mut hasher = Sha1::new();
hasher.update(&content);
let checksum = hex::encode(hasher.finalize());
@@ -39,10 +37,20 @@ impl SyncModel {
} else if ext == "json" {
Ok(Some((serde_json::from_reader(content.as_slice())?, content, checksum)))
} else {
Err(InvalidSyncFile(file_path.to_str().unwrap().to_string()))
let p = file_path.to_str().unwrap().to_string();
Err(InvalidSyncFile(format!("Unknown file extension {p}")))
}
}
pub fn from_file(file_path: &Path) -> Result<Option<(SyncModel, Vec<u8>, String)>> {
let content = match fs::read(file_path) {
Ok(c) => c,
Err(_) => return Ok(None),
};
Self::from_bytes(content, file_path)
}
pub fn to_file_contents(&self, rel_path: &Path) -> Result<(Vec<u8>, String)> {
let ext = rel_path.extension().unwrap_or_default();
let content = if ext == "yaml" || ext == "yml" {

View File

@@ -1,4 +1,3 @@
use crate::error::Error::InvalidSyncFile;
use crate::error::Result;
use crate::models::SyncModel;
use chrono::Utc;
@@ -164,13 +163,12 @@ pub(crate) async fn get_fs_candidates(dir: &Path) -> Result<Vec<FsCandidate>> {
};
let path = dir_entry.path();
let (model, _, checksum) = match SyncModel::from_file(&path).await {
let (model, _, checksum) = match SyncModel::from_file(&path) {
Ok(Some(m)) => m,
Ok(None) => continue,
Err(InvalidSyncFile(_)) => continue,
Err(e) => {
warn!("Failed to read sync file {e}");
continue;
return Err(e);
}
};
@@ -315,7 +313,7 @@ pub(crate) async fn apply_sync_ops<R: Runtime>(
}
debug!(
"Sync ops {}",
"Applying sync ops {}",
sync_ops.iter().map(|op| op.to_string()).collect::<Vec<String>>().join(", ")
);
let mut sync_state_ops = Vec::new();

View File

@@ -45,9 +45,19 @@ pub(crate) async fn watch_directory(
Some(event_res) = async_rx.recv() => {
match event_res {
Ok(event) => {
// Filter out any ignored directories and see if we still get a result
let paths = event.paths.into_iter()
.map(|p| p.strip_prefix(&dir).unwrap().to_path_buf())
.filter(|p| !p.starts_with(".git") && !p.starts_with("node_modules"))
.collect::<Vec<PathBuf>>();
if paths.is_empty() {
continue;
}
channel
.send(WatchEvent {
paths: event.paths,
paths,
kind: format!("{:?}", event.kind),
})
.expect("Failed to send watch event");