Skip to content

AntonButov/code-factory

Repository files navigation

GitHub release (latest by date) Maven Central GitHub issues License Kotlin Version KSP GitHub stars

Code Factory

Write your tests, and let AI generate the code.

If changes are needed, simply update the tests, and AI will regenerate the code to meet the new requirements.

Setup

  1. Create a local.properties file and add the following:
API_KEY="sk-proj-mSwcp..."
  1. Kotlin (KTS)
plugins {
    id("com.google.devtools.ksp") version "1.9.24-1.0.20"
}
  1. Add dependencies
dependencies {
    ksp("io.github.antonbutov:code-factory-processor:$last_version")
}

How it works

  1. Create an interface without an implementation
interface ForGenerate {
    fun plus(first: Int, second: Int): Int
}
  1. Write a test with not implemented implementation
class GeneratedTest {

    @Test
    fun generatedFileGets() {
       val forGenerate: ForGenerate = GeneratedCode()
       val expected = forGenerate.plus(2, 2)
       assertEquals(4, expected)
    }
}
  1. Build the module 🚀. The plugin has to generate
class GeneratedCode(): ForGenerate {
    override fun plus(first: Int, second: Int): Int {
    return first + second
    }
}

You will see all requests to OpenAI:

w: [ksp] Request: ---->
You had to implement interface: ForGenerate Return only code implementation please.
interface ForGenerate {
    fun plus(
        first: Int,
        second: Int,
    ): Int
}

import kotlin.test.Test
import kotlin.test.assertEquals

class GeneratedTest {
    @Test
    fun generatedFileGets() {
        val forGenerate: ForGenerate = GeneratedCode()
        val expected = forGenerate.plus(2, 2)
        assertEquals(4, expected)
    }
}

Response: <----
    Sure, the implementation of the interface `ForGenerate` can be done as follows:

    ```kotlin
class GeneratedCode(): ForGenerate {
    override fun plus(first: Int, second: Int): Int {
    return first + second
    }
}

Samples

kotlin-code-factory-samples

Technologies Used

Code Factory is built with the following technologies:

Contributions Welcome!

We follow best practices to ensure a robust and maintainable codebase. Contributions are welcome! 🚀 We're looking for skilled contributors, especially those with expertise in AI and Kotlin. Join us in enhancing Code Factory's capabilities!

If you need OpenAI API key, write to me at [email protected]. I can give it to you within 24 hours.

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.