Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing installations #1554

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions qlty-check/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ pub trait RuntimeTool: Tool {
fn package_tool(&self, name: &str, plugin: &PluginDef) -> Box<dyn Tool>;
}

fn finalize_installation_from_cmd_result(
pub fn finalize_installation_from_cmd_result(
tool: &dyn Tool,
result: &std::io::Result<Output>,
installation: &mut Installation,
Expand All @@ -696,9 +696,7 @@ fn finalize_installation_from_cmd_result(
log_file.write_all(installation.stderr.clone().unwrap_or_default().as_bytes())?;

installation.finished_at = Some(Utc::now().into());
if let Err(err) = write_to_file(installation) {
error!("Error writing debug data: {}", err);
}
write_to_file(installation);

Ok(())
}
Expand Down
6 changes: 1 addition & 5 deletions qlty-check/src/tool/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::io::Cursor;
use std::path::{Path, PathBuf};
use tar::Archive;
use tempfile::tempfile;
use tracing::error;
use tracing::{info, trace, warn};
use zip::ZipArchive;

Expand Down Expand Up @@ -420,10 +419,7 @@ fn finalize_installation_from_download_result(
installation.download_success = Some(false);
}
installation.finished_at = Some(Utc::now().into());

if let Err(err) = write_to_file(installation) {
error!("Error writing debug data: {}", err);
}
write_to_file(installation);

Ok(())
}
6 changes: 2 additions & 4 deletions qlty-check/src/tool/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use qlty_config::config::{Cpu, DownloadDef, OperatingSystem, PluginDef, ReleaseD
use qlty_config::version::QLTY_VERSION;
use qlty_types::analysis::v1::Installation;
use sha2::Digest;
use tracing::{debug, error, info, trace};
use tracing::{debug, info, trace};

const GITHUB_API_VERSION: &str = "2022-11-28";
const USER_AGENT_PREFIX: &str = "qlty-check";
Expand Down Expand Up @@ -425,9 +425,7 @@ fn finalize_installation_from_assets_fetch(
}
installation.finished_at = Some(Utc::now().into());

if let Err(err) = write_to_file(installation) {
error!("Error writing debug data: {}", err);
}
write_to_file(installation);
}

#[cfg(test)]
Expand Down
8 changes: 7 additions & 1 deletion qlty-check/src/tool/installations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ pub fn initialize_installation(tool: &dyn Tool) -> Installation {
}
}

