NoTeX 1.0.0
A modern noteworthy LaTeX template
Loading...
Searching...
No Matches
environment.cpp
1
9
10#include "notex/environment.hpp"
11
12#include "notex/errors.hpp"
13#include "notex/logging.hpp"
14#include "notex/system.hpp"
15
16namespace notex {
17
18std::string_view to_string(InstallationType type) {
19 switch (type) {
21 return "none";
23 return "local";
25 return "global";
26 }
27 return "none";
28}
29
30std::filesystem::path Environment::resolve_texmf_home() {
31 if (const auto env_value = system::get_env("TEXMFHOME");
32 env_value.has_value() && !env_value->empty()) {
33 PLOG_DEBUG << "Resolved TEXMFHOME from the environment variable: "
34 << *env_value;
35 return std::filesystem::path(*env_value);
36 }
37
38 PLOG_DEBUG << "TEXMFHOME is not set; falling back to "
39 "'kpsewhich -var-value=TEXMFHOME'.";
40 try {
41 const std::string kpsewhich_output =
42 system::run_command("kpsewhich -var-value=TEXMFHOME");
43 if (!kpsewhich_output.empty()) {
44 PLOG_DEBUG << "Resolved TEXMFHOME via kpsewhich: "
45 << kpsewhich_output;
46 return std::filesystem::path(kpsewhich_output);
47 }
48 } catch (const NotexError& e) {
49 // kpsewhich is absent or failed; fall through to the error below,
50 // which reports both resolution paths having failed.
51 PLOG_DEBUG << "kpsewhich fallback failed: " << e.what();
52 }
53
54 PLOG_WARNING << "Could not resolve TEXMFHOME from either the "
55 "environment variable or kpsewhich.";
56 throw EnvironmentError(
57 "could not resolve TEXMFHOME: set the TEXMFHOME environment "
58 "variable, or make sure 'kpsewhich' is available on PATH.");
59}
60
61Environment::Environment(std::filesystem::path start_dir)
62 : texmf_home_(resolve_texmf_home()),
63 global_latex_dir_(texmf_home_ / "tex" / "latex" / "notex"),
64 global_fonts_dir_(texmf_home_ / "fonts" / "truetype" / "notex"),
65 local_latex_dir_(start_dir / "settings"),
66 local_fonts_dir_(start_dir / "fonts") {
67 const bool local_present =
68 std::filesystem::exists(local_latex_dir_ / "notex.cls");
69 const bool global_present =
70 std::filesystem::exists(global_latex_dir_ / "notex.cls");
71
72 if (local_present) {
73 installation_type_ = InstallationType::LOCAL;
74 } else if (global_present) {
75 installation_type_ = InstallationType::GLOBAL;
76 } else {
77 installation_type_ = InstallationType::NONE;
78 }
79 PLOG_DEBUG << "Installation type detected: "
80 << to_string(installation_type_);
81}
82
83} // namespace notex
Environment(std::filesystem::path start_dir=std::filesystem::current_path())
Resolves TEXMFHOME and checks start_dir for a local installation.
Environment: discovers where TeX lives and whether NoTeX is installed.
Exception hierarchy and process exit codes used throughout NoTeX.
Logging utility with plog initialization.
std::optional< std::string > get_env(const std::string &name)
Reads an environment variable.
Definition system.cpp:52
std::string run_command(const std::string &command)
Runs command through the shell and returns everything it wrote to standard output (and standard error...
Definition system.cpp:19
@ LOCAL
A local installation exists (takes precedence over global).
@ GLOBAL
Only a global installation exists.
@ NONE
Neither a local nor a global installation was found.
std::string_view to_string(InstallationType type)
Process execution and environment-variable access.