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

Cleanup #5

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
1,431 changes: 595 additions & 836 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions builder/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! Implementation of the package building procedure.

use crate::desc::BuildDescriptor;
use crate::WORK_DIR;
use crate::{desc::BuildDescriptor, WORK_DIR};
use anyhow::Result;
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs;
use std::fs::File;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::str;
use std::sync::Arc;
use common::serde_json;
use flate2::{write::GzEncoder, Compression};
use std::{
fs,
fs::File,
io,
path::{Path, PathBuf},
process::Command,
str,
sync::Arc,
};

/// A build process is the operation of converting source code into an installable package.
///
Expand All @@ -38,7 +38,8 @@ impl BuildProcess {
/// `input_path` is the path to the directory containing information to build the package.
pub fn new(input_path: PathBuf) -> io::Result<Self> {
let build_desc_path = input_path.join("package.json");
let build_desc = common::util::read_json::<BuildDescriptor>(&build_desc_path)?;
let build_desc = fs::read_to_string(build_desc_path)?;
let build_desc = serde_json::from_str(&build_desc)?;
Ok(Self {
input_path,
build_desc,
Expand Down Expand Up @@ -98,12 +99,9 @@ impl BuildProcess {
.env("HOST", host)
.env("TARGET", target)
.env("SYSROOT", &self.sysroot)
.env("PKG_NAME", self.build_desc.package.get_name())
.env(
"PKG_VERSION",
self.build_desc.package.get_version().to_string(),
)
.env("PKG_DESC", self.build_desc.package.get_description())
.env("PKG_NAME", &self.build_desc.package.name)
.env("PKG_VERSION", self.build_desc.package.version.to_string())
.env("PKG_DESC", &self.build_desc.package.description)
.env("JOBS", jobs.to_string())
.current_dir(&self.build_dir)
.status()
Expand Down
12 changes: 6 additions & 6 deletions builder/src/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

use anyhow::Result;
use common::package::Package;
use serde::Deserialize;
use serde::Serialize;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use std::{
fs,
path::{Path, PathBuf},
};

// TODO add an option to allow fetching a tarball without decompressing it?

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Source {
let (path, _) = common::util::create_tmp_file(WORK_DIR)?;
let mut download_task = DownloadTask::new(url, &path).await?;
// TODO progress bar
while download_task.next().await? {}
while download_task.next().await? > 0 {}
// TODO check integrity with hash if specified
common::util::decompress(&path, &dest_path)?;
// TODO remove archive?
Expand Down
50 changes: 28 additions & 22 deletions builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ mod build;
mod desc;
mod util;

use crate::build::BuildProcess;
use crate::util::{get_build_triplet, get_jobs_count};
use anyhow::Result;
use anyhow::{anyhow, bail};
use common::repository::Repository;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::exit;
use std::str;
use crate::{
build::BuildProcess,
util::{get_build_triplet, get_jobs_count},
};
use anyhow::{anyhow, bail, Result};
use common::{repository::Repository, serde_json};
use std::{env, fs, io, path::PathBuf, process::exit, str};
use tokio::runtime::Runtime;

/// The path to the work directory.
Expand Down Expand Up @@ -44,6 +41,25 @@ fn print_usage(bin: &str) {
eprintln!("All environment variable are optional");
}

/// Prepares the repository's directory for the package.
///
/// On success, the function returns the output archive path.
fn prepare(build_process: &BuildProcess, to: PathBuf) -> io::Result<PathBuf> {
// Create directory
let build_desc = build_process.get_build_desc();
let name = &build_desc.package.name;
let version = &build_desc.package.version;
let package_path = to.join(name).join(version.to_string());
fs::create_dir_all(&package_path)?;
// Create descriptor
let desc_path = package_path.join("desc");
let desc = serde_json::to_string(&build_desc.package)?;
fs::write(desc_path, desc)?;
// Get archive path
let repo = Repository::load(to)?;
Ok(repo.get_archive_path(name, version))
}

/// Builds the package.
///
/// `from` and `to` correspond to the command line arguments.
Expand Down Expand Up @@ -73,18 +89,8 @@ fn build(from: PathBuf, to: PathBuf) -> Result<()> {
bail!("Package build failed!");
}
println!("[INFO] Prepare repository at `{}`...", to.display());
// TODO Move to separate function
let archive_path = {
let build_desc = build_process.get_build_desc();
let name = build_desc.package.get_name();
let version = build_desc.package.get_version();
let package_path = to.join(name).join(version.to_string());
fs::create_dir_all(&package_path)?;
let desc_path = package_path.join("desc");
common::util::write_json(&desc_path, &build_desc.package)?;
let repo = Repository::load(to)?;
repo.get_archive_path(name, version)
};
let archive_path = prepare(&build_process, to)
.map_err(|e| anyhow!("Failed to prepare directory for package: {e}"))?;
println!("[INFO] Create archive...");
build_process
.create_archive(&archive_path)
Expand Down
5 changes: 1 addition & 4 deletions builder/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

use anyhow::{anyhow, Result};
use core::str;
use std::ffi::OsStr;
use std::num::NonZeroUsize;
use std::process::Command;
use std::{env, io, thread};
use std::{env, ffi::OsStr, io, num::NonZeroUsize, process::Command, thread};

/// Default build triplet in case it cannot be retrieved.
const DEFAULT_BUILD_TRIPLET: &str = "x86_64-linux-gnu";
Expand Down
2 changes: 0 additions & 2 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.87"
common = { path = "../common" }
tokio = { version = "1.40.0", features = ["macros", "rt", "rt-multi-thread"] }

[features]
default = []
Expand Down
41 changes: 12 additions & 29 deletions client/src/confirm.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
//! This module implements a confirmation prompt.
//! Confirmation prompt.

use std::io;
use std::io::BufRead;
use std::io::Write;
use common::maestro_utils;

/// Asks for confirmation. If yes, true is returned. Else, false is returned.
pub fn prompt() -> bool {
let mut first = true;
let stdin = io::stdin();
let mut response;

loop {
if first {
print!("Confirm? [y/n] ");
} else {
print!("Please type `y` or `n`. ");
}

let _ = io::stdout().flush();

// Waiting for an input line
let input = stdin.lock().lines().next().unwrap().unwrap();
response = input == "y";

// If the input is correct, stop asking
if input == "y" || input == "n" {
break;
let Some(input) = maestro_utils::prompt::prompt(Some("Confirm? [Y/n] "), false) else {
// Input closed, abort
break false;
};
let input = input.trim().to_lowercase();
match input.as_str() {
"" | "y" | "ye" | "yes" => break true,
"n" | "no" => break false,
// Retry
_ => {}
}

first = false;
}

println!();

response
}
Loading