Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Sep 3, 2024
1 parent a9c2741 commit 6bbb4af
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 140 deletions.
21 changes: 11 additions & 10 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/common2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ smol_str = "0.1.24"
salsa = { git = "https://github.com/salsa-rs/salsa", rev = "a1bf3a6" }
indexmap = "2.2"
parser = { path = "../parser2", package = "fe-parser2" }
toml = "0.8.13"
serde = { version = "1", features = ["derive"] }
git2 = "0.18.3"
37 changes: 37 additions & 0 deletions crates/common2/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// use camino::Utf8PathBuf;
// use indexmap::IndexSet;
// use serde::{Deserialize, Serialize};
// use toml::Table;

// use crate::resolver::ingot::IngotDesc;

// #[derive(Deserialize, Serialize)]
// pub struct Config {
// pub modes: Table,
// pub base_registry: Table,
// }

// impl Config {
// pub fn new() -> Self {
// Self {
// base_registry: Table::new(),
// }
// }

// pub fn base_registry(&self) -> IndexSet<IngotDesc> {
// self.base_registry
// .iter()
// .map(|(name, value)| IngotDesc::from(name, value))
// .collect()
// }
// }

// pub fn load_config(path: Utf8PathBuf) -> Config {
// let text = std::fs::read_to_string(path).unwrap();
// toml::from_str(&text).unwrap()
// }

// pub fn write_config(path: Utf8PathBuf, config: Config) {
// let text = toml::to_string(&config).unwrap();
// std::fs::write(path, text).unwrap();
// }
2 changes: 1 addition & 1 deletion crates/common2/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl InputIngot {
}

