Skip to content

Commit

Permalink
Update current project branches, remotes and tags (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
hateofhades authored Aug 31, 2024
1 parent b98792d commit f722492
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 60 deletions.
37 changes: 5 additions & 32 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name: "Rust Checker and Coverage"
on:
pull_request:
merge_group:
push:
branches:
- main

jobs:
lint-build:
name: "Lint And Build"
name: "Lint, Build and Test"
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -41,37 +45,6 @@ jobs:
- name: "Build"
run: yarn tauri build

test:
name: "Test"
runs-on: ubuntu-latest
needs: lint-build

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20

- name: "Install Dependencies"
run: |
sudo apt-get update
sudo apt-get install libwebkit2gtk-4.0-dev \
build-essential \
curl \
wget \
file \
libssl-dev \
libgtk-3-dev \
libayatana-appindicator3-dev \
librsvg2-dev
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh -s -- -y
rustup update stable
npm install --global yarn
yarn
yarn build

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/vue.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: "Vue Checker and Coverage"
name: "Vue Checker"
on:
pull_request:
merge_group:
push:
branches:
- main
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ strum_macros = "0.26.4"
tempdir = "0.3.7"
thiserror = "1.0.63"
lazy_static = "1.5.0"
tokio = "1.40.0"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
41 changes: 41 additions & 0 deletions src-tauri/src/database/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Database {
path: String,
test_mode: bool,
projects: Vec<GitProject>,
current_project: Option<GitProject>,
}

impl Database {
Expand All @@ -36,6 +37,7 @@ impl Database {
path: String::new(),
test_mode: false,
projects: Vec::new(),
current_project: None,
}
}

Expand Down Expand Up @@ -99,12 +101,34 @@ impl Database {
pub fn set_test_mode(&mut self, test_mode: bool) {
self.test_mode = test_mode;
}

pub fn set_current_project(&mut self, project: Option<GitProject>) {
self.current_project = project;
}

pub fn get_current_project(&self) -> Option<GitProject> {
self.current_project.clone()
}

pub fn update_project(&mut self, project: GitProject) -> Result<()> {
let index = self
.projects
.iter()
.position(|p| p.get_directory() == project.get_directory())
.unwrap();
self.projects[index] = project;
self.save()?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use tempdir::TempDir;

use crate::git::git_project::GitProjectState;

use super::*;

#[test]
Expand Down Expand Up @@ -161,4 +185,21 @@ mod tests {
assert!(db.save().is_ok());
assert!(db.load().is_ok());
}

#[test]
fn test_update_project() {
let dir = TempDir::new("test_database").expect("Failed to create temp dir");

let mut db = Database::new();
let _ = db.set_path(dir.path().to_str().unwrap().to_string());
let project = GitProject::new("test");
db.add_project(project.clone())
.expect("Failed to add project");
let mut project2 = project.clone();
project2.set_state(GitProjectState::Invalid);
db.update_project(project2.clone())
.expect("Failed to update project");
let projects = db.get_projects();
assert_eq!(projects[0].get_state(), GitProjectState::Invalid);
}
}
20 changes: 19 additions & 1 deletion src-tauri/src/errors/git_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
use crate::git::git_project::GitProject;

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub enum GitError {
DatabaseSaveError,
Expand All @@ -10,3 +12,19 @@ pub enum GitError {
NoGitFolder,
NoLocalBranches,
}

#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GitErrorProject {
pub error: GitError,
pub project: GitProject
}

impl GitErrorProject {
pub fn new(error: GitError, project: GitProject) -> GitErrorProject {
GitErrorProject {
error,
project
}
}
}
20 changes: 20 additions & 0 deletions src-tauri/src/git/git_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ impl GitProject{
}
}

pub fn update(&mut self) -> Result<(), GitError> {
self.local_branches.clear();
self.remotes.clear();
self.remote_branches.clear();
self.tags.clear();

self.fetch_remotes_directories()?;
self.fetch_branches(GitBranchType::Local)?;
self.fetch_branches(GitBranchType::Tags)?;

let remotes = self.remotes.clone();
for remote in remotes {
self.fetch_branches(GitBranchType::Remote(remote))?;
}

self.state = GitProjectState::Valid;

Ok(())
}

pub fn fetch_remotes_directories(&mut self) -> Result<(), GitError> {
self.has_required_files().map_err(|_| {
self.state = GitProjectState::Invalid;
Expand Down
62 changes: 62 additions & 0 deletions src-tauri/src/git/project_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ pub fn check_valid_git_project(directory: &str) -> Result<GitProject, GitError>
.map_err(|_| GitError::CannotOpenFolder)?
}

#[tauri::command]
pub fn set_current_project(project: Option<GitProject>) {
DATABASE.lock().unwrap().set_current_project(project);
}

#[tauri::command]
pub fn open_git_project(directory: &str) -> Result<GitProject, GitError> {
check_valid_git_project(directory).map(|mut git_project| {
Expand Down Expand Up @@ -292,6 +297,30 @@ mod tests {
.contains(&"feature/test".to_string()));
}

#[test]
fn test_git_project_update() {
let folder = TempDir::new("test_git_project_update").unwrap();
let test_git_folder = folder.path().to_str().unwrap();

create_sample_git_folder(test_git_folder);
create_local_branch(test_git_folder, "feature/test");

let mut git_project = open_git_project(test_git_folder).unwrap();
let _ = git_project.fetch_branches(GitBranchType::Local);

assert!(git_project
.get_local_branches()
.contains(&"feature/test".to_string()));

create_local_branch(test_git_folder, "feature/test2");
git_project.update().unwrap();

assert_eq!(git_project.get_local_branches().len(), 2);
assert!(git_project
.get_local_branches()
.contains(&"feature/test2".to_string()));
}

#[test]
fn test_git_project_no_local_branches_folder() {
let folder = TempDir::new("test_git_project_no_local_branches_folder").unwrap();
Expand Down Expand Up @@ -343,4 +372,37 @@ mod tests {

fs::remove_dir_all(test_git_folder).unwrap();
}

#[test]
fn test_set_current_project() {
let folder = TempDir::new("test_set_current_project").unwrap();
let test_git_folder = folder.path().to_str().unwrap();

create_sample_git_folder(test_git_folder);

let git_project = open_git_project(test_git_folder).unwrap();
set_current_project(Some(git_project.clone()));

let current_project = DATABASE.lock().unwrap().get_current_project();
assert_eq!(current_project, Some(git_project));
}

#[test]
fn test_remove_current_project() {
let folder = TempDir::new("test_remove_current_project").unwrap();
let test_git_folder = folder.path().to_str().unwrap();

create_sample_git_folder(test_git_folder);

let git_project = open_git_project(test_git_folder).unwrap();
set_current_project(Some(git_project.clone()));

let current_project = DATABASE.lock().unwrap().get_current_project();
assert_eq!(current_project, Some(git_project));

set_current_project(None);

let current_project = DATABASE.lock().unwrap().get_current_project();
assert_eq!(current_project, None);
}
}
58 changes: 46 additions & 12 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,62 @@ pub mod git;
use std::fs;

use database::storage::DATABASE;
use git::project_folder::{get_database_projects, open_git_project, remove_database_project};
use errors::git_error::GitErrorProject;
use git::project_folder::{
get_database_projects, open_git_project, remove_database_project, set_current_project,
};
use tauri::{AppHandle, Manager};

async fn setup(app: AppHandle) {
fs::create_dir_all(app.path_resolver().app_data_dir().unwrap())
.expect("Failed to create app data directory");

let _ = DATABASE.lock().unwrap().set_path(
app.path_resolver()
.app_data_dir()
.unwrap()
.display()
.to_string(),
);
}

async fn event_loop(app: AppHandle) {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(5));
loop {
interval.tick().await;
let mutex = DATABASE.lock().unwrap();
if let Some(mut project) = mutex.get_current_project() {
drop(mutex);
match project.update() {
Ok(_) => {
app.emit_all("project_update", &project).unwrap();

let _ = DATABASE.lock().unwrap().update_project(project.clone());
}
Err(e) => {
app.emit_all("project_update_error", GitErrorProject::new(e, project.clone()))
.unwrap();

let _ = DATABASE.lock().unwrap().update_project(project.clone());
}
}
}
}
}

fn main() {
tauri::Builder::default()
.setup(|app| {
fs::create_dir_all(app.handle().path_resolver().app_data_dir().unwrap())
.expect("Failed to create app data directory");

let _ = DATABASE.lock().unwrap().set_path(
app.handle()
.path_resolver()
.app_data_dir()
.unwrap()
.display().to_string()
);
tauri::async_runtime::block_on(setup(app.handle()));
tauri::async_runtime::spawn(event_loop(app.handle()));

Ok(())
})
.invoke_handler(tauri::generate_handler![
open_git_project,
get_database_projects,
remove_database_project
remove_database_project,
set_current_project
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
Loading

0 comments on commit f722492

Please sign in to comment.