Skip to content

Commit

Permalink
added a way better toml handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-eth committed Aug 5, 2023
1 parent 5d6af5a commit 92a2bea
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 98 deletions.
49 changes: 48 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ tokio = "1.29.1"
tokio-dl-stream-to-disk = "1.0.0"
zip-extract = "0.1.2"
curl = "0.4.44"
reqwest = "0.11.18"
reqwest = "0.11.18"
toml_edit = "0.9.1"
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ The full list of dependencies is available [here](./all_dependencies.toml).

### TODO

- A better way to write to the TOML file.
- Parallel downloads of the dependencies.
- A better way to handle the dependencies.
- Error handling.
Expand Down
10 changes: 5 additions & 5 deletions soldeer.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[remappings]
enabled = true


[dependencies]
"openzeppelin~v4.9.2" = "https://github.com/OpenZeppelin/openzeppelin-contracts/archive/refs/tags/v4.9.2.zip"
"solady~v0.0.107" = "https://github.com/Vectorized/solady/archive/refs/tags/v0.0.107.zip"
"uniswap-v3-periphery~v1.0.0" = "https://github.com/Uniswap/v3-periphery/archive/refs/tags/v1.0.0.zip"
"solady~v0.0.49" = "https://github.com/Vectorized/solady/archive/refs/tags/v0.0.49.zip"
"openzeppelin~v1.0.5" = "https://github.com/OpenZeppelin/openzeppelin-contracts/archive/refs/tags/v1.0.5.zip"

[remappings]
enabled = true
98 changes: 40 additions & 58 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::fs;
use std::path::PathBuf;
use std::path::Path;
use std::fs::{ self, File };
use std::path::{ PathBuf, Path };
use std::process::exit;
use serde_derive::Deserialize;
use toml;
use toml::Table;
use std::io::Write;
use std::fs::File;
use std::io::{ BufRead, BufReader };
use toml::{ self, Table };
use std::io::{ Write, BufRead, BufReader };
use crate::utils::get_current_working_dir;
extern crate toml_edit;
use toml_edit::{ Document, value };

