Asuite plugin: add real time install mechanism.

Bug: 161092947
Test: atest --host -m plugin_lib_unittests

Change-Id: I2438208be412a7e55a1cc5705f0f8ee905de4066
diff --git a/aidegen/Android.bp b/aidegen/Android.bp
index ce5d038..c1353ff 100644
--- a/aidegen/Android.bp
+++ b/aidegen/Android.bp
@@ -60,6 +60,20 @@
     ]
 }
 
+python_library_host {
+    name: "aidegen_lib_common_util",
+    defaults: ["aidegen_default"],
+    srcs: [
+        "lib/common_util.py",
+        "lib/errors.py",
+        "constant.py",
+    ],
+    exclude_srcs: [
+        "*_unittest.py",
+        "*/*_unittest.py",
+    ]
+}
+
 python_test_host {
     name: "aidegen_unittests",
     main: "aidegen_run_unittests.py",
diff --git a/plugin_lib/Android.bp b/plugin_lib/Android.bp
index 8092ef7..cf049f4 100644
--- a/plugin_lib/Android.bp
+++ b/plugin_lib/Android.bp
@@ -37,6 +37,11 @@
         "*_unittest.py",
         "**/*_unittest.py",
     ],
+    libs: [
+        "aidegen_lib_common_util",
+        "atest_module_info",
+        "asuite_cc_client",
+    ],
 }
 
 python_test_host {
@@ -46,6 +51,11 @@
     srcs: [
         "**/*.py",
     ],
+    libs: [
+        "aidegen_lib_common_util",
+        "atest_module_info",
+        "asuite_cc_client",
+    ],
     test_config: "plugin_lib_unittests.xml",
     test_suites: ["general-tests"],
     defaults: ["plugin_default"],
diff --git a/plugin_lib/deployment.py b/plugin_lib/deployment.py
index 74c7e22..f6b4c2e 100644
--- a/plugin_lib/deployment.py
+++ b/plugin_lib/deployment.py
@@ -15,6 +15,26 @@
 # limitations under the License.
 
 """Asuite plugin deployment."""
+import os
+import subprocess
+
+from aidegen.lib import common_util
+
+_ASK_INSTALL_PLUGIN = """\nAsuite plugin is a new tool with following features:
+    -Atest UI widget. For more information: go/atest_plugin
+    -Code search integration. For more information and locate build module: go/android-platform-plugin
+Would you like to install the Asuite plugin? (Yes/No/Auto)"""
+_ASK_UPGRADE_PLUGIN = ('\nAsuite plugin has a new version. Would you like to '
+                       'upgrade Asuite plugin? (Yes/No/Auto)')
+_YES_RESPONSE = 'Thank you, Asuit plugin will be installed in IntelliJ.'
+_NO_RESPONSE = ('Thank you, if you want to install Asuite plugin, please use '
+                'aidegen --plugin.')
+_AUTO_RESPONSE = ('Thank you, Asuit plugin will be installed in IntelliJ, and '
+                  'automatically updated to the newest version.')
+_THANKS_UPGRADE = 'Thank you for upgrading the Asuite plugin.'
+_NO_NEED_UPGRADE = 'Awesome! You have the newest Asuite plugin.'
+_SELECTION_ITEM = {'yes': 'yes', 'no': 'no', 'auto': 'auto', 'y': 'yes',
+                   'n': 'no', 'a': 'auto'}
 
 
 class PluginDeployment:
@@ -37,6 +57,19 @@
 
     def _ask_for_install(self):
         """Asks the user to install the Asuite plugin."""
+        input_data = input(_ASK_INSTALL_PLUGIN)
+        while input_data.lower() not in _SELECTION_ITEM.keys():
+            input_data = input(_ASK_INSTALL_PLUGIN)
+        choice = _SELECTION_ITEM.get(input_data)
+        self._write_selection(choice)
+        if choice == 'no':
+            print(_NO_RESPONSE)
+        else:
+            self._copy_jars()
+            if choice == 'yes':
+                print(_YES_RESPONSE)
+            else:
+                print(_AUTO_RESPONSE)
 
     def _ask_for_upgrade(self):
         """Asks the user to upgrade the Asuite plugin."""
@@ -44,6 +77,14 @@
     def _copy_jars(self):
         """Copies jars to IntelliJ plugin folders."""
 
+    def _build_jars(self):
+        """builds jars to IntelliJ plugin folders."""
+        asuite_plugin_path = os.path.join(common_util.get_android_root_dir(),
+                                          'tools/asuite/asuite_plugin/')
+        asuite_plugin_gradle_path = os.path.join(asuite_plugin_path, 'gradlew')
+        cmd = [asuite_plugin_gradle_path, 'build']
+        subprocess.check_call(cmd, cwd=asuite_plugin_path)
+
     def _is_plugin_installed(self):
         """Checks if the user has installed Asuite plugin before.
 
@@ -58,14 +99,18 @@
             True if all plugins' versions are up to date.
         """
 
-    def _write_selection(self):
-        """Writes the user's selection to config file."""
+    def _write_selection(self, selection):
+        """Writes the user's selection to config file.
+
+        Args:
+            selection: A string of the user's selection: yes/no/auto.
+        """
 
     def _read_selection(self):
         """Reads the user's selection from config file.
 
         Return:
-            A string of the user's selection: yes/no/auto
+            A string of the user's selection: yes/no/auto.
         """
 
     @staticmethod
diff --git a/plugin_lib/deployment_unittest.py b/plugin_lib/deployment_unittest.py
index 1006339..bce5814 100644
--- a/plugin_lib/deployment_unittest.py
+++ b/plugin_lib/deployment_unittest.py
@@ -15,13 +15,26 @@
 # limitations under the License.
 
 """Unittests for deployment."""
-
+import subprocess
 import unittest
+from unittest import mock
+
+from deployment import PluginDeployment
 
 
+# pylint: disable=protected-access
 class DeploymentUnittests(unittest.TestCase):
     """Unit tests for deployment.py."""
 
-    def test_install_asuite_plugin(self):
-        """Test install_asuite_plugin."""
-        pass
+    @mock.patch('builtins.input')
+    def test_ask_for_install(self, mock_input):
+        """Test _ask_for_install."""
+        mock_input.return_value = 'y'
+        PluginDeployment()._ask_for_install()
+        self.assertTrue(mock_input.call)
+
+    @mock.patch.object(subprocess, 'check_call')
+    def test_build_jars(self, mock_check_call):
+        """Test _build_jars."""
+        PluginDeployment()._build_jars()
+        self.assertTrue(mock_check_call.call)