xref: /aosp_15_r20/tools/asuite/plugin_lib/deployment.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2020 - The Android Open Source Project
4*c2e18aaaSAndroid Build Coastguard Worker#
5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*c2e18aaaSAndroid Build Coastguard Worker#
9*c2e18aaaSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*c2e18aaaSAndroid Build Coastguard Worker#
11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License.
16*c2e18aaaSAndroid Build Coastguard Worker
17*c2e18aaaSAndroid Build Coastguard Worker"""Asuite plugin deployment."""
18*c2e18aaaSAndroid Build Coastguard Workerimport os
19*c2e18aaaSAndroid Build Coastguard Workerimport subprocess
20*c2e18aaaSAndroid Build Coastguard Worker
21*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util
22*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import config
23*c2e18aaaSAndroid Build Coastguard Worker
24*c2e18aaaSAndroid Build Coastguard Worker_ASK_INSTALL_PLUGIN = """\nAsuite plugin is a new tool with following features:
25*c2e18aaaSAndroid Build Coastguard Worker    -Atest UI widget. For more information: go/atest_plugin
26*c2e18aaaSAndroid Build Coastguard Worker    -Code search integration. For more information and locate build module: go/android-platform-plugin
27*c2e18aaaSAndroid Build Coastguard WorkerWould you like to install the Asuite plugin? (Yes/no/auto)"""
28*c2e18aaaSAndroid Build Coastguard Worker_ASK_UPGRADE_PLUGIN = ('\nAsuite plugin has a new version. Would you like to '
29*c2e18aaaSAndroid Build Coastguard Worker                       'upgrade Asuite plugin? (Yes/no/auto)')
30*c2e18aaaSAndroid Build Coastguard Worker_YES_RESPONSE = 'Thank you, Asuit plugin will be installed in IntelliJ.'
31*c2e18aaaSAndroid Build Coastguard Worker_NO_RESPONSE = ('Thank you, if you want to install Asuite plugin, please use '
32*c2e18aaaSAndroid Build Coastguard Worker                'aidegen --plugin.')
33*c2e18aaaSAndroid Build Coastguard Worker_AUTO_RESPONSE = ('Thank you, Asuit plugin will be installed in IntelliJ, and '
34*c2e18aaaSAndroid Build Coastguard Worker                  'automatically updated to the newest version.')
35*c2e18aaaSAndroid Build Coastguard Worker_THANKS_UPGRADE = 'Thank you for upgrading the Asuite plugin.'
36*c2e18aaaSAndroid Build Coastguard Worker_NO_NEED_UPGRADE = 'Awesome! You have the newest Asuite plugin.'
37*c2e18aaaSAndroid Build Coastguard Worker_SELECTION_ITEM = {'yes': 'yes', 'no': 'no', 'auto': 'auto', 'y': 'yes',
38*c2e18aaaSAndroid Build Coastguard Worker                   'n': 'no', 'a': 'auto', '': 'yes'}
39*c2e18aaaSAndroid Build Coastguard Worker
40*c2e18aaaSAndroid Build Coastguard Worker
41*c2e18aaaSAndroid Build Coastguard Workerclass PluginDeployment:
42*c2e18aaaSAndroid Build Coastguard Worker    """The util class of Asuite plugin deployment.
43*c2e18aaaSAndroid Build Coastguard Worker
44*c2e18aaaSAndroid Build Coastguard Worker    Usage:
45*c2e18aaaSAndroid Build Coastguard Worker        PluginDeployment.install_asuite_plugin()
46*c2e18aaaSAndroid Build Coastguard Worker        It will start installation process.
47*c2e18aaaSAndroid Build Coastguard Worker
48*c2e18aaaSAndroid Build Coastguard Worker    Attributes:
49*c2e18aaaSAndroid Build Coastguard Worker        is_internal: True if the user is a internal user.
50*c2e18aaaSAndroid Build Coastguard Worker    """
51*c2e18aaaSAndroid Build Coastguard Worker
52*c2e18aaaSAndroid Build Coastguard Worker    def __init__(self):
53*c2e18aaaSAndroid Build Coastguard Worker        """PluginDeployment initialize."""
54*c2e18aaaSAndroid Build Coastguard Worker        self.is_internal = self._is_internal_user()
55*c2e18aaaSAndroid Build Coastguard Worker
56*c2e18aaaSAndroid Build Coastguard Worker    def install_asuite_plugin(self):
57*c2e18aaaSAndroid Build Coastguard Worker        """It is the main entry function for installing Asuite plugin."""
58*c2e18aaaSAndroid Build Coastguard Worker
59*c2e18aaaSAndroid Build Coastguard Worker    def _ask_for_install(self):
60*c2e18aaaSAndroid Build Coastguard Worker        """Asks the user to install the Asuite plugin."""
61*c2e18aaaSAndroid Build Coastguard Worker        input_data = input(_ASK_INSTALL_PLUGIN)
62*c2e18aaaSAndroid Build Coastguard Worker        while input_data.lower() not in _SELECTION_ITEM.keys():
63*c2e18aaaSAndroid Build Coastguard Worker            input_data = input(_ASK_INSTALL_PLUGIN)
64*c2e18aaaSAndroid Build Coastguard Worker        choice = _SELECTION_ITEM.get(input_data)
65*c2e18aaaSAndroid Build Coastguard Worker        self._user_selection = choice
66*c2e18aaaSAndroid Build Coastguard Worker        if choice == 'no':
67*c2e18aaaSAndroid Build Coastguard Worker            print(_NO_RESPONSE)
68*c2e18aaaSAndroid Build Coastguard Worker        else:
69*c2e18aaaSAndroid Build Coastguard Worker            self._copy_jars()
70*c2e18aaaSAndroid Build Coastguard Worker            if choice == 'yes':
71*c2e18aaaSAndroid Build Coastguard Worker                print(_YES_RESPONSE)
72*c2e18aaaSAndroid Build Coastguard Worker            else:
73*c2e18aaaSAndroid Build Coastguard Worker                print(_AUTO_RESPONSE)
74*c2e18aaaSAndroid Build Coastguard Worker
75*c2e18aaaSAndroid Build Coastguard Worker    def _ask_for_upgrade(self):
76*c2e18aaaSAndroid Build Coastguard Worker        """Asks the user to upgrade the Asuite plugin."""
77*c2e18aaaSAndroid Build Coastguard Worker
78*c2e18aaaSAndroid Build Coastguard Worker    def _copy_jars(self):
79*c2e18aaaSAndroid Build Coastguard Worker        """Copies jars to IntelliJ plugin folders."""
80*c2e18aaaSAndroid Build Coastguard Worker
81*c2e18aaaSAndroid Build Coastguard Worker    def _build_jars(self):
82*c2e18aaaSAndroid Build Coastguard Worker        """builds jars to IntelliJ plugin folders."""
83*c2e18aaaSAndroid Build Coastguard Worker        asuite_plugin_path = os.path.join(common_util.get_android_root_dir(),
84*c2e18aaaSAndroid Build Coastguard Worker                                          'tools/asuite/asuite_plugin/')
85*c2e18aaaSAndroid Build Coastguard Worker        asuite_plugin_gradle_path = os.path.join(asuite_plugin_path, 'gradlew')
86*c2e18aaaSAndroid Build Coastguard Worker        cmd = [asuite_plugin_gradle_path, 'build']
87*c2e18aaaSAndroid Build Coastguard Worker        subprocess.check_call(cmd, cwd=asuite_plugin_path)
88*c2e18aaaSAndroid Build Coastguard Worker
89*c2e18aaaSAndroid Build Coastguard Worker    def _is_plugin_installed(self):
90*c2e18aaaSAndroid Build Coastguard Worker        """Checks if the user has installed Asuite plugin before.
91*c2e18aaaSAndroid Build Coastguard Worker
92*c2e18aaaSAndroid Build Coastguard Worker        Return:
93*c2e18aaaSAndroid Build Coastguard Worker            True if the user has installed Asuite plugin.
94*c2e18aaaSAndroid Build Coastguard Worker        """
95*c2e18aaaSAndroid Build Coastguard Worker
96*c2e18aaaSAndroid Build Coastguard Worker    def _is_version_up_to_date(self):
97*c2e18aaaSAndroid Build Coastguard Worker        """Checks if all plugins' versions are up to date or not.
98*c2e18aaaSAndroid Build Coastguard Worker
99*c2e18aaaSAndroid Build Coastguard Worker        Return:
100*c2e18aaaSAndroid Build Coastguard Worker            True if all plugins' versions are up to date.
101*c2e18aaaSAndroid Build Coastguard Worker        """
102*c2e18aaaSAndroid Build Coastguard Worker
103*c2e18aaaSAndroid Build Coastguard Worker    @property
104*c2e18aaaSAndroid Build Coastguard Worker    def _user_selection(self):
105*c2e18aaaSAndroid Build Coastguard Worker        """Reads the user's selection from config file.
106*c2e18aaaSAndroid Build Coastguard Worker
107*c2e18aaaSAndroid Build Coastguard Worker        Return:
108*c2e18aaaSAndroid Build Coastguard Worker            A string of the user's selection: yes/no/auto.
109*c2e18aaaSAndroid Build Coastguard Worker        """
110*c2e18aaaSAndroid Build Coastguard Worker        with config.AidegenConfig() as aconf:
111*c2e18aaaSAndroid Build Coastguard Worker            return aconf.plugin_preference
112*c2e18aaaSAndroid Build Coastguard Worker
113*c2e18aaaSAndroid Build Coastguard Worker    @_user_selection.setter
114*c2e18aaaSAndroid Build Coastguard Worker    def _user_selection(self, selection):
115*c2e18aaaSAndroid Build Coastguard Worker        """Writes the user's selection to config file.
116*c2e18aaaSAndroid Build Coastguard Worker
117*c2e18aaaSAndroid Build Coastguard Worker        Args:
118*c2e18aaaSAndroid Build Coastguard Worker            selection: A string of the user's selection: yes/no/auto.
119*c2e18aaaSAndroid Build Coastguard Worker        """
120*c2e18aaaSAndroid Build Coastguard Worker        with config.AidegenConfig() as aconf:
121*c2e18aaaSAndroid Build Coastguard Worker            aconf.plugin_preference = selection
122*c2e18aaaSAndroid Build Coastguard Worker
123*c2e18aaaSAndroid Build Coastguard Worker    @staticmethod
124*c2e18aaaSAndroid Build Coastguard Worker    def _is_internal_user():
125*c2e18aaaSAndroid Build Coastguard Worker        """Checks if the user is internal user or external user.
126*c2e18aaaSAndroid Build Coastguard Worker
127*c2e18aaaSAndroid Build Coastguard Worker        Return:
128*c2e18aaaSAndroid Build Coastguard Worker            True if the user is a internal user.
129*c2e18aaaSAndroid Build Coastguard Worker        """
130*c2e18aaaSAndroid Build Coastguard Worker        return True
131