Skip to content

Commit

Permalink
Refactor + add init config
Browse files Browse the repository at this point in the history
  • Loading branch information
paudrow committed Feb 18, 2024
1 parent 61a017a commit 9c0b2a5
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 25 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions src/yatm_v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ octocrab = "0.33.4"
percent-encoding = "2.3.1"
serde = { version = "1.0.196", features = ["derive"] }
serde_yaml = "0.9.31"
tempfile = "3.10.0"
tokio = { version = "1.36.0", features = ["full"] }
url = "2.5.0"
7 changes: 5 additions & 2 deletions src/yatm_v2/src/utils/cli.rs → src/yatm_v2/src/app/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::app::init_config::init_config;
use std::path::PathBuf;

use anyhow::Result;
use clap::{Parser, Subcommand};

// Define the main application
Expand Down Expand Up @@ -72,12 +74,12 @@ enum GithubSubcommands {
Preview,
}

pub fn cli() {
pub fn cli() -> Result<()> {
let cli = MyApp::parse();

match cli.command {
Commands::Init { path } => {
println!("Creating a new project at {:?}", path);
init_config(&path)?;
}
Commands::Requirements { subcommand } => match subcommand {
RequirementsSubcommands::Validate(options) => {
Expand Down Expand Up @@ -110,4 +112,5 @@ pub fn cli() {
}
},
}
Ok(())
}
159 changes: 159 additions & 0 deletions src/yatm_v2/src/app/init_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use crate::types::Config;
use anyhow::{Context, Result};
use serde_yaml;
use std::{os, path::PathBuf};

pub fn init_config(dir: &PathBuf) -> Result<()> {
make_sure_empty_dir_exists(dir)?;

let config = Config::default();
let config_file = dir.join("config.yaml");
std::fs::write(
&config_file,
serde_yaml::to_string(&config).context("Failed to serialize the config")?,
)
.context("Failed to write the config file")?;

if config.requirements_dirs.len() == 1 {
let requirements_dir = dir.join(&config.requirements_dirs[0]);
make_sure_empty_dir_exists(&requirements_dir)?;
}

let generated_files_dir = dir.join(&config.generated_files_dir);
make_sure_empty_dir_exists(&generated_files_dir)?;

// add a .gitignore file with the generated_files_dir
let gitignore_file = dir.join(".gitignore");
std::fs::write(
&gitignore_file,
&format!("/{}", config.generated_files_dir.to_string_lossy()),
)
.context("Failed to write the .gitignore file")?;

Ok(())
}

#[cfg(test)]
mod test_init_config {
use super::init_config;
use std::fs;
use tempfile::tempdir;

#[test]
fn init_config_creates_files() {
let dir = tempdir().unwrap().path().to_path_buf();
init_config(&dir).unwrap();
assert!(dir.join("config.yaml").is_file());
assert!(dir.join("requirements").is_dir());
assert!(dir.join(".generated_files").is_dir());
assert!(dir.join(".gitignore").is_file());
}

#[test]
fn init_config_exists_not_empty() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::create_dir(&dir).unwrap();
fs::File::create(dir.join("file")).unwrap();
assert!(init_config(&dir).is_err());
}

#[test]
fn init_config_exists_file() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::File::create(&dir).unwrap();
assert!(init_config(&dir).is_err());
}
}

fn make_sure_empty_dir_exists(dir: &PathBuf) -> Result<()> {
if dir.is_file() {
anyhow::bail!(format!("Path already exists and is a file: {:?}", dir));
}
if dir.is_dir() {
if !is_empty_directory(dir)? {
anyhow::bail!(format!("Directory is not empty: {:?}", dir));
}
} else {
std::fs::create_dir(dir).context("Failed to create the directory")?;
}
Ok(())
}

#[cfg(test)]
mod test_make_sure_empty_dir_exists {
use super::is_empty_directory;
use super::make_sure_empty_dir_exists;
use std::fs;
use tempfile::tempdir;

#[test]
fn create_empty_dir() {
let dir = tempdir().unwrap().path().to_path_buf();
make_sure_empty_dir_exists(&dir).unwrap();
assert!(dir.is_dir());
assert!(is_empty_directory(&dir).unwrap());
}

#[test]
fn create_empty_dir_exists() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::create_dir(&dir).unwrap();
make_sure_empty_dir_exists(&dir).unwrap();
assert!(dir.is_dir());
assert!(is_empty_directory(&dir).unwrap());
}

#[test]
fn create_empty_dir_exists_not_empty() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::create_dir(&dir).unwrap();
fs::File::create(dir.join("file")).unwrap();
assert!(make_sure_empty_dir_exists(&dir).is_err());
}

#[test]
fn create_empty_dir_exists_file() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::File::create(&dir).unwrap();
assert!(make_sure_empty_dir_exists(&dir).is_err());
}
}

fn is_empty_directory(dir: &PathBuf) -> Result<bool> {
if dir.is_dir() {
let mut entries = std::fs::read_dir(dir).context("Failed to read the directory")?;
if entries.next().is_none() {
return Ok(true);
}
}
Ok(false)
}

#[cfg(test)]
mod test_is_empty_directory {
use super::is_empty_directory;
use std::fs;
use std::path::PathBuf;
use tempfile::tempdir;

#[test]
fn is_empty() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::create_dir(&dir).unwrap();
assert!(is_empty_directory(&dir).unwrap());
}

#[test]
fn is_not_empty() {
let dir = tempdir().unwrap().path().to_path_buf();
fs::create_dir(&dir).unwrap();
fs::File::create(dir.join("file")).unwrap();
assert!(!is_empty_directory(&dir).unwrap());
}

#[test]
fn is_not_exists() {
let dir = tempdir().unwrap().path().to_path_buf();
assert!(!is_empty_directory(&dir).unwrap());
}
}
4 changes: 4 additions & 0 deletions src/yatm_v2/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod cli;
mod init_config;

pub use cli::cli;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::local_issue::LocalIssue;
use crate::types::LocalIssue;
use octocrab::models::issues::Issue as GithubIssue;

struct GithubIssueHelper {
Expand Down Expand Up @@ -136,10 +136,7 @@ mod test_get_local_issues_without_matches {
}
}

pub fn is_local_issue_match_github_issue(
local_issue: &LocalIssue,
github_issue: &GithubIssue,
) -> bool {
fn is_local_issue_match_github_issue(local_issue: &LocalIssue, github_issue: &GithubIssue) -> bool {
let github_issue_helper = GithubIssueHelper {
title: github_issue.title.clone(),
labels: github_issue
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/yatm_v2/src/github/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod get_local_issues_without_matches;
mod github_api;

pub use get_local_issues_without_matches::get_local_issues_without_matches;
pub use github_api::Github;
17 changes: 10 additions & 7 deletions src/yatm_v2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
mod utils;
mod app;
mod github;
mod test_cases;
mod types;

use anyhow::{Context, Ok, Result};
use utils::local_issue;
use types::LocalIssue;

use crate::utils::github::Github;
use crate::utils::github_utils::get_local_issues_without_matches;
use crate::utils::local_issue::LocalIssue;
use crate::utils::template::get_github_issue_content;
use crate::github::get_local_issues_without_matches;
use crate::github::Github;
use crate::test_cases::test_case_to_markdown;

use chrono;
use common::types::TestCase;
Expand Down Expand Up @@ -94,7 +97,7 @@ fn demo_template() {
selected_permutation,
};

let result = get_github_issue_content(test_case).unwrap();
let result = test_case_to_markdown(test_case).unwrap();
println!("{}", result.title);
println!("{}", result.text_body);
println!("{:?}", result.labels);
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/yatm_v2/src/test_cases/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod make_test_cases;
mod test_case_to_markdown;

pub use make_test_cases::make_test_cases;
pub use test_case_to_markdown::test_case_to_markdown;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::utils::local_issue::LocalIssue;
use crate::types::LocalIssue;
use anyhow::{Context, Result};
use askama::Template;
use common::types::{Action, Expect, Link, Step, TestCase};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Template, Clone)]
Expand All @@ -14,7 +13,7 @@ struct GithubIssueTemplate {
selected_permutation: HashMap<String, String>,
}

pub fn get_github_issue_content(test_case: TestCase) -> Result<LocalIssue> {
pub fn test_case_to_markdown(test_case: TestCase) -> Result<LocalIssue> {
let labels = get_labels(&test_case);

let template = GithubIssueTemplate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Config {
pub yatm_v2_version: String,
pub repo_name: String,
pub repo_owner: String,
pub requirements_dirs: Vec<PathBuf>,
Expand All @@ -11,11 +12,13 @@ pub struct Config {

impl Config {
pub fn default() -> Self {
const VERSION: &str = env!("CARGO_PKG_VERSION");
Config {
yatm_v2_version: VERSION.to_string(),
repo_name: "repo_name".to_string(),
repo_owner: "repo_owner".to_string(),
requirements_dirs: vec![PathBuf::new().join("requirements")],
generated_files_dir: PathBuf::new(),
generated_files_dir: PathBuf::new().join(".generated_files"),
}
}
}
File renamed without changes.
5 changes: 5 additions & 0 deletions src/yatm_v2/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod config;
mod local_issue;

pub use config::Config;
pub use local_issue::LocalIssue;
7 changes: 0 additions & 7 deletions src/yatm_v2/src/utils/mod.rs

This file was deleted.

0 comments on commit 9c0b2a5

Please sign in to comment.