1#![allow(dead_code)]
2use std::path::PathBuf;
3
4use anyhow::{Ok, Result};
5use dirs::home_dir;
6use serde::{Deserialize, Serialize};
7use tokio::fs;
8
9#[allow(unused_imports)]
10use crate::git::{remote::get_remote_branch_hash, repo::Repo};
11use crate::{config::ProjectConfig, log::logger::Logger};
12
13#[doc = include_str!("docs/watch_context.md")]
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct WatchContext {
16 pub repo: Repo,
17 pub config: ProjectConfig,
18 pub project_dir: String,
19 pub id: String,
20 pub paused: bool,
21 #[serde(skip, default = "Logger::placeholder")]
22 pub logger: Logger,
23}
24
25pub struct WatchContextBuilder {
26 repo: Repo,
27 config: ProjectConfig,
28 project_dir: String,
29 id: String,
30 paused: bool,
31}
32
33#[doc = include_str!("docs/build.md")]
34impl WatchContextBuilder {
35 pub fn new(repo: Repo, config: ProjectConfig, project_dir: String, id: String) -> Self {
36 Self {
37 repo,
38 config,
39 project_dir,
40 id,
41 paused: false,
42 }
43 }
44
45 pub async fn build(self) -> Result<WatchContext, anyhow::Error> {
46 let logger = Logger::new(&self.log_path()).await?;
48
49 Ok(WatchContext {
51 repo: self.repo,
52 config: self.config,
53 project_dir: self.project_dir,
54 id: self.id,
55 paused: self.paused,
56 logger,
57 })
58 }
59
60 fn log_path(&self) -> PathBuf {
61 let home = home_dir().unwrap();
62
63 let log_dir = home.join(".fleet").join("logs");
64 log_dir.join(self.id.to_string() + ".log")
65 }
66}
67
68impl WatchContext {
69 pub fn stop(&mut self) {
70 self.paused = true;
71 }
72
73 pub fn run(&mut self) {
74 self.paused = false;
75 }
76
77 pub fn log_path(&self) -> PathBuf {
78 let home: PathBuf = home_dir().unwrap();
79
80 let log_dir = home.join(".fleet").join("logs");
81 log_dir.join(self.id.to_string() + ".log")
82 }
83
84 pub fn log_path_by_id(id: &str) -> PathBuf {
85 let home = home_dir().unwrap();
86
87 let log_dir = home.join(".fleet").join("logs");
88 log_dir.join(id.to_string() + ".log")
89 }
90
91 pub async fn init_logs() -> Result<()> {
92 let home = home_dir().ok_or_else(|| anyhow::anyhow!("Failed to find HOME directory"))?;
93
94 let log_dir = home.join(".fleet").join("logs");
95
96 if !fs::try_exists(&log_dir).await? {
97 fs::create_dir_all(&log_dir).await?;
98 println!("init logs directory : {}", log_dir.display());
99 } else {
100 println!("log folder already exist : {}", log_dir.display());
101 }
102 Ok(())
103 }
104}
105
106#[doc = include_str!("docs/watch_once.md")]
107pub fn watch_once(repo: &mut Repo) -> Result<Option<String>, anyhow::Error> {
108 #[cfg(not(feature = "force_commit"))]
109 {
110 let mut name = String::new();
111 let res = repo.branches.try_for_each(|b| {
112 let remote_hash = get_remote_branch_hash(&b.remote, &b.branch)?;
113
114 if remote_hash != b.last_commit {
115 b.last_commit = remote_hash.clone();
116 name = b.branch.clone();
117 println!("new commit detected: {} -> {}", b.last_commit, remote_hash);
118 return Ok(Some(remote_hash));
119 }
120 return Ok(None);
121 })?;
122
123 let first_new = res.into_iter().flatten().next();
124 if let Some(commit) = &first_new {
125 repo.branches.last_commit = commit.clone();
126 repo.branches.last_name = name;
127 }
128 Ok(first_new)
129 }
130 #[cfg(feature = "force_commit")]
131 return Ok(Some(repo.branches.last_commit.clone()));
132}