From 1218f2cc629b81a67fb02e2d127b2c39c6162682 Mon Sep 17 00:00:00 2001 From: Malte Bastian Date: Tue, 24 Sep 2024 22:58:31 +0200 Subject: [PATCH] feat: set flags based on biome-version to add backwards compatibility --- .../biomejs/intellijbiome/BiomePackage.kt | 20 ++++++++++++++++ .../biomejs/intellijbiome/BiomeRunner.kt | 2 +- .../biomejs/intellijbiome/BiomeStdinRunner.kt | 23 ++++++++++++++----- .../intellijbiome/actions/BiomeCheckRunner.kt | 8 +++---- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/github/biomejs/intellijbiome/BiomePackage.kt b/src/main/kotlin/com/github/biomejs/intellijbiome/BiomePackage.kt index beaced2..69f34d7 100644 --- a/src/main/kotlin/com/github/biomejs/intellijbiome/BiomePackage.kt +++ b/src/main/kotlin/com/github/biomejs/intellijbiome/BiomePackage.kt @@ -92,4 +92,24 @@ class BiomePackage(private val project: Project) { } return null } + + fun compareVersion(version1: String, version2: String): Int { + val parts1 = version1.split(".").map { it.toInt() } + val parts2 = version2.split(".").map { it.toInt() } + + val maxLength = maxOf(parts1.size, parts2.size) + + for (i in 0 until maxLength){ + val v1 = if (i < parts1.size) parts1[i] else 0 + val v2 = if (i < parts2.size) parts2[i] else 0 + + when { + v1 < v2 -> return -1 + v1 > v2 -> return 1 + } + } + + return 0 + } + } diff --git a/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeRunner.kt b/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeRunner.kt index 124accb..61965a8 100644 --- a/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeRunner.kt +++ b/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeRunner.kt @@ -20,7 +20,7 @@ interface BiomeRunner { val DEFAULT_TIMEOUT = 30000.milliseconds } - fun check(request: Request, features: EnumSet): Response + fun check(request: Request, features: EnumSet, biomePackage: BiomePackage): Response fun createCommandLine(file: VirtualFile, action: String, args: List = listOf()): GeneralCommandLine diff --git a/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeStdinRunner.kt b/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeStdinRunner.kt index 3b6e4e2..0ea0967 100644 --- a/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeStdinRunner.kt +++ b/src/main/kotlin/com/github/biomejs/intellijbiome/BiomeStdinRunner.kt @@ -18,8 +18,8 @@ import kotlin.time.Duration class BiomeStdinRunner(private val project: Project) : BiomeRunner { private val biomePackage = BiomePackage(project) - override fun check(request: BiomeRunner.Request, features: EnumSet): BiomeRunner.Response { - val commandLine = createCommandLine(request.virtualFile, "check", getCheckFlags(features)) + override fun check(request: BiomeRunner.Request, features: EnumSet, biomePackage: BiomePackage): BiomeRunner.Response { + val commandLine = createCommandLine(request.virtualFile, "check", getCheckFlags(features, biomePackage)) val file = request.virtualFile val timeout = request.timeout val failureMessage = BiomeBundle.message( @@ -83,7 +83,7 @@ class BiomeStdinRunner(private val project: Project) : BiomeRunner { return future } - private fun getCheckFlags(features: EnumSet): List { + fun getCheckFlags(features: EnumSet, biomePackage: BiomePackage): List { val args = SmartList() if (features.isEmpty()) return args @@ -98,11 +98,22 @@ class BiomeStdinRunner(private val project: Project) : BiomeRunner { args.add("--linter-enabled=false") } - args.add("--write") - if (features.contains(Feature.UnsafeFixes)) { - args.add("--unsafe") + val version = biomePackage.versionNumber() + if (version === null || version.isEmpty() || biomePackage.compareVersion(version, "1.8.0") >= 0) { + args.add("--write") + if (features.contains(Feature.UnsafeFixes)) { + args.add("--unsafe") + } + } else { + if (features.contains(Feature.UnsafeFixes)) { + args.add("--apply-unsafe") + } else { + args.add("--apply") + } } + args.add("--skip-errors") + return args } } diff --git a/src/main/kotlin/com/github/biomejs/intellijbiome/actions/BiomeCheckRunner.kt b/src/main/kotlin/com/github/biomejs/intellijbiome/actions/BiomeCheckRunner.kt index 3b5a25a..153d3ec 100644 --- a/src/main/kotlin/com/github/biomejs/intellijbiome/actions/BiomeCheckRunner.kt +++ b/src/main/kotlin/com/github/biomejs/intellijbiome/actions/BiomeCheckRunner.kt @@ -1,9 +1,6 @@ package com.github.biomejs.intellijbiome.actions -import com.github.biomejs.intellijbiome.BiomeBundle -import com.github.biomejs.intellijbiome.BiomeRunner -import com.github.biomejs.intellijbiome.BiomeStdinRunner -import com.github.biomejs.intellijbiome.Feature +import com.github.biomejs.intellijbiome.* import com.github.biomejs.intellijbiome.settings.BiomeSettings import com.intellij.lang.javascript.linter.GlobPatternUtil import com.intellij.openapi.command.WriteCommandAction @@ -40,6 +37,7 @@ class BiomeCheckRunner { val runner = BiomeStdinRunner(project) val manager = FileDocumentManager.getInstance() val settings = BiomeSettings.getInstance(project) + val biomePackage = BiomePackage(project) val requests = documents .mapNotNull { document -> manager.getFile(document)?.let { document to it } } .filter { GlobPatternUtil.isFileMatchingGlobPattern(project, settings.filePattern, it.second) } @@ -52,7 +50,7 @@ class BiomeCheckRunner { indicator.text = commandDescription requests.forEach { request -> - val response = runner.check(request, features) + val response = runner.check(request, features, biomePackage) if (!indicator.isCanceled) { applyChanges(project, request, response)