summaryrefslogtreecommitdiff
path: root/src/buildtool/build_engine/base_maps/entity_name.hpp
blob: 12fbd6ee417c2e8525a4680fca99ec105b2a57db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_BASE_MAPS_ENTITY_NAME_HPP
#define INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_BASE_MAPS_ENTITY_NAME_HPP

#include <filesystem>
#include <optional>
#include <utility>

#include "nlohmann/json.hpp"
#include "gsl-lite/gsl-lite.hpp"
#include "src/buildtool/build_engine/base_maps/entity_name_data.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/common/repository_config.hpp"
#include "src/utils/cpp/hash_combine.hpp"

namespace BuildMaps::Base {

[[nodiscard]] inline auto ParseEntityNameFromJson(
    nlohmann::json const& json,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        if (json.is_string()) {
            return EntityName{current.repository,
                              current.module,
                              json.template get<std::string>()};
        }
        if (json.is_array() and json.size() == 2 and json[0].is_string() and
            json[1].is_string()) {
            return EntityName{current.repository,
                              json[0].template get<std::string>(),
                              json[1].template get<std::string>()};
        }
        if (json.is_array() and json.size() == 3 and json[0].is_string() and
            json[0].template get<std::string>() ==
                EntityName::kFileLocationMarker and
            json[2].is_string()) {
            auto name = json[2].template get<std::string>();
            if (json[1].is_null()) {
                return EntityName{
                    current.repository, current.module, name, true};
            }
            if (json[1].is_string()) {
                auto middle = json[1].template get<std::string>();
                if (middle == "." or middle == current.module) {
                    return EntityName{
                        current.repository, current.module, name, true};
                }
            }
            if (logger) {
                (*logger)(
                    fmt::format("Invalid module name {} for file reference",
                                json[1].dump()));
            }
        }
        else if (json.is_array() and json.size() == 3 and
                 json[0].is_string() and
                 json[0].template get<std::string>() ==
                     EntityName::kRelativeLocationMarker and
                 json[1].is_string() and json[2].is_string()) {
            auto relmodule = json[1].template get<std::string>();
            auto name = json[2].template get<std::string>();

            std::filesystem::path m{current.module};
            auto module = (m / relmodule).lexically_normal().string();
            if (module.compare(0, 3, "../") != 0) {
                return EntityName{current.repository, module, name};
            }
            if (logger) {
                (*logger)(fmt::format(
                    "Relative module name {} is outside of workspace",
                    relmodule));
            }
        }
        else if (json.is_array() and json.size() == 3 and
                 json[0].is_string() and
                 json[0].template get<std::string>() ==
                     EntityName::kAnonymousMarker) {
            if (logger) {
                (*logger)(fmt::format(
                    "Parsing anonymous target from JSON is not supported."));
            }
        }
        else if (json.is_array() and json.size() == 4 and
                 json[0].is_string() and
                 json[0].template get<std::string>() ==
                     EntityName::kLocationMarker and
                 json[1].is_string() and json[2].is_string() and
                 json[3].is_string()) {
            auto local_repo_name = json[1].template get<std::string>();
            auto module = json[2].template get<std::string>();
            auto target = json[3].template get<std::string>();
            auto const* repo_name = RepositoryConfig::Instance().GlobalName(
                current.repository, local_repo_name);
            if (repo_name != nullptr) {
                return EntityName{*repo_name, module, target};
            }
            if (logger) {
                (*logger)(fmt::format("Cannot resolve repository name {}",
                                      local_repo_name));
            }
        }
        else if (logger) {
            (*logger)(fmt::format("Syntactically invalid entity name: {}.",
                                  json.dump()));
        }
    } catch (...) {
    }
    return std::nullopt;
}

[[nodiscard]] inline auto ParseEntityNameFromExpression(
    ExpressionPtr const& expr,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        if (expr) {
            if (expr->IsString()) {
                return EntityName{current.repository,
                                  current.module,
                                  expr->Value<std::string>()->get()};
            }
            if (expr->IsList()) {
                auto const& list = expr->Value<Expression::list_t>()->get();
                if (list.size() == 2 and list[0]->IsString() and
                    list[1]->IsString()) {
                    return EntityName{current.repository,
                                      list[0]->Value<std::string>()->get(),
                                      list[1]->Value<std::string>()->get()};
                }
                if (list.size() == 3 and list[0]->IsString() and
                    list[0]->String() == EntityName::kFileLocationMarker and
                    list[2]->IsString()) {
                    auto name = list[2]->Value<std::string>()->get();
                    if (list[1]->IsNone()) {
                        return EntityName{
                            current.repository, current.module, name, true};
                    }
                    if (list[1]->IsString() and
                        (list[1]->String() == "." or
                         list[1]->String() == current.module)) {
                        return EntityName{
                            current.repository, current.module, name, true};
                    }
                    if (logger) {
                        (*logger)(fmt::format(
                            "Invalid module name {} for file reference",
                            list[1]->ToString()));
                    }
                }
                else if (list.size() == 3 and list[0]->IsString() and
                         list[0]->String() ==
                             EntityName::kRelativeLocationMarker and
                         list[1]->IsString() and list[2]->IsString()) {
                    std::filesystem::path m{current.module};
                    auto module =
                        (m / (list[1]->String())).lexically_normal().string();
                    if (module.compare(0, 3, "../") != 0) {
                        return EntityName{
                            current.repository, module, list[2]->String()};
                    }
                    if (logger) {
                        (*logger)(fmt::format(
                            "Relative module name {} is outside of workspace",
                            list[1]->String()));
                    }
                }
                else if (list.size() == 3 and list[0]->IsString() and
                         list[0]->String() ==
                             EntityName::kRelativeLocationMarker and
                         list[1]->IsMap() and list[2]->IsNode()) {
                    return EntityName{AnonymousTarget{list[1], list[2]}};
                }
                else if (list.size() == 4 and list[0]->IsString() and
                         list[0]->String() == EntityName::kLocationMarker and
                         list[1]->IsString() and list[2]->IsString() and
                         list[3]->IsString()) {
                    auto const* repo_name =
                        RepositoryConfig::Instance().GlobalName(
                            current.repository, list[1]->String());
                    if (repo_name != nullptr) {
                        return EntityName{
                            *repo_name, list[2]->String(), list[3]->String()};
                    }
                    if (logger) {
                        (*logger)(
                            fmt::format("Cannot resolve repository name {}",
                                        list[1]->String()));
                    }
                }
                else if (logger) {
                    (*logger)(
                        fmt::format("Syntactically invalid entity name: {}.",
                                    expr->ToString()));
                }
            }
        }
    } catch (...) {
    }
    return std::nullopt;
}

}  // namespace BuildMaps::Base

#endif