Skip to content

Commit

Permalink
Check new factorio API releases (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
serieznyi authored May 11, 2024
1 parent 2b58af6 commit 01a048e
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 16 deletions.
29 changes: 25 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ jobs:
- { name: Upload `junit-report` artifact, uses: actions/upload-artifact@v4, with: { name: junit-report, path: build/reports/junit, retention-days: "${{ env.ARTIFACT_TTL }}" } }
- { name: Upload `jacoco-report` artifact, uses: actions/upload-artifact@v4, with: { name: jacoco-report, path: build/reports/jacoco, retention-days: "${{ env.ARTIFACT_TTL }}" } }

analyse:
name: Analyse
sonarQube:
name: SonarQube
runs-on: ubuntu-latest
needs: [testing]
steps:
Expand All @@ -59,7 +59,6 @@ jobs:
- { name: Download `junit-report`, uses: actions/download-artifact@v4, with: { name: junit-report, path: build/reports/junit } }
- { name: Download `jacoco-report`, uses: actions/download-artifact@v4, with: { name: jacoco-report, path: build/reports/jacoco } }
- { name: Download `compiled`, uses: actions/download-artifact@v4, with: { name: compiled, path: build/classes/ } }
- run: find .
- name: Cache SonarCloud packages
uses: actions/cache@v4
with:
Expand All @@ -79,10 +78,32 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_SCANNER_OPTS: "-Dsonar.qualitygate.wait=true -Dsonar.projectVersion=$CI_COMMIT_TAG"

checkApiVersion:
name: Check API version
runs-on: ubuntu-latest
steps:
- { name: Fetch sources, uses: actions/checkout@v4, with: { fetch-depth: 0 } }
- name: Parse last exists API version
run: >-
curl https://lua-api.factorio.com/latest/runtime-api.json 2>/dev/null \
| jq .application_version \
| tr -d '"' \
| { read VAR; echo "LAST_API_VERSION=$VAR" >> $GITHUB_ENV; }
- name: Parse last supported API version
run: >-
grep "supportedApiVersions.max" src/main/resources/FactorioApiCompletion.properties \
| awk -F '=' '{print $2}' \
| { read VAR; echo "LAST_SUPPORTED_API_VERSION=$VAR" >> $GITHUB_ENV; }
- name: Fail if not support latest version
if: ${{ env.LAST_API_VERSION != env.LAST_SUPPORTED_API_VERSION }}
uses: actions/github-script@v3
with:
script: core.setFailed('Plugin not support latest API version')

verifyPlugin:
name: Verify Plugin
runs-on: ubuntu-latest
needs: [analyse]
needs: [sonarQube]
steps:
- { name: Fetch sources, uses: actions/checkout@v4, with: { fetch-depth: 0 } }
- { name: Set up JDK, uses: actions/setup-java@v4, with: { distribution: "${{ env.JAVA_DISTRIBUTION }}", java-version: "${{ env.JAVA_VERSION }}" } }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.serieznyi.intellij.factorioapicompletion.core.factorio.version

import com.intellij.util.text.SemVer
import io.serieznyi.intellij.factorioapicompletion.core.factorio.version.ApiVersion.Companion.createVersion
import io.serieznyi.intellij.factorioapicompletion.core.versioning.SemVer
import io.serieznyi.intellij.factorioapicompletion.intellij.FactorioApiCompletionBundle
import org.jsoup.Jsoup
import java.io.IOException
import java.util.*
Expand All @@ -12,10 +13,10 @@ import java.util.stream.Collectors
*
* @see ApiVersion
*/
class ApiVersionResolver {
private val minimalSupportedVersion = SemVer("1.1.100", 1, 1, 100)
private val maximalSupportedVersion = SemVer("1.1.107", 1, 1, 107)

class ApiVersionResolver(
private val minimalSupportedVersion: SemVer,
private val maximalSupportedVersion: SemVer
) {
@Throws(IOException::class)
fun supportedVersions(): ApiVersionCollection {
val allSupportedVersions = allVersions()
Expand Down Expand Up @@ -44,15 +45,25 @@ class ApiVersionResolver {
val mainPageDoc = Jsoup.connect(VERSION_HTML_PAGE).get()
val allLinks = mainPageDoc.select("a")
for (link in allLinks) {
val semVer = SemVer.parseFromText(link.text()) ?: continue
val rawVersion = link.text()

versions.add(semVer)
if (SemVer.isValid(rawVersion)) {
versions.add(SemVer(rawVersion))
}
}

return versions
}

companion object {
private const val VERSION_HTML_PAGE = "https://lua-api.factorio.com/"

@JvmStatic
fun instance(): ApiVersionResolver {
val minVersionRaw = FactorioApiCompletionBundle.INSTANCE.message("supportedApiVersions.max")
val maxVersionRaw = FactorioApiCompletionBundle.INSTANCE.message("supportedApiVersions.min")

return ApiVersionResolver(SemVer(maxVersionRaw), SemVer(minVersionRaw))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.serieznyi.intellij.factorioapicompletion.core.versioning

class SemVer(): Comparable<SemVer> {
private lateinit var rawVersion: String

constructor(rawVersion: String) : this() {
this.rawVersion = checkRawVersion(rawVersion)
}

override operator fun compareTo(other: SemVer): Int {
val thisVersion = com.intellij.util.text.SemVer.parseFromText(rawVersion)
val otherVersion = com.intellij.util.text.SemVer.parseFromText(other.rawVersion)

return thisVersion!!.compareTo(otherVersion)
}

override fun toString(): String {
return this.rawVersion
}

companion object {
fun checkRawVersion(rawVersion: String): String {
if (!isValid(rawVersion)) {
throw IllegalArgumentException("Wrong version")
}

return rawVersion
}

fun isValid(rawVersion: String): Boolean {
val semVer = com.intellij.util.text.SemVer.parseFromText(rawVersion)

return semVer != null
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.serieznyi.intellij.factorioapicompletion.intellij

import com.intellij.DynamicBundle
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.PropertyKey


class FactorioApiCompletionBundle() : DynamicBundle(BUNDLE_FILE) {
fun message(
@NotNull @PropertyKey(resourceBundle = BUNDLE_FILE) key: String,
@NotNull vararg params: Any
): String {
return INSTANCE.getMessage(key, params)
}

companion object {
private const val BUNDLE_FILE = "FactorioApiCompletion"
val INSTANCE = FactorioApiCompletionBundle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PluginSettings : PersistentStateComponent<PluginSettings> {
var useLatestApiVersion: Boolean = true

@OptionTag(converter = ApiVersionConverter::class)
var selectedApiVersion: ApiVersion = (ApiVersionResolver().supportedVersions().latestVersion())
var selectedApiVersion: ApiVersion = (ApiVersionResolver.instance().supportedVersions().latestVersion())

override fun getState(): PluginSettings {
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public PluginConfigurable(Project project) {
pluginSettings = project.getService(PluginSettings.class);
this.project = project;

apiVersionResolver = new ApiVersionResolver();
apiVersionResolver = ApiVersionResolver.instance();

buildUiComponents();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/FactorioApiCompletion.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pluginName=Factorio Api Completion
ui.settings.enable=&Enable
supportedApiVersions.min=1.1.100
supportedApiVersions.max=1.1.107
ui.settings.reload=Reload
ui.settings.selectApiVersionLabel=Select Factorio API version
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ApiParserTest {
companion object {
@JvmStatic
fun parseProvider(): Stream<ApiVersion> {
return ApiVersionResolver().supportedVersions().reversed().stream()
return ApiVersionResolver.instance().supportedVersions().reversed().stream()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.serieznyi.intellij.factorioapicompletion.core.factorio.version
import org.junit.jupiter.api.Assertions

class ApiVersionResolverTest {
private var apiVersionResolver: ApiVersionResolver = ApiVersionResolver()
private var apiVersionResolver: ApiVersionResolver = ApiVersionResolver.instance()

@org.junit.jupiter.api.BeforeEach
fun setUp() {
Expand Down

0 comments on commit 01a048e

Please sign in to comment.