Skip to content

Commit 2b5ceef

Browse files
committed
reviews + update
1 parent d7a5969 commit 2b5ceef

50 files changed

Lines changed: 307 additions & 395 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/vix/cli/CLI.hpp

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,5 @@
1-
/**
2-
* @file CLI.hpp
3-
* @brief Declaration of the Vix.cpp Command Line Interface (CLI) class.
4-
*
5-
* The `Vix::CLI` class provides the entry point for interacting with the Vix.cpp
6-
* framework via command line. It manages the registration and execution of
7-
* developer commands such as project creation, building, and running applications.
8-
*
9-
* ## Overview
10-
* The CLI parses user input, maps commands to corresponding handlers,
11-
* and executes them dynamically. It supports built-in commands (`help`, `version`)
12-
* as well as modular commands like `new`, `build`, and `run`.
13-
*
14-
* ## Key Responsibilities
15-
* - Register and manage available CLI commands.
16-
* - Parse and dispatch command-line arguments.
17-
* - Handle errors gracefully and log them through the global `Logger`.
18-
*
19-
* ## Example
20-
* ```bash
21-
* vix new myapp
22-
* vix build myapp --config Release
23-
* vix run myapp -- --port 8080
24-
* ```
25-
*
26-
* @note
27-
* This class is lightweight and self-contained.
28-
* Command handlers are defined in separate modules (e.g. `NewCommand`, `RunCommand`).
29-
*
30-
* @namespace Vix
31-
* @class CLI
32-
* @version 0.1.0
33-
* @date 2025
34-
* @authors
35-
* SoftAdAstra
36-
*/
37-
38-
#ifndef VIX_CLI_HPP
39-
#define VIX_CLI_HPP
1+
#ifndef CLI_HPP
2+
#define CLI_HPP
403

414
#include <string>
425
#include <unordered_map>
@@ -46,63 +9,17 @@
469

4710
namespace vix
4811
{
49-
/**
50-
* @class CLI
51-
* @brief Core class implementing the Vix.cpp command-line interface.
52-
*
53-
* The CLI acts as the main command dispatcher. It holds a map of command names
54-
* and associated function handlers, enabling modular and extensible command logic.
55-
*/
5612
class CLI
5713
{
5814
public:
59-
/**
60-
* @brief Constructs the CLI and registers all available commands.
61-
*
62-
* Initializes default commands (`help`, `version`) as well as
63-
* user-facing commands (`new`, `build`, `run`) and helpful aliases.
64-
*/
6515
CLI();
66-
67-
/**
68-
* @brief Entry point for executing a CLI command.
69-
*
70-
* Parses the provided command-line arguments, matches the command name,
71-
* and executes the corresponding handler.
72-
*
73-
* @param argc Number of command-line arguments.
74-
* @param argv Array of argument strings.
75-
* @return Exit code (0 for success, non-zero for error).
76-
*
77-
* @note Displays help automatically if no arguments are provided.
78-
*/
7916
int run(int argc, char **argv);
8017

8118
private:
82-
/// Type alias for command handler functions.
8319
using CommandHandler = std::function<int(const std::vector<std::string> &)>;
8420

85-
/// Internal registry mapping command names to their handlers.
8621
std::unordered_map<std::string, CommandHandler> commands_;
87-
88-
/**
89-
* @brief Displays all available commands and usage examples.
90-
*
91-
* Called when the user runs `vix help` or uses `-h` / `--help`.
92-
*
93-
* @param args Optional arguments (ignored).
94-
* @return Always returns 0.
95-
*/
9622
int help(const std::vector<std::string> &args);
97-
98-
/**
99-
* @brief Prints the current CLI version and author information.
100-
*
101-
* Called when the user runs `vix version` or uses `-v` / `--version`.
102-
*
103-
* @param args Optional arguments (ignored).
104-
* @return Always returns 0.
105-
*/
10623
int version(const std::vector<std::string> &args);
10724
};
10825
}

include/vix/cli/ErrorHandler.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// ============================================================
2-
// File: modules/cli/include/vix/cli/ErrorHandler.hpp
3-
// ============================================================
4-
#pragma once
1+
#ifndef ERROR_HANDLER_HPP
2+
#define ERROR_HANDLER_HPP
53

