Skip to content

Instantly share code, notes, and snippets.

@MahmoudMabrok
Created December 19, 2023 14:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MahmoudMabrok/609018eb414e44fc982836ff578f1915 to your computer and use it in GitHub Desktop.
Save MahmoudMabrok/609018eb414e44fc982836ff578f1915 to your computer and use it in GitHub Desktop.
Prepare files for code review
tasks.register("addSpaceToEndOfLines") {
description = "Add space to the end of each line in .kt files and commit changes to Git"
doLast {
println("Starting magic")
project.exec {
commandLine("git", "checkout", "-b" , "refactor/submitCode")
}
println("Checked out new branch")
// Set the directory containing Kotlin files
val kotlinDir = file("src/main/")
// Get all .kt files in the directory
val kotlinFiles = fileTree(kotlinDir).matching { include("**/*.kt") }
// Iterate over each Kotlin file
println("processing to files ${kotlinFiles.files.size}")
for (file in kotlinFiles) {
// Read the content of the Kotlin file
val originalContent = file.readText()
// Add a space to the end of each line
val modifiedContent = originalContent.replace(Regex("$", RegexOption.MULTILINE), " ")
// Write the modified content back to the same file
file.writeText(modifiedContent)
}
println("Adding files")
project.exec {
commandLine("git", "add", ".")
}
println("Committing")
project.exec {
commandLine("git", "commit", "-m", "refactor: prepare files for code review")
}
println("Push to remote")
project.exec {
commandLine("git", "push" , "fork")
}
println("Gradle task 'addSpaceToEndOfLines' completed successfully ")
}
}
@MahmoudMabrok
Copy link
Author

open gradle tab on top right,
then choose excuse gradle task,
it will show you task search for the new one.

@MahmoudMabrok
Copy link
Author

MahmoudMabrok commented Dec 19, 2023

How to use

  • add script to gradle.kts
  • use above steps
  • replace fork with remote name, normally be 'origin'

Modifications

  • you can choose which folder you need by modifying val kotlinDir = file("src/main/")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment