Skip to content

Commit

Permalink
ref(compile): use Pathbuf instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 26, 2022
1 parent 438cf5a commit ec35b61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
15 changes: 7 additions & 8 deletions src/compile/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::path::PathBuf;

use super::CompileFlags;

// TODO: using PathBuf
/// Single Compilation Database Command Representation
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -19,22 +18,22 @@ pub struct CompilationCommand {
pub name: Option<String>,
/// The path of the main file for the compilation, which may be relative to `directory`.
#[serde(skip_serializing_if = "Option::is_none")]
pub file: Option<String>,
pub file: Option<PathBuf>,
/// The working directory for the compilation
pub directory: String,
/// The compile command, this is alias with commandLine or split form of command
pub command: String,
/// Source code files.
#[serde(skip_serializing_if = "Option::is_none")]
pub files: Option<Vec<String>>,
pub files: Option<Vec<PathBuf>>,
/// For SwiftFileList
pub file_lists: Vec<String>,
pub file_lists: Vec<PathBuf>,
/// The name of the build output
#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<String>,
/// Index store path. Kept for the caller to further process.
#[serde(skip)]
pub index_store_path: Option<String>,
pub index_store_path: Option<PathBuf>,
}

impl CompilationCommand {
Expand All @@ -58,7 +57,7 @@ impl CompilationCommand {

// Swift File Lists
self.file_lists.iter().for_each(|path| {
match fs::get_files_list(&PathBuf::from(path.as_str())) {
match fs::get_files_list(&path) {
Ok(file_list) => {
file_list.into_iter().for_each(|file_path: PathBuf| {
info.insert(file_path, flags.clone());
Expand All @@ -71,14 +70,14 @@ impl CompilationCommand {
// Swift Module Files
self.files.as_ref().map(|files| {
files.iter().for_each(|file| {
info.insert(PathBuf::from(file), flags.clone());
info.insert(file.clone(), flags.clone());
})
});

// Single File Command
self.file
.as_ref()
.map(|file| info.insert(PathBuf::from(file), flags.clone()));
.map(|file| info.insert(file.clone(), flags.clone()));

Ok(info)
}
Expand Down
18 changes: 9 additions & 9 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::Result;
pub use command::CompilationCommand;
pub use flags::CompileFlags;
use serde::{Deserialize, Serialize};
use std::path;
use std::path::{Path, PathBuf};
use tap::Pipe;
use xcodebuild::parser::Step;

Expand Down Expand Up @@ -43,7 +43,7 @@ impl IntoIterator for CompilationDatabase {
/// ```no_run use xbase::compile::CompilationDatabase;
/// CompilationDatabase::from_file("/path/to/xcode_build_logs");
/// ```
pub fn parse_from_file<P: AsRef<path::Path> + Clone>(path: P) -> Result<CompilationDatabase> {
pub fn parse_from_file<P: AsRef<Path> + Clone>(path: P) -> Result<CompilationDatabase> {
std::fs::read_to_string(path)?
.pipe_ref(|s| serde_json::from_str::<CompilationDatabase>(s))?
.pipe(Ok)
Expand Down Expand Up @@ -89,11 +89,12 @@ pub async fn generate_from_steps(steps: &Vec<Step>) -> Result<CompilationDatabas
if val == "-module-name" {
name = Some(arguments[i + 1].to_owned());
} else if val == "-index-store-path" {
index_store_path = Some(arguments[i + 1].to_owned());
let ref val = arguments[i + 1];
index_store_path = Some(val.pipe(PathBuf::from));
} else if val.ends_with(".swift") {
files.push(val.to_owned());
files.push(PathBuf::from(val));
} else if val.ends_with(".SwiftFileList") {
file_lists.push(val.replace("@", "").to_owned());
file_lists.push(val.replace("@", "").pipe(PathBuf::from));
}
}
if let Some(ref index_store_path) = index_store_path {
Expand Down Expand Up @@ -127,7 +128,6 @@ use {
xcodegen,
},
process_stream::StreamExt,
std::path::PathBuf,
tokio::{
fs::{metadata, File},
io::AsyncWriteExt,
Expand All @@ -136,7 +136,7 @@ use {
};

#[cfg(feature = "daemon")]
pub async fn update_compilation_file(root: &path::PathBuf) -> Result<()> {
pub async fn update_compilation_file(root: &PathBuf) -> Result<()> {
// TODO(build): Ensure that build successed. check for Exit Code
let steps = fresh_build(&root).await?.collect::<Vec<Step>>().await;
let compile_commands = steps.pipe_ref(generate_from_steps).await?;
Expand All @@ -153,7 +153,7 @@ pub async fn update_compilation_file(root: &path::PathBuf) -> Result<()> {

/// Ensure that buildServer.json exists in root directory.
#[cfg(feature = "daemon")]
pub async fn ensure_server_config(root: &path::PathBuf) -> Result<()> {
pub async fn ensure_server_config(root: &PathBuf) -> Result<()> {
use crate::constants::SERVER_BINARY_PATH;

let path = root.join("buildServer.json");
Expand Down Expand Up @@ -197,7 +197,7 @@ pub async fn ensure_server_support<'a>(
let compile_path = root.join(".compile");
let compile_exists = metadata(compile_path).await.is_ok();

if ensure_server_config(&root).await.is_err() {
if ensure_server_config(root).await.is_err() {
"fail to ensure build server configuration!"
.pipe(|msg| state.clients.echo_err(root, name, msg))
.await;
Expand Down

0 comments on commit ec35b61

Please sign in to comment.