Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
klahap committed Nov 25, 2024
0 parents commit c49d04d
Show file tree
Hide file tree
Showing 39 changed files with 2,041 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Publish Gradle Plugin

on:
push:
tags:
- '*.*.*'

permissions:
contents: read

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: publish
env:
GIT_TAG_VERSION: ${{ github.ref_name }}
GRADLE_PUBLISH_KEY: ${{ secrets.GRADLE_PUBLISH_KEY }}
GRADLE_PUBLISH_SECRET: ${{ secrets.GRADLE_PUBLISH_SECRET }}
run: ./gradlew publishPlugins
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.gradle
.idea
.kotlin
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
.env
test-schema.sql

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Kotlin ###
.kotlin

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 klahap

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Kotlin Exposed Table Generator Gradle Plugin

## Overview

The **Kotlin Exposed Table Generator** is a Gradle plugin designed to generate
Kotlin [Exposed](https://github.com/JetBrains/Exposed) table definitions directly from a PostgreSQL database schema.
Simplify your workflow by automating the creation of table mappings and keeping them in sync with your database.

## Features

- Generate Kotlin Exposed DSL table definitions.
- Filter tables by schema for precise control.
- Keep your code synchronized with database schema changes effortlessly.

## Installation

Add the plugin to your `build.gradle.kts`:

```kotlin
plugins {
id("io.github.klahap.pgen") version "$VERSION"
}
```

## Configuration

To configure the plugin, add the `pgen` block to your `build.gradle.kts`:

```kotlin
pgen {
dbConnectionConfig(
url = System.getenv("DB_URL"), // Database URL
user = System.getenv("DB_USER"), // Database username
password = System.getenv("DB_PASSWORD") // Database password
)
packageName("io.example.db") // Target package for generated tables
tableFilter {
addSchemas("public") // Include only specific schemas (e.g., "public")
}
outputPath("./output") // Output directory for generated files
}
```

### Environment Variables

Make sure to set the following environment variables:

- `DB_URL`: The connection URL for your PostgreSQL database.
- `DB_USER`: Your database username.
- `DB_PASSWORD`: Your database password.

## Running the Plugin

Once configured, generate your Kotlin Exposed table definitions by running:

```bash
./gradlew pgen
```

This will create Kotlin files in the specified `outputPath`.

## Example

### Input: Database Schema

Assume a PostgreSQL schema with the following table:

```sql
CREATE TYPE status AS ENUM ('ACTIVE', 'INACTIVE');

CREATE TABLE users
(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
status status
);
```

### Output: Generated Kotlin File

The plugin will generate a Kotlin Exposed DSL table:

```kotlin
package io.example.db

import org.jetbrains.exposed.sql.Table

enum class Status(
override val pgEnumLabel: String,
) : PgEnum {
ACTIVE(pgEnumLabel = "ACTIVE"),
INACTIVE(pgEnumLabel = "INACTIVE");

override val pgEnumTypeName: String = "public.status"
}

object Users : Table("users") {
val id: Column<Int> = integer(name = "id")
val name: Column<String> = text(name = "name")
val status: Column<Status> = customEnumeration(
name = "status",
sql = "status",
fromDb = { getPgEnumByLabel(it as String) },
toDb = { it.toPgObject() },
)

override val primaryKey: Table.PrimaryKey = PrimaryKey(id, name = "users_pkey")
}
```

## Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve the plugin.

## License

This project is licensed under the [MIT License](LICENSE).
46 changes: 46 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

plugins {
id("com.gradle.plugin-publish") version "1.2.1"
kotlin("jvm") version "2.0.20"
}

val groupStr = "io.github.klahap.pgen"
val gitRepo = "https://github.com/klahap/pgen"

version = System.getenv("GIT_TAG_VERSION") ?: "1.0.0-SNAPSHOT"
group = groupStr

repositories {
mavenCentral()
}

dependencies {
implementation("com.squareup:kotlinpoet:2.0.0")
implementation("org.postgresql:postgresql:42.7.2")
}

kotlin {
compilerOptions {
freeCompilerArgs.add("-Xjsr305=strict")
freeCompilerArgs.add("-Xcontext-receivers")
jvmTarget.set(JvmTarget.JVM_21)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
}
}

gradlePlugin {
website = gitRepo
vcsUrl = "$gitRepo.git"

val generateFrappeDsl by plugins.creating {
id = groupStr
implementationClass = "$groupStr.Plugin"
displayName = "Generate Kotlin Exposed tables from a PostgreSQL database schema"
description =
"This Gradle plugin simplifies the development process by automatically generating Kotlin Exposed table definitions from a PostgreSQL database schema. It connects to your database, introspects the schema, and creates Kotlin code for Exposed DSL, including table definitions, column mappings, and relationships. Save time and eliminate boilerplate by keeping your Exposed models synchronized with your database schema effortlessly."
tags =
listOf("Kotlin Exposed", "PostgreSQL", "Exposed", "Kotlin DSL", "Database Integration", "Code Generation")
}
}
17 changes: 17 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3"

services:
db:
image: pgvector/pgvector:0.8.0-pg17
command: [ ]
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- "5438:5432"
volumes:
- db-data:/var/lib/postgresql/default_code

volumes:
db-data:
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.code.style=official
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Sun Nov 17 11:56:54 CET 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit c49d04d

Please sign in to comment.