From 437c3f60e2ff1960fc7b55de5fdf1b4682a94467 Mon Sep 17 00:00:00 2001 From: alandefreitas Date: Fri, 1 Dec 2023 20:19:51 -0300 Subject: [PATCH] feat: find addons relative to executable --- .github/workflows/ci.yml | 4 ++-- include/mrdocs/Support/Error.hpp | 2 +- include/mrdocs/Support/Path.hpp | 9 +++++++++ src/lib/Support/Path.cpp | 16 ++++++++++++++++ src/tool/Addons.cpp | 30 +++++++++++++++--------------- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4f02e7165..fee40b27e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -299,7 +299,7 @@ jobs: - name: Install mrdocs from release package run: | set -x - sudo find packages -name 'MrDocs-*-Linux.tar.gz' -exec tar -xzf {} -C /usr/local --strip-components=1 \; + sudo find packages -name 'MrDocs-*-Linux.tar.gz' -exec tar -vxzf {} -C /usr/local --strip-components=1 \; mrdocs --version - name: Clone Boost.URL @@ -358,7 +358,7 @@ jobs: [[ $variant = multi ]] && multiline="true" || multiline="false" printf "$config_template\n" $format $multiline > $(pwd)/boost/libs/url/mrdocs.yml mkdir -p "demos/boost-url/$variant/$format" - mrdocs --config="$(pwd)/boost/libs/url/mrdocs.yml" "$(pwd)/boost/libs/url/__build__/compile_commands.json" --addons="$(pwd)/share/mrdocs/addons" --output="$(pwd)/demos/boost-url/$variant/$format" + mrdocs --config="$(pwd)/boost/libs/url/mrdocs.yml" "$(pwd)/boost/libs/url/__build__/compile_commands.json" --output="$(pwd)/demos/boost-url/$variant/$format" done asciidoctor -R "$(pwd)/demos/boost-url/$variant/adoc" -D "$(pwd)/demos/boost-url/$variant/adoc-asciidoc" "$(pwd)/demos/boost-url/$variant/adoc/**/*.adoc" done diff --git a/include/mrdocs/Support/Error.hpp b/include/mrdocs/Support/Error.hpp index c7ffb2fbd..03ad0dd79 100644 --- a/include/mrdocs/Support/Error.hpp +++ b/include/mrdocs/Support/Error.hpp @@ -594,7 +594,7 @@ namespace detail /// Check existing expected-like type # define MRDOCS_CHECK_VOID(var) \ - if (!detail::failed(var)) { \ + if (detail::failed(var)) { \ return Unexpected(detail::error(var)); \ } \ void(0) diff --git a/include/mrdocs/Support/Path.hpp b/include/mrdocs/Support/Path.hpp index 719b9eff9..48c931df5 100644 --- a/include/mrdocs/Support/Path.hpp +++ b/include/mrdocs/Support/Path.hpp @@ -282,6 +282,15 @@ appendPath( std::string_view name2, std::string_view name3); +MRDOCS_DECL +std::string +appendPath( + std::string_view basePath, + std::string_view name1, + std::string_view name2, + std::string_view name3, + std::string_view name4); + /** Return an error if the path is not a directory. */ MRDOCS_DECL diff --git a/src/lib/Support/Path.cpp b/src/lib/Support/Path.cpp index 1d057f540..963086e75 100644 --- a/src/lib/Support/Path.cpp +++ b/src/lib/Support/Path.cpp @@ -324,6 +324,22 @@ appendPath( return static_cast(temp.str()); } +std::string +appendPath( + std::string_view basePath, + std::string_view name1, + std::string_view name2, + std::string_view name3, + std::string_view name4) +{ + namespace path = llvm::sys::path; + + SmallPathString temp(makeDirsy(basePath)); + path::append(temp, name1, name2, name3, name4); + path::remove_dots(temp, true); + return static_cast(temp.str()); +} + Error requireDirectory( std::string_view pathName) diff --git a/src/tool/Addons.cpp b/src/tool/Addons.cpp index 02a2e3a7a..dccc0bdda 100644 --- a/src/tool/Addons.cpp +++ b/src/tool/Addons.cpp @@ -36,26 +36,26 @@ setupAddonsDir( return {}; } - // Set addons dir from process working directory - std::string addonsDir = llvm::sys::fs::getMainExecutable(argv0, addressOfMain); - MRDOCS_CHECK(addonsDir, "getMainExecutable failed"); - addonsDir = files::makeDirsy(files::appendPath( - files::getParentDir(addonsDir), "addons")); - Error err = files::requireDirectory(addonsDir); - if (!err.failed()) + // Set addons dir from environment variable + auto addonsEnvVar = llvm::sys::Process::GetEnv("MRDOCS_ADDONS_DIR"); + if (addonsEnvVar) { + MRDOCS_CHECK(*addonsEnvVar, "MRDOCS_ADDONS_DIR is empty"); + std::string addonsDir = files::makeDirsy(files::normalizePath(*addonsEnvVar)); + MRDOCS_TRY(files::requireAbsolute(addonsDir)); + MRDOCS_TRY(files::requireDirectory(addonsDir)); addonsDirArg.getValue() = addonsDir; return {}; } - // Set addons dir from environment variable - MRDOCS_TRY( - std::string addonsEnvVar, - llvm::sys::Process::GetEnv("MRDOCS_ADDONS_DIR"), - "no MRDOCS_ADDONS_DIR in environment"); - addonsDir = files::makeDirsy(files::normalizePath(addonsEnvVar)); - MRDOCS_TRY(files::requireAbsolute(addonsDir)); - MRDOCS_TRY(files::requireDirectory(addonsDir)); + // Set addons dir from process working directory + std::string execPath = llvm::sys::fs::getMainExecutable(argv0, addressOfMain); + MRDOCS_CHECK(execPath, "getMainExecutable failed"); + std::string binDir = files::getParentDir(execPath); + std::string addonsDir = files::makeDirsy(files::appendPath( + binDir, "..", "share", "mrdocs", "addons")); + Error err = files::requireDirectory(addonsDir); + MRDOCS_CHECK(err); addonsDirArg.getValue() = addonsDir; return {}; }