Skip to content

Commit

Permalink
git: refactoring code to guess a language
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Feb 26, 2024
1 parent 90fd9e4 commit 22817d4
Showing 1 changed file with 59 additions and 42 deletions.
101 changes: 59 additions & 42 deletions coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::any::Any;
use std::path::Path;

use async_trait::async_trait;
use git2;

Check warning on line 5 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `git2` is imported redundantly

Check warning on line 5 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `git2` is imported redundantly
Expand Down Expand Up @@ -52,7 +53,7 @@ fn is_hidden(entry: &DirEntry) -> bool {
}

struct IndexingInfo {
config: Conf,
config: Option<Conf>,
lang: PluginLang,
exec_path: String,
}
Expand Down Expand Up @@ -105,7 +106,7 @@ impl Github {

let exec_path = format!("{root_path}/{}", conf_file.plugin.main);
return Ok(Some(IndexingInfo {
config: conf_file,
config: Some(conf_file),
lang: plugin_lang,
exec_path,
}));
Expand All @@ -114,6 +115,57 @@ impl Github {
Ok(None)
}

/// When a configuration file is not found this function is called
/// and we try to guess the programming language used.
async fn try_guess_language(
&self,
plugin_path: &Path,
) -> Result<Option<IndexingInfo>, CoffeeError> {
log::debug!("conf file not found, so we try to guess the language");
// try to understand the language from the file
let files = WalkDir::new(plugin_path).max_depth(1);
for file in files {
let file_dir = file.unwrap().clone();
let (derived_root_path, derived_name) = get_plugin_info_from_path(file_dir.path())?;
let exec_path = None;
let plugin_name = Some(derived_name.to_string());

Check warning on line 131 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `plugin_name`

Check warning on line 131 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `plugin_name`

Check warning on line 131 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `plugin_name`

Check warning on line 131 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `plugin_name`

Check warning on line 131 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `plugin_name`

Check warning on line 131 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `plugin_name`
debug!("looking for {derived_name} in {derived_root_path}");
let file_name = file_dir.file_name().to_str().unwrap();
let plugin_lang = match file_name {
"requirements.txt" => {
let exec_path = Some(format!("{derived_root_path}/{derived_name}.py"));

Check warning on line 136 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `exec_path`

Check warning on line 136 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `exec_path`

Check warning on line 136 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `exec_path`

Check warning on line 136 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `exec_path`

Check warning on line 136 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `exec_path`

Check warning on line 136 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `exec_path`
PluginLang::PyPip
}
"pyproject.toml" => {
let exec_path = Some(format!("{derived_root_path}/{derived_name}.py"));

Check warning on line 140 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `exec_path`

Check warning on line 140 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `exec_path`

Check warning on line 140 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `exec_path`

Check warning on line 140 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `exec_path`

Check warning on line 140 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `exec_path`

Check warning on line 140 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `exec_path`
PluginLang::PyPoetry
}
// We dot have any information on standard pattern on where to find the
// plugin exec path, so for now we skip the indexing!
//
// N.B: The plugin should use the coffee manifest, period.
"go.mod" => PluginLang::Go,
"cargo.toml" => PluginLang::Rust,
"pubspec.yaml" => PluginLang::Dart,
"package.json" => PluginLang::JavaScript,
"tsconfig.json" => PluginLang::TypeScript,
_ => PluginLang::Unknown,
};
if plugin_lang != PluginLang::Unknown {
// TODO: call recursive to look under sub directory
break;
} else {
return Ok(Some(IndexingInfo {
exec_path: exec_path.unwrap(),
config: None,
lang: plugin_lang,
}));
}
}
// There is nothing in this path
Ok(None)
}

/// Index the repository to store information
/// related to the plugins
pub async fn index_repository(&mut self) -> Result<(), CoffeeError> {
Expand All @@ -140,47 +192,12 @@ impl Github {
if let Some(index_info) = self.lookup_config_file(&root_path).await? {
exec_path = Some(index_info.exec_path);
plugin_lang = index_info.lang;
plugin_name = Some(index_info.config.plugin.name.to_owned());
conf = Some(index_info.config);
// SAFETY: it is safe to unwrap because a config gile has always a file
let config = index_info.config.clone().unwrap();
plugin_name = Some(config.plugin.name.to_owned());
conf = index_info.config;
} else {
// check if there was a coffee configuration file
log::debug!("conf file not found, so we try to guess the language");
// try to understand the language from the file
let files = WalkDir::new(plugin_path.path()).max_depth(1);
for file in files {
let file_dir = file.unwrap().clone();
let (derived_root_path, derived_name) =
get_plugin_info_from_path(file_dir.path())?;

plugin_name = Some(derived_name.to_string());
debug!("looking for {derived_name} in {derived_root_path}");
let file_name = file_dir.file_name().to_str().unwrap();
plugin_lang = match file_name {
"requirements.txt" => {
exec_path =
Some(format!("{derived_root_path}/{derived_name}.py"));
PluginLang::PyPip
}
"pyproject.toml" => {
exec_path =
Some(format!("{derived_root_path}/{derived_name}.py"));
PluginLang::PyPoetry
}
// We dot have any information on standard pattern on where to find the
// plugin exec path, so for now we skip the indexing!
//
// N.B: The plugin should use the coffee manifest, period.
"go.mod" => PluginLang::Go,
"cargo.toml" => PluginLang::Rust,
"pubspec.yaml" => PluginLang::Dart,
"package.json" => PluginLang::JavaScript,
"tsconfig.json" => PluginLang::TypeScript,
_ => PluginLang::Unknown,
};
if plugin_lang != PluginLang::Unknown {
break;
}
}
let index_info = self.try_guess_language(plugin_path.path()).await?;

Check warning on line 200 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `index_info`

Check warning on line 200 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused variable: `index_info`

Check warning on line 200 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `index_info`

Check warning on line 200 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

unused variable: `index_info`

Check warning on line 200 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `index_info`

Check warning on line 200 in coffee_github/src/repository.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused variable: `index_info`
}
debug!("possible plugin language: {:?}", plugin_lang);
if exec_path.is_none() {
Expand Down

0 comments on commit 22817d4

Please sign in to comment.