/// Set the list of files which the ingot contains.
/// All files must bee set before the ingot is used.
/// All files must be set before the ingot is used.
pub fn set_files(self, db: &mut dyn InputDb, files: IndexSet<InputFile>) {
self.__set_files_impl(db).to(files);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/common2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod config;
pub mod diagnostics;
pub mod indexmap;
pub mod input;
pub mod resolver;

pub use input::{InputFile, InputIngot};

Expand Down
11 changes: 11 additions & 0 deletions crates/common2/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod fs;
pub mod git;
pub mod ingot;

pub trait Resolver {
type ResourceDesc;
type Resource;
type ResolutionError;

fn resolve(&self, id: &Self::ResourceDesc) -> Result<Self::Resource, Self::ResolutionError>;
}
53 changes: 53 additions & 0 deletions crates/common2/src/resolver/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use camino::Utf8PathBuf;
use serde::Deserialize;

use super::Resolver;

#[derive(Deserialize)]
pub struct DirPath {
pub path: String,
}

pub struct DirReader {
path: Utf8PathBuf,
}

pub struct LazyDirResolver;

impl Resolver for LazyDirResolver {
type ResourceDesc = DirPath;
type Resource = DirReader;
type ResolutionError = String;

fn resolve(&self, desc: &DirPath) -> Result<DirReader, String> {
if let Ok(path) = Utf8PathBuf::try_from(desc.path.clone()) {
if !path.exists() {
Err("Path does not exist".to_string())
} else {
Ok(DirReader { path })
}
} else {
Err("Invalid path".to_string())
}
}
}

// fn collect_file_paths(
// path: &Utf8PathBuf,
// files: &mut IndexSet<Utf8PathBuf>,
// ) -> std::io::Result<()> {
// if path.is_dir() {
// for entry in path.read_dir()? {
// let entry = entry?;
// let path = Utf8PathBuf::from_path_buf(entry.path())
// .expect("could not covert PathBuf to Utf8PathBuf");

// if path.is_dir() {
// collect_file_paths(&path, files)?;
// } else {
// files.insert(path);
// }
// }
// }
// Ok(())
// }
90 changes: 90 additions & 0 deletions crates/common2/src/resolver/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use camino::Utf8PathBuf;
use serde::Deserialize;

use super::Resolver;

#[derive(Deserialize)]
pub struct GitRepoDesc {
remote: String,
refspec: String,
local_path: Option<String>,
}

pub struct GitRepo {
repo: git2::Repository,
}

pub struct GitResolver {
pub default_clone_path: Utf8PathBuf,
}

// pub enum GitResolutionError {
// GitResolutionError(()),
// FsResolutionError(std::io::Error),
// }

impl Resolver for GitResolver {
type ResourceDesc = GitRepoDesc;
type Resource = GitRepo;
type ResolutionError = String;

fn resolve(&self, desc: &GitRepoDesc) -> Result<GitRepo, String> {
todo!("")
}
}

// use git2::{FetchOptions, Oid, Repository};
// use std::error::Error;
// use std::path::Path;

// /// Fetch and checkout the specified refspec from the remote repository without
// /// fetching any additional history.
// pub fn fetch_and_checkout<P: AsRef<Path>>(
// remote: &str,
// target_directory: P,
// refspec: &str,
// ) -> Result<(), Box<dyn Error>> {
// // We initialize the repo here so that we can be sure that we created a directory that
// // needs to be clean up in case of an error. If the init fails, there won't be anything
// // to clean up.
// let repo = Repository::init(&target_directory)?;
// let res = _fetch_and_checkout(remote, repo, refspec);
// if res.is_err() {
// std::fs::remove_dir_all(target_directory).expect("Failed to clean up directory");
// }

// res
// }

// fn _fetch_and_checkout(
// remote: &str,
// repo: Repository,
// refspec: &str,
// ) -> Result<(), Box<dyn Error>> {
// let mut remote = repo.remote("origin", remote)?;

// let mut fetch_options = FetchOptions::new();

// fetch_options.depth(1);

// // Fetch the specified SHA1 with depth 1
// if let Err(e) = remote.fetch(&[refspec], Some(&mut fetch_options), None) {
// if let (git2::ErrorClass::Net, git2::ErrorCode::GenericError) = (e.class(), e.code()) {
// // That's a pretty cryptic error for the common case of the refspec not existing.
// // We keep the cryptic error (because it might have other causes) but add a hint.
// return Err(format!("{}\nMake sure revision {} exists in remote", e, refspec).into());
// } else {
// return Err(e.into());
// }
// }

// // Find the fetched commit by SHA1
// let oid = Oid::from_str(refspec)?;
// let commit = repo.find_commit(oid)?;

// // Checkout the commit
// repo.checkout_tree(commit.as_object(), None)?;
// repo.set_head_detached(oid)?;

// Ok(())
// }
67 changes: 67 additions & 0 deletions crates/common2/src/resolver/ingot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::marker::PhantomData;

use camino::Utf8PathBuf;
use indexmap::IndexSet;
use semver::Version;
use serde::Deserialize;
use smol_str::SmolStr;
use toml::Table;

use super::{fs::LazyDirResolver, git::GitResolver, Resolver};

#[derive(Deserialize)]
pub struct IngotDesc<FD> {
name: SmolStr,
version: String,
#[serde(flatten)]
files_desc: FD,
}

#[derive(Deserialize)]
pub struct Config {
pub name: SmolStr,
pub version: SmolStr,
dependencies: Table,
}

impl Config {
fn dependencies<FD>(&self) -> IndexSet<IngotDesc<FD>> {
todo!("dependency")
}
}

trait IngotFiles {
fn config(&self) -> Config;
fn src(&self) -> IndexSet<(Utf8PathBuf, Vec<u8>)>;
fn root_path(&self) -> Utf8PathBuf;
}

pub struct Ingot<F>
where
F: IngotFiles,
{
desc_name: SmolStr,
desc_version: Version,
files: F,
}

pub struct IngotResolver<FR> {
files_resolver: FR,
}

pub type FsIngotResolver = IngotResolver<LazyDirResolver>;
pub type GitIngotResolver = IngotResolver<GitResolver>;

impl<FR> Resolver for IngotResolver<FR>
where
FR: Resolver,
FR::Resource: IngotFiles,
{
type ResourceDesc = IngotDesc<FR::ResourceDesc>;
type Resource = Ingot<FR::Resource>;
type ResolutionError = ();

fn resolve(&self, desc: &IngotDesc<FR::ResourceDesc>) -> Result<Ingot<FR::Resource>, ()> {
Err(())
}
}
4 changes: 1 addition & 3 deletions crates/driver2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ common = { path = "../common2", package = "fe-common2" }
hir-analysis = { path = "../hir-analysis", package = "fe-hir-analysis" }
camino = "1.1.4"
clap = { version = "4.3", features = ["derive"] }
toml = "0.8.13"
serde = { version = "1", features = ["derive"] }
semver = "1.0.23"
walkdir = "2"
include_dir = "0.7"
dirs = "5.0.1"
dirs = "5.0.1"
Loading

0 comments on commit 6bbb4af

Please sign in to comment.