Skip to content

Commit

Permalink
#409 | Automatically adjust Kotlin Compiler based on kotlinnature s…
Browse files Browse the repository at this point in the history
…ettings and project JDK
  • Loading branch information
mlytvyn authored May 16, 2023
1 parent 1a2927a commit a6f8a06
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 64 deletions.
1 change: 1 addition & 0 deletions resources/META-INF/plugin-release-info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<li><i>Feature:</i> <strong>Kotlin</strong> language support (<a href="https://github.com/mlytvyn/kotlinnature">kotlinnature</a>)
<ul>
<li>Register Kotlin Facet for extensions with <code>kotlinsrc</code> / <code>kotlintestsrc</code> directories (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/407" target="_blank" rel="nofollow">#407</a>)</li>
<li>Automatically adjust Kotlin Compiler based on <code>kotlinnature</code> settings and project JDK (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/409" target="_blank" rel="nofollow">#409</a>)</li>
<li>Show different icon for <code>kotlinnature</code> extension (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/408" target="_blank" rel="nofollow">#408</a>)</li>
</ul>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.intellij.facet.ModifiableFacetModel;
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisModuleDescriptor;
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisProjectDescriptor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ModifiableRootModel;
import org.jetbrains.annotations.NotNull;
Expand All @@ -30,7 +31,7 @@
public interface FacetConfigurator {

void configure(
@NotNull ModifiableFacetModel modifiableFacetModel,
final @NotNull HybrisProjectDescriptor hybrisProjectDescriptor, @NotNull ModifiableFacetModel modifiableFacetModel,
@NotNull HybrisModuleDescriptor moduleDescriptor,
@NotNull Module javaModule,
@NotNull ModifiableRootModel modifiableRootModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import com.intellij.openapi.project.Project
interface KotlinCompilerConfigurator {

fun configure(descriptor: HybrisProjectDescriptor, project: Project, cache: HybrisConfiguratorCache)

fun configureAfterImport(project: Project)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,68 @@
*/
package com.intellij.idea.plugin.hybris.project.configurators.impl

import com.intellij.facet.FacetManager
import com.intellij.ide.GeneralSettingsConfigurableEP
import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.impex.utils.ProjectPropertiesUtils
import com.intellij.idea.plugin.hybris.project.configurators.HybrisConfiguratorCache
import com.intellij.idea.plugin.hybris.project.configurators.KotlinCompilerConfigurator
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisProjectDescriptor
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootManager
import org.jetbrains.kotlin.cli.common.arguments.unfrozen
import com.intellij.openapi.projectRoots.JavaSdk
import com.intellij.openapi.projectRoots.JavaSdkVersion
import com.intellij.openapi.roots.ProjectRootManager
import org.jetbrains.kotlin.idea.compiler.configuration.Kotlin2JvmCompilerArgumentsHolder
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCompilerSettings
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.kotlin.idea.facet.KotlinFacetType
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinJpsPluginSettings

class DefaultKotlinCompilerConfigurator : KotlinCompilerConfigurator {

override fun configure(descriptor: HybrisProjectDescriptor, project: Project, cache: HybrisConfiguratorCache) {
// val moduleRootManager = ModuleRootManager.getInstance(project)
// val modules = moduleRootManager.modules
//
//
// for (module in modules) {
// val kotlinFacet = KotlinFacet.get(module)
//
// if (kotlinFacet == null) {
// val facetManager = FacetManager.getInstance(module)
// val facetType = KotlinFacetType.INSTANCE
// val facet = facetManager.createFacet(facetType, facetType.defaultFacetName, null)
// facetManager.addFacet(facet)
//
// val facetConfiguration = facet.configuration
// facetConfiguration.settings.compilerSettings.kotlinCompilerVersion = "1.5.0"
//
// // Save the changes
// facetConfiguration.commit()
// } else {
// val facetConfiguration = kotlinFacet.configuration
//
// // Set the desired Kotlin compiler version
// facetConfiguration.kotlinCompilerVersion = "1.5.0"
//
// // Save the changes
// kotlinFacet.configuration.commit()
// }
// }
val hasKotlinnatureExtension = descriptor.modulesChosenForImport.stream()
.anyMatch { HybrisConstants.EXTENSION_NAME_KOTLIN_NATURE == it.name }
if (!hasKotlinnatureExtension) return

setKotlinCompilerVersion(project, HybrisConstants.KOTLIN_COMPILER_FALLBACK_VERSION)
setKotlinJvmTarget(project)
}

override fun configureAfterImport(project: Project) {
val hasKotlinnatureExtension = ModuleManager.getInstance(project).modules
.any { HybrisConstants.EXTENSION_NAME_KOTLIN_NATURE == it.name }
if (!hasKotlinnatureExtension) return

val compilerVersion = ProjectPropertiesUtils.findMacroProperty(project, HybrisConstants.KOTLIN_COMPILER_VERSION_PROPERTY_KEY)
?.value
?: HybrisConstants.KOTLIN_COMPILER_FALLBACK_VERSION
setKotlinCompilerVersion(project, compilerVersion)
}

//
// val kotlinCompilerSettings = KotlinCompilerSettings.getInstance(project)
//
// val compilerSettings = kotlinCompilerSettings.settings.unfrozen()
// val k2JVMCompilerArguments = Kotlin2JvmCompilerArgumentsHolder.getInstance(project).settings.unfrozen()
//
// org.jetbrains.kotlin.config.KotlinCompilerSettings
// k2JVMCompilerArguments.apiVersion = "1.8"
// Kotlin compiler version will be updated after project import / refresh in BGT
// we have to have indexes ready to be able to get correct value of the project property responsible for custom Kotlin compiler version
private fun setKotlinCompilerVersion(project: Project, compilerVersion: String) {
ApplicationManager.getApplication().runReadAction {
KotlinJpsPluginSettings.getInstance(project).update {
version = compilerVersion
}
}
}

private fun setKotlinJvmTarget(project: Project) {
ApplicationManager.getApplication().runReadAction {
val projectRootManager = ProjectRootManager.getInstance(project)
val sdk = projectRootManager.projectSdk ?: return@runReadAction
val javaSdk = sdk.sdkType as? JavaSdk ?: return@runReadAction
val javaSdkVersion = javaSdk.getVersion(sdk) ?: return@runReadAction

val projectJvmTarget = if (javaSdkVersion == JavaSdkVersion.JDK_1_8) {
"1.8"
} else {
javaSdkVersion.description
}
Kotlin2JvmCompilerArgumentsHolder.getInstance(project).update {
jvmTarget = projectJvmTarget
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class DefaultPostImportConfigurator(val project: Project) : PostImportConfigurat
ApplicationManager.getApplication().invokeLater {
if (project.isDisposed) return@invokeLater

configuratorFactory.kotlinCompilerConfigurator
?.configureAfterImport(project)

configuratorFactory.mavenConfigurator
?.let {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ package com.intellij.idea.plugin.hybris.project.configurators.impl
import com.intellij.facet.ModifiableFacetModel
import com.intellij.idea.plugin.hybris.project.configurators.FacetConfigurator
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisModuleDescriptor
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisProjectDescriptor
import com.intellij.openapi.module.Module
import com.intellij.openapi.roots.ModifiableRootModel
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.kotlin.idea.facet.KotlinFacetType

/**
* Kotlin Facets will be configured only if `kotlinnature` extension is available and
*/
class KotlinFacetConfigurator : FacetConfigurator {

override fun configure(
hybrisProjectDescriptor: HybrisProjectDescriptor,
modifiableFacetModel: ModifiableFacetModel,
moduleDescriptor: HybrisModuleDescriptor,
javaModule: Module,
modifiableRootModel: ModifiableRootModel
) {
// Remove previously registered Kotlin Facet for extensions with removed kotlin sources
modifiableFacetModel.getFacetByType(KotlinFacetType.TYPE_ID)
?.takeUnless { moduleDescriptor.isKotlinEnabled }
?.takeUnless { moduleDescriptor.hasKotlinSourceDirectories() }
?.let { modifiableFacetModel.removeFacet(it) }

if (!moduleDescriptor.isKotlinEnabled) return
if (!moduleDescriptor.hasKotlinSourceDirectories()) return
if (hybrisProjectDescriptor.kotlinNatureModuleDescriptor == null) return

val facet = KotlinFacet.get(javaModule)
?: createFacet(javaModule)
Expand All @@ -47,10 +53,10 @@ class KotlinFacetConfigurator : FacetConfigurator {
}

private fun createFacet(javaModule: Module) = with(KotlinFacetType.INSTANCE) {
this.createFacet(
createFacet(
javaModule,
this.defaultFacetName,
this.createDefaultConfiguration(),
defaultFacetName,
createDefaultConfiguration(),
null
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.intellij.idea.plugin.hybris.project.configurators.FacetConfigurator;
import com.intellij.idea.plugin.hybris.project.descriptors.CCv2HybrisModuleDescriptor;
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisModuleDescriptor;
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisProjectDescriptor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.roots.ModifiableRootModel;
Expand All @@ -45,7 +46,7 @@ public class SpringFacetConfigurator implements FacetConfigurator {

@Override
public void configure(
@NotNull final ModifiableFacetModel modifiableFacetModel,
final @NotNull HybrisProjectDescriptor hybrisProjectDescriptor, @NotNull final ModifiableFacetModel modifiableFacetModel,
@NotNull final HybrisModuleDescriptor moduleDescriptor,
@NotNull final Module javaModule,
@NotNull final ModifiableRootModel modifiableRootModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.intellij.idea.plugin.hybris.common.HybrisConstants;
import com.intellij.idea.plugin.hybris.project.configurators.FacetConfigurator;
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisModuleDescriptor;
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisProjectDescriptor;
import com.intellij.javaee.DeploymentDescriptorsConstants;
import com.intellij.javaee.web.facet.WebFacet;
import com.intellij.openapi.application.WriteAction;
Expand All @@ -49,7 +50,7 @@ public class WebFacetConfigurator implements FacetConfigurator {

@Override
public void configure(
@NotNull final ModifiableFacetModel modifiableFacetModel,
final @NotNull HybrisProjectDescriptor hybrisProjectDescriptor, @NotNull final ModifiableFacetModel modifiableFacetModel,
@NotNull final HybrisModuleDescriptor moduleDescriptor,
@NotNull final Module javaModule,
@NotNull final ModifiableRootModel modifiableRootModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ public File getWebRoot() {
}

@Override
public boolean isKotlinEnabled() {
return new File(this.getRootDirectory(), HybrisConstants.KOTLIN_SRC_DIRECTORY).exists()
|| new File(this.getRootDirectory(), HybrisConstants.KOTLIN_TEST_SRC_DIRECTORY).exists();
public boolean hasKotlinSourceDirectories() {
return new File(getRootDirectory(), HybrisConstants.KOTLIN_SRC_DIRECTORY).exists()
|| new File(getRootDirectory(), HybrisConstants.KOTLIN_TEST_SRC_DIRECTORY).exists();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public class DefaultHybrisProjectDescriptor implements HybrisProjectDescriptor {
private ConfigHybrisModuleDescriptor configHybrisModuleDescriptor;
@NotNull
private PlatformHybrisModuleDescriptor platformHybrisModuleDescriptor;
@Nullable
private HybrisModuleDescriptor kotlinNatureModuleDescriptor;
private final Set<File> vcs = new HashSet<>();

private void processLocalExtensions() {
Expand Down Expand Up @@ -783,6 +785,9 @@ public void setModulesChosenForImport(@NotNull final List<HybrisModuleDescriptor
if (module instanceof PlatformHybrisModuleDescriptor) {
platformHybrisModuleDescriptor = (PlatformHybrisModuleDescriptor) module;
}
if (HybrisConstants.EXTENSION_NAME_KOTLIN_NATURE.equals(module.getName())) {
kotlinNatureModuleDescriptor = module;
}
});
}

Expand All @@ -798,6 +803,12 @@ public PlatformHybrisModuleDescriptor getPlatformHybrisModuleDescriptor() {
return platformHybrisModuleDescriptor;
}

@Nullable
@Override
public HybrisModuleDescriptor getKotlinNatureModuleDescriptor() {
return kotlinNatureModuleDescriptor;
}

@NotNull
@Override
public Set<HybrisModuleDescriptor> getAlreadyOpenedModules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ enum IMPORT_STATUS {MANDATORY, UNUSED}

boolean isAddOn();

boolean isKotlinEnabled();
/**
* This method will return true if module has `kotlinsrc` or `kotlintestsrc` directories
*/
boolean hasKotlinSourceDirectories();

@NotNull
HybrisModuleDescriptorType getDescriptorType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface HybrisProjectDescriptor {
@NotNull
PlatformHybrisModuleDescriptor getPlatformHybrisModuleDescriptor();

@Nullable HybrisModuleDescriptor getKotlinNatureModuleDescriptor();

@NotNull
Set<HybrisModuleDescriptor> getAlreadyOpenedModules();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public synchronized void run(@NotNull final ProgressIndicator indicator) {
final HybrisConfiguratorCache cache = new HybrisConfiguratorCache();
final List<HybrisModuleDescriptor> allModules = getHybrisModuleDescriptors();

final SpringConfigurator springConfigurator = configuratorFactory.getSpringConfigurator();
final var springConfigurator = configuratorFactory.getSpringConfigurator();
final var groupModuleConfigurator = configuratorFactory.getGroupModuleConfigurator();
final VersionControlSystemConfigurator versionControlSystemConfigurator = configuratorFactory.getVersionControlSystemConfigurator();
final var versionControlSystemConfigurator = configuratorFactory.getVersionControlSystemConfigurator();

this.initializeHybrisProjectSettings(project);
this.updateProjectDictionary(project, hybrisProjectDescriptor.getModulesChosenForImport());
Expand Down Expand Up @@ -301,7 +301,7 @@ private Module createJavaModule(

indicator.setText2(message("hybris.project.import.module.facet"));
for (final FacetConfigurator facetConfigurator : configuratorFactory.getFacetConfigurators()) {
facetConfigurator.configure(modifiableFacetModel, moduleDescriptor, javaModule, modifiableRootModel);
facetConfigurator.configure(hybrisProjectDescriptor, modifiableFacetModel, moduleDescriptor, javaModule, modifiableRootModel);
}
return javaModule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
/**
* Created by Martin Zdarsky-Jones ([email protected]) on 20/3/17.
*/
public class PluginCommon {
public final class PluginCommon {

public static final String ANT_SUPPORT_PLUGIN_ID = "AntSupport";
public static final String JAVAEE_PLUGIN_ID = "com.intellij.javaee";
public static final String SPRING_PLUGIN_ID = "com.intellij.spring";
public static final String KOTLIN_PLUGIN_ID = "org.jetbrains.kotlin";
public static final String SHOW_UNLINKED_GRADLE_POPUP = "show.inlinked.gradle.project.popup";

private PluginCommon() {
}

public static boolean isPluginActive(final String id) {
final IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId(id));
if (plugin == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HybrisProjectStructureStartupActivity : ProjectActivity {
override suspend fun execute(project: Project) {
if (project.isDisposed) return

val commonIdeaService = ApplicationManager.getApplication().getService(CommonIdeaService::class.java)
val commonIdeaService = CommonIdeaService.getInstance()
val isHybrisProject = commonIdeaService.isHybrisProject(project)

if (isHybrisProject) {
Expand Down Expand Up @@ -105,7 +105,7 @@ class HybrisProjectStructureStartupActivity : ProjectActivity {
}

private fun resetSpringGeneralSettings(project: Project) {
val commonIdeaService = ApplicationManager.getApplication().getService(CommonIdeaService::class.java)
val commonIdeaService = CommonIdeaService.getInstance()

if (commonIdeaService.isHybrisProject(project) && PluginCommon.isPluginActive(PluginCommon.SPRING_PLUGIN_ID)) {
val springGeneralSettings = SpringGeneralSettings.getInstance(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.intellij.psi.PsiManager
class ImpexHeaderHighlighterStartupActivity : ProjectActivity, Disposable {

override suspend fun execute(project: Project) {
if (!ApplicationManager.getApplication().getService(CommonIdeaService::class.java).isHybrisProject(project)) {
if (!CommonIdeaService.getInstance().isHybrisProject(project)) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.intellij.psi.search.GlobalSearchScope
class ItemsXmlFileOpenStartupActivity : ProjectActivity {

override suspend fun execute(project: Project) {
if (!ApplicationManager.getApplication().getService(CommonIdeaService::class.java).isHybrisProject(project)) return
if (!CommonIdeaService.getInstance().isHybrisProject(project)) return
if (!HybrisApplicationSettingsComponent.getInstance().state.warnIfGeneratedItemsAreOutOfDate) return

val task = object : Task.Backgroundable(project, message("hybris.startupActivity.itemsXmlValidation.progress.title")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.intellij.openapi.startup.ProjectActivity
class PreLoadSystemsStartupActivity : ProjectActivity {

override suspend fun execute(project: Project) {
if (!ApplicationManager.getApplication().getService(CommonIdeaService::class.java).isHybrisProject(project)) {
if (!CommonIdeaService.getInstance().isHybrisProject(project)) {
return
}

Expand Down

0 comments on commit a6f8a06

Please sign in to comment.