64
#include <filesystem>
75
#include <string>
@@ -10,19 +8,14 @@ namespace vix::cli
108
{
119
namespace fs = std::filesystem;
1210

13-
/// High-level error reporting helper for the Vix CLI.
14-
///
15-
/// Responsibilities:
16-
/// - Parse raw compiler / linker logs (Clang/GCC-style).
17-
/// - Detect "known" patterns and show friendly, colored explanations.
18-
/// - Fall back to compact error list when no special pattern matches.
1911
class ErrorHandler
2012
{
2113
public:
22-
/// Parse a build log and print a friendly summary to stderr.
2314
static void printBuildErrors(
2415
const std::string &buildLog,
2516
const fs::path &sourceFile,
2617
const std::string &contextMessage = "Script build failed");
2718
};
2819
} // namespace vix::cli
20+
21+
#endif

include/vix/cli/Style.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef VIX_CLI_STYLE_HPP
2-
#define VIX_CLI_STYLE_HPP
1+
#ifndef CLI_STYLE_HPP
2+
#define CLI_STYLE_HPP
33

44
#include <iostream>
55
#include <string>
@@ -59,4 +59,4 @@ namespace vix::cli::style
5959

6060
} // namespace vix::cli::style
6161

62-
#endif // VIX_CLI_STYLE_HPP
62+
#endif // CLI_STYLE_HPP

include/vix/cli/Utils.hpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#pragma once
1+
#ifndef UTILS_HPP
2+
#define UTILS_HPP
23

34
#include <filesystem>
45
#include <fstream>
@@ -13,15 +14,10 @@ namespace vix::cli::util
1314
{
1415
namespace fs = std::filesystem;
1516

16-
// Écrit un fichier texte de manière sûre : fichier temporaire puis renommage.
17-
// - Crée les répertoires parents si nécessaires.
18-
// - Utilise un fichier .tmp dans le même dossier pour éviter les problèmes de volume.
19-
// - Tente un remplacement atomique (selon l’OS / FS).
2017
inline void write_text_file(const fs::path &p, std::string_view content)
2118
{
2219
std::error_code ec;
2320

24-
// 1) Assure l'existence du parent (si applicable)
2521
const fs::path parent = p.parent_path();
2622
if (!parent.empty())
2723
{
@@ -34,7 +30,6 @@ namespace vix::cli::util
3430
}
3531
}
3632

37-
// 2) Fichier temporaire à côté de la cible (avec quelques tentatives)
3833
auto make_tmp_name = [&]()
3934
{
4035
std::mt19937_64 rng{std::random_device{}()};
@@ -55,12 +50,11 @@ namespace vix::cli::util
5550
}
5651
}
5752

58-
// 3) Écriture (binaire) + flush + close
5953
{
6054
std::ofstream ofs(tmp, std::ios::binary | std::ios::trunc);
6155
if (!ofs)
6256
{
63-
fs::remove(tmp, ec); // best-effort cleanup
57+
fs::remove(tmp, ec);
6458
throw std::runtime_error(
6559
"Cannot open temp file for write: " + tmp.string());
6660
}
@@ -80,36 +74,31 @@ namespace vix::cli::util
8074
fs::remove(tmp, ec);
8175
throw std::runtime_error("Failed to flush file: " + tmp.string());
8276
}
83-
// fermeture via le destructeur
8477
}
8578

86-
// 4) Rename → p (tentative atomique). Sous Windows, si p existe, rename peut échouer.
8779
fs::rename(tmp, p, ec);
8880
if (ec)
8981
{
90-
// Essaye de supprimer la cible existante puis renommer à nouveau
91-
fs::remove(p, ec); // ignorer l'erreur (si absent / verrouillé)
82+
fs::remove(p, ec);
9283
ec.clear();
9384
fs::rename(tmp, p, ec);
9485
if (ec)
9586
{
9687
std::error_code ec2;
97-
fs::remove(tmp, ec2); // éviter les orphelins
88+
fs::remove(tmp, ec2);
9889
throw std::runtime_error(
9990
"Failed to move temp file to destination: " +
10091
tmp.string() + "" + p.string() + "" + ec.message());
10192
}
10293
}
10394
}
10495

105-
// Renvoie true si le répertoire est vide ou n'existe pas; false sinon.
106-
// N'émet **pas** d'exception : utile dans les checks préalables.
10796
inline bool is_dir_empty(const fs::path &p) noexcept
10897
{
10998
std::error_code ec;
11099

111100
if (!fs::exists(p, ec))
112-
return true; // inexistant = considéré vide
101+
return true;
113102
if (ec)
114103
return false;
115104
if (!fs::is_directory(p, ec))
@@ -123,9 +112,6 @@ namespace vix::cli::util
123112
return (it == fs::directory_iterator{});
124113
}
125114

