Skip to content

Commit

Permalink
Config
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Aug 4, 2023
1 parent 2a0312e commit 9b4492a
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ jobs:
- name: Build the app
uses: tauri-apps/tauri-action@v0

- name: 🚀 Upload DMG
- name: 🚀 Upload macOS dmg
uses: actions/upload-artifact@v3
with:
name: Commit.dmg
path: |
target/release/bundle/dmg/Commit*.dmg
- name: 🚀 Upload release artifacts
- name: 🚀 Upload macOS App
uses: actions/upload-artifact@v3
with:
name: Commit.app
Expand Down
78 changes: 77 additions & 1 deletion Cargo.lock

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

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ authors = ["Miguel Piedrafita <sup@miguel.build>"]
description = "Open-source Git client for minimalists"

[dependencies]
confy = "0.5.1"
git2 = "0.17.2"
rayon = "1.7.0"
walkdir = "2.3.3"
Expand All @@ -16,10 +17,17 @@ serde_json = "1.0"
priority-queue = "1.3.2"
window-vibrancy = "0.4.0"
serde = { version = "1.0", features = ["derive"] }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }

[dependencies.tauri]
version = "1.4"
features = ["macos-private-api", "system-tray", "config-toml", "global-shortcut", "notification"]
features = [
"macos-private-api",
"system-tray",
"config-toml",
"global-shortcut",
"notification",
]

[features]
custom-protocol = ["tauri/custom-protocol"]
Expand Down
47 changes: 25 additions & 22 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{path::PathBuf, process::Command};

use git2::Repository;
use tauri::{api::notification::Notification, AppHandle, Invoke, Window, Wry};
use std::{path::PathBuf, process::Command};
use tauri::{api::notification::Notification, AppHandle, Invoke, State, Window, Wry};

use crate::{utils, window};
use crate::{config::GetConfig, utils, window};

#[tauri::command]
fn commit(
Expand All @@ -12,6 +11,7 @@ fn commit(
path: PathBuf,
title: String,
description: Option<String>,
config: State<GetConfig>,
) -> Result<(), String> {
let repo = Repository::open(path).map_err(|e| e.to_string())?;

Expand All @@ -31,24 +31,27 @@ fn commit(
.show()
.unwrap();

tauri::async_runtime::spawn(async move {
let status = Command::new("git")
.arg("push")
.current_dir(repo.path())
.status()
.expect("Failed to execute git push");

let alert = Notification::new(&app.config().tauri.bundle.identifier);
if status.success() {
alert.title("Push").body("Push successful!")
} else {
alert
.title("Failed to push")
.body("Failed to push to remote repository")
}
.show()
.unwrap()
});
let config = config.read().unwrap();
if config.should_push {
tauri::async_runtime::spawn(async move {
let status = Command::new("git")
.arg("push")
.current_dir(repo.path())
.status()
.expect("Failed to execute git push");

let alert = Notification::new(&app.config().tauri.bundle.identifier);
if status.success() {
alert.title("Push").body("Push successful!")
} else {
alert
.title("Failed to push")
.body("Failed to push to remote repository")
}
.show()
.unwrap()
});
}

Ok(())
}
Expand Down
49 changes: 49 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::{path::PathBuf, process::Command, sync::RwLock};

use anyhow::Result;

pub type GetConfig = RwLock<Config>;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Config {
pub autostart: bool,
pub should_push: bool,
pub repo_paths: Vec<PathBuf>,
}

impl Config {
pub fn load() -> Result<Self, confy::ConfyError> {
let config: Self = confy::load("commit", Some("Settings"))?;

config.save()?;
Ok(config)
}

pub fn save(&self) -> Result<(), confy::ConfyError> {
confy::store("commit", Some("Settings"), self)
}

pub fn manage(self) -> RwLock<Self> {
RwLock::new(self)
}

pub fn config_path() -> Result<PathBuf, confy::ConfyError> {
confy::get_configuration_file_path("commit", Some("Settings"))
}
}

impl Default for Config {
fn default() -> Self {
Self {
autostart: false,
should_push: true,
repo_paths: vec![],
}
}
}

pub fn edit() -> Result<()> {
Command::new("open").arg(Config::config_path()?).status()?;

Ok(())
}
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use anyhow::anyhow;
use config::Config;
use std::error::Error;
use tauri::{generate_context, ActivationPolicy, Builder as Tauri, GlobalShortcutManager, Manager};
use tauri_plugin_autostart::{self as tauri_autostart, MacosLauncher};

mod commands;
mod config;
mod tray;
mod utils;
mod window;

fn main() {
let config = Config::load().unwrap();

let app = Tauri::new()
.setup(setup_tauri)
.system_tray(tray::build())
.manage(config.manage())
.on_window_event(window::handler)
.invoke_handler(commands::handler())
.on_system_tray_event(tray::handle)
.plugin(tauri_autostart::init(MacosLauncher::LaunchAgent, None))
.build(generate_context!())
.expect("error while running tauri application");

Expand All @@ -33,10 +40,18 @@ fn setup_tauri(app: &mut tauri::App) -> Result<(), Box<(dyn Error + 'static)>> {
anyhow!("Unsupported platform! 'apply_vibrancy' is only supported on macOS")
})?;

app.global_shortcut_manager()
.register("CmdOrControl+Alt+Shift+C", move || {
window::toggle(&window).unwrap();
})?;
let mut shortcuts = app.global_shortcut_manager();

let window_handle = window.clone();
shortcuts.register("CmdOrControl+Alt+Shift+C", move || {
window::toggle(&window_handle).unwrap()
})?;

shortcuts.register("CmdOrControl+,", move || {
if window.is_focused().unwrap() {
config::edit().unwrap();
}
})?;

Ok(())
}
Loading

0 comments on commit 9b4492a

Please sign in to comment.