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