• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2023 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 
34 namespace panda::es2panda {
35 
36 class ArkTsConfig {
37 public:
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:
DynamicImportData(Language lang,bool hasDecl)63         explicit DynamicImportData(Language lang, bool hasDecl) : lang_(lang), hasDecl_(hasDecl) {}
64 
GetLanguage()65         Language GetLanguage() const
66         {
67             return lang_;
68         }
69 
HasDecl()70         bool HasDecl() const
71         {
72             return hasDecl_;
73         }
74 
75     private:
76         Language lang_;
77         bool hasDecl_;
78     };
79 
ArkTsConfig(std::string configPath)80     explicit ArkTsConfig(std::string configPath) : configPath_(std::move(configPath)) {}
81     bool Parse();
82 
83     std::string ResolvePath(const std::string &path);
84 
ConfigPath()85     std::string ConfigPath() const
86     {
87         return configPath_;
88     }
89 
BaseUrl()90     std::string BaseUrl() const
91     {
92         return baseUrl_;
93     }
RootDir()94     std::string RootDir() const
95     {
96         return rootDir_;
97     }
OutDir()98     std::string OutDir() const
99     {
100         return outDir_;
101     }
Files()102     const std::vector<std::string> &Files() const
103     {
104         return files_;
105     }
Paths()106     const std::unordered_map<std::string, std::vector<std::string>> &Paths() const
107     {
108         return paths_;
109     }
DynamicPaths()110     const std::unordered_map<std::string, DynamicImportData> &DynamicPaths() const
111     {
112         return dynamicPaths_;
113     }
114 #ifdef ARKTSCONFIG_USE_FILESYSTEM
Include()115     const std::vector<Pattern> &Include() const
116     {
117         return include_;
118     }
Exclude()119     const std::vector<Pattern> &Exclude() const
120     {
121         return exclude_;
122     }
123 #endif  // ARKTSCONFIG_USE_FILESYSTEM
124 
125 private:
126     void Inherit(const ArkTsConfig &base);
127 
128     bool isParsed_ = false;
129     std::string configPath_;
130 
131     std::string baseUrl_ {};
132     std::string outDir_ {};
133     std::string rootDir_ {};
134     std::unordered_map<std::string, std::vector<std::string>> paths_ {};
135     std::unordered_map<std::string, DynamicImportData> dynamicPaths_ {};
136     std::vector<std::string> files_ {};
137 #ifdef ARKTSCONFIG_USE_FILESYSTEM
138     std::vector<Pattern> include_ {};
139     std::vector<Pattern> exclude_ {};
140 #endif  // ARKTSCONFIG_USE_FILESYSTEM
141 };
142 
143 // Find source files and compute destination locations
144 // Return: vector of path pairs <source file, destination abc file>
145 std::vector<std::pair<std::string, std::string>> FindProjectSources(const std::shared_ptr<ArkTsConfig> &arktsConfig);
146 
147 std::string JoinPaths(const std::string &a, const std::string &b);
148 std::string ParentPath(const std::string &path);
149 
150 }  // namespace panda::es2panda
151 
152 #endif  // ES2PANDA_AOT_TSCONFIG_H
153