Skip to content

Instantly share code, notes, and snippets.

@Sedose
Last active July 22, 2025 07:42
Show Gist options
  • Select an option

  • Save Sedose/6e69430e11f287cb42115ca9f4dd4df8 to your computer and use it in GitHub Desktop.

Select an option

Save Sedose/6e69430e11f287cb42115ca9f4dd4df8 to your computer and use it in GitHub Desktop.

Gradle Task: Copy Source files Context to Clipboard for Use with LLMs

This Gradle task collects .java source files and other files, concatenates their contents, and copies the result to your system clipboard.

It’s designed to help developers programmatically control what context is given to LLMs (like ChatGPT, Claude, Gemini) by automating the process of assembling relevant code and docs into a single pasteable chunk.

Features

  • Collects all .java files under src/
  • Includes key project files (README.md, AGENTS.md, build.gradle.kts)
  • Joins all file contents with readable spacing
  • Copies the result directly to your system clipboard (Windows-only)
  • Ideal for sending real, runnable project context into LLM prompts

🛠 Usage

  1. Copy the task into your build.gradle.kts:
tasks.register("copySourcesToClipboard") {
    group = "custom"
    description = "Collects project files and copies them to clipboard"

    doLast {
        val projectRoot = project.projectDir.toPath()

        val sourceFiles =
            Files.walk(projectRoot.resolve("src"))
                .filter { it.toString().endsWith(".java") }
                .toList()

        val extraFiles =
            listOf("README.md", "AGENTS.md", "build.gradle.kts")
                .map { projectRoot.resolve(it) }
                .filter { Files.exists(it) }

        val allFiles = sourceFiles + extraFiles

        val combined =
            allFiles.joinToString(System.lineSeparator().repeat(2)) { path ->
                Files.readString(path)
            }

        val tmpFile = Files.createTempFile("collected", ".txt")
        Files.writeString(tmpFile, combined, StandardOpenOption.TRUNCATE_EXISTING)

        exec {
            commandLine("clip.exe")
            standardInput = tmpFile.toFile().inputStream()
        }

        println("Copied ${allFiles.size} files to clipboard.")
    }
}
  1. Run the task:
./gradlew copySourcesToClipboard
  1. Paste into your favorite LLM prompt window. It now has full project context for accurate answers, suggestions, or code generation.

💡 Use Cases

  • Debugging with AI assistants
  • Prompt engineering from live project code
  • Bootstrapping feature implementations from current source
  • AI-generated documentation or refactoring plans

⚠️ Notes

  • This version only works on Windows (uses clip.exe).
  • It runs successfully from WSL2 Ubuntu.
  • On macOS, replace clip.exe with pbcopy.
  • On Linux, use something like xclip -selection clipboard or xsel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment