GitRoot
craft your forge, build your project, grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: MIT
4
5use crate::{
6 Commit, Exec, ExecStatus, ForgeConf, PluginRun, ReportLevel, ReportToGitroot, fs::Fs, imports,
7};
8
9pub struct Server {
10 at_least_one_change: bool,
11 pub run: Vec<PluginRun>,
12}
13
14impl Server {
15 pub fn new(run: Vec<PluginRun>) -> Self {
16 Server {
17 at_least_one_change: false,
18 run,
19 }
20 }
21
22 pub fn forge_conf(&self) -> Result<ForgeConf, String> {
23 imports::forge_conf()
24 }
25
26 pub fn worktree(&self) -> Fs {
27 Fs::new(String::from("/worktree/"))
28 }
29
30 pub fn webcontent(&self) -> Fs {
31 Fs::new(String::from("/webcontent/"))
32 }
33
34 pub fn cache(&self) -> Fs {
35 Fs::new(String::from("/cache"))
36 }
37
38 pub fn modify_content(&mut self, filepath: String, content: String) {
39 self.at_least_one_change = true;
40 imports::modify_content(filepath, content);
41 }
42
43 pub fn modify_web_content(&self, filepath: String, content: String) {
44 imports::modify_web_content(filepath, content);
45 }
46
47 pub fn replace_web_content(&self, filepath: String, old_content: String, content: String) {
48 imports::replace_web_content(filepath, old_content, content);
49 }
50
51 pub fn modify_cache_content(&self, filepath: String, content: String) {
52 imports::modify_cache_content(filepath, content);
53 }
54
55 pub fn commit_all_if_needed(&self, message: String) {
56 if self.at_least_one_change {
57 imports::commit_all(message);
58 }
59 }
60
61 pub fn diff_with_parent(
62 &self,
63 hash: String,
64 old_filepath: String,
65 new_filepath: String,
66 ) -> Result<String, String> {
67 imports::diff_with_parent(hash, old_filepath, new_filepath)
68 }
69
70 pub fn log(&self, message: String) {
71 imports::log(message);
72 }
73
74 pub fn log_error(&self, message: String, err: String) {
75 imports::log_error(message, err);
76 }
77
78 pub fn merge(&self, from: String, to: String) {
79 imports::merge(from, to);
80 }
81
82 pub fn commits(&self, from: String, to: String) -> Result<Vec<Commit>, String> {
83 imports::commits(from, to)
84 }
85
86 pub fn exec(&self, cmd: &Exec) -> Result<ExecStatus, String> {
87 imports::exec(cmd)
88 }
89
90 pub fn report(&self, level: ReportLevel, content: Vec<String>) {
91 imports::report_to_gitroot(ReportToGitroot { level, content })
92 }
93}