-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring - cleaner package structure and command line interface (#39)
* refactoring - cleaner package structure and command line interface * update documentation * update norm-cli to norm-codegen * upload the cli as artifact * gradle autoupgrade
- Loading branch information
Showing
36 changed files
with
549 additions
and
301 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
name: Build | ||
name: Build the Distribution | ||
|
||
on: [push] | ||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: Set up JDK | ||
uses: actions/[email protected] | ||
with: | ||
java-version: 11 | ||
- name: Build with Gradle | ||
run: ./gradlew build | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up JDK 11 | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: 11 | ||
|
||
- name: Grant execute permission for gradlew | ||
run: chmod +x gradlew | ||
|
||
- name: Build with Gradle | ||
run: gradle clean build | ||
|
||
- name: Dist | ||
run: gradle cli:distZip | ||
|
||
- name: Upload binary | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: norm-codegen | ||
path: cli/build/distributions/norm-codegen.zip |
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,27 @@ | ||
## runs gradle wrapper task and creates a PR | ||
|
||
name: Upgrade Gradle | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
|
||
schedule: | ||
- cron: 0 2 * * 1 | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Run a one-line script | ||
run: | | ||
LATEST_VER=$(curl https://api.github.com/repos/gradle/gradle/releases/latest | jq -r .name) | ||
./gradlew wrapper --gradle-version ${LATEST_VER} --distribution-type all | ||
- name: Create Pull Request | ||
uses: peter-evans/create-pull-request@v2 | ||
with: | ||
commit-message: "[gradle-updater] updating gradle wrapper to latest version" | ||
title: "Upgrade Gradle" |
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,25 @@ | ||
package norm.api | ||
|
||
import norm.analyzer.SqlAnalyzer | ||
import norm.codegen.CodeGenerator | ||
import java.sql.Connection | ||
|
||
/** | ||
* Initialize with a given postgres connection | ||
* | ||
* Generates code of classes against given SQL Query | ||
*/ | ||
class NormApi( | ||
connection: Connection | ||
) { | ||
private val sqlAnalyzer = SqlAnalyzer(connection) | ||
private val codeGenerator = CodeGenerator() | ||
|
||
/** | ||
* Generates code of classes against given SQL Query | ||
*/ | ||
fun generate(query: String, packageName: String, baseName: String): String { | ||
val sqlModel = sqlAnalyzer.sqlModel(query) | ||
return codeGenerator.generate(sqlModel, packageName, baseName) | ||
} | ||
} |
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,83 @@ | ||
package norm.cli | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.options.* | ||
import com.github.ajalt.clikt.parameters.types.file | ||
import norm.api.NormApi | ||
import norm.fs.IO | ||
import norm.fs.globSearch | ||
import norm.util.withPGConnection | ||
import java.io.File | ||
import kotlin.system.exitProcess | ||
|
||
|
||
/** | ||
* Entry point - The main function | ||
*/ | ||
fun main(args: Array<String>) = NormCli().main(args) | ||
|
||
|
||
/** | ||
* Implementation of CLI using Norm API | ||
* | ||
* Can use env variable to pass in sensitive information | ||
*/ | ||
class NormCli : CliktCommand( // command name is inferred as norm-cli | ||
name = "norm-codegen", | ||
help = """ | ||
Generates Kotlin Source files for given SQL files using the Postgres database connection | ||
""" | ||
) { | ||
|
||
private val jdbcUrl by option("-j", "--jdbc-url", help = "JDBC connection URL (can use env var PG_JDBC_URL)", envvar = "PG_JDBC_URL") | ||
.default("jdbc:postgresql://localhost/postgres") | ||
|
||
private val username by option("-u", "--username", help = "Username (can use env var PG_USERNAME)", envvar = "PG_USERNAME") | ||
.default("postgres") | ||
|
||
private val password by option("-p", "--password", help = "Password (can use env var PG_PASSWORD)", envvar = "PG_PASSWORD") | ||
.default("") | ||
|
||
private val basePath by option("-b", "--base-path", help = " relative path from this dir will be used to infer package name") | ||
.file(canBeFile = false, canBeDir = true, mustExist = true) | ||
.default(File(".")) // Current working dir | ||
|
||
private val inputFiles by option("-f", "--file", help = "[Multiple] SQL files, the file path will be used to infer package name") | ||
.file(canBeFile = true, canBeDir = false, mustExist = true) | ||
.multiple() | ||
.unique() | ||
|
||
private val inputDir by option("-d", "--in-dir", help = "Dir containing .sql files, relative path from this dir will be used to infer package name") | ||
.file(canBeFile = false, canBeDir = true, mustExist = true) | ||
|
||
private val outDir by option("-o", "--out-dir", help = "Output dir where source should be generated") | ||
.file(canBeFile = false, canBeDir = true, mustExist = true) | ||
.required() | ||
|
||
|
||
override fun run() { | ||
try { | ||
withPGConnection(jdbcUrl, username, password) { connection -> | ||
val normApi = NormApi(connection) | ||
|
||
// If dir is provided, relativize to itself | ||
inputDir?.let { dir -> | ||
globSearch(dir, "**.sql").forEach { sqlFile -> | ||
IO(sqlFile, dir, outDir).process(normApi::generate) | ||
} | ||
} | ||
|
||
// If dir is provided, relativize to basePath | ||
inputFiles.forEach { sqlFile -> | ||
IO(sqlFile, basePath, outDir).process(normApi::generate) | ||
} | ||
} | ||
exitProcess(0) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
exitProcess(1) | ||
} | ||
} | ||
} | ||
|
||
|
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,25 @@ | ||
package norm.fs | ||
|
||
import norm.util.toTitleCase | ||
import java.io.File | ||
|
||
class IO( | ||
private val sqlFile: File, | ||
baseDir: File, | ||
outDir: File | ||
) { | ||
private val sqlFileRelativeToSource = sqlFile.relativeTo(baseDir) | ||
private val nameWithoutExtension = sqlFileRelativeToSource.nameWithoutExtension | ||
private val parentPath = sqlFileRelativeToSource.parent | ||
private val packageName = parentPath.replace(File.separator, ".") | ||
private val baseName = toTitleCase(nameWithoutExtension) | ||
private val outFileParentDir = File(outDir, parentPath) | ||
private val outputFile = File(outFileParentDir, "$baseName.kt") | ||
|
||
fun process(block: (query: String, packageName: String, baseName: String) -> String) { | ||
outFileParentDir.mkdirs() | ||
println("will write to $outputFile") | ||
outputFile.writeText(block(sqlFile.readText(), packageName, baseName)) | ||
} | ||
} | ||
|
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,18 @@ | ||
package norm.fs | ||
|
||
import java.io.File | ||
import java.nio.file.FileSystems | ||
|
||
/** | ||
* Searches within a directory using glob style patterns | ||
* | ||
* example "**.txt" | ||
* | ||
* @param root must be Root Directory in which search will be performed | ||
* @param pattern must be valid Glob Pattern | ||
* | ||
*/ | ||
fun globSearch(root: File, pattern: String): Sequence<File> { | ||
val pathMatcher = FileSystems.getDefault().getPathMatcher("glob:$pattern")!! | ||
return root.walkTopDown().filter { file -> pathMatcher.matches(file.toPath()) } | ||
} |
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,14 @@ | ||
package norm.util | ||
|
||
import org.postgresql.ds.PGSimpleDataSource | ||
import java.sql.Connection | ||
|
||
/** | ||
* provides connection to the callback and closes it when done, this way code block does not need to manage connection | ||
*/ | ||
fun withPGConnection(url: String, username: String, password: String, fn: (Connection) -> Unit) = | ||
PGSimpleDataSource().also { | ||
it.setUrl(url) // url is not a property | ||
it.user = username | ||
it.password = password | ||
}.connection.use(fn) |
File renamed without changes.
Oops, something went wrong.