MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
sys.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <optional>
5#include <stdexcept>
6#include <string>
7#include <string_view>
8
9#ifdef _WIN32
10# define MIM_WHICH "where"
11#else
12# define MIM_WHICH "which"
13#endif
14
15namespace mim {
16
17namespace fs = std::filesystem;
18
19namespace sys {
20
21/// Yields `std::nullopt` if an error occurred.
22/// The returned path is adjusted so `<parent>/mim` resolves to the default in-tree plugin directory when possible.
23std::optional<fs::path> path_to_libmim();
24
25/// Executes command @p cmd.
26/// @returns the output as string.
27std::string exec(std::string cmd);
28
29std::string find_cmd(std::string);
30
31/// Thrown by sys::require_cmd when a command cannot be located on the system.
32class CmdNotFound : public std::logic_error {
33public:
34 CmdNotFound(const std::string& s)
35 : std::logic_error(s) {}
36};
37
38/// Locates @p name on the system or throws CmdNotFound.
39std::string require_cmd(std::string_view name);
40
41/// Wraps `std::system` and makes the return value usable.
42int system(std::string);
43
44/// Runs @p cmd via sys::system and throws if it exits with a non-zero status.
45void require_run(const std::string& cmd);
46
47/// Wraps sys::system and puts `.exe` at the back (Windows) and `./` at the front (otherwise) of @p cmd.
48int run(std::string cmd, std::string args = {});
49
50/// Returns the @p path as `std::string` and escapes all whitespaces with backslash.
51std::string escape(const std::filesystem::path& path);
52
53} // namespace sys
54} // namespace mim
CmdNotFound(const std::string &s)
Definition sys.h:34
Definition sys.h:19
int run(std::string cmd, std::string args={})
Wraps sys::system and puts .exe at the back (Windows) and ./ at the front (otherwise) of cmd.
Definition sys.cpp:117
void require_run(const std::string &cmd)
Runs cmd via sys::system and throws if it exits with a non-zero status.
Definition sys.cpp:113
int system(std::string)
Wraps std::system and makes the return value usable.
Definition sys.cpp:107
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
Definition sys.cpp:126
std::string find_cmd(std::string)
Definition sys.cpp:95
std::string require_cmd(std::string_view name)
Locates name on the system or throws CmdNotFound.
Definition sys.cpp:101
std::optional< fs::path > path_to_libmim()
Yields std::nullopt if an error occurred.
Definition sys.cpp:52
std::string exec(std::string cmd)
Executes command cmd.
Definition sys.cpp:83
Definition ast.h:14
Definition span.h:126