@Override public void setupRootModel(ModifiableRootModel model) throws ConfigurationException { String contentPath = getContentEntryPath(); if (StringUtil.isEmpty(contentPath)) { return; } File contentRootDir = new File(contentPath); FileUtilRt.createDirectory(contentRootDir); LocalFileSystem fileSystem = LocalFileSystem.getInstance(); VirtualFile vContentRootDir = fileSystem.refreshAndFindFileByIoFile(contentRootDir); if (vContentRootDir == null) { return; } model.addContentEntry(vContentRootDir); VirtualFile configFile = getExternalProjectConfigFile(vContentRootDir); if (configFile != null && myTemplateConfigName != null) { FileTemplateManager manager = FileTemplateManager.getInstance(); FileTemplate template = manager.getInternalTemplate(myTemplateConfigName); try { VfsUtil.saveText(configFile, template.getText()); } catch (IOException e) { LOG.warn( String.format( "Unexpected exception on applying template %s config", myExternalSystemId.getReadableName()), e); throw new ConfigurationException( e.getMessage(), String.format( "Can't apply %s template config text", myExternalSystemId.getReadableName())); } } AbstractExternalSystemSettings settings = ExternalSystemApiUtil.getSettings(model.getProject(), myExternalSystemId); S externalProjectSettings = createSettings(); if (myExternalProjectSettingsControl != null) { String errorMessage = myExternalProjectSettingsControl.apply(externalProjectSettings); myExternalProjectSettingsControl.disposeUIResources(); if (errorMessage != null) { throw new ConfigurationException(errorMessage); } } //noinspection unchecked settings.linkProject(externalProjectSettings); }
@Nullable private Location buildLocation() { if (mySelectedTaskProvider == null) { return null; } ExternalTaskExecutionInfo task = mySelectedTaskProvider.produce(); if (task == null) { return null; } String projectPath = task.getSettings().getExternalProjectPath(); String name = myExternalSystemId.getReadableName() + projectPath + StringUtil.join(task.getSettings().getTaskNames(), " "); // We create a dummy text file instead of re-using external system file in order to avoid // clashing with other configuration producers. // For example gradle files are enhanced groovy scripts but we don't want to run them via // regular IJ groovy script runners. // Gradle tooling api should be used for running gradle tasks instead. IJ execution sub-system // operates on Location objects // which encapsulate PsiElement and groovy runners are automatically applied if that PsiElement // IS-A GroovyFile. PsiFile file = PsiFileFactory.getInstance(myProject) .createFileFromText(name, PlainTextFileType.INSTANCE, "nichts"); return new ExternalSystemTaskLocation(myProject, file, task); }
public static void storeLastUsedExternalProjectPath( @Nullable String path, @NotNull ProjectSystemId externalSystemId) { if (path != null) { PropertiesComponent.getInstance() .setValue(LAST_USED_PROJECT_PATH_PREFIX + externalSystemId.getReadableName(), path); } }
private void processOrphanModules() { if (myProject.isDisposed()) return; if (ExternalSystemDebugEnvironment.DEBUG_ORPHAN_MODULES_PROCESSING) { LOG.info( String.format( "Checking for orphan modules. External paths returned by external system: '%s'", myExternalModulePaths)); } PlatformFacade platformFacade = ServiceManager.getService(PlatformFacade.class); List<Module> orphanIdeModules = ContainerUtilRt.newArrayList(); String externalSystemIdAsString = myExternalSystemId.toString(); for (Module module : platformFacade.getModules(myProject)) { String s = module.getOptionValue(ExternalSystemConstants.EXTERNAL_SYSTEM_ID_KEY); String p = module.getOptionValue(ExternalSystemConstants.LINKED_PROJECT_PATH_KEY); if (ExternalSystemDebugEnvironment.DEBUG_ORPHAN_MODULES_PROCESSING) { LOG.info( String.format( "IDE module: EXTERNAL_SYSTEM_ID_KEY - '%s', LINKED_PROJECT_PATH_KEY - '%s'.", s, p)); } if (externalSystemIdAsString.equals(s) && !myExternalModulePaths.contains(p)) { orphanIdeModules.add(module); if (ExternalSystemDebugEnvironment.DEBUG_ORPHAN_MODULES_PROCESSING) { LOG.info( String.format( "External paths doesn't contain IDE module LINKED_PROJECT_PATH_KEY anymore => add to orphan IDE modules.")); } } } if (!orphanIdeModules.isEmpty()) { ruleOrphanModules(orphanIdeModules, myProject, myExternalSystemId); } }
@Contract("_, null -> false") public static boolean isExternalSystemAwareModule( @NotNull ProjectSystemId systemId, @Nullable Module module) { return module != null && !module.isDisposed() && systemId .getId() .equals(module.getOptionValue(ExternalSystemConstants.EXTERNAL_SYSTEM_ID_KEY)); }
@Nullable public static ExternalSystemManager<?, ?, ?, ?, ?> getManager( @NotNull ProjectSystemId externalSystemId) { for (ExternalSystemManager manager : ExternalSystemManager.EP_NAME.getExtensions()) { if (externalSystemId.equals(manager.getSystemId())) { return manager; } } return null; }
@SuppressWarnings("unchecked") public static <S extends AbstractExternalSystemLocalSettings> S getLocalSettings( @NotNull Project project, @NotNull ProjectSystemId externalSystemId) throws IllegalArgumentException { ExternalSystemManager<?, ?, ?, ?, ?> manager = getManager(externalSystemId); if (manager == null) { throw new IllegalArgumentException( String.format( "Can't retrieve local external system settings for id '%s'. Reason: no such external system is registered", externalSystemId.getReadableName())); } return (S) manager.getLocalSettingsProvider().fun(project); }
@Override public boolean configureTask( RunConfiguration runConfiguration, ExternalSystemBeforeRunTask task) { ExternalSystemEditTaskDialog dialog = new ExternalSystemEditTaskDialog(myProject, task.getTaskExecutionSettings(), mySystemId); dialog.setTitle( ExternalSystemBundle.message("tasks.select.task.title", mySystemId.getReadableName())); if (!dialog.showAndGet()) { return false; } return true; }
@SuppressWarnings("unchecked") public static <S extends ExternalSystemExecutionSettings> S getExecutionSettings( @NotNull Project project, @NotNull String linkedProjectPath, @NotNull ProjectSystemId externalSystemId) throws IllegalArgumentException { ExternalSystemManager<?, ?, ?, ?, ?> manager = getManager(externalSystemId); if (manager == null) { throw new IllegalArgumentException( String.format( "Can't retrieve external system execution settings for id '%s'. Reason: no such external system is registered", externalSystemId.getReadableName())); } return (S) manager.getExecutionSettingsProvider().fun(Pair.create(project, linkedProjectPath)); }
@Nullable public static ToolWindow ensureToolWindowContentInitialized( @NotNull Project project, @NotNull ProjectSystemId externalSystemId) { final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project); if (toolWindowManager == null) return null; final ToolWindow toolWindow = toolWindowManager.getToolWindow(externalSystemId.getReadableName()); if (toolWindow == null) return null; if (toolWindow instanceof ToolWindowImpl) { ((ToolWindowImpl) toolWindow).ensureContentInitialized(); } return toolWindow; }
private static long getTimeStamp( @NotNull ExternalProjectSettings externalProjectSettings, @NotNull ProjectSystemId externalSystemId) { long timeStamp = 0; for (ExternalSystemConfigLocator locator : ExternalSystemConfigLocator.EP_NAME.getExtensions()) { if (!externalSystemId.equals(locator.getTargetExternalSystemId())) { continue; } for (VirtualFile virtualFile : locator.findAll(externalProjectSettings)) { timeStamp += virtualFile.getTimeStamp(); } } return timeStamp; }
@Nullable public static AbstractExternalSystemTaskConfigurationType findConfigurationType( @NotNull ProjectSystemId externalSystemId) { for (ConfigurationType type : Extensions.getExtensions(ConfigurationType.CONFIGURATION_TYPE_EP)) { if (type instanceof AbstractExternalSystemTaskConfigurationType) { AbstractExternalSystemTaskConfigurationType candidate = (AbstractExternalSystemTaskConfigurationType) type; if (externalSystemId.equals(candidate.getExternalSystemId())) { return candidate; } } } return null; }
@Override public String getDescription(ExternalSystemBeforeRunTask task) { final String externalProjectPath = task.getTaskExecutionSettings().getExternalProjectPath(); if (task.getTaskExecutionSettings().getTaskNames().isEmpty()) { return ExternalSystemBundle.message("tasks.before.run.empty", mySystemId.getReadableName()); } String desc = StringUtil.join(task.getTaskExecutionSettings().getTaskNames(), " "); for (Module module : ModuleManager.getInstance(myProject).getModules()) { if (!mySystemId .toString() .equals(module.getOptionValue(ExternalSystemConstants.EXTERNAL_SYSTEM_ID_KEY))) continue; if (StringUtil.equals( externalProjectPath, module.getOptionValue(ExternalSystemConstants.LINKED_PROJECT_PATH_KEY))) { desc = module.getName() + ": " + desc; break; } } return ExternalSystemBundle.message("tasks.before.run", mySystemId.getReadableName(), desc); }
public static void ensureToolWindowInitialized( @NotNull Project project, @NotNull ProjectSystemId externalSystemId) { ToolWindowManager manager = ToolWindowManager.getInstance(project); if (!(manager instanceof ToolWindowManagerEx)) { return; } ToolWindowManagerEx managerEx = (ToolWindowManagerEx) manager; String id = externalSystemId.getReadableName(); ToolWindow window = manager.getToolWindow(id); if (window != null) { return; } ToolWindowEP[] beans = Extensions.getExtensions(ToolWindowEP.EP_NAME); for (final ToolWindowEP bean : beans) { if (id.equals(bean.id)) { managerEx.initToolWindow(bean); } } }
@Override public String getPresentableName() { return ExternalSystemBundle.message("module.type.title", myExternalSystemId.getReadableName()); }
/** * Historically we prefer to work with third-party api not from ide process but from dedicated * slave process (there is a risk that third-party api has bugs which might make the whole ide * process corrupted, e.g. a memory leak at the api might crash the whole ide process). * * <p>However, we do allow to explicitly configure the ide to work with third-party external * system api from the ide process. * * <p>This method allows to check whether the ide is configured to use 'out of process' or 'in * process' mode for the system. * * @param externalSystemId target external system * @return <code>true</code> if the ide is configured to work with external system api from the * ide process; <code>false</code> otherwise */ public static boolean isInProcessMode(ProjectSystemId externalSystemId) { return Registry.is( externalSystemId.getId() + ExternalSystemConstants.USE_IN_PROCESS_COMMUNICATION_REGISTRY_KEY_SUFFIX, false); }
public static boolean isExternalSystemLibrary( @NotNull Library library, @NotNull ProjectSystemId externalSystemId) { return library.getName() != null && StringUtil.startsWith(library.getName(), externalSystemId.getReadableName() + ": "); }
/** * Asks current builder to ensure that target external project is defined. * * @param wizardContext current wizard context * @throws ConfigurationException if gradle project is not defined and can't be constructed */ @SuppressWarnings("unchecked") public void ensureProjectIsDefined(@NotNull WizardContext wizardContext) throws ConfigurationException { final String externalSystemName = myExternalSystemId.getReadableName(); File projectFile = getProjectFile(); if (projectFile == null) { throw new ConfigurationException(ExternalSystemBundle.message("error.project.undefined")); } projectFile = getExternalProjectConfigToUse(projectFile); final Ref<ConfigurationException> error = new Ref<ConfigurationException>(); final ExternalProjectRefreshCallback callback = new ExternalProjectRefreshCallback() { @Override public void onSuccess(@Nullable DataNode<ProjectData> externalProject) { myExternalProjectNode = externalProject; } @Override public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) { if (!StringUtil.isEmpty(errorDetails)) { LOG.warn(errorDetails); } error.set( new ConfigurationException( ExternalSystemBundle.message("error.resolve.with.reason", errorMessage), ExternalSystemBundle.message("error.resolve.generic"))); } }; final Project project = getProject(wizardContext); final File finalProjectFile = projectFile; final String externalProjectPath = FileUtil.toCanonicalPath(finalProjectFile.getAbsolutePath()); final Ref<ConfigurationException> exRef = new Ref<ConfigurationException>(); executeAndRestoreDefaultProjectSettings( project, new Runnable() { @Override public void run() { try { ExternalSystemUtil.refreshProject( project, myExternalSystemId, externalProjectPath, callback, true, ProgressExecutionMode.MODAL_SYNC); } catch (IllegalArgumentException e) { exRef.set( new ConfigurationException( e.getMessage(), ExternalSystemBundle.message( "error.cannot.parse.project", externalSystemName))); } } }); ConfigurationException ex = exRef.get(); if (ex != null) { throw ex; } if (myExternalProjectNode == null) { ConfigurationException exception = error.get(); if (exception != null) { throw exception; } } else { applyProjectSettings(wizardContext); } }
@Override public String getDescription() { return ExternalSystemBundle.message( "module.type.description", myExternalSystemId.getReadableName()); }
@Override public String getName() { return ExternalSystemBundle.message("tasks.before.run.empty", mySystemId.getReadableName()); }
public KeymapGroup createGroup(Condition<AnAction> condition, final Project project) { KeymapGroup result = KeymapGroupFactory.getInstance() .createGroup( ExternalSystemBundle.message("external.system.keymap.group"), ExternalSystemIcons.TaskGroup); if (project == null) return result; MultiMap<ProjectSystemId, String> projectToActionsMapping = MultiMap.create(); for (ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) { projectToActionsMapping.putValues(manager.getSystemId(), ContainerUtil.<String>emptyList()); } ActionManager actionManager = ActionManager.getInstance(); if (actionManager != null) { for (String eachId : actionManager.getActionIds(getActionPrefix(project, null))) { AnAction eachAction = actionManager.getAction(eachId); if (!(eachAction instanceof MyExternalSystemAction)) continue; if (condition != null && !condition.value(actionManager.getActionOrStub(eachId))) continue; MyExternalSystemAction taskAction = (MyExternalSystemAction) eachAction; projectToActionsMapping.putValue(taskAction.getSystemId(), eachId); } } Map<ProjectSystemId, KeymapGroup> keymapGroupMap = ContainerUtil.newHashMap(); for (ProjectSystemId systemId : projectToActionsMapping.keySet()) { if (!keymapGroupMap.containsKey(systemId)) { final Icon projectIcon = ExternalSystemUiUtil.getUiAware(systemId).getProjectIcon(); KeymapGroup group = KeymapGroupFactory.getInstance().createGroup(systemId.getReadableName(), projectIcon); result.addGroup(group); keymapGroupMap.put(systemId, group); } } for (Map.Entry<ProjectSystemId, Collection<String>> each : projectToActionsMapping.entrySet()) { Collection<String> tasks = each.getValue(); final ProjectSystemId systemId = each.getKey(); final KeymapGroup systemGroup = keymapGroupMap.get(systemId); for (String actionId : tasks) { systemGroup.addActionId(actionId); } if (systemGroup instanceof Group) { Icon icon = SystemInfoRt.isMac ? AllIcons.ToolbarDecorator.Mac.Add : AllIcons.ToolbarDecorator.Add; ((Group) systemGroup) .addHyperlink( new Hyperlink(icon, "Choose a task to assign a shortcut") { @Override public void onClick(MouseEvent e) { SelectExternalTaskDialog dialog = new SelectExternalTaskDialog(systemId, project); if (dialog.showAndGet() && dialog.getResult() != null) { TaskData taskData = dialog.getResult().second; String ownerModuleName = dialog.getResult().first; ExternalSystemTaskAction externalSystemAction = (ExternalSystemTaskAction) getOrRegisterAction(project, ownerModuleName, taskData); ApplicationManager.getApplication() .getMessageBus() .syncPublisher(KeymapListener.CHANGE_TOPIC) .processCurrentKeymapChanged(); Settings allSettings = Settings.KEY.getData( DataManager.getInstance().getDataContext(e.getComponent())); KeymapPanel keymapPanel = allSettings != null ? allSettings.find(KeymapPanel.class) : null; if (keymapPanel != null) { // clear actions filter keymapPanel.showOption(""); keymapPanel.selectAction(externalSystemAction.myId); } } } }); } } for (ActionsProvider extension : ActionsProvider.EP_NAME.getExtensions()) { KeymapGroup group = extension.createGroup(condition, project); if (group != null) { result.addGroup(group); } } return result; }