• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import ts from 'typescript';
17import path from 'path';
18
19import { projectConfig } from '../main';
20import {
21  resourceFileName,
22  isDynamic
23} from './process_ui_syntax';
24import {
25  PAGE_PATH,
26  INTEGRATED_HSP,
27  TRUE,
28  FALSE,
29  RESOURCE_NAME_BUNDLE,
30  RESOURCE_NAME_MODULE
31} from './pre_define';
32import constantDefine from './constant_define';
33
34export function routerOrNavPathWrite(context: ts.TransformationContext, keyName: string, projectPath: string,
35  projectRootPath: string = ''): ts.PropertyAssignment {
36  return context.factory.createPropertyAssignment(
37    context.factory.createIdentifier(keyName),
38    context.factory.createStringLiteral(
39      projectConfig.compileHar ? keyName === PAGE_PATH ? byteCodeHarPagePath(projectRootPath) : '' :
40        pathMessage(projectPath))
41  );
42}
43
44function byteCodeHarPagePath(projectRootPath: string): string {
45  return projectConfig.byteCodeHar ? pathMessage(projectRootPath) : constantDefine.HAR_DEFAULT_PAGE_PATH;
46}
47
48function pathMessage(projectPathName: string): string {
49  return path.relative(projectPathName || '', resourceFileName).replace(/\\/g, '/').replace(/\.ets$/, '');
50}
51
52export function integratedHspType(): string {
53  return projectConfig.integratedHsp ? TRUE : projectConfig.compileHar ? constantDefine.HAR_DEFAULT_INTEGRATED_HSP_TYPE : FALSE;
54}
55
56export function routerModuleType(context: ts.TransformationContext): ts.PropertyAssignment {
57  return context.factory.createPropertyAssignment(
58    context.factory.createIdentifier(constantDefine.MODULE_TYPE),
59    context.factory.createStringLiteral(moduleType())
60  );
61}
62
63function moduleType(): string {
64  if (projectConfig.compileHar) {
65    return projectConfig.byteCodeHar ? constantDefine.BYTE_CODE_HAR : constantDefine.CLOSED_SOURCE_HAR;
66  } else if (projectConfig.compileShared) {
67    return projectConfig.integratedHsp ? INTEGRATED_HSP : constantDefine.SHARED_HSP;
68  }
69  return constantDefine.FOLLOW_WITH_HAP;
70}
71
72export function routerBundleOrModule(context: ts.TransformationContext, type: string): ts.PropertyAssignment {
73  const typeKey: string = type === RESOURCE_NAME_BUNDLE ? RESOURCE_NAME_BUNDLE : RESOURCE_NAME_MODULE;
74  if (isDynamic()) {
75    return context.factory.createPropertyAssignment(
76      context.factory.createIdentifier(typeKey),
77      context.factory.createIdentifier(type === RESOURCE_NAME_BUNDLE ? (projectConfig.allowEmptyBundleName ? '' : '__BUNDLE_NAME__') : '__MODULE_NAME__')
78    );
79  }
80  return context.factory.createPropertyAssignment(
81    context.factory.createIdentifier(typeKey),
82    context.factory.createStringLiteral(type === RESOURCE_NAME_BUNDLE ? (projectConfig.allowEmptyBundleName ? '' : (projectConfig.bundleName || '')) :
83      (projectConfig.moduleName || ''))
84  );
85}
86