-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from sideeffffect/guardrailDiscoveredOpenApiF…
…iles Allow automatic discovery of OpenAPI spec files
- Loading branch information
Showing
17 changed files
with
1,642 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,23 +10,20 @@ jobs: | |
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
java: [ '8', '11', '15' ] | ||
java: [ '8', '11', '17' ] | ||
scala: [ '2.12.16' ] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
- name: Set up JDK | ||
uses: actions/setup-java@v1 | ||
uses: actions/setup-java@v3.4.1 | ||
with: | ||
distribution: 'temurin' | ||
java-version: ${{ matrix.java }} | ||
- name: print Java version | ||
run: java -version | ||
- uses: actions/cache@v1 | ||
with: | ||
path: ~/.cache/coursier | ||
key: ${{ runner.os }}-scala-${{ matrix.scala }}-${{ hashFiles('**/*.sbt') }} | ||
restore-keys: | | ||
${{ runner.os }}-scala-${{ matrix.scala }}- | ||
- name: Coursier cache | ||
uses: coursier/[email protected] | ||
- name: Run tests | ||
run: sbt ++${{ matrix.scala }} "^ test; scripted" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dev.guardrail | ||
package sbt | ||
|
||
import _root_.sbt._ | ||
|
||
object GuardrailHelpers { | ||
|
||
private def dropExtension(filePath: String): String = { | ||
filePath.split('.').toList match { | ||
case l@List() => l | ||
case l@List(_) => l | ||
case l => l.dropRight(1) | ||
} | ||
}.mkString(".") | ||
|
||
private def recursiveListFiles(f: File): List[File] = { | ||
val these = Option(f.listFiles).toList.flatten | ||
these.filter(f => f.isFile) ++ these.filter(_.isDirectory).flatMap(recursiveListFiles) | ||
} | ||
|
||
def isOpenApiSpec(file: File): Boolean = { | ||
import org.snakeyaml.engine.v2.api.{Load, LoadSettings} | ||
|
||
import java.util.{Map => JMap} | ||
import scala.io.Source | ||
import scala.jdk.CollectionConverters._ | ||
import scala.util.{Try, Using} | ||
|
||
Using(Source.fromFile(file)) { source => | ||
val reader = source.bufferedReader() | ||
val docs = new Load(LoadSettings.builder().setAllowDuplicateKeys(true).setAllowRecursiveKeys(true).build()).loadAllFromReader(reader).asScala | ||
val yamls = docs.flatMap(d => Try(d.asInstanceOf[JMap[String, Any]].asScala).toOption).toList | ||
yamls.exists(m => m.contains("openapi") || m.contains("swagger")) | ||
}.toOption.getOrElse(false) | ||
} | ||
|
||
case class DiscoveredFile(base: File, file: File, fileRelative: File) { | ||
val fileRelativePath: String = fileRelative.getPath | ||
val fileRelativePathWithoutExtension: String = dropExtension(fileRelative.getPath) | ||
val pkg: String = fileRelativePathWithoutExtension.replace(Path.sep, '.') | ||
} | ||
|
||
def discoverFiles(base: File): List[DiscoveredFile] = recursiveListFiles(base).flatMap { file => | ||
file.relativeTo(base).map { fileRelative => | ||
DiscoveredFile(base, file, fileRelative) | ||
} | ||
} | ||
|
||
def discoverOpenApiFiles(base: File): List[DiscoveredFile] = | ||
discoverFiles(base).filter(f => isOpenApiSpec(f.file)) | ||
|
||
def createGuardrailTasks( | ||
sourceDirectory: File, | ||
)(discoveredFileToTasks: DiscoveredFile => List[dev.guardrail.sbt.Types.Args]): List[dev.guardrail.sbt.Types.Args] = | ||
discoverOpenApiFiles(sourceDirectory).flatMap(discoveredFileToTasks) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.