-
Notifications
You must be signed in to change notification settings - Fork 0
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 #13 from samtkit/codegen
Codegen
- Loading branch information
Showing
72 changed files
with
4,040 additions
and
169 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
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,41 @@ | ||
package tools.samt.cli | ||
|
||
import tools.samt.common.DiagnosticController | ||
import tools.samt.common.SamtConfiguration | ||
import tools.samt.common.SamtLinterConfiguration | ||
import tools.samt.config.SamtConfigurationParser | ||
import java.nio.file.InvalidPathException | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.notExists | ||
|
||
internal object CliConfigParser { | ||
fun readConfig(file: String, controller: DiagnosticController): Pair<SamtConfiguration, SamtLinterConfiguration>? { | ||
val configFile = try { | ||
Path(file) | ||
} catch (e: InvalidPathException) { | ||
controller.reportGlobalError("Invalid path '${file}': ${e.message}") | ||
return null | ||
} | ||
if (configFile.notExists()) { | ||
controller.reportGlobalInfo("Configuration file '${configFile.toUri()}' does not exist, using default configuration") | ||
} | ||
val configuration = try { | ||
SamtConfigurationParser.parseConfiguration(configFile) | ||
} catch (e: Exception) { | ||
controller.reportGlobalError("Failed to parse configuration file '${configFile.toUri()}': ${e.message}") | ||
return null | ||
} | ||
val samtLintConfigFile = configFile.resolveSibling(".samtrc.yaml") | ||
if (samtLintConfigFile.notExists()) { | ||
controller.reportGlobalInfo("Lint configuration file '${samtLintConfigFile.toUri()}' does not exist, using default lint configuration") | ||
} | ||
val linterConfiguration = try { | ||
SamtConfigurationParser.parseLinterConfiguration(samtLintConfigFile) | ||
} catch (e: Exception) { | ||
controller.reportGlobalError("Failed to parse lint configuration file '${samtLintConfigFile.toUri()}': ${e.message}") | ||
return null | ||
} | ||
|
||
return Pair(configuration, linterConfiguration) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,39 @@ | ||
package tools.samt.cli | ||
|
||
import tools.samt.api.plugin.CodegenFile | ||
import java.io.IOException | ||
import java.nio.file.InvalidPathException | ||
import java.nio.file.Path | ||
import kotlin.io.path.* | ||
|
||
internal object OutputWriter { | ||
@Throws(IOException::class) | ||
fun write(outputDirectory: Path, files: List<CodegenFile>) { | ||
if (!outputDirectory.exists()) { | ||
try { | ||
outputDirectory.createDirectories() | ||
} catch (e: IOException) { | ||
throw IOException("Failed to create output directory '${outputDirectory}'", e) | ||
} | ||
} | ||
if (!outputDirectory.isDirectory()) { | ||
throw IOException("Path '${outputDirectory}' does not point to a directory") | ||
} | ||
for (file in files) { | ||
val outputFile = try { | ||
outputDirectory.resolve(file.filepath) | ||
} catch (e: InvalidPathException) { | ||
throw IOException("Invalid path '${file.filepath}'", e) | ||
} | ||
try { | ||
outputFile.parent.createDirectories() | ||
if (outputFile.notExists()) { | ||
outputFile.createFile() | ||
} | ||
outputFile.writeText(file.source) | ||
} catch (e: IOException) { | ||
throw IOException("Failed to write file '${outputFile.toUri()}'", e) | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
plugins { | ||
id("samt-core.kotlin-conventions") | ||
alias(libs.plugins.kover) | ||
} | ||
|
||
dependencies { | ||
implementation(project(":common")) | ||
implementation(project(":parser")) | ||
implementation(project(":semantic")) | ||
implementation(project(":public-api")) | ||
testImplementation(project(":lexer")) | ||
testImplementation(project(":samt-config")) | ||
} |
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,61 @@ | ||
package tools.samt.codegen | ||
|
||
import tools.samt.api.plugin.CodegenFile | ||
import tools.samt.api.plugin.Generator | ||
import tools.samt.api.plugin.GeneratorParams | ||
import tools.samt.api.plugin.TransportConfigurationParser | ||
import tools.samt.api.types.SamtPackage | ||
import tools.samt.codegen.http.HttpTransportConfigurationParser | ||
import tools.samt.codegen.kotlin.KotlinTypesGenerator | ||
import tools.samt.codegen.kotlin.ktor.KotlinKtorConsumerGenerator | ||
import tools.samt.codegen.kotlin.ktor.KotlinKtorProviderGenerator | ||
import tools.samt.common.DiagnosticController | ||
import tools.samt.common.SamtGeneratorConfiguration | ||
import tools.samt.semantic.SemanticModel | ||
|
||
object Codegen { | ||
private val generators: List<Generator> = listOf( | ||
KotlinTypesGenerator, | ||
KotlinKtorProviderGenerator, | ||
KotlinKtorConsumerGenerator, | ||
) | ||
|
||
private val transports: List<TransportConfigurationParser> = listOf( | ||
HttpTransportConfigurationParser, | ||
) | ||
|
||
internal class SamtGeneratorParams( | ||
semanticModel: SemanticModel, | ||
private val controller: DiagnosticController, | ||
override val options: Map<String, String>, | ||
) : GeneratorParams { | ||
private val apiMapper = PublicApiMapper(transports, controller) | ||
override val packages: List<SamtPackage> = semanticModel.global.allSubPackages.map { apiMapper.toPublicApi(it) } | ||
|
||
override fun reportError(message: String) { | ||
controller.reportGlobalError(message) | ||
} | ||
|
||
override fun reportWarning(message: String) { | ||
controller.reportGlobalWarning(message) | ||
} | ||
|
||
override fun reportInfo(message: String) { | ||
controller.reportGlobalInfo(message) | ||
} | ||
} | ||
|
||
fun generate( | ||
semanticModel: SemanticModel, | ||
configuration: SamtGeneratorConfiguration, | ||
controller: DiagnosticController, | ||
): List<CodegenFile> { | ||
val matchingGenerators = generators.filter { it.name == configuration.name } | ||
when (matchingGenerators.size) { | ||
0 -> controller.reportGlobalError("No matching generator found for '${configuration.name}'") | ||
1 -> return matchingGenerators.single().generate(SamtGeneratorParams(semanticModel, controller, configuration.options)) | ||
else -> controller.reportGlobalError("Multiple matching generators found for '${configuration.name}'") | ||
} | ||
return emptyList() | ||
} | ||
} |
Oops, something went wrong.