From 4b05b3faefe25f284909ca24b30180fa86aa172e Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Wed, 24 Apr 2024 15:31:28 +0200 Subject: just-mr rc: Extract location object parser in separate library This will make it available also to just. --- src/buildtool/common/location.cpp | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/buildtool/common/location.cpp (limited to 'src/buildtool/common/location.cpp') diff --git a/src/buildtool/common/location.cpp b/src/buildtool/common/location.cpp new file mode 100644 index 00000000..bc3a57dc --- /dev/null +++ b/src/buildtool/common/location.cpp @@ -0,0 +1,54 @@ +// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/buildtool/common/location.hpp" + +#include "fmt/core.h" +#include "src/buildtool/file_system/file_system_manager.hpp" +#include "src/buildtool/logging/log_level.hpp" +#include "src/buildtool/logging/logger.hpp" +#include "src/buildtool/storage/config.hpp" + +auto ReadLocationObject(nlohmann::json const& location, + std::optional const& ws_root) + -> std::variant> { + if (not location.contains("path") or not location.contains("root")) { + return fmt::format("Malformed location object: {}", location.dump(-1)); + } + auto root = location["root"].get(); + auto path = location["path"].get(); + auto base = location.contains("base") ? location["base"].get() + : std::string("."); + + std::filesystem::path root_path{}; + if (root == "workspace") { + if (not ws_root) { + Logger::Log(LogLevel::Warning, + "Not in workspace root, ignoring location {}.", + location.dump(-1)); + return std::nullopt; + } + root_path = *ws_root; + } + if (root == "home") { + root_path = StorageConfig::GetUserHome(); + } + if (root == "system") { + root_path = FileSystemManager::GetCurrentDirectory().root_path(); + } + return std::make_pair(std::filesystem::weakly_canonical( + std::filesystem::absolute(root_path / path)), + std::filesystem::weakly_canonical( + std::filesystem::absolute(root_path / base))); +} -- cgit v1.2.3