Skip to content

Commit

Permalink
first steps into the direction of dependency management
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Dec 15, 2023
1 parent 68314d4 commit f026200
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 25 deletions.
19 changes: 11 additions & 8 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ termion = "2.0"
git2 = "0.18"
run_script = "0.10"
tempfile = "3.0"
#version = { version = "3.0", features = [] }
url = { version = "2.5", features = ["serde"] }
anyhow = "1.0.75"
54 changes: 54 additions & 0 deletions src/package/management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::path::PathBuf;
use serde_derive::{Deserialize, Serialize};
use url::Url;
use std::path::Path;
use crate::package::version::{Version, to_version_string, from_version_string};

#[derive(Clone, Deserialize, Serialize)]
enum ProjectSource {
#[serde(rename="git")]
Git(Url),
#[serde(rename="tarball")]
TarBall(Url),
#[serde(rename="path")]
Path(PathBuf)
}

/// Dependency with source and version
#[derive(Clone, Deserialize, Serialize)]
pub struct DetailedDependency {
#[serde(deserialize_with="from_version_string", serialize_with="to_version_string")]
version: Version,
#[serde(flatten)]
mutual_exclusive: ProjectSource
}

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {

Check warning on line 26 in src/package/management.rs

View workflow job for this annotation

GitHub Actions / Check

function `copy_dir_all` is never used

Check warning on line 26 in src/package/management.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

function `copy_dir_all` is never used

warning: function `copy_dir_all` is never used --> src/package/management.rs:26:4 | 26 | fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> { | ^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
std::fs::create_dir_all(&dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}


impl DetailedDependency {
fn fetch(&self, library_path: &PathBuf) -> anyhow::Result<()>{

Check warning on line 42 in src/package/management.rs

View workflow job for this annotation

GitHub Actions / Check

method `fetch` is never used

Check warning on line 42 in src/package/management.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

method `fetch` is never used

warning: method `fetch` is never used --> src/package/management.rs:42:8 | 41 | impl DetailedDependency { | ----------------------- method in this implementation 42 | fn fetch(&self, library_path: &PathBuf) -> anyhow::Result<()>{ | ^^^^^
match &self.mutual_exclusive {
ProjectSource::Path(path_buf) => {
Ok(copy_dir_all(path_buf, library_path)?)
},
ProjectSource::Git(git_url ) => {
git2::Repository::clone(git_url.as_str(), library_path)?;
Ok(())
},
_ => todo!("Not Supported")
}
}
}
22 changes: 5 additions & 17 deletions src/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod version;
pub mod management;

use crate::args::{BuildSystem, InitArgs, Platform, TargetLanguage};
use crate::util::{analyzer, copy_recursively};

Expand All @@ -14,7 +17,9 @@ use crate::args::BuildSystem::{CMake, LFC};
use crate::util::errors::{BuildResult, LingoError};
use git2::Repository;
use tempfile::tempdir;

use which::which;
use management::DetailedDependency;

fn is_valid_location_for_project(path: &std::path::Path) -> bool {
!path.join("src").exists() && !path.join(".git").exists() && !path.join("application").exists()
Expand Down Expand Up @@ -128,23 +133,6 @@ impl App {
}
}

/// Simple or DetailedDependency
#[derive(Clone, Deserialize, Serialize)]
pub enum FileDependency {
// the version string
Simple(String),
/// version string and source
Advanced(DetailedDependency),
}

/// Dependency with source and version
#[derive(Clone, Deserialize, Serialize)]
pub struct DetailedDependency {
version: String,
git: Option<String>,
tarball: Option<String>,
zip: Option<String>,
}

#[derive(Deserialize, Serialize, Clone)]
pub struct PackageDescription {
Expand Down
78 changes: 78 additions & 0 deletions src/package/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::fmt;
use std::fmt::Display;
use std::str::FromStr;
use serde::{Deserializer, Serializer};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Version {
pub major : u32,
pub minor : u32,
pub patch : u32
}

impl Display for Version {
fn fmt( &self, fmtr : &mut fmt::Formatter ) -> fmt::Result {
write!( fmtr, "{}.{}.{}", self.major, self.minor, self.patch )
}
}

impl FromStr for Version {
type Err = String;

fn from_str( s : &str ) -> Result<Version, Self::Err> {
let parts : Vec<Result<u32, &str>> =
s.split( '.' )
.map( | elm | elm.parse::<u32>()
.map_err( |_| elm ) )
.collect();

if parts.len() != 3 {
return
Err( format!( "Invalid version format: expected 3 components, got {}."
, parts.len() ) );
}

for part in &parts {
match part {
&Err( err ) =>
return
Err( format!( "Invalid version format: expected integer, got '{}'."
, err ) ),
_ => {}
}

Check warning on line 42 in src/package/version.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` --> src/package/version.rs:36:13 | 36 | / match part { 37 | | &Err( err ) => 38 | | return 39 | | Err( format!( "Invalid version format: expected integer, got '{}'." 40 | | , err ) ), 41 | | _ => {} 42 | | } | |_____________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match = note: `#[warn(clippy::single_match)]` on by default help: try | 36 ~ if let &Err( err ) = part { return 37 + Err( format!( "Invalid version format: expected integer, got '{}'." 38 + , err ) ) } |
}

Ok( Version {
major: parts[0].unwrap(),
minor: parts[1].unwrap(),
patch: parts[2].unwrap()
} )
}
}

pub(crate) fn from_version_string<'de, D: Deserializer<'de>>(d: D) -> Result<Version, D::Error> {
struct VersionVisitor;

impl<'de> serde::de::Visitor<'de> for VersionVisitor {
type Value = Version;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "cannot parse version")
}

fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Version, E> {
Ok(Version::from_str(s).map_err(|e| E::invalid_value(serde::de::Unexpected::Str(s), &self))?)

Check warning on line 64 in src/package/version.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `e`

Check warning on line 64 in src/package/version.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

question mark operator is useless here

warning: question mark operator is useless here --> src/package/version.rs:64:13 | 64 | Ok(Version::from_str(s).map_err(|e| E::invalid_value(serde::de::Unexpected::Str(s), &self))?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `Version::from_str(s).map_err(|e| E::invalid_value(serde::de::Unexpected::Str(s), &self))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark = note: `#[warn(clippy::needless_question_mark)]` on by default

Check warning on line 64 in src/package/version.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

unused variable: `e`

warning: unused variable: `e` --> src/package/version.rs:64:46 | 64 | Ok(Version::from_str(s).map_err(|e| E::invalid_value(serde::de::Unexpected::Str(s), &self))?) | ^ help: if this is intentional, prefix it with an underscore: `_e` | = note: `#[warn(unused_variables)]` on by default
}
}

d.deserialize_any(VersionVisitor)
}

pub(crate) fn to_version_string<S>(version: &Version, s: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
let serialized_string = format!("{}", version);
Ok(s.serialize_str(&serialized_string)?)

Check warning on line 75 in src/package/version.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

question mark operator is useless here

warning: question mark operator is useless here --> src/package/version.rs:75:5 | 75 | Ok(s.serialize_str(&serialized_string)?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `s.serialize_str(&serialized_string)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
}


0 comments on commit f026200

Please sign in to comment.