126-
// Récupère la valeur de --dir / -d si présente.
127-
// Supporte: "-d PATH", "--dir PATH", et "--dir=PATH".
128-
// Évite de prendre une autre option comme valeur (ex: "-d --flag").
129115
inline std::optional<std::string> pick_dir_opt(
130116
const std::vector<std::string> &args,
131117
std::string_view shortOpt = "-d",
@@ -146,16 +132,13 @@ namespace vix::cli::util
146132
{
147133
return args[i + 1];
148134
}
149-
// option sans valeur → ignorée (caller gère le défaut)
150135
return std::nullopt;
151136
}
152137

153-
// format --dir=/chemin
154138
const std::string prefix(longOpt);
155139
if (!prefix.empty() && a.rfind(prefix + "=", 0) == 0)
156140
{
157141
std::string val = a.substr(prefix.size() + 1);
158-
// autoriser --dir="" (revient à std::nullopt)
159142
if (val.empty())
160143
return std::nullopt;
161144
return val;
@@ -165,3 +148,5 @@ namespace vix::cli::util
165148
}
166149

167150
} // namespace vix::cli::util
151+
152+
#endif
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef VIX_BUILD_COMMAND_HPP
2-
#define VIX_BUILD_COMMAND_HPP
1+
#ifndef BUILD_COMMAND_HPP
2+
#define BUILD_COMMAND_HPP
33

44
#include <string>
55
#include <vector>
@@ -10,4 +10,4 @@ namespace vix::commands::BuildCommand
1010
int help();
1111
}
1212

13-
#endif // VIX_BUILD_COMMAND_HPP
13+
#endif // BUILD_COMMAND_HPP
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#pragma once
1+
#ifndef CHECK_COMMAND_HPP
2+
#define CHECK_COMMAND_HPP
3+
24
#include <string>
35
#include <vector>
46

@@ -7,3 +9,5 @@ namespace vix::commands::CheckCommand
79
int run(const std::vector<std::string> &args);
810
int help();
911
} // namespace vix::commands::CheckCommand
12+
13+
#endif
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
#pragma once
1+
#ifndef DEV_COMMAND_HPP
2+
#define DEV_COMMAND_HPP
23

34
#include <string>
45
#include <vector>
56

67
namespace vix::commands::DevCommand
78
{
8-
/// Entry point for `vix dev [args...]`.
9-
///
10-
/// High-level behaviour (v1):
11-
/// - Forwards all arguments to `vix run`, and ensures that a
12-
/// `--watch` / reload-friendly mode is enabled.
13-
/// - This makes `vix dev` a convenient alias for:
14-
/// vix run <args...> --watch
15-
///
16-
/// Future versions may implement a richer "dev server" experience:
17-
/// - file watching on source directories
18-
/// - smart rebuild / restart cycles
19-
/// - friendly URL + server banner
209
int run(const std::vector<std::string> &args);
21-
22-
/// Help text for `vix dev`.
23-
///
24-
/// Printed when the user calls:
25-
/// vix dev --help
26-
/// or:
27-
/// vix help dev
2810
int help();
2911
} // namespace vix::commands::DevCommand
12+
13+
#endif

include/vix/cli/commands/Dispatch.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#pragma once
1+
#ifndef DISPATCH_HPP
2+
#define DISPATCH_HPP
3+
24
#include <string>
35
#include <vector>
46
#include <functional>
@@ -18,29 +20,23 @@ namespace vix::cli::dispatch
1820
std::string summary; // one-liner shown in REPL help list
1921

2022
RunFn run;
21-
HelpFn help; // must exist
23+
HelpFn help;
2224
};
2325

2426
class Dispatcher
2527
{
2628
public:
2729
Dispatcher();
28-
2930
bool has(const std::string &cmd) const;
30-
31-
// normal execution
3231
int run(const std::string &cmd, const Args &args) const;
33-
34-
// help
3532
int help(const std::string &cmd) const;
36-
37-
// list all commands (for REPL help + completion later)
3833
const std::unordered_map<std::string, Entry> &entries() const noexcept;
3934

4035
private:
4136
std::unordered_map<std::string, Entry> map_;
4237
};
4338

44-
// Global singleton accessor (optional but convenient)
4539
Dispatcher &global();
4640
}
41+
42+
#endif
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#pragma once
1+
#ifndef INSTALL_COMMAND_HPP
2+
#define INSTALL_COMMAND_HPP
3+
24
#include <vector>
35
#include <string>
46

@@ -7,3 +9,5 @@ namespace vix::commands::InstallCommand
79
int run(const std::vector<std::string> &args);
810
int help();
911
}
12+
13+
#endif

0 commit comments

Comments
 (0)