|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Performs a build using a provided black-box build command, which ought to |
| 12 | +//! return a list of save-analysis JSON files to be reloaded by the RLS. |
| 13 | +//! Please note that since the command is ran externally (at a file/OS level) |
| 14 | +//! this doesn't work with files that are not saved. |
| 15 | +
|
| 16 | +use std::io::Read; |
| 17 | +use std::fs::File; |
| 18 | +use std::path::{Path, PathBuf}; |
| 19 | +use std::process::{Command, Stdio}; |
| 20 | + |
| 21 | +use super::BuildResult; |
| 22 | + |
| 23 | +use rls_data::Analysis; |
| 24 | +use log::{log, trace}; |
| 25 | + |
| 26 | +/// Performs a build using an external command and interprets the results. |
| 27 | +/// The command should output on stdout a list of save-analysis .json files |
| 28 | +/// to be reloaded by the RLS. |
| 29 | +/// Note: This is *very* experimental and preliminary - this can viewed as |
| 30 | +/// an experimentation until a more complete solution emerges. |
| 31 | +pub(super) fn build_with_external_cmd<S: AsRef<str>>(path: S, build_dir: PathBuf) -> BuildResult { |
| 32 | + let path = path.as_ref(); |
| 33 | + let (cmd, args) = { |
| 34 | + let mut words = path.split_whitespace(); |
| 35 | + let cmd = match words.next() { |
| 36 | + Some(cmd) => cmd, |
| 37 | + None => { |
| 38 | + return BuildResult::Err("Specified build_command is empty".into(), None); |
| 39 | + } |
| 40 | + }; |
| 41 | + (cmd, words) |
| 42 | + }; |
| 43 | + let spawned = Command::new(&cmd) |
| 44 | + .args(args) |
| 45 | + .current_dir(&build_dir) |
| 46 | + .stdout(Stdio::piped()) |
| 47 | + .stderr(Stdio::null()) |
| 48 | + .spawn(); |
| 49 | + |
| 50 | + let child = match spawned { |
| 51 | + Ok(child) => child, |
| 52 | + Err(io) => { |
| 53 | + let err_msg = format!("Couldn't execute: {} ({:?})", path, io.kind()); |
| 54 | + trace!("{}", err_msg); |
| 55 | + return BuildResult::Err(err_msg, Some(path.to_owned())); |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + // TODO: Timeout? |
| 60 | + let reader = std::io::BufReader::new(child.stdout.unwrap()); |
| 61 | + use std::io::BufRead; |
| 62 | + |
| 63 | + let files = reader.lines().filter_map(|res| res.ok()) |
| 64 | + .map(PathBuf::from) |
| 65 | + // Relative paths are relative to build command, not RLS itself (cwd may be different) |
| 66 | + .map(|path| if !path.is_absolute() { build_dir.join(path) } else { path }); |
| 67 | + |
| 68 | + let analyses = match read_analysis_files(files) { |
| 69 | + Ok(analyses) => analyses, |
| 70 | + Err(cause) => { |
| 71 | + let err_msg = format!("Couldn't read analysis data: {}", cause); |
| 72 | + return BuildResult::Err(err_msg, Some(path.to_owned())); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + BuildResult::Success(build_dir.clone(), vec![], analyses, false) |
| 77 | +} |
| 78 | + |
| 79 | +/// Reads and deserializes given save-analysis JSON files into corresponding |
| 80 | +/// `rls_data::Analysis` for each file. If an error is encountered, a `String` |
| 81 | +/// with the error message is returned. |
| 82 | +fn read_analysis_files<I>(files: I) -> Result<Vec<Analysis>, String> |
| 83 | +where |
| 84 | + I: Iterator, |
| 85 | + I::Item: AsRef<Path> |
| 86 | +{ |
| 87 | + let mut analyses = Vec::new(); |
| 88 | + |
| 89 | + for path in files { |
| 90 | + trace!("external::read_analysis_files: Attempt to read `{}`", path.as_ref().display()); |
| 91 | + |
| 92 | + let mut file = File::open(path).map_err(|e| e.to_string())?; |
| 93 | + let mut contents = String::new(); |
| 94 | + file.read_to_string(&mut contents).map_err(|e| e.to_string())?; |
| 95 | + |
| 96 | + let data: Analysis = serde_json::from_str(&contents).map_err(|e| e.to_string())?; |
| 97 | + analyses.push(data); |
| 98 | + } |
| 99 | + |
| 100 | + Ok(analyses) |
| 101 | +} |
0 commit comments