pub fn write_to_file(installation: &Installation) -> Result<()> {
pub fn write_to_file(installation: &Installation) {
if let Err(err) = write_to_file_impl(installation) {
error!("Error writing debug data: {}", err);
}
}

fn write_to_file_impl(installation: &Installation) -> Result<()> {
let installation_id = generate_random_id(6);
let installation_files_directory = PathBuf::from(&installation.directory);
if let Err(err) = create_dir_all(&installation_files_directory) {
Expand Down
18 changes: 12 additions & 6 deletions qlty-check/src/tool/php.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod composer;
use super::command_builder::{default_command_builder, CommandBuilder};
use super::installations::initialize_installation;
use super::runnable_archive::RunnableArchive;
use super::Tool;
use super::ToolType;
use crate::tool::RuntimeTool;
use crate::tool::{finalize_installation_from_cmd_result, RuntimeTool};
use crate::ui::{ProgressBar, ProgressTask};
use anyhow::Context;
use anyhow::Result;
Expand Down Expand Up @@ -44,7 +45,7 @@ impl Tool for Php {

fn install(&self, task: &ProgressTask) -> Result<()> {
task.set_message("Verifying Php installation");
Php::verify_installation(self.env())?;
self.verify_installation(self.env())?;

task.set_message("Installing composer");
let composer = Composer {
Expand Down Expand Up @@ -75,15 +76,20 @@ impl Tool for Php {
}

impl Php {
fn verify_installation(env: HashMap<String, String>) -> Result<()> {
fn verify_installation(&self, env: HashMap<String, String>) -> Result<()> {
let cmd = cmd!("php", "--version")
.full_env(env)
.stderr_to_stdout()
.stdout_capture();

debug!("{:?}", cmd);
cmd.run()
.with_context(|| "Ensure `php` is installed and in $PATH")?;
let script = format!("{:?}", cmd);
debug!(script);

let mut installation = initialize_installation(self);
let result = cmd.run();
finalize_installation_from_cmd_result(self, &result, &mut installation, script).ok();

result.with_context(|| "Ensure `php` is installed and in $PATH")?;

Ok(())
}
Expand Down
21 changes: 17 additions & 4 deletions qlty-check/src/tool/php/composer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::tool::command_builder::CommandBuilder;
use crate::tool::finalize_installation_from_cmd_result;
use crate::tool::installations::initialize_installation;
use crate::tool::node::package_json::PackageJson;
use crate::ui::ProgressBar;
use crate::{tool::ToolType, ui::ProgressTask, Tool};
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use itertools::Itertools;
use qlty_analysis::utils::fs::path_to_native_string;
use serde_json::Value;
Expand Down Expand Up @@ -100,10 +102,21 @@ impl Composer {
)
.dir(install_dir)
.full_env(self.env())
.stderr_to_stdout()
.stdout_file(php_package.install_log_file()?);
.stderr_capture()
.stdout_capture()
.unchecked(); // Capture output for debugging

cmd.run()?;
let script = format!("{:?}", cmd);
debug!(script);

let mut installation = initialize_installation(php_package);
let result = cmd.run();
let _ =
finalize_installation_from_cmd_result(php_package, &result, &mut installation, script);

if result?.status.code() != Some(0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new behavior in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes previously we were just writing the cmd output to the log file here, but now that we want to capture that into installation including what happens in an error (non-zero exit code) scenario, so using unchecked() in the command expression above so it won't bail out prematurely on a non-zero exit code and we have the stderr and stdout of the command captured into the installation file.

bail!("Failed to install composer package file");
}

Ok(())
}
Expand Down
14 changes: 11 additions & 3 deletions qlty-check/src/tool/ruby/gemfile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::RubygemsPackage;
use crate::tool::finalize_installation_from_cmd_result;
use crate::tool::installations::initialize_installation;
use crate::ui::ProgressBar;
use crate::{tool::Tool, ui::ProgressTask};
use anyhow::{bail, Context, Result};
Expand Down Expand Up @@ -27,9 +29,15 @@ impl RubyGemfile {
.stdout_capture()
.unchecked();

let list_output: std::process::Output = list_command
.run()
.with_context(|| "Failed to run: ruby -S bundle list")?;
let script = format!("{:?}", list_command);
debug!(script);

let mut installation = initialize_installation(self);
let result = list_command.run();
let _ = finalize_installation_from_cmd_result(self, &result, &mut installation, script);

let list_output: std::process::Output =
result.with_context(|| "Failed to run: ruby -S bundle list")?;

let stdout = String::from_utf8_lossy(&list_output.stdout).to_string();
let stderr = String::from_utf8_lossy(&list_output.stderr).to_string();
Expand Down
35 changes: 33 additions & 2 deletions qlty-check/src/tool/ruby/sys/linux.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::{
tool::ruby::PlatformRuby,
tool::{
installations::{initialize_installation, write_to_file},
ruby::PlatformRuby,
},
ui::{ProgressBar, ProgressTask},
Tool,
};
use anyhow::{bail, Result};
use ar::Entry;
use chrono::Utc;
use qlty_analysis::{join_path_string, utils::fs::path_to_string};
use qlty_types::analysis::v1::Installation;
use std::{
collections::HashMap,
io::{BufReader, Cursor, Read},
Expand Down Expand Up @@ -99,7 +104,15 @@ impl RubyLinux {
"https://ftp.debian.org/debian/pool/main/{}_{}.deb",
package, ARCH
);
match ureq::get(url.as_str()).call() {
let mut installation = initialize_installation(tool);
installation.download_url = Some(url.to_string());
installation.download_file_type = Some(".deb".to_string());
installation.download_binary_name = Some(package.to_string());

let result = ureq::get(url.as_str()).call();
Self::finalize_installation(&mut installation, &result);

match result {
Ok(response) => {
self.extract_dependency_deb_archive(
response.into_reader(),
Expand All @@ -116,6 +129,24 @@ impl RubyLinux {
Ok(())
}

fn finalize_installation(
installation: &mut Installation,
result: &std::result::Result<ureq::Response, ureq::Error>,
) {
installation.finished_at = Some(Utc::now().into());
match result {
Ok(_) => {
installation.download_success = Some(true);
}
Err(err) => {
installation.download_success = Some(false);
installation.stderr = Some(format!("{:?}", err));
}
}

write_to_file(installation);
}

fn extract_dependency_deb_archive(
&self,
reader: impl Read,
Expand Down
15 changes: 13 additions & 2 deletions qlty-check/src/tool/ruby/sys/macos.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
tool::{command_builder::CommandBuilder, ruby::PlatformRuby},
tool::{
command_builder::CommandBuilder, finalize_installation_from_cmd_result,
installations::initialize_installation, ruby::PlatformRuby,
},
ui::{ProgressBar, ProgressTask},
Tool,
};
Expand Down Expand Up @@ -56,7 +59,15 @@ impl PlatformRuby for RubyMacos {
.full_env(tool.env());

debug!("Running: {:?}", cmd);
cmd.run()?;

let script = format!("{:?}", cmd);
debug!(script);

let mut installation = initialize_installation(tool);
let result = cmd.run();
let _ = finalize_installation_from_cmd_result(tool, &result, &mut installation, script);

result?;

Ok(())
}
Expand Down
17 changes: 12 additions & 5 deletions qlty-check/src/tool/ruby/sys/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
tool::{command_builder::Command, download::Download, ruby::PlatformRuby},
tool::{
command_builder::Command, download::Download, finalize_installation_from_cmd_result,
installations::initialize_installation, ruby::PlatformRuby,
},
ui::{ProgressBar, ProgressTask},
Tool,
};
Expand Down Expand Up @@ -64,10 +67,14 @@ impl RubyWindows {
.stderr_to_stdout()
.stdout_capture();

debug!("Verify system Ruby: {:?}", cmd);
let output = cmd
.run()
.with_context(|| "Ensure `ruby` is installed and in $PATH")?;
let script = format!("{:?}", cmd);
debug!("Verify system Ruby: {:?}", script);

let mut installation = initialize_installation(tool);
let result = cmd.run();
let _ = finalize_installation_from_cmd_result(tool, &result, &mut installation, script);

let output = result.with_context(|| "Ensure `ruby` is installed and in $PATH")?;
debug!("Verified system ruby: {:?}", output);

Ok(())
Expand Down
Loading