// TODO need to improve this, to propagate the error to main and not exit here.
pub fn read_config(filename: String) -> Vec<Dependency> {
Expand Down Expand Up @@ -80,36 +78,26 @@ pub fn define_config_file() -> String {
pub fn add_to_config(dependency_name: &str, dependency_version: &str, dependency_url: &str) {
println!("Adding dependency {}-{} to config file", dependency_name, dependency_version);
let filename: String = define_config_file();
let dependencies: Vec<Dependency> = read_config(filename.clone());
let mut dependency_exists: bool = false;
for dependency in dependencies.iter() {
if dependency.name == dependency_name && dependency.version == dependency_version {
dependency_exists = true;
}
}
if dependency_exists {
let contents = read_file_to_string(filename.clone());
let mut doc: Document = contents.parse::<Document>().expect("invalid doc");

if !doc["dependencies"].get(format!("{}~{}", dependency_name, dependency_version)).is_none() {
println!(
"Dependency {}-{} already exists in the config file",
dependency_name,
dependency_version
);
return;
}
doc["dependencies"][format!("{}~{}", dependency_name, dependency_version)] =
value(dependency_url);
let mut file: std::fs::File = fs::OpenOptions
::new()
.write(true)
.append(true)
.append(false)
.open(filename)
.unwrap();
if
let Err(e) = writeln!(
file,
"\n\"{}~{}\" = \"{}\"",
dependency_name,
dependency_version,
dependency_url
)
{
if let Err(e) = write!(file, "{}", doc.to_string()) {
eprintln!("Couldn't write to file: {}", e);
}
}
Expand All @@ -122,26 +110,12 @@ pub fn remappings() {
}

fn update_foundry() {
//TODO need to create the remappings file if it does not exists.
if !Path::new("remappings.txt").exists() {
File::create("remappings.txt").unwrap();
}
println!("Updating foundry...");
// Read the contents of the file using a `match` block
// to return the `data: Ok(c)` as a `String`
// or handle any `errors: Err(_)`.
let contents: String = match fs::read_to_string("remappings.txt") {
// If successful return the files text as `contents`.
// `c` is a local variable.
Ok(c) => c,
// Handle the `error` case.
Err(_) => {
// Write `msg` to `stderr`.
eprintln!("Could not read file `{}`", "remappings.txt");
// Exit the program with exit code `1`.
exit(1);
}
};
let contents = read_file_to_string(String::from("remappings.txt"));

let existing_remappings: Vec<String> = contents
.split("\n")
.map(|s| s.to_string())
Expand All @@ -160,7 +134,12 @@ fn update_foundry() {
if index.is_none() {
println!("Adding a new remap {}", &dependency.name);
new_remappings.push_str(
&format!("{}=dependencies/{}-{}\n", &dependency.name, &dependency.name, &dependency.version)
&format!(
"{}=dependencies/{}-{}\n",
&dependency.name,
&dependency.name,
&dependency.version
)
);
}
});
Expand Down Expand Up @@ -225,21 +204,8 @@ fn remove_empty_lines(filename: String) {

fn enable_remappings() -> bool {
let filename = define_config_file();
// Read the contents of the file using a `match` block
// to return the `data: Ok(c)` as a `String`
// or handle any `errors: Err(_)`.
let contents: String = match fs::read_to_string(&filename) {
// If successful return the files text as `contents`.
// `c` is a local variable.
Ok(c) => c,
// Handle the `error` case.
Err(_) => {
// Write `msg` to `stderr`.
eprintln!("Could not read file `{}`", &filename);
// Exit the program with exit code `1`.
exit(1);
}
};

let contents: String = read_file_to_string(filename.clone());

// Use a `match` block to return the
// file `contents` as a `Data struct: Ok(d)`
Expand All @@ -259,6 +225,22 @@ fn enable_remappings() -> bool {
};
return data.remappings.get("enabled").unwrap().as_bool().unwrap();
}

fn read_file_to_string(filename: String) -> String {
let contents: String = match fs::read_to_string(&filename) {
// If successful return the files text as `contents`.
// `c` is a local variable.
Ok(c) => c,
// Handle the `error` case.
Err(_) => {
// Write `msg` to `stderr`.
eprintln!("Could not read file `{}`", &filename);
// Exit the program with exit code `1`.
exit(1);
}
};
return contents;
}
// Top level struct to hold the TOML data.
#[derive(Deserialize)]
#[derive(Debug)]
Expand Down
23 changes: 7 additions & 16 deletions src/dependency_downloader.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
use std::path::Path;
use std::io::Cursor;
use std::io::Read;
use std::io::BufReader;
use std::fs::File;
use std::io::{ Cursor, Read, BufReader };
use reqwest::{ get, Response };
use std::fs::{ self, remove_file, File };
use std::fmt;
use tokio_dl_stream_to_disk::AsyncDownload;
use zip_extract::ZipExtractError;
use reqwest::get;
use reqwest::Response;
use std::fs;
use std::fs::remove_file;

use crate::config::Dependency;
use crate::config::read_config;
use crate::config::{ Dependency, read_config };
use crate::utils::get_current_working_dir;

const REMOTE_REPOSITORY: &str = "https://raw.githubusercontent.com/mario-eth/soldeer/main/all_dependencies.toml";

// TODOs:
// - needs to be downloaded in parallel
pub async fn download_dependencies(dependencies: &Vec<Dependency>) -> Result<(), DownloadError> {
Expand Down Expand Up @@ -50,11 +42,10 @@ pub fn unzip_dependencies(dependencies: &Vec<Dependency>) -> Result<(), ZipExtra

pub async fn download_dependency_remote(
dependency_name: &String,
dependency_version: &String
dependency_version: &String,
remote_url: &String
) -> Result<String, DownloadError> {
let res: Response = get(
format!("{}/{}~{}.zip", REMOTE_REPOSITORY, dependency_name, dependency_version)
).await.unwrap();
let res: Response = get(format!("{}", remote_url)).await.unwrap();
let body: String = res.text().await.unwrap();
let tmp_path: std::path::PathBuf = get_current_working_dir()
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions src/janitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs;
use std::fs::{ metadata, remove_file };

use crate::config::Dependency;
use crate::utils::get_current_working_dir;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn healthcheck_dependency(
println!("Health-checking dependency {}-{}", dependency_name, dependency_version);
let file_name: String = format!("{}-{}", &dependency_name, &dependency_version);
let new_path: std::path::PathBuf = get_current_working_dir().unwrap().join("dependencies");
match fs::metadata(new_path.join(file_name)) {
match metadata(new_path.join(file_name)) {
Ok(_) => { Ok(()) }
Err(_) => {
return Err(MissingDependencies::new(&dependency_name));
Expand All @@ -66,7 +66,7 @@ pub fn cleanup_dependency(
println!("Cleaning up dependency {}-{}", dependency_name, dependency_version);
let file_name: String = format!("{}-{}.zip", dependency_name, dependency_version);
let new_path: std::path::PathBuf = get_current_working_dir().unwrap().join("dependencies");
match fs::remove_file(new_path.join(file_name)) {
match remove_file(new_path.join(file_name)) {
Ok(_) => { Ok(()) }
Err(_) => {
return Err(MissingDependencies::new(&dependency_name));
Expand Down
Loading

0 comments on commit 92a2bea

Please sign in to comment.