1/**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "importPathManager.h"
17#include <libpandabase/os/filesystem.h>
18
19#ifdef USE_UNIX_SYSCALL
20#include <dirent.h>
21#include <sys/types.h>
22#include <unistd.h>
23#else
24#if __has_include(<filesystem>)
25#include <filesystem>
26namespace fs = std::filesystem;
27#elif __has_include(<experimental/filesystem>)
28#include <experimental/filesystem>
29namespace fs = std::experimental::filesystem;
30#endif
31#endif
32namespace ark::es2panda::util {
33
34constexpr size_t SUPPORTED_INDEX_FILES_SIZE = 2;
35constexpr size_t SUPPORTED_EXTENSIONS_SIZE = 2;
36
37static bool IsCompatibleExtension(const std::string &extension)
38{
39    return extension == ".sts" || extension == ".ts";
40}
41
42StringView ImportPathManager::ResolvePath(const StringView &currentModulePath, const StringView &importPath) const
43{
44    if (importPath.Empty()) {
45        throw Error(ErrorType::GENERIC, "", "Import path cannot be empty");
46    }
47
48    if (IsRelativePath(importPath)) {
49        const size_t pos = currentModulePath.Mutf8().find_last_of(pathDelimiter_);
50        ASSERT(pos != std::string::npos);
51
52        auto currentDirectory = currentModulePath.Mutf8().substr(0, pos);
53        auto resolvedPath = UString(currentDirectory, allocator_);
54        resolvedPath.Append(pathDelimiter_);
55        resolvedPath.Append(importPath.Mutf8());
56
57        return AppendExtensionOrIndexFileIfOmitted(resolvedPath.View());
58    }
59
60    std::string baseUrl;
61    if (importPath.Mutf8()[0] == pathDelimiter_.at(0)) {
62        baseUrl = arktsConfig_->BaseUrl();
63        baseUrl.append(importPath.Mutf8(), 0, importPath.Mutf8().length());
64        return AppendExtensionOrIndexFileIfOmitted(UString(baseUrl, allocator_).View());
65    }
66
67    auto &dynamicPaths = arktsConfig_->DynamicPaths();
68    if (auto it = dynamicPaths.find(importPath.Mutf8()); it != dynamicPaths.cend() && !it->second.HasDecl()) {
69        return AppendExtensionOrIndexFileIfOmitted(importPath);
70    }
71
72    const size_t pos = importPath.Mutf8().find(pathDelimiter_);
73    bool containsDelim = (pos != std::string::npos);
74    auto rootPart = containsDelim ? importPath.Substr(0, pos) : importPath;
75    if (!stdLib_.empty() &&
76        (rootPart.Is("std") || rootPart.Is("escompat"))) {  // Get std or escompat path from CLI if provided
77        baseUrl = stdLib_ + pathDelimiter_.at(0) + rootPart.Mutf8();
78    } else {
79        ASSERT(arktsConfig_ != nullptr);
80        auto resolvedPath = arktsConfig_->ResolvePath(importPath.Mutf8());
81        if (!resolvedPath) {
82            throw Error(ErrorType::GENERIC, "",
83                        "Can't find prefix for '" + importPath.Mutf8() + "' in " + arktsConfig_->ConfigPath());
84        }
85
86        return AppendExtensionOrIndexFileIfOmitted(UString(resolvedPath.value(), allocator_).View());
87    }
88
89    if (containsDelim) {
90        baseUrl.append(1, pathDelimiter_.at(0));
91        baseUrl.append(importPath.Mutf8(), rootPart.Mutf8().length() + 1, importPath.Mutf8().length());
92    }
93
94    return UString(baseUrl, allocator_).View();
95}
96
97#ifdef USE_UNIX_SYSCALL
98void ImportPathManager::UnixWalkThroughDirectoryAndAddToParseList(const StringView &directoryPath,
99                                                                  const ImportFlags importFlags)
100{
101    DIR *dir = opendir(directoryPath.Mutf8().c_str());
102    if (dir == nullptr) {
103        throw Error(ErrorType::GENERIC, "", "Cannot open folder: " + directoryPath.Mutf8());
104    }
105
106    struct dirent *entry;
107    while ((entry = readdir(dir)) != nullptr) {
108        if (entry->d_type != DT_REG) {
109            continue;
110        }
111
112        std::string fileName = entry->d_name;
113        std::string::size_type pos = fileName.find_last_of('.');
114        if (pos == std::string::npos || !IsCompatibleExtension(fileName.substr(pos))) {
115            continue;
116        }
117
118        std::string filePath = directoryPath.Mutf8() + "/" + entry->d_name;
119        AddToParseList(UString(filePath, allocator_).View(), importFlags);
120    }
121
122    closedir(dir);
123    return;
124}
125#endif
126
127void ImportPathManager::AddToParseList(const StringView &resolvedPath, const ImportFlags importFlags)
128{
129    const bool isDefaultImport = (importFlags & ImportFlags::DEFAULT_IMPORT) != 0;
130    const bool isImplicitPackageImport = (importFlags & ImportFlags::IMPLICIT_PACKAGE_IMPORT) != 0;
131    const auto parseInfo = ParseInfo {resolvedPath, false, isImplicitPackageImport};
132
133    if (ark::os::file::File::IsDirectory(resolvedPath.Mutf8())) {
134#ifdef USE_UNIX_SYSCALL
135        UnixWalkThroughDirectoryAndAddToParseList(resolvedPath, importFlags);
136#else
137        for (auto const &entry : fs::directory_iterator(resolvedPath.Mutf8())) {
138            if (!fs::is_regular_file(entry) || !IsCompatibleExtension(entry.path().extension().string())) {
139                continue;
140            }
141
142            AddToParseList(UString(entry.path().string(), allocator_).View(), importFlags);
143        }
144        return;
145#endif
146    }
147
148    // Check if file has been already added to parse list
149    if (const auto &found =
150            std::find_if(parseList_.begin(), parseList_.end(),
151                         [&resolvedPath](const ParseInfo &info) { return (info.sourcePath == resolvedPath); });
152        found != parseList_.end()) {
153        // The 'parseList_' can contain at most 1 record with the same source file path (else it'll break things).
154        //
155        // If a file is added as implicit package imported before, then we may add it again without the implicit import
156        // directive (and remove the other one), to handle when an implicitly package imported file explicitly imports
157        // it. Re-parsing it is necessary, because if the implicitly package imported file contains a syntax error, then
158        // it'll be ignored, but we must not ignore it if an explicitly imported file contains a parse error. Also this
159        // addition can happen during parsing the files in the parse list, so re-addition is necessary in order to
160        // surely re-parse it.
161        //
162        // If a file was already not implicitly package imported, then it's just a duplicate, return
163        if (!found->isImplicitPackageImported) {
164            return;
165        }
166
167        parseList_.erase(found);
168    }
169
170    if (const auto &dynamicPaths = arktsConfig_->DynamicPaths();
171        dynamicPaths.find(resolvedPath.Mutf8()) != dynamicPaths.cend()) {
172        parseList_.emplace(parseList_.begin(), parseInfo);
173        return;
174    }
175
176    if (!ark::os::file::File::IsRegularFile(resolvedPath.Mutf8())) {
177        throw Error(ErrorType::GENERIC, "", "Not an available source path: " + resolvedPath.Mutf8());
178    }
179
180    // 'Object.sts' must be the first in the parse list
181    // NOTE (mmartin): still must be the first?
182    const std::size_t position = resolvedPath.Mutf8().find_last_of(pathDelimiter_);
183    if (isDefaultImport && resolvedPath.Substr(position + 1, resolvedPath.Length()).Is("Object.sts")) {
184        parseList_.emplace(parseList_.begin(), parseInfo);
185    } else {
186        parseList_.emplace_back(parseInfo);
187    }
188}
189
190ImportPathManager::ImportData ImportPathManager::GetImportData(const util::StringView &path,
191                                                               const ScriptExtension &extension) const
192{
193    const auto &dynamicPaths = arktsConfig_->DynamicPaths();
194    auto key = ark::os::NormalizePath(path.Mutf8());
195
196    auto it = dynamicPaths.find(key);
197    if (it == dynamicPaths.cend()) {
198        key = ark::os::RemoveExtension(key);
199    }
200
201    while (it == dynamicPaths.cend() && !key.empty()) {
202        it = dynamicPaths.find(key);
203        if (it != dynamicPaths.cend()) {
204            break;
205        }
206        key = ark::os::GetParentDir(key);
207    }
208
209    if (it != dynamicPaths.cend()) {
210        return {it->second.GetLanguage(), key, it->second.HasDecl()};
211    }
212
213    return {ToLanguage(extension), path.Mutf8(), true};
214}
215
216void ImportPathManager::MarkAsParsed(const StringView &path)
217{
218    for (auto &parseInfo : parseList_) {
219        if (parseInfo.sourcePath == path) {
220            parseInfo.isParsed = true;
221            return;
222        }
223    }
224}
225
226bool ImportPathManager::IsRelativePath(const StringView &path) const
227{
228    std::string currentDirReference = ".";
229    std::string parentDirReference = "..";
230
231    currentDirReference.append(pathDelimiter_);
232    parentDirReference.append(pathDelimiter_);
233
234    return ((path.Mutf8().find(currentDirReference) == 0) || (path.Mutf8().find(parentDirReference) == 0));
235}
236
237StringView ImportPathManager::GetRealPath(const StringView &path) const
238{
239    const std::string realPath = ark::os::GetAbsolutePath(path.Mutf8());
240    if (realPath.empty() || realPath == path.Mutf8()) {
241        return path;
242    }
243
244    return UString(realPath, allocator_).View();
245}
246
247StringView ImportPathManager::AppendExtensionOrIndexFileIfOmitted(const StringView &path) const
248{
249    StringView realPath = GetRealPath(path);
250    if (ark::os::file::File::IsRegularFile(realPath.Mutf8())) {
251        return realPath;
252    }
253
254    if (ark::os::file::File::IsDirectory(realPath.Mutf8())) {
255        // Supported index files: keep this checking order
256        std::array<std::string, SUPPORTED_INDEX_FILES_SIZE> supportedIndexFiles = {"index.sts", "index.ts"};
257        for (const auto &indexFile : supportedIndexFiles) {
258            std::string indexFilePath = realPath.Mutf8() + pathDelimiter_.data() + indexFile;
259            if (ark::os::file::File::IsRegularFile(indexFilePath)) {
260                return GetRealPath(UString(indexFilePath, allocator_).View());
261            }
262        }
263
264        return realPath;
265    }
266
267    // Supported extensions: keep this checking order
268    std::array<std::string, SUPPORTED_EXTENSIONS_SIZE> supportedExtensions = {".sts", ".ts"};
269
270    for (const auto &extension : supportedExtensions) {
271        if (ark::os::file::File::IsRegularFile(path.Mutf8() + extension)) {
272            return GetRealPath(UString(path.Mutf8().append(extension), allocator_).View());
273        }
274    }
275
276    auto &dynamicPaths = arktsConfig_->DynamicPaths();
277    if (auto it = dynamicPaths.find(path.Mutf8()); it != dynamicPaths.cend()) {
278        return path;
279    }
280
281    throw Error(ErrorType::GENERIC, "", "Not supported path: " + path.Mutf8());
282}
283
284}  // namespace ark::es2panda::util
285#undef USE_UNIX_SYSCALL
286