1use std::{
2 fs::{self, File},
3 path::{Path, PathBuf},
4};
5
6use rust_terminal::Terminal;
7use url::Url;
8
9use crate::SolarError;
10
11pub const SOLARCONFIGNAME: &str = "solar.config.json";
12
13pub struct Global {}
14
15macro_rules! match_target {
16 ($macos_arm:expr, $macos_x:expr, $linux_x:expr, $windows_x:expr, $default:expr) => {
17 #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
18 return $macos_arm;
19 #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
20 return $macos_x;
21 #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
22 return $linux_x;
23 #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
24 return $windows_x;
25 #[cfg(not(any(
26 all(target_os = "macos", target_arch = "aarch64"),
27 all(target_os = "macos", target_arch = "x86_64"),
28 all(target_os = "linux", target_arch = "x86_64"),
29 all(target_os = "windows", target_arch = "x86_64")
30 )))]
31 $default
32 };
33}
34
35impl Global {
36 pub fn is_git(destination: &Path) -> Result<bool, SolarError> {
38 Ok(fs::exists(destination.join(".git"))?)
39 }
40
41 pub fn ovrwrt_file_or_default(file_path: &Path) -> Result<File, SolarError> {
42 if !fs::exists(file_path)? {
43 File::create(file_path)?;
44 }
45 Ok(fs::File::options()
46 .write(true)
47 .truncate(true)
48 .open(file_path)?)
49 }
50
51 pub fn git_init(destination: &Path) -> Result<(), SolarError> {
53 if !Self::is_git(destination)? {
54 Terminal::command()
55 .current_dir(destination)
56 .piped()
57 .run("git", vec!["init"])?;
58 }
59 Ok(())
60 }
61
62 pub fn git_hooks_path(path: &Path) -> Result<PathBuf, SolarError> {
63 let command_output = Terminal::command()
64 .current_dir(path)
65 .run("git", ["config", "core.hooksPath"])
66 .unwrap();
67 let output_text = String::from_utf8(command_output.stdout)?;
68 if output_text.is_empty() {
69 return Ok(PathBuf::from(".git/hooks"));
70 }
71 Ok(PathBuf::from(output_text.trim()))
72 }
73
74 pub fn default_git_hook_dir() -> PathBuf {
75 PathBuf::from(".git/hooks")
76 }
77
78 pub fn licenses_url(spdx: &str) -> Result<Url, SolarError> {
79 Ok(Url::parse(&format!(
80 "https://github.com/nraynes/licenses/raw/refs/heads/main/LICENSES/LICENSE-{}",
81 spdx
82 ))?)
83 }
84
85 pub fn commitalyzer_exec_download() -> Result<Url, SolarError> {
86 match_target!(
87 Ok(Url::parse(
88 "https://github.com/nraynes/commitalyzer/raw/refs/heads/master/bin/arm-macos/commit-msg",
89 )?),
90 Ok(Url::parse(
91 "https://github.com/nraynes/commitalyzer/raw/refs/heads/master/bin/intel-macos/commit-msg",
92 )?),
93 Ok(Url::parse(
94 "https://github.com/nraynes/commitalyzer/raw/refs/heads/master/bin/linux/commit-msg",
95 )?),
96 Ok(Url::parse(
97 "https://github.com/nraynes/commitalyzer/raw/refs/heads/master/bin/windows/commit-msg",
98 )?),
99 Err(SolarError::from("No download available for this target"))
100 );
101 }
102
103 pub fn commitalyzer_conventional_commits_ruleset() -> Result<Url, SolarError> {
104 Ok(Url::parse(
105 "https://github.com/nraynes/commitalyzer/raw/refs/heads/master/commit-rules/conventional-commits.yml",
106 )?)
107 }
108
109 pub fn semver_release_exec_download() -> Result<Url, SolarError> {
110 Ok(Url::parse(
111 "https://github.com/nraynes/semver-release/raw/refs/heads/master/bin/arm-macos/semver-release",
112 )?)
113 }
114
115 pub fn semver_release_config_url() -> Result<Url, SolarError> {
116 Ok(Url::parse(
117 "https://github.com/nraynes/semver-release/raw/refs/heads/master/sample.config.semver.json",
118 )?)
119 }
120
121 pub fn semver_cargo_exec_download() -> Result<Url, SolarError> {
122 Ok(Url::parse(
123 "https://github.com/nraynes/semver-cargo/raw/refs/heads/master/bin/arm-macos/semver-cargo",
124 )?)
125 }
126}