-
Notifications
You must be signed in to change notification settings - Fork 0
Add new plugins system #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7988e0e
feat: enhance init codebase
LuernOutOfOrder bb684e9
refactor: remove unused project structure
LuernOutOfOrder f206994
fix: import all needed fn and mod
LuernOutOfOrder 1a441cc
feat: add basic plugins command and plugins health check
LuernOutOfOrder b885eb0
feat: update .gitignore
LuernOutOfOrder 6e2bca4
feat: add parsing plugin file and basic plugin struct
LuernOutOfOrder c06e104
feat: add basic creation of project with new plugin system
LuernOutOfOrder 8b5ed0a
fix: use correct path when creating new project but still error when …
LuernOutOfOrder 44661a2
feat(plugins): pass project name args to init command from plugins
LuernOutOfOrder 805d5a8
chore: bump version to 2.2.0
LuernOutOfOrder 2cdec6a
feat(docs): add usage and example in the plugins README
LuernOutOfOrder cca5293
feat(git): add misc section with ALL possible path for fucking DS_Sto…
LuernOutOfOrder debdc5d
fix(plugins): set name params to unused to avoid warning compilation …
LuernOutOfOrder d42aba4
fix: version to 2.12.0 instead of 2.2.0
LuernOutOfOrder 827829e
fix: inconsistence in code between command and help commands
LuernOutOfOrder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,28 @@ | ||
| # Here's the plugins directory used for plugin system | ||
|
|
||
| ## Usage | ||
|
|
||
| Add as many plugins as you want and add the name of it to the nyx's config file. The name of the plugin must match the name of the file without file extension. | ||
|
|
||
| ## Exampled | ||
|
|
||
| Simple example for a plugin file: | ||
|
|
||
| ```toml | ||
| [plugin] | ||
| name = "rust" | ||
| plugin_type = "Simple" | ||
| version = "0.1.0" | ||
| enabled = true | ||
|
|
||
| [core] | ||
| build_command = true | ||
| clean_command = true | ||
| run_command = true | ||
|
|
||
| [commands] | ||
| init_command = ["cargo", "init", "--bin"] | ||
| build_command = [] | ||
| clean_command = [] | ||
| run_command = [] | ||
| ``` |
This file contains hidden or 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 @@ | ||
| use std::env; | ||
|
|
||
| use crate::{nxfs, utils::log::log_from_log_level}; | ||
|
|
||
| pub fn init_command() { | ||
| let args: Vec<String> = env::args().collect(); | ||
| if args.len() <= 2 { | ||
| init_nyx(); | ||
| } | ||
| } | ||
|
|
||
| // Basic init of NYX | ||
| fn init_nyx() { | ||
| log_from_log_level(crate::nxfs::config::LogLevel::Info, "Initialize NYX.."); | ||
| nxfs::nxs::create_data(); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,122 @@ | ||
| use std::{ | ||
| env, fs, path::PathBuf, process::{Child, Command} | ||
| }; | ||
|
|
||
| use lrncore::{path::change_work_dir, usage_exit::command_usage}; | ||
| use parser::parse_plugin_file; | ||
| use serde::Deserialize; | ||
|
|
||
| use crate::{ | ||
| nxfs::config::{parse_config_file, LogLevel}, | ||
| utils::log::log_from_log_level, | ||
| }; | ||
|
|
||
| pub mod parser; | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| pub struct Plugin { | ||
| pub plugin: PluginSection, | ||
| pub core: CoreSection, | ||
| pub commands: CommandsSection, | ||
| } | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| pub struct PluginSection { | ||
| pub name: String, | ||
| pub version: String, | ||
| pub enabled: bool, | ||
| } | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| pub struct CoreSection { | ||
| pub build_command: bool, | ||
| pub clean_command: bool, | ||
| pub run_command: bool, | ||
| } | ||
|
|
||
| #[derive(Deserialize, Debug)] | ||
| pub struct CommandsSection { | ||
| pub init_command: Vec<String>, | ||
| pub build_command: Vec<String>, | ||
| pub clean_command: Vec<String>, | ||
| pub run_command: Vec<String>, | ||
| } | ||
|
|
||
| fn plugins_help() -> &'static str { | ||
| (r" | ||
| Usage: nyx plugins [subcommand] [arguments] [options] | ||
|
|
||
| Subcommands: | ||
| health Check plugins health | ||
|
|
||
| Options: | ||
| -h, --help Show this help message | ||
| ") as _ | ||
| } | ||
|
|
||
| pub fn plugins_command() { | ||
| let args: Vec<String> = env::args().collect(); | ||
| if args.len() <= 2 { | ||
| command_usage(plugins_help()); | ||
| } | ||
| match args[2].as_str() { | ||
| "health" => health_plugins(), | ||
| _ => { | ||
| command_usage(plugins_help()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check plugins installed | ||
| fn health_plugins() { | ||
| // Load config file | ||
| let config = parse_config_file().expect("Failed to parse config file"); | ||
| let mut invalid_plugins_number: u8 = 0; | ||
| // Iterate over plugins list | ||
| for each in config.plugins.list { | ||
| let file_path: &str = &format!("plugins/{each}.toml"); | ||
| if !fs::exists(file_path).expect("Failed to check path") { | ||
| invalid_plugins_number += 1; | ||
| log_from_log_level( | ||
| LogLevel::Warn, | ||
| format!("Failed to load plugin: {each}").as_str(), | ||
| ); | ||
| } else { | ||
| match parse_plugin_file(&each) { | ||
| Ok(_) => (), | ||
| Err(e) => { | ||
| log_from_log_level( | ||
| LogLevel::Warn, | ||
| format!("Failed to parse plugin: {each}").as_str(), | ||
| ); | ||
| eprintln!("{e}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // Log depending on number of invalid plugins | ||
| if invalid_plugins_number != 0 { | ||
| log_from_log_level( | ||
| LogLevel::Warn, | ||
| format!("Plugin unabled to be load: {invalid_plugins_number}").as_str(), | ||
| ); | ||
| } else { | ||
| log_from_log_level(LogLevel::Info, "Successfully load all plugins"); | ||
| } | ||
| } | ||
|
|
||
| /// Run init command from given plugin | ||
| pub fn run_init_command(plugin: Plugin, _name: &str, path: PathBuf) { | ||
| change_work_dir(path.as_os_str().to_str().expect("Failed to cast pathbuf to os_str to str")); | ||
| let mut init_commands_vec: Vec<String> = plugin.commands.init_command; | ||
| let bin = init_commands_vec.remove(0); | ||
| let mut command: Child = Command::new(bin) | ||
| .args(init_commands_vec) | ||
| .spawn() | ||
| .expect("Failed to fork process"); | ||
| let wait_command = command.wait().expect("Failed to wait forked process"); | ||
| if !wait_command.success() { | ||
| log_from_log_level(LogLevel::Error, "Failed to run init command"); | ||
| } | ||
| log_from_log_level(LogLevel::Info, "Successfully run init command"); | ||
| } | ||
This file contains hidden or 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 lrncore::path::change_work_dir; | ||
| use toml::de::Error as toml_error; | ||
|
|
||
| use crate::utils; | ||
|
|
||
| use super::Plugin; | ||
|
|
||
| /// Parse given plugin and return struct | ||
| pub fn parse_plugin_file(name: &str) -> Result<Plugin, toml_error> { | ||
| change_work_dir(&utils::env::get_nyx_env_var()); | ||
| let file_path: &str = &format!("plugins/{name}.toml"); | ||
| let file = | ||
| std::fs::read_to_string(file_path).expect("Failed to read the plugin file to string"); | ||
| let plugin: Plugin = match toml::from_str(&file) { | ||
| Ok(p) => p, | ||
| Err(e) => { | ||
| println!("Failed to parse the configuration file: {e:?}"); | ||
| return Err(e); | ||
| } | ||
| }; | ||
| Ok(plugin) | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.