Skip to content

Commit a201b2e

Browse files
committed
ref(core): extract code to xcodegen module
1 parent 8694c47 commit a201b2e

File tree

5 files changed

+95
-48
lines changed

5 files changed

+95
-48
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ edition = "2021"
55

66
[features]
77
default = [ ]
8-
daemon = [ "serial", "completion", "logging", "xcode", "async", "watcher", "proc" ]
8+
xcodegen = [ "async", "dirs" ]
9+
daemon = [ "serial", "completion", "logging", "xcode", "async", "watcher", "proc", "xcodegen" ]
910
lua = [ "mlua" ]
1011
serial = [ "serde", "serde_json", "serde_yaml" ]
1112
completion = [ "serial", "lazy_static", "regex" ]

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ pub use state::*;
99
#[cfg(feature = "xcode")]
1010
pub mod xcode;
1111

12+
#[cfg(feature = "xcodegen")]
13+
pub mod xcodegen;
14+
1215
// Utilities
1316

1417
pub mod util;

src/state/workspace.rs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use std::path::PathBuf;
1111

1212
use crate::Project;
1313

14+
#[cfg(feature = "xcodegen")]
15+
use crate::xcodegen;
16+
1417
/// Managed Workspace
1518
#[derive(Debug)]
1619
pub struct Workspace {
@@ -94,14 +97,17 @@ impl Workspace {
9497
}
9598

9699
/// Regenerate compiled commands and xcodeGen if project.yml exists
97-
#[cfg(feature = "xcode")]
98-
pub async fn on_dirctory_change(
100+
#[cfg(all(feature = "xcode", feature = "watcher"))]
101+
pub async fn on_directory_change(
99102
&mut self,
100103
path: PathBuf,
101104
_event: notify::EventKind,
102105
) -> Result<()> {
103-
if self.is_xcodegen_project() {
106+
if crate::xcodegen::is_workspace(self) {
104107
let is_config_file = path.file_name().unwrap().eq("project");
108+
// FIXME: should've been true
109+
tracing::debug!("is_config_file: {is_config_file}");
110+
105111
self.update_xcodeproj(is_config_file).await?;
106112
}
107113

@@ -112,55 +118,31 @@ impl Workspace {
112118
}
113119

114120
/// Update .compile commands
121+
#[cfg(feature = "xcodegen")]
115122
pub async fn update_xcodeproj(&mut self, update_config: bool) -> Result<()> {
116-
/*
117-
FIXME: make xCodeGen binary path configurable.
118-
119-
Current implementation will not work unless the user has xcodeGen located in
120-
`~/.mint/bin/xcodegen`. Should either make it configurable as well as support a
121-
number of paths by default.
122-
*/
123-
let xcodegen_path = dirs::home_dir().unwrap().join(".mint/bin/xcodegen");
124-
// TODO: move xcodegen generate command to it's own module
125-
let xcodegen = tokio::process::Command::new(xcodegen_path)
126-
.current_dir(self.root.clone())
127-
.stdout(std::process::Stdio::null())
128-
.arg("generate")
129-
.spawn()
130-
.expect("Failed to start xcodeGen.")
131-
.wait()
132-
.await
133-
.expect("Failed to run xcodeGen.");
134-
135-
if xcodegen.success() {
136-
tracing::info!("Updated {}.xcodeproj", self.name());
137-
if update_config {
138-
tracing::debug!("Updated internal state.{}.project", self.name());
139-
let path = self.xcodegen_config_path();
140-
self.project = Project::new_from_project_yml(self.root.clone(), path).await?;
123+
match xcodegen::generate(&self.root).await {
124+
Ok(msg) => {
125+
tracing::info!("Updated {}.xcodeproj", self.name());
126+
tracing::trace!("{:?}", msg);
127+
if update_config {
128+
tracing::info!("Updated internal state.{}.project", self.name());
129+
self.project = Project::new_from_project_yml(
130+
self.root.clone(),
131+
xcodegen::config_path(self),
132+
)
133+
.await?;
134+
}
135+
Ok(())
136+
}
137+
Err(e) => {
138+
tracing::error!("{:?}", e);
139+
Err(e)
141140
}
142141
}
143-
144-
Ok(())
145142
}
146143

147-
/// TODO: move xcodegen related code to it's own module
148-
/// Checks whether current workspace is xcodegen project.
149-
pub fn is_xcodegen_project(&self) -> bool {
150-
self.xcodegen_config_path().exists()
151-
}
152-
153-
pub fn xcodegen_config_path(&self) -> PathBuf {
154-
/*
155-
TODO: support otherways to identify xcodegen project
156-
157-
Some would have xcodegen config as json file or
158-
have different location to where they store xcodegen project config.
159-
*/
160-
self.root.join("project.yml")
161-
}
162144
pub fn get_ignore_patterns(&self) -> Option<Vec<String>> {
163-
if self.is_xcodegen_project() {
145+
if crate::xcodegen::is_workspace(self) {
164146
return Some(self.project.config().ignore.clone());
165147
}
166148
return None;

src/util/watch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ fn new(state: crate::SharedState, root: String) -> tokio::task::JoinHandle<anyho
182182

183183
match state.lock().await.workspaces.get_mut(&root) {
184184
Some(w) => {
185-
w.on_dirctory_change(path, event.kind).await?;
185+
w.on_directory_change(path, event.kind).await?;
186186
}
187187
// NOTE: should stop watch here
188188
None => continue,

src/xcodegen/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::fmt::Debug;
2+
use std::path::{Path, PathBuf};
3+
4+
use anyhow::Result;
5+
6+
use crate::Workspace;
7+
8+
/*
9+
FIXME: make xCodeGen binary path configurable.
10+
11+
Current implementation will not work unless the user has xcodeGen located in
12+
`~/.mint/bin/xcodegen`. Should either make it configurable as well as support a
13+
number of paths by default.
14+
*/
15+
lazy_static::lazy_static! {
16+
static ref XCODEGEN: PathBuf = dirs::home_dir().unwrap().join(".mint/bin/xcodegen");
17+
}
18+
19+
#[inline]
20+
fn xcodgen() -> tokio::process::Command {
21+
tokio::process::Command::new(&*XCODEGEN)
22+
}
23+
24+
// Run xcodgen generate
25+
pub async fn generate<P: AsRef<Path> + Debug>(root: P) -> Result<Vec<String>> {
26+
let output = xcodgen()
27+
.current_dir(root)
28+
.stdout(std::process::Stdio::piped())
29+
.stderr(std::process::Stdio::piped())
30+
.arg("generate")
31+
.spawn()
32+
.expect("Failed to start xcodeGen.")
33+
.wait_with_output()
34+
.await
35+
.expect("Failed to run xcodeGen.");
36+
37+
if output.status.code().unwrap().ne(&0) {
38+
anyhow::bail!("{:#?}", output.stderr)
39+
} else {
40+
Ok(String::from_utf8(output.stdout)?
41+
.split("\n")
42+
.map(|s| s.to_string())
43+
.collect())
44+
}
45+
}
46+
47+
// NOTE: passing workspace in-case in the future we would allow configurability of project.yml path
48+
pub fn config_path(ws: &Workspace) -> PathBuf {
49+
/*
50+
TODO: support otherways to identify xcodegen project
51+
52+
Some would have xcodegen config as json file or
53+
have different location to where they store xcodegen project config.
54+
*/
55+
ws.root.join("project.yml")
56+
}
57+
58+
/// Checks whether current workspace is xcodegen project.
59+
pub fn is_workspace(ws: &Workspace) -> bool {
60+
crate::xcodegen::config_path(ws).exists()
61+
}

0 commit comments

Comments
 (0)