Skip to content

Commit 3bf1267

Browse files
Release v3 (#104)
[BREAKING] Use ant pattern matching to check for included files. This might be a breaking change for some people, please check your included files in the settings if you encounter any issues. E.g. the default inclusion pattern **openapi.json would now no longer match the path openapi/v1-openapi.json, please use **/*openapi.json instead.
1 parent 8b5bb5d commit 3bf1267

File tree

10 files changed

+78
-36
lines changed

10 files changed

+78
-36
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
4343
# Check out current repository
4444
- name: Fetch Sources
45-
uses: actions/checkout@v3
45+
uses: actions/checkout@v4
4646

4747
# Validate wrapper
4848
- name: Gradle Wrapper Validation
@@ -148,7 +148,7 @@ jobs:
148148

149149
# Check out current repository
150150
- name: Fetch Sources
151-
uses: actions/checkout@v3
151+
uses: actions/checkout@v4
152152

153153
# Remove old release drafts by using the curl request for the available releases with a draft flag
154154
- name: Remove Old Release Drafts

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
# Check out current repository
2323
- name: Fetch Sources
24-
uses: actions/checkout@v3
24+
uses: actions/checkout@v4
2525
with:
2626
ref: ${{ github.event.release.tag_name }}
2727

.github/workflows/run-ui-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333

3434
# Check out current repository
3535
- name: Fetch Sources
36-
uses: actions/checkout@v3
36+
uses: actions/checkout@v4
3737

3838
# Setup Java 17 environment for the next steps
3939
- name: Setup Java

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55

66
## [Unreleased]
77

8+
### Added
9+
- Improve check for files to lint on non unix operating systems
10+
11+
### Changed
12+
- [BREAKING] Use [ant pattern matching](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html) to check for included files. This might be a breaking change for some people, please check your included files in the settings if you encounter any issues. E.g. the default inclusion pattern `**openapi.json` would now no longer match the path `openapi/v1-openapi.json`, please use `**/*openapi.json` instead.
13+
814
## [2.1.3] - 2023-09-01
915

1016
### Changed
11-
- Update depenencies
17+
- Update dependencies
1218

1319
## [2.1.2] - 2023-08-28
1420

@@ -68,8 +74,7 @@
6874
- BREAKING: Spectral no longer comes bundled with the plugin and needs to be installed additionally now
6975
- BREAKING: Settings now only apply on a project level
7076

71-
[Unreleased]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.3...HEAD
72-
[2.1.3]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.2...v2.1.3
77+
[Unreleased]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.2...HEAD
7378
[2.1.2]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.1...v2.1.2
7479
[2.1.1]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.1.0...v2.1.1
7580
[2.1.0]: https://github.com/SchwarzIT/spectral-intellij-plugin/compare/v2.0.0...v2.1.0

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,25 @@ Examples:
4545

4646
- Link to a hosted ruleset: `https://raw.githubusercontent.com/SchwarzIT/api-linter-rules/main/spectral-api.yml`
4747
- Local ruleset relative to Project base-path: `.spectral.json`
48-
- Fully-qualified path: `/Users/mick/.spectral.yaml`
48+
- Fully-qualified path: `/Users/user/.spectral.yaml`
4949

5050
### Configurable Included path patterns
5151

5252
Select the files that will be linted. By default, every file called "openapi.json", "openapi.yml" or "openapi.yaml"
5353
within the Project root will be matched for linting by the plugin when it's opened.
5454

55-
You can adjust this in the settings under Preferences -> Tools -> Spectral -> Included path patterns. All paths are relative
56-
to the project's root directory unless absolute.
55+
You can adjust this in the settings under Preferences -> Tools -> Spectral -> Included path patterns. All paths are
56+
relative to the project's root directory unless absolute.
5757

5858
Examples:
5959

6060
- `openapi.json`: Matches the file called "openapi.json" inside the root directory of the project
61-
- `components/**.yaml`: Matches all files inside the project subdirectory "components" that end with ".yaml"
62-
- `/Users/mick/code/**/openapi*.yaml`: Matches all YAML files within the absolute path "/Users/mick/code" that start with "openapi" and end with ".yaml"
61+
- `components/**/*.yaml`: Matches all files inside the project subdirectory "components" that end with ".yaml"
62+
- `/Users/user/code/**/openapi*.yaml`: Matches all YAML files within the absolute path "/Users/mick/code" that start
63+
with "openapi" and end with ".yaml"
6364

64-
**Note:** Each file must also be recognised by the IDE as a JSON or YAML file - that is with a suitable File Type association.
65+
**Note:** Each file must also be recognised by the IDE as a JSON or YAML file - that is with a suitable File Type
66+
association.
6567
If it is detected as a plain text (or any other type) it will be ignored.
6668

6769
<!-- Plugin description end -->

build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
// Gradle IntelliJ Plugin
1313
id("org.jetbrains.intellij") version "1.15.0"
1414
// Gradle Changelog Plugin
15-
id("org.jetbrains.changelog") version "2.1.2"
15+
id("org.jetbrains.changelog") version "2.2.0"
1616
// Gradle Qodana Plugin
1717
id("org.jetbrains.qodana") version "0.1.13"
1818
// Gradle Kover Plugin
@@ -141,5 +141,7 @@ dependencies {
141141
implementation(kotlin("stdlib"))
142142
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
143143
implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.10")
144+
implementation("org.springframework:spring-core:6.0.11")
145+
144146
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
145147
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin
33
pluginName=Spectral
44
pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin
55
# SemVer format -> https://semver.org
6-
pluginVersion=2.1.3
6+
pluginVersion=3.0.0
77
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
88
pluginSinceBuild=222
99
#pluginUntilBuild=231.*

src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import com.intellij.psi.PsiDocumentManager
1515
import com.intellij.psi.PsiFile
1616
import com.schwarzit.spectralIntellijPlugin.settings.ProjectSettingsState
1717
import org.jetbrains.yaml.psi.YAMLFile
18-
import java.nio.file.FileSystems
18+
import org.springframework.util.AntPathMatcher
19+
import java.io.File
1920
import java.nio.file.Path
2021
import java.nio.file.Paths
2122

@@ -31,7 +32,15 @@ class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile, Editor>, List<
3132
val includedFiles = settings?.includedFiles?.lines() ?: emptyList()
3233

3334
try {
34-
if (!isFileIncluded(file.project.basePath ?: "", file.virtualFile.toNioPath(), includedFiles)) return null
35+
if (!isFileIncluded(
36+
Paths.get(file.project.basePath ?: "/"),
37+
file.virtualFile.toNioPath(),
38+
includedFiles
39+
)
40+
) {
41+
logger.trace("The given file ${file.virtualFile.toNioPath()} did not match any pattern defined in the settings")
42+
return null
43+
}
3544
} catch (e: Throwable) {
3645
logger.error("Failed to check if current file is included. Parameters: basePath: ${file.project.basePath}, path: ${file.virtualFile.toNioPath()}, includedFiles: $includedFiles")
3746
return null
@@ -40,18 +49,23 @@ class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile, Editor>, List<
4049
return Pair(file, editor)
4150
}
4251

43-
fun isFileIncluded(basePath: String, path: Path, includedFiles: List<String>): Boolean {
44-
var matcherPattern = "glob:{"
45-
val iterator = includedFiles.iterator()
46-
while (iterator.hasNext()) {
47-
val pathPattern = iterator.next()
48-
if (!Paths.get(pathPattern).isAbsolute) matcherPattern += "$basePath/"
49-
matcherPattern += pathPattern
50-
matcherPattern += if (iterator.hasNext()) "," else "}"
52+
fun isFileIncluded(
53+
basePath: Path,
54+
path: Path,
55+
includedFiles: List<String>,
56+
separator: String = File.separator
57+
): Boolean {
58+
val pathMatcher = AntPathMatcher(separator)
59+
60+
return includedFiles.any { s ->
61+
var globPattern = s
62+
if (!Paths.get(s).isAbsolute) {
63+
var base = basePath.toString()
64+
if (!base.endsWith(separator)) base += separator
65+
globPattern = base + s
66+
}
67+
return pathMatcher.match(globPattern, path.toString())
5168
}
52-
logger.trace(matcherPattern)
53-
val fileMatcher = FileSystems.getDefault().getPathMatcher(matcherPattern)
54-
return fileMatcher.matches(path)
5569
}
5670

5771
override fun doAnnotate(info: Pair<PsiFile, Editor>): List<SpectralIssue> {

src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/ProjectSettingsState.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class ProjectSettingsState : PersistentStateComponent<ProjectSettingsState> {
1515

1616
var ruleset: String = "https://raw.githubusercontent.com/SchwarzIT/api-linter-rules/main/spectral-api.yml"
1717
var includedFiles: String = """
18-
**openapi.json
19-
**openapi.yml
20-
**openapi.yaml
18+
**/*openapi.json
19+
**/*openapi.yml
20+
**/*openapi.yaml
2121
""".trimIndent()
2222

2323
override fun getState(): ProjectSettingsState {

src/test/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotatorTest.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ class SpectralExternalAnnotatorTest {
1111

1212
@ParameterizedTest(name = "isFileIncluded {index} - {arguments}")
1313
@MethodSource("provideIsFileIncludedTestParams")
14-
fun isFileIncluded(basePath: String, path: String, includedFiles: List<String>, isIncluded: Boolean) {
14+
fun isFileIncluded(
15+
basePath: String,
16+
path: String,
17+
includedFiles: List<String>,
18+
isIncluded: Boolean,
19+
separator: String
20+
) {
1521
val spectralExternalAnnotator = SpectralExternalAnnotator()
16-
val fileIncluded = spectralExternalAnnotator.isFileIncluded(basePath, Paths.get(path), includedFiles)
22+
val fileIncluded =
23+
spectralExternalAnnotator.isFileIncluded(Paths.get(basePath), Paths.get(path), includedFiles, separator)
1724
Assertions.assertEquals(fileIncluded, isIncluded)
1825
}
1926

@@ -24,9 +31,21 @@ class SpectralExternalAnnotatorTest {
2431
Arguments.of(
2532
"/home/user/project",
2633
"/home/user/project/src/openapi.json",
27-
listOf("**openapi.json"),
28-
true
29-
)
34+
listOf("**/openapi.json"),
35+
true,
36+
"/"
37+
),
38+
Arguments.of(
39+
"C:\\Users\\Username\\Projekt\\",
40+
"C:\\Users\\Username\\Projekt\\API_Name.yaml",
41+
listOf("**.yaml"),
42+
true,
43+
"\\"
44+
),
45+
Arguments.of("/test", "/test/testing.test", listOf("*.test"), true, "/"),
46+
Arguments.of("/", "/foo/bar/something/test.json", listOf("**/*.json"), true, "/"),
47+
Arguments.of("/", "test.test", listOf("*.json", "*.yml"), false, "/"),
48+
Arguments.of("/", "/test/openapi.json", listOf("**.json"), false, "/")
3049
)
3150
}
3251
}

0 commit comments

Comments
 (0)