Repository: pantsbuild/pants

Issue:
Generate a .idea compatible seed project
Follow up for https://github.com/pantsbuild/pants/pull/8676, since ipr is legacy version.


Discussion:

Resolving patch:
diff --git a/src/python/pants/backend/project_info/tasks/idea_plugin_gen.py b/src/python/pants/backend/project_info/tasks/idea_plugin_gen.py
--- a/src/python/pants/backend/project_info/tasks/idea_plugin_gen.py
+++ b/src/python/pants/backend/project_info/tasks/idea_plugin_gen.py
@@ -82,12 +82,10 @@ def __init__(self, *args, **kwargs):
     self.open = self.get_options().open
 
     self.java_encoding = self.get_options().java_encoding
-    self.project_template = os.path.join(_TEMPLATE_BASEDIR,
-                                         'project-12.mustache')
-    self.workspace_template = os.path.join(_TEMPLATE_BASEDIR,
+    self.idea_modules_template = os.path.join(_TEMPLATE_BASEDIR,
+                                           'modules-12.mustache')
+    self.idea_workspace_template = os.path.join(_TEMPLATE_BASEDIR,
                                            'workspace-12.mustache')
-    self.rootmodule_template = os.path.join(_TEMPLATE_BASEDIR,
-                                           'rootmodule-12.mustache')
     self.java_language_level = self.get_options().java_language_level
 
     if self.get_options().java_jdk_name:
@@ -99,16 +97,13 @@ def __init__(self, *args, **kwargs):
     safe_mkdir(output_dir)
 
     with temporary_dir(root_dir=output_dir, cleanup=False) as output_project_dir:
-      project_name = self.get_project_name(self.context.options.specs)
-
       self.gen_project_workdir = output_project_dir
-      self.project_filename = os.path.join(self.gen_project_workdir,
-                                           '{}.ipr'.format(project_name))
-      self.workspace_filename = os.path.join(self.gen_project_workdir,
-                                             '{}.iws'.format(project_name))
-      self.rootmodule_filename = os.path.join(self.gen_project_workdir,
-                                              'rootmodule.iml')
+      self.idea_workspace_filename = os.path.join(self.gen_project_workdir,
+                                                  ".idea", "workspace.xml")
+      self.idea_modules_filename = os.path.join(self.gen_project_workdir,
+                                                  ".idea", "modules.xml")
       self.intellij_output_dir = os.path.join(self.gen_project_workdir, 'out')
+      self.intellij_idea_dir = os.path.join(self.gen_project_workdir, '.idea')
 
   @classmethod
   def get_project_name(cls, target_specs):
@@ -146,21 +141,20 @@ def generate_project(self):
 
     # Generate (without merging in any extra components).
     safe_mkdir(os.path.abspath(self.intellij_output_dir))
+    safe_mkdir(os.path.abspath(self.intellij_idea_dir))
 
     def gen_file(template_file_name, **mustache_kwargs):
       return self._generate_to_tempfile(
         Generator(pkgutil.get_data(__name__, template_file_name).decode(), **mustache_kwargs)
       )
 
-    ipr = gen_file(self.project_template, project=configured_project)
-    iws = gen_file(self.workspace_template, workspace=configured_workspace)
-    iml_root = gen_file(self.workspace_template)
+    idea_ws = gen_file(self.idea_workspace_template, workspace=configured_workspace)
+    idea_modules = gen_file(self.idea_modules_template, project=configured_project)
 
-    shutil.move(ipr, self.project_filename)
-    shutil.move(iws, self.workspace_filename)
-    shutil.move(iml_root, self.rootmodule_filename)
+    shutil.move(idea_ws, self.idea_workspace_filename)
+    shutil.move(idea_modules, self.idea_modules_filename)
 
-    return self.project_filename
+    return self.gen_project_workdir
 
   def _generate_to_tempfile(self, generator):
     """Applies the specified generator to a temp file and returns the path to that file.
@@ -191,6 +185,6 @@ def console_output(self, _targets):
         subprocess.Popen([open_with, ide_file], stdout=null, stderr=null)
       else:
         try:
-          desktop.ui_open(ide_file)
+          desktop.idea_open(ide_file)
         except desktop.OpenError as e:
           raise TaskError(e)
diff --git a/src/python/pants/util/desktop.py b/src/python/pants/util/desktop.py
--- a/src/python/pants/util/desktop.py
+++ b/src/python/pants/util/desktop.py
@@ -11,10 +11,22 @@ class OpenError(Exception):
   """Indicates an error opening a file in a desktop application."""
 
 
+def _mac_open_with_idea(file_: str) -> None:
+  subprocess.call(['open', "-a", "/Applications/IntelliJ IDEA CE.app", file_])
+
+
 def _mac_open(files: Sequence[str]) -> None:
   subprocess.call(['open'] + list(files))
 
 
+def _linux_open_with_idea(file_: str) -> None:
+  cmd = "idea"
+  if not _cmd_exists(cmd):
+    raise OpenError("The program '{}' isn't in your PATH. Please install and re-run this "
+                    "goal.".format(cmd))
+  subprocess.Popen(["nohup", cmd, file_])
+
+
 def _linux_open(files: Sequence[str]) -> None:
   cmd = "xdg-open"
   if not _cmd_exists(cmd):
@@ -36,6 +48,25 @@ def _cmd_exists(cmd: str) -> bool:
   'linux': _linux_open
 }
 
+_IDEA_BY_OS = {
+  'darwin': _mac_open_with_idea,
+  'linux': _linux_open_with_idea
+}
+
+
+def idea_open(file_: str) -> None:
+  """Attempts to open the given files using the preferred desktop viewer or editor.
+
+  :raises :class:`OpenError`: if there is a problem opening any of the files.
+  """
+  if file_:
+    osname = get_os_name()
+    opener = _IDEA_BY_OS.get(osname)
+    if opener:
+      opener(file_)
+    else:
+      raise OpenError('Open currently not supported for ' + osname)
+
 
 def ui_open(*files: str) -> None:
   """Attempts to open the given files using the preferred desktop viewer or editor.


Test patch:
diff --git a/tests/python/pants_test/backend/project_info/tasks/test_idea_plugin_integration.py b/tests/python/pants_test/backend/project_info/tasks/test_idea_plugin_integration.py
--- a/tests/python/pants_test/backend/project_info/tasks/test_idea_plugin_integration.py
+++ b/tests/python/pants_test/backend/project_info/tasks/test_idea_plugin_integration.py
@@ -5,7 +5,7 @@
 import os
 from xml.dom import minidom
 
-from pants.backend.project_info.tasks.idea_plugin_gen import IDEA_PLUGIN_VERSION, IdeaPluginGen
+from pants.backend.project_info.tasks.idea_plugin_gen import IDEA_PLUGIN_VERSION
 from pants.base.build_environment import get_buildroot
 from pants.base.cmd_line_spec_parser import CmdLineSpecParser
 from pants.testutil.pants_run_integration_test import PantsRunIntegrationTest
@@ -17,11 +17,11 @@ def _do_check(self, project_dir_path, expected_project_path, expected_targets,
                 incremental_import=None):
     """Check to see that the project contains the expected source folders."""
 
-    iws_file = os.path.join(
-      project_dir_path, f'{IdeaPluginGen.get_project_name(expected_targets)}.iws'
+    workspace_file = os.path.join(
+      project_dir_path, ".idea", 'workspace.xml'
     )
-    self.assertTrue(os.path.exists(iws_file))
-    dom = minidom.parse(iws_file)
+    self.assertTrue(os.path.exists(workspace_file))
+    dom = minidom.parse(workspace_file)
     self.assertEqual(1, len(dom.getElementsByTagName("project")))
     project = dom.getElementsByTagName("project")[0]