1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from containers.arg import Arg
20from containers.status import throw_exception
21from exceptions.ohos_exception import OHOSException
22from services.gn import CMDTYPE
23from resolver.interface.args_resolver_interface import ArgsResolverInterface
24from modules.interface.tool_module_interface import ToolModuleInterface
25from util.component_util import ComponentUtil
26
27
28class ToolArgsResolver(ArgsResolverInterface):
29
30    def __init__(self, args_dict: dict):
31        super().__init__(args_dict)
32
33    @staticmethod
34    def resolve_list_targets(target_arg: Arg, tool_module: ToolModuleInterface):
35        out_dir = ''
36        args_list = []
37        for arg in target_arg.arg_value:
38            if '-' not in arg and len(out_dir) == 0:
39                out_dir = arg
40            else:
41                args_list.append(arg)
42        tool_module.gn.execute_gn_cmd(
43            cmd_type=CMDTYPE.LS, out_dir=out_dir, args_list=args_list)
44
45    @staticmethod
46    def resolve_desc_targets(target_arg: Arg, tool_module: ToolModuleInterface):
47        out_dir = ''
48        args_list = []
49        for arg in target_arg.arg_value:
50            if ':' in arg:
51                try:
52                    component_name, module_name = arg.split(':')
53                    args_list.append(ComponentUtil.get_component_module_full_name(
54                        out_dir, component_name, module_name))
55                except Exception:
56                    raise OHOSException(
57                        'Invalid desc args: {} ,need <component:module>'.format(arg))
58            elif '-' not in arg and len(out_dir) == 0:
59                out_dir = arg
60            else:
61                args_list.append(arg)
62        tool_module.gn.execute_gn_cmd(
63            cmd_type=CMDTYPE.DESC, out_dir=out_dir, args_list=args_list)
64
65    @staticmethod
66    def resolve_path_targets(target_arg: Arg, tool_module: ToolModuleInterface):
67        out_dir = ''
68        args_list = []
69        for arg in target_arg.arg_value:
70            if ':' in arg:
71                try:
72                    component_name, module_name = arg.split(':')
73                    args_list.append(ComponentUtil.get_component_module_full_name(
74                        out_dir, component_name, module_name))
75                except Exception:
76                    raise OHOSException(
77                        'Invalid path args: {} ,need <component:module>'.format(arg))
78            elif '-' not in arg and len(out_dir) == 0:
79                out_dir = arg
80            else:
81                args_list.append(arg)
82        tool_module.gn.execute_gn_cmd(
83            cmd_type=CMDTYPE.PATH, out_dir=out_dir, args_list=args_list)
84
85    @staticmethod
86    def resolve_refs_targets(target_arg: Arg, tool_module: ToolModuleInterface):
87        out_dir = ''
88        args_list = []
89        for arg in target_arg.arg_value:
90            if ':' in arg:
91                try:
92                    component_name, module_name = arg.split(':')
93                    args_list.append(ComponentUtil.get_component_module_full_name(
94                        out_dir, component_name, module_name))
95                except Exception:
96                    raise OHOSException(
97                        'Invalid refs args: {} ,need <component:module>'.format(arg))
98            elif '-' not in arg and len(out_dir) == 0:
99                out_dir = arg
100            else:
101                args_list.append(arg)
102        tool_module.gn.execute_gn_cmd(
103            cmd_type=CMDTYPE.REFS, out_dir=out_dir, args_list=args_list)
104
105    @staticmethod
106    def resolve_format_targets(target_arg: Arg, tool_module: ToolModuleInterface):
107        tool_module.gn.execute_gn_cmd(
108            cmd_type=CMDTYPE.FORMAT, args_list=target_arg.arg_value)
109
110    @staticmethod
111    def resolve_clean_targets(target_arg: Arg, tool_module: ToolModuleInterface):
112        out_dir = ''
113        out_dir = target_arg.arg_value[0]
114        tool_module.gn.execute_gn_cmd(cmd_type=CMDTYPE.CLEAN, out_dir=out_dir)
115