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 "sa_ts_interface_code_emitter.h"
17 #include "util/file.h"
18 #include "util/logger.h"
19
20 namespace OHOS {
21 namespace Idl {
22 namespace {
23 const std::string ERR_CODE_TYPE = "errCode: number";
24 }
ResolveDirectory(const std::string & targetDirectory)25 bool SaTsInterfaceCodeEmitter::ResolveDirectory(const std::string &targetDirectory)
26 {
27 directory_ = GetFileParentPath(targetDirectory);
28 if (!File::CreateParentDir(directory_)) {
29 Logger::E("SaTsInterfaceCodeEmitter", "Create '%s' failed!", directory_.c_str());
30 return false;
31 }
32
33 return true;
34 }
35
EmitCode()36 void SaTsInterfaceCodeEmitter::EmitCode()
37 {
38 if (!CheckInterfaceType()) {
39 return;
40 }
41 EmitInterfaceHeaderFile();
42 }
43
EmitInterfaceHeaderFile()44 void SaTsInterfaceCodeEmitter::EmitInterfaceHeaderFile()
45 {
46 std::string filePath =
47 File::AdapterPath(StringHelper::Format("%s/%s.ts", directory_.c_str(), FileName(interfaceName_).c_str()));
48 File file(filePath, File::WRITE);
49
50 StringBuilder sb;
51
52 EmitLicense(sb);
53 EmitInterfaceSelfDefinedTypeImports(sb);
54 sb.Append("\n");
55 EmitInterfaceDefinition(sb);
56 sb.Append("\n");
57
58 std::string data = sb.ToString();
59 file.WriteData(data.c_str(), data.size());
60 file.Flush();
61 file.Close();
62 return;
63 }
64
EmitInterfaceDefinition(StringBuilder & sb) const65 void SaTsInterfaceCodeEmitter::EmitInterfaceDefinition(StringBuilder &sb) const
66 {
67 sb.AppendFormat("export default interface %s {\n", interface_->GetName().c_str());
68 int methodNumber = static_cast<int>(interface_->GetMethodNumber());
69 for (int i = 0; i < methodNumber; i++) {
70 AutoPtr<ASTMethod> method = interface_->GetMethod(i);
71 EmitInterfaceMethod(method, sb, TAB);
72 }
73 sb.Append("}\n");
74
75 for (int i = 0; i < methodNumber; i++) {
76 AutoPtr<ASTMethod> method = interface_->GetMethod(i);
77 EmitInterfaceMethodCallback(method, sb);
78 }
79 }
80
EmitInterfaceMethod(AutoPtr<ASTMethod> & method,StringBuilder & sb,const std::string & prefix) const81 void SaTsInterfaceCodeEmitter::EmitInterfaceMethod(AutoPtr<ASTMethod> &method, StringBuilder &sb,
82 const std::string &prefix) const
83 {
84 sb.Append(prefix).AppendFormat("%s(", MethodName(method->GetName()).c_str());
85 int paramNumber = static_cast<int>(method->GetParameterNumber());
86 for (int i = 0; i < paramNumber; i++) {
87 AutoPtr<ASTParameter> param = method->GetParameter(i);
88 if (param->GetAttribute() & ASTParamAttr::PARAM_IN) {
89 AutoPtr<SaTypeEmitter> typeEmitter = GetTypeEmitter(param->GetType());
90 if (typeEmitter != nullptr) {
91 sb.AppendFormat("%s: %s, ", param->GetName().c_str(), typeEmitter->EmitTsType().c_str());
92 }
93 }
94 }
95 sb.AppendFormat("callback: %sCallback): void;\n", MethodName(method->GetName()).c_str());
96 }
97
EmitInterfaceMethodCallback(AutoPtr<ASTMethod> & method,StringBuilder & sb) const98 void SaTsInterfaceCodeEmitter::EmitInterfaceMethodCallback(AutoPtr<ASTMethod> &method, StringBuilder &sb) const
99 {
100 int paramNumber = static_cast<int>(method->GetParameterNumber());
101 sb.AppendFormat("export type %sCallback = (%s", MethodName(method->GetName()).c_str(), ERR_CODE_TYPE.c_str());
102 AutoPtr<ASTType> returnType = method->GetReturnType();
103 if (returnType->GetTypeKind() != TypeKind::TYPE_VOID) {
104 AutoPtr<SaTypeEmitter> typeEmitter = GetTypeEmitter(returnType);
105 if (typeEmitter != nullptr) {
106 sb.AppendFormat(", %s: %s", RETURN_VALUE, typeEmitter->EmitTsType().c_str());
107 }
108 }
109
110 for (int i = 0; i < paramNumber; i++) {
111 AutoPtr<ASTParameter> param = method->GetParameter(i);
112 if (param->GetAttribute() & ASTParamAttr::PARAM_OUT) {
113 AutoPtr<SaTypeEmitter> typeEmitter = GetTypeEmitter(param->GetType());
114 if (typeEmitter != nullptr) {
115 sb.AppendFormat(", %s: %s", param->GetName().c_str(), typeEmitter->EmitTsType().c_str());
116 }
117 }
118 }
119 sb.Append(") => void;\n");
120 }
121 } // namespace Idl
122 } // namespace OHOS