Skip to content

Commit

Permalink
refactor: removed nested lib
Browse files Browse the repository at this point in the history
This way it will be easier to automate crate.io release
  • Loading branch information
alesbrelih committed Apr 4, 2024
1 parent 9cd3cdf commit 34a552c
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 336 deletions.
19 changes: 0 additions & 19 deletions Cargo.lock

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

22 changes: 3 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log.workspace = true
lsp-server.workspace = true
lsp-types.workspace = true
serde.workspace = true
serde_json.workspace = true
simple-logging.workspace = true
simple_logger.workspace = true
tokio.workspace = true
anyhow.workspace = true
gitlab-ci-ls-parser = { version = "0.1.0", path = "./gitlab-ci-ls-parser" }
git2.workspace = true
tree-sitter.workspace = true
tree-sitter-yaml.workspace = true
regex = "1.10.4"

[workspace.dependencies]
log = "0.4.21"
lsp-server = "0.7.6"
lsp-types = "0.95.1"
Expand All @@ -37,9 +21,9 @@ serde = "1.0.197"
git2 = "0.18.3"
tree-sitter = "0.20.10"
tree-sitter-yaml = "0.0.1"

[workspace]
members = ["gitlab-ci-ls-parser"]
yaml-rust = "0.4.5"
reqwest = { version = "0.12.2", features = ["blocking"] }
regex = "1.10.4"

[lints.clippy]
all = "deny"
Expand Down
1 change: 0 additions & 1 deletion gitlab-ci-ls-parser/.gitignore

This file was deleted.

24 changes: 0 additions & 24 deletions gitlab-ci-ls-parser/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions gitlab-parser/Cargo.lock

This file was deleted.

79 changes: 38 additions & 41 deletions gitlab-ci-ls-parser/src/git.rs → src/gitlab_ci_ls_parser/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use std::{
time::Duration,
};

use super::{parser_utils, GitlabFile};
use log::{debug, error, info};
use reqwest::{blocking::Client, header::IF_NONE_MATCH, StatusCode, Url};

use crate::{parser_utils::ParserUtils, GitlabFile};

pub trait Git {
fn clone_repo(&self, repo_dest: &str, remote_tag: &str, remote_pkg: &str);
fn fetch_remote_repository(
Expand All @@ -23,6 +22,7 @@ pub trait Git {
fn fetch_remote(&self, url: Url) -> anyhow::Result<GitlabFile>;
}

#[allow(clippy::module_name_repetitions)]
pub struct GitImpl {
package_map: HashMap<String, String>,
remote_urls: Vec<String>,
Expand All @@ -36,8 +36,8 @@ impl GitImpl {
cache_path: String,
) -> Self {
Self {
remote_urls,
package_map,
remote_urls,
cache_path,
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Git for GitImpl {
"1",
"--branch",
remote_tag,
format!("{}{}", origin, remote_pkg).as_str(),
format!("{origin}{remote_pkg}").as_str(),
repo_dest,
])
.output()
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Git for GitImpl {

let mut files = vec![];
for file in remote_files {
let file_path = format!("{}{}", repo_dest, file);
let file_path = format!("{repo_dest}{file}");
debug!("filepath: {}", file_path);

let content = match std::fs::read_to_string(&file_path) {
Expand Down Expand Up @@ -158,8 +158,8 @@ impl Git for GitImpl {
std::fs::create_dir_all(&remote_cache_path)?;

// check if file was changed
let file_hash = ParserUtils::remote_path_to_hash(url.as_str());
let file_name_pattern = format!("_{}.yaml", file_hash);
let file_hash = parser_utils::ParserUtils::remote_path_to_hash(url.as_str());
let file_name_pattern = format!("_{file_hash}.yaml");

let dir_entry = fs::read_dir(&remote_cache_path)?
.filter_map(Result::ok)
Expand Down Expand Up @@ -189,45 +189,42 @@ impl Git for GitImpl {

let mut req = client.get(url);
if let Some(etag) = &existing_etag {
req = req.header(IF_NONE_MATCH, format!("\"{}\"", etag));
req = req.header(IF_NONE_MATCH, format!("\"{etag}\""));
}

let res = req.send()?;
let response = req.send()?;

match res.status() {
StatusCode::NOT_MODIFIED => {
let fpath = file_path.expect("File path must exist for NOT_MODIFIED response");
let content = fs::read_to_string(&fpath)?;
if response.status() == StatusCode::NOT_MODIFIED {
let fpath = file_path.expect("File path must exist for NOT_MODIFIED response");
let content = fs::read_to_string(&fpath)?;

info!("CACHED");
info!("CACHED");

Ok(GitlabFile {
path: format!("file://{}", fpath.to_str().unwrap()),
content,
})
}
_ => {
info!("NOT CACHED");

let headers = res.headers().clone();
let etag = headers.get("etag").unwrap().to_str()?;
let text = res.text()?;

let path = format!(
"{}/{}_{}.yaml",
remote_cache_path,
ParserUtils::strip_quotes(etag),
file_hash
);

let mut file = File::create(&path)?;
file.write_all(text.as_bytes())?;

Ok(GitlabFile {
path,
content: text,
})
}
Ok(GitlabFile {
path: format!("file://{}", fpath.to_str().unwrap()),
content,
})
} else {
info!("NOT CACHED");

let headers = response.headers().clone();
let etag = headers.get("etag").unwrap().to_str()?;
let text = response.text()?;

let path = format!(
"{}/{}_{}.yaml",
remote_cache_path,
parser_utils::ParserUtils::strip_quotes(etag),
file_hash
);

let mut file = File::create(&path)?;
file.write_all(text.as_bytes())?;

Ok(GitlabFile {
path,
content: text,
})
}
}
}
Loading

0 comments on commit 34a552c

Please sign in to comment.