Skip to content

Commit

Permalink
docs: add missing doc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed Apr 26, 2022
1 parent 96059c4 commit 315f62d
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/daemon.rs
@@ -1,3 +1,4 @@
//! Handle requests from neovim and manage dev workflow
use anyhow::{bail, Context, Result};
mod command;
pub mod state;
Expand All @@ -11,6 +12,7 @@ pub const DAEMON_SOCKET_PATH: &str = "/tmp/xcodebase-daemon.socket";
pub const DAEMON_BINARY: &str =
"/Users/tami5/repos/neovim/xcodebase.nvim/target/debug/xcodebase-daemon";

/// Representation of daemon
pub struct Daemon {
#[cfg(feature = "daemon")]
pub state: std::sync::Arc<tokio::sync::Mutex<state::DaemonStateData>>,
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/command.rs
Expand Up @@ -12,12 +12,14 @@ pub use register::Register;
pub use rename_file::RenameFile;
pub use run::Run;

/// Trait to be implemented by actions to be processable
#[async_trait::async_trait]
#[cfg(feature = "daemon")]
pub trait DaemonCommandExt {
async fn handle(&self, state: crate::daemon::DaemonState) -> Result<()>;
}

/// Daemon Actions
#[derive(Debug)]
pub enum DaemonCommand {
Build(Build),
Expand Down
4 changes: 2 additions & 2 deletions src/daemon/command/build.rs
@@ -1,7 +1,7 @@
use anyhow::Result;

use crate::daemon::Daemon;
use anyhow::Result;

/// Action to build a project.
#[derive(Debug)]
pub struct Build {
pub target: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/command/drop.rs
@@ -1,6 +1,6 @@
use anyhow::Result;

/// Register new client with workspace
/// Drop a client
#[derive(Debug)]
pub struct Drop {
pub pid: i32,
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/command/run.rs
@@ -1,6 +1,6 @@
use anyhow::Result;

/// Rename file + class
/// Action to Run a project.
#[derive(Debug)]
pub struct Run {
_simulator: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/state/workspace.rs
Expand Up @@ -130,7 +130,7 @@ impl Workspace {
tracing::info!("Updating State.{}.Project", self.name());
self.project = Project::new_from_project_yml(
self.root.clone(),
xcodegen::config_path(self),
xcodegen::get_config_path(self),
)
.await?;
}
Expand Down
17 changes: 5 additions & 12 deletions src/lib.rs
@@ -1,18 +1,11 @@
//! Daemon Module: Manage projects and Handle requests from neovim
pub mod daemon;

// Submodules
#[cfg(feature = "xcode")]
pub mod xcode;

#[cfg(feature = "compilation")]
pub mod compile;

#[cfg(feature = "server")]
pub mod server;
#[cfg(feature = "xcode")]
pub mod xcode;
#[cfg(feature = "xcodegen")]
pub mod xcodegen;

// Utilities
pub mod daemon;
pub mod util;

#[cfg(feature = "server")]
pub mod server;
1 change: 1 addition & 0 deletions src/util/fs.rs
@@ -1,3 +1,4 @@
//! Functions to query filesystem for files and directories
use anyhow::Result;
use std::{
fs::read_to_string,
Expand Down
1 change: 1 addition & 0 deletions src/util/mlua.rs
@@ -1,3 +1,4 @@
//! mlua functions and extensions
use mlua::prelude::*;

pub trait LuaExtension {
Expand Down
1 change: 1 addition & 0 deletions src/util/mod.rs
@@ -1,3 +1,4 @@
//! General utilities
#[cfg(any(feature = "server", feature = "daemon"))]
pub mod fs;
#[cfg(feature = "lua")]
Expand Down
1 change: 1 addition & 0 deletions src/util/proc.rs
@@ -1,3 +1,4 @@
//! Wrapping functions for managing processes
pub fn exists(pid: &i32, cb: impl FnOnce()) -> bool {
if libproc::libproc::proc_pid::name(*pid).is_err() {
cb();
Expand Down
1 change: 1 addition & 0 deletions src/util/regex.rs
@@ -1,3 +1,4 @@
//! Functions to match strings
use lazy_static::lazy_static;
use regex::Regex;

Expand Down
1 change: 1 addition & 0 deletions src/util/tracing.rs
@@ -1,3 +1,4 @@
//! Wrapping functions for tracing module
use anyhow::Result;
use std::io;
use tracing::subscriber::set_global_default;
Expand Down
140 changes: 58 additions & 82 deletions src/util/watch.rs
@@ -1,92 +1,14 @@
//! Function to watch file system
//!
//! Mainly used for creation/removal of files and editing of xcodegen config.
use notify::{Error, Event, RecommendedWatcher, RecursiveMode, Watcher};
#[cfg(feature = "daemon")]
use std::{path::Path, time::Duration};
#[cfg(feature = "async")]
use tokio::sync::{mpsc, Mutex};
use wax::{Glob, Pattern};

/// HACK: ignore seen paths.
///
/// Sometiems we get event for the same path, particularly
/// `ModifyKind::Name::Any` is ommited twice for the new path
/// and once for the old path.
///
/// This will compare last_seen with path, updates `last_seen` if not match,
/// else returns true.
#[cfg(feature = "async")]
async fn should_ignore(last_seen: std::sync::Arc<Mutex<String>>, path: &str) -> bool {
// HACK: Always return false for project.yml
let path = path.to_string();
if path.contains("project.yml") {
return false;
}
let mut last_seen = last_seen.lock().await;
if last_seen.to_string() == path {
return true;
} else {
*last_seen = path;
return false;
}
}

// TODO: Stop handle
#[cfg(feature = "daemon")]
pub async fn update(state: crate::daemon::DaemonState, _msg: crate::daemon::DaemonCommand) {
let copy = state.clone();
let mut current_state = copy.lock().await;
let mut watched_roots: Vec<String> = vec![];
let mut start_watching: Vec<String> = vec![];

// TODO: Remove wathcers for workspaces that are no longer exist

for key in current_state.watchers.keys() {
watched_roots.push(key.clone());
}

for key in current_state.workspaces.keys() {
if !watched_roots.contains(key) {
start_watching.push(key.clone());
}
}

for root in start_watching {
let handle = handler(state.clone(), root.clone());
#[cfg(feature = "logging")]
tracing::info!("Watching {root}");
current_state.watchers.insert(root, handle);
}
}

// TODO: Cleanup get_ignore_patterns and decrease duplications
#[cfg(feature = "daemon")]
async fn get_ignore_patterns(state: crate::daemon::DaemonState, root: &String) -> Vec<String> {
let mut patterns: Vec<String> = vec![
"**/.git/**",
"**/*.xcodeproj/**",
"**/.*",
"**/build/**",
"**/buildServer.json",
]
.iter()
.map(|e| e.to_string())
.collect();

// FIXME: Adding extra ignore patterns to `ignore` local config requires restarting daemon.
let extra_patterns = state
.lock()
.await
.workspaces
.get(root)
.unwrap()
.get_ignore_patterns();

if let Some(extra_patterns) = extra_patterns {
patterns.extend(extra_patterns);
}

patterns
}

/// Create new handler to watch workspace root.
#[cfg(feature = "daemon")]
pub fn handler(
state: crate::daemon::DaemonState,
Expand Down Expand Up @@ -215,3 +137,57 @@ pub fn handler(
Ok(())
})
}

/// HACK: ignore seen paths.
///
/// Sometiems we get event for the same path, particularly
/// `ModifyKind::Name::Any` is ommited twice for the new path
/// and once for the old path.
///
/// This will compare last_seen with path, updates `last_seen` if not match,
/// else returns true.
#[cfg(feature = "async")]
async fn should_ignore(last_seen: std::sync::Arc<Mutex<String>>, path: &str) -> bool {
// HACK: Always return false for project.yml
let path = path.to_string();
if path.contains("project.yml") {
return false;
}
let mut last_seen = last_seen.lock().await;
if last_seen.to_string() == path {
return true;
} else {
*last_seen = path;
return false;
}
}

// TODO: Cleanup get_ignore_patterns and decrease duplications
#[cfg(feature = "daemon")]
async fn get_ignore_patterns(state: crate::daemon::DaemonState, root: &String) -> Vec<String> {
let mut patterns: Vec<String> = vec![
"**/.git/**",
"**/*.xcodeproj/**",
"**/.*",
"**/build/**",
"**/buildServer.json",
]
.iter()
.map(|e| e.to_string())
.collect();

// FIXME: Adding extra ignore patterns to `ignore` local config requires restarting daemon.
let extra_patterns = state
.lock()
.await
.workspaces
.get(root)
.unwrap()
.get_ignore_patterns();

if let Some(extra_patterns) = extra_patterns {
patterns.extend(extra_patterns);
}

patterns
}
2 changes: 2 additions & 0 deletions src/xcode.rs
@@ -1,3 +1,4 @@
//! Helper functions to communicate with xcodebuild
use anyhow::Result;
use serde_json::json;
use std::ffi;
Expand Down Expand Up @@ -63,6 +64,7 @@ where
.await
}

/// Ensure that buildServer.json exists in root directory.
pub async fn ensure_server_config_file(root: &PathBuf) -> Result<()> {
let path = root.join("buildServer.json");
if fs::File::open(&path).await.is_ok() {
Expand Down
11 changes: 5 additions & 6 deletions src/xcodegen/mod.rs
@@ -1,11 +1,10 @@
//! Helper functions to communicate with xcodegen
use crate::daemon::state::Workspace;
use anyhow::{Context, Result};
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;

use anyhow::{Context, Result};

use crate::daemon::state::Workspace;

/*
FIXME: make xCodeGen binary path configurable.
Expand Down Expand Up @@ -37,7 +36,7 @@ pub async fn generate<P: AsRef<Path> + Debug>(root: P) -> Result<ExitStatus> {
}

// NOTE: passing workspace in-case in the future we would allow configurability of project.yml path
pub fn config_path(ws: &Workspace) -> PathBuf {
pub fn get_config_path(ws: &Workspace) -> PathBuf {
/*
TODO: support otherways to identify xcodegen project
Expand All @@ -49,5 +48,5 @@ pub fn config_path(ws: &Workspace) -> PathBuf {

/// Checks whether current workspace is xcodegen project.
pub fn is_workspace(ws: &Workspace) -> bool {
crate::xcodegen::config_path(ws).exists()
crate::xcodegen::get_config_path(ws).exists()
}

0 comments on commit 315f62d

Please sign in to comment.