1/**
2 * Copyright (c) 2023-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#ifndef ES2PANDA_AOT_ARKTSCONFIG_H
17#define ES2PANDA_AOT_ARKTSCONFIG_H
18
19#include <memory>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24#include "util/language.h"
25
26// NOTE(ivagin): If ARKTSCONFIG_USE_FILESYSTEM is not defined part of ArkTsConfig functionality is disabled.
27//       Only build configuration which prevents us from usage of std::filesystem is "MOBILE" build
28//       because of lack of std::filesystem in our current version of mobile nativ development kit.
29//       nativ development kit version should be updated and ARKTSCONFIG_USE_FILESYSTEM removed
30#if not defined PANDA_TARGET_MOBILE
31#define ARKTSCONFIG_USE_FILESYSTEM
32#endif
33
34namespace ark::es2panda {
35
36class ArkTsConfig {
37public:
38#ifdef ARKTSCONFIG_USE_FILESYSTEM
39    // Pattern describes arktsconfig path pattern for 'include' or 'exclude' properties
40    // e.g. src/**, src/**/*, src/util?.ts
41    class Pattern {
42    public:
43        Pattern() = default;
44        Pattern(std::string value, std::string base);
45
46        bool IsPattern() const;
47
48        // Get root from which sources file search should be started
49        // e.g. src/** -> src; src/index?.ts -> src; src/component/*/index.ts -> src/component; src/index* -> src/
50        std::string GetSearchRoot() const;
51
52        // Test if absolute path is matched by pattern
53        bool Match(const std::string &path) const;
54
55    private:
56        std::string value_ {};
57        std::string base_ {};
58    };
59#endif  // ARKTSCONFIG_USE_FILESYSTEM
60
61    class DynamicImportData {
62    public:
63        explicit DynamicImportData(Language lang, bool hasDecl) : lang_(lang), hasDecl_(hasDecl) {}
64
65        Language GetLanguage() const
66        {
67            return lang_;
68        }
69
70        bool HasDecl() const
71        {
72            return hasDecl_;
73        }
74
75    private:
76        Language lang_;
77        bool hasDecl_;
78    };
79
80    explicit ArkTsConfig(std::string configPath) : configPath_(std::move(configPath)) {}
81    bool Parse();
82
83    std::optional<std::string> ResolvePath(const std::string &path) const;
84
85    std::string ConfigPath() const
86    {
87        return configPath_;
88    }
89
90    std::string BaseUrl() const
91    {
92        return baseUrl_;
93    }
94    std::string RootDir() const
95    {
96        return rootDir_;
97    }
98    std::string OutDir() const
99    {
100        return outDir_;
101    }
102    const std::vector<std::string> &Files() const
103    {
104        return files_;
105    }
106    const std::unordered_map<std::string, std::vector<std::string>> &Paths() const
107    {
108        return paths_;
109    }
110    const std::unordered_map<std::string, DynamicImportData> &DynamicPaths() const
111    {
112        return dynamicPaths_;
113    }
114#ifdef ARKTSCONFIG_USE_FILESYSTEM
115    const std::vector<Pattern> &Include() const
116    {
117        return include_;
118    }
119    const std::vector<Pattern> &Exclude() const
120    {
121        return exclude_;
122    }
123#endif  // ARKTSCONFIG_USE_FILESYSTEM
124
125private:
126    void Inherit(const ArkTsConfig &base);
127#ifdef ARKTSCONFIG_USE_FILESYSTEM
128    bool ParseExtends(const std::string &extends, const std::string &configDir);
129#endif  // ARKTSCONFIG_USE_FILESYSTEM
130
131    bool isParsed_ = false;
132    std::string configPath_;
133
134    std::string baseUrl_ {};
135    std::string outDir_ {};
136    std::string rootDir_ {};
137    std::unordered_map<std::string, std::vector<std::string>> paths_ {};
138    std::unordered_map<std::string, DynamicImportData> dynamicPaths_ {};
139    std::vector<std::string> files_ {};
140#ifdef ARKTSCONFIG_USE_FILESYSTEM
141    std::vector<Pattern> include_ {};
142    std::vector<Pattern> exclude_ {};
143#endif  // ARKTSCONFIG_USE_FILESYSTEM
144};
145
146// Find source files and compute destination locations
147// Return: vector of path pairs <source file, destination abc file>
148std::vector<std::pair<std::string, std::string>> FindProjectSources(const std::shared_ptr<ArkTsConfig> &arktsConfig);
149
150std::string JoinPaths(const std::string &a, const std::string &b);
151std::string ParentPath(const std::string &path);
152
153}  // namespace ark::es2panda
154
155#endif  // ES2PANDA_AOT_TSCONFIG_H
156