-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb1860e
commit fba4dbb
Showing
7 changed files
with
137 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
\documentclass{article} | ||
\usepackage[utf8]{inputenc} | ||
|
||
\title{Title} | ||
\author{Author} | ||
\date{September 2022} | ||
|
||
\begin{document} | ||
|
||
\maketitle | ||
|
||
\newpage | ||
|
||
\section{Introduction} | ||
|
||
\end{document} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,50 @@ | ||
use log::trace; | ||
use std::{fs, fs::read_dir, path::Path, path::PathBuf}; | ||
use crate::prelude::*; | ||
use std::path::PathBuf; | ||
|
||
#[allow(dead_code)] | ||
pub enum CopyFilesExitCode { | ||
SomeFilesExist, | ||
AllFilesExist, | ||
Other, | ||
} | ||
|
||
pub fn copy_files(from: PathBuf, to: &Path, dry_run: bool) -> Result<isize, CopyFilesExitCode> { | ||
let template_files = read_dir(from).expect("could not find template folder"); | ||
pub fn copy_files(from: PathBuf, dry_run: bool, info: &crate::info::TemplateInfo) -> Result<(), CopyFilesExitCode> { | ||
let template_folder = std::fs::read_dir(from); | ||
let template_folder = unwrap_result!(template_folder, "Could not read from templates folder"); | ||
|
||
let mut end: bool = false; | ||
let pwd = std::env::current_dir().unwrap(); | ||
|
||
for file in template_files { | ||
let file = file.unwrap().path(); | ||
for file in template_folder { | ||
let file = unwrap_result!(file, "Could not read file"); | ||
|
||
let pwd = to.to_owned().clone(); | ||
|
||
if dry_run && pwd.with_file_name(file.file_name().unwrap().to_str().unwrap()).exists() { | ||
println!("file exists: {}", file.file_name().unwrap().to_str().unwrap()); | ||
end = true; | ||
if should_ignore_file(file.file_name().to_string_lossy().to_string(), info) { | ||
continue; | ||
} | ||
|
||
trace!( | ||
"pwd = {}", | ||
pwd.clone() | ||
.with_file_name(file.clone().file_name().unwrap().to_str().unwrap()) | ||
.to_str() | ||
.unwrap() | ||
); | ||
|
||
if file.clone().file_name().unwrap().to_str().unwrap() != "itex-info.toml" | ||
&& file.clone().file_name().unwrap().to_str().unwrap() != "Makefile" | ||
&& !dry_run | ||
&& fs::copy(&file, pwd.with_file_name(file.file_name().unwrap().to_str().unwrap())).is_err() | ||
{ | ||
println!("Error copying file"); | ||
let file_path = pwd.clone().join(file.file_name()); | ||
|
||
log::trace!("Copying file: {}, is dry run: {}", file.file_name().to_string_lossy(), dry_run); | ||
|
||
if dry_run && file_path.exists() { | ||
return Err(CopyFilesExitCode::SomeFilesExist); | ||
} else { | ||
unwrap_result!(std::fs::copy(file.path(), file_path.clone()), "Could not copy file"); | ||
} | ||
} | ||
|
||
if end { | ||
return Err(CopyFilesExitCode::SomeFilesExist); | ||
Ok(()) | ||
} | ||
|
||
fn should_ignore_file(filename: String, info: &TemplateInfo) -> bool { | ||
let filename = filename.trim().to_owned(); | ||
if &filename == "itex-info.toml" { | ||
return true; | ||
} | ||
|
||
if let Some(excluded_files) = info.excluded_files.clone() { | ||
if excluded_files.contains(&filename) { | ||
return true; | ||
} | ||
} | ||
|
||
Ok(0) | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::prelude::*; | ||
use std::path::PathBuf; | ||
|
||
pub struct TemplateList { | ||
pub names: Vec<String>, | ||
} | ||
|
||
impl std::fmt::Display for TemplateList { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f, "{}", console::style("Templates available:").green().bright()).unwrap(); | ||
for name in &self.names { | ||
writeln!(f, " {}", name).unwrap(); | ||
} | ||
|
||
std::fmt::Result::Ok(()) | ||
} | ||
} | ||
|
||
impl TemplateList { | ||
pub fn from_path(search_path: Option<PathBuf>, disable_os_search: bool) -> Self { | ||
let template_folder = resolve_templates_folder(disable_os_search, &search_path); | ||
|
||
let mut template_names: Vec<String> = vec![]; | ||
for folder in std::fs::read_dir(template_folder).unwrap() { | ||
let filename = folder.unwrap().file_name(); | ||
let filename = filename.to_string_lossy(); | ||
|
||
if filename.ends_with(".toml") { | ||
continue; | ||
} | ||
|
||
template_names.push(filename.to_string()); | ||
} | ||
TemplateList { names: template_names } | ||
} | ||
} | ||
|
||
pub fn list(search_path: Option<PathBuf>, disable_os_search: bool) { | ||
let template_list = TemplateList::from_path(search_path, disable_os_search); | ||
|
||
print!("{}", template_list); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn list_templates() { | ||
super::TemplateList::from_path(None, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.