-
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.
feat: stora förändringar, och liten cli
- Loading branch information
1 parent
04543e9
commit 90a9217
Showing
24 changed files
with
319 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
{ | ||
"command_dir": "./src/app/commands" | ||
} |
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,6 +1,6 @@ | ||
use clier::Clier; | ||
|
||
fn main() { | ||
let cli = Clier::parse().args; | ||
println!("{:#?}", cli); | ||
let args = Clier::parse().args; | ||
println!("{:#?}", args); | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,14 +1,54 @@ | ||
use std::fs; | ||
use std::io::ErrorKind; | ||
|
||
use clier::command::{CmdArgs, Command}; | ||
use clier::hooks::Flag; | ||
use clier::hooks::{use_flag, use_flags, Flag}; | ||
|
||
use crate::app::generators::{get_config, CommandGenerator}; | ||
|
||
pub fn generate_command() -> Command { | ||
Command::new("generate", "Generates parts of program", command) | ||
.usage("generate [--flags=value]") | ||
.flags(vec![Flag::new("test", "testing".to_string())]) | ||
Command::new("generate", "Generates parts of program", command) | ||
.usage("generate [--flags=value]") | ||
.flags(vec![Flag::new("type", "type of thing to generate".to_string()).short('t')]) | ||
} | ||
fn command(_args: CmdArgs) -> i32 { | ||
// let flags = use_flags(&args); | ||
// let result = CommandGenerator::generate(); | ||
// println!("{:?}", flags); | ||
0 | ||
|
||
fn command(args: CmdArgs) -> i32 { | ||
let flags = use_flags(&args); | ||
let t = flags.get("type").unwrap().clone(); | ||
|
||
if t.as_str() == "command" { | ||
let config = get_config(); | ||
|
||
if let Err(err) = fs::create_dir(&config.command_dir) { | ||
if err.kind() != ErrorKind::AlreadyExists { | ||
println!("Unknown error: {}", err.kind()); | ||
std::process::exit(1); | ||
} | ||
} | ||
|
||
let command_name = match use_flag("name", Some('n'), &args.args.flags).value { | ||
Some(value) => value, | ||
None => { | ||
eprintln!("flag name, is required"); | ||
std::process::exit(1); | ||
} | ||
}; | ||
let description = | ||
use_flag("desc", Some('d'), &args.args.flags).value.unwrap_or("todo...".to_string()); | ||
let file_writing = CommandGenerator::generate(config.clone(), &command_name, description); | ||
|
||
match file_writing { | ||
Ok(_) => { | ||
println!( | ||
"command {} written at {}/{}.rs", | ||
command_name, &config.command_dir, &command_name | ||
); | ||
println!("Note: This tool doesn't add the command to the main.rs file, coming soon..."); | ||
} | ||
Err(err) => println!("Unknown Error: {}", err), | ||
} | ||
} else if t.as_str() == "flag" { | ||
println!("Generating flags"); | ||
}; | ||
0 | ||
} |
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 +1,2 @@ | ||
pub mod generate; | ||
pub mod new; |
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,22 @@ | ||
use clier::{ | ||
command::{CmdArgs, Command}, | ||
hooks::{use_flags, Flag}, | ||
}; | ||
|
||
use crate::app::generators::ProjectGenerator; | ||
|
||
const NAME: &str = "new"; | ||
const DESCRIPTION: &str = "todo..."; | ||
|
||
pub fn new_command() -> Command { | ||
Command::new(NAME, DESCRIPTION, command).flags(vec![ | ||
Flag::new("name", "Name of the project".to_string()).short('n'), | ||
Flag::new("desc", "Description".to_string()).short('d'), | ||
]) | ||
} | ||
|
||
fn command(args: CmdArgs) -> i32 { | ||
let flags = use_flags(&args); | ||
ProjectGenerator::generate(flags.get("name").unwrap(), flags.get("desc").unwrap()); | ||
0 | ||
} |
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,27 +1,39 @@ | ||
use super::Generator; | ||
use serde::{Deserialize, Serialize}; | ||
use std::io::prelude::*; | ||
use super::Config; | ||
use std::{fs::File, io::Write}; | ||
|
||
pub struct CommandGenerator; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
struct Config { | ||
command_dir: Box<str>, | ||
} | ||
impl CommandGenerator { | ||
pub fn generate( | ||
config: Config, | ||
name: impl Into<String>, | ||
description: impl Into<String>, | ||
) -> Result<(), std::io::Error> { | ||
let name = name.into(); | ||
let description = description.into(); | ||
|
||
let file_path = format!("{}/{}.rs", &config.command_dir, name); | ||
let mut file = File::create(file_path).unwrap(); | ||
let file_writing = file.write_all( | ||
format!( | ||
"use clier::command::{{CmdArgs, Command}}; | ||
const NAME: &'static str = \"{name}\"; | ||
const DESCRIPTION: &'static str = \"{description}\"; | ||
impl Generator for CommandGenerator { | ||
fn generate() -> Result<(), ()> { | ||
let path = CommandGenerator::find_path().unwrap(); | ||
let mut config = String::from(""); | ||
let _ = std::fs::File::open(path) | ||
.unwrap() | ||
.read_to_string(&mut config) | ||
.expect("Could not read file"); | ||
pub fn {name}_command() -> Command {{ | ||
Command::new(NAME, DESCRIPTION, command) | ||
}} | ||
let config: Config = serde_json::from_str(&config).unwrap(); | ||
let dir = config.clone().command_dir; | ||
fn command(args: CmdArgs) -> i32 {{ | ||
println!(\"Hello World\"); | ||
0 | ||
}} | ||
", | ||
) | ||
.as_bytes(), | ||
); | ||
|
||
println!("{dir}"); | ||
Ok(()) | ||
} | ||
file_writing | ||
} | ||
} |
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,37 +1,41 @@ | ||
pub mod command; | ||
use std::{ | ||
fs::{read_dir, DirEntry, File}, | ||
io::BufReader, | ||
path::Path, | ||
}; | ||
|
||
use std::{env::current_dir, path::PathBuf}; | ||
use serde::{Deserialize, Serialize}; | ||
mod command; | ||
pub use command::*; | ||
mod project; | ||
pub use project::*; | ||
|
||
pub trait Generator { | ||
fn generate() -> Result<(), ()>; | ||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Config { | ||
pub command_dir: String, | ||
} | ||
|
||
fn find_path() -> Result<PathBuf, ()> { | ||
let path = current_dir().unwrap(); | ||
let mut item: Option<PathBuf> = None; | ||
path.ancestors().for_each(|v| { | ||
for dir in v.read_dir().into_iter() { | ||
for file in dir.into_iter().flatten() { | ||
if file.file_name() == "clier.config.json" { | ||
item = Some(file.path()); | ||
} | ||
} | ||
} | ||
}); | ||
pub fn get_config() -> Config { | ||
let dir = read_dir(".").unwrap(); | ||
let mut config_file: Option<DirEntry> = None; | ||
|
||
let mut right = false; | ||
for i in dir { | ||
if config_file.is_some() { | ||
break; | ||
} | ||
let i = i.unwrap(); | ||
if i.file_name() == "clier.config.json" { | ||
config_file = Some(i); | ||
} | ||
} | ||
|
||
for i in | ||
item.clone().into_iter().next().unwrap().parent().unwrap().read_dir().unwrap().flatten() | ||
{ | ||
if i.file_name() == "Cargo.toml" { | ||
right = true; | ||
} | ||
} | ||
let Some(file_config) = config_file else { | ||
eprintln!("clier.config.json could not be found"); | ||
std::process::exit(1); | ||
}; | ||
|
||
if right { | ||
item.ok_or(()) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
let file = File::open(Path::new(&file_config.path())).unwrap(); | ||
let reader = BufReader::new(file); | ||
|
||
serde_json::from_reader(reader).unwrap() | ||
} |
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,41 @@ | ||
use std::fs::{create_dir, File}; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
pub struct ProjectGenerator; | ||
|
||
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
impl ProjectGenerator { | ||
pub fn generate(name: impl Into<String>, description: impl Into<String>) { | ||
let name = name.into(); | ||
let description = description.into(); | ||
let path = Path::new(&name); | ||
|
||
if !path.exists() { | ||
let _ = create_dir(path); | ||
} | ||
|
||
let _ = create_dir(path.join("src")); | ||
|
||
let _ = File::create(path.join("Cargo.toml")).unwrap().write_all( | ||
format!( | ||
" | ||
[package] | ||
name = \"{name}\" | ||
version = \"0.0.1\" | ||
edition = \"2021\" | ||
description = \"{description}\" | ||
[dependencies] | ||
clier = \"{PKG_VERSION}\" | ||
", | ||
) | ||
.as_bytes(), | ||
); | ||
|
||
let _ = File::create(path.join("src/main.rs")) | ||
.unwrap() | ||
.write_all(include_bytes!("../../../examples/parser.rs")); | ||
} | ||
} |
Oops, something went wrong.