|
| 1 | +package io.github.kscripting.kscript.generator |
| 2 | + |
| 3 | +import io.github.kscripting.kscript.util.Logger.errorMsg |
| 4 | +import io.github.kscripting.kscript.util.Logger.infoMsg |
| 5 | +package io.github.kscripting.kscript.generator |
| 6 | + |
| 7 | +import io.github.kscripting.kscript.cache.Cache |
| 8 | +import io.github.kscripting.kscript.model.Config |
| 9 | +import io.github.kscripting.kscript.model.Script |
| 10 | +import io.github.kscripting.kscript.parser.Parser |
| 11 | +import io.github.kscripting.kscript.resolver.InputOutputResolver |
| 12 | +import io.github.kscripting.kscript.resolver.ScriptResolver |
| 13 | +import io.github.kscripting.kscript.resolver.SectionResolver |
| 14 | +import io.github.kscripting.kscript.util.Logger.errorMsg |
| 15 | +import io.github.kscripting.kscript.util.Logger.infoMsg |
| 16 | +import java.io.File // Keep this for scriptFile parameter if needed by Kscript.kt, but internally use scriptFilePath |
| 17 | +import java.nio.file.Files |
| 18 | +import java.nio.file.Path |
| 19 | +import java.nio.file.Paths // Added for Paths.get() |
| 20 | +import java.util.Locale // Added for titlecase |
| 21 | + |
| 22 | +// Changed signature: scriptFile: File -> scriptFilePath: String, added config: Config |
| 23 | +fun exportToGradleProject(scriptFilePathString: String, outputDir: Path, cliOptions: Map<String, String?>, config: Config) { |
| 24 | + // Remove duplicate package declaration that might have been introduced by previous edits |
| 25 | + // package io.github.kscripting.kscript.generator // This line is effectively a comment if it was duplicated |
| 26 | + |
| 27 | + val scriptFile = File(scriptFilePathString) // Create File object for name and extension access |
| 28 | + infoMsg("Attempting to export script '$scriptFilePathString' to new Gradle project at '$outputDir'...") |
| 29 | + infoMsg("Output directory: ${outputDir.toAbsolutePath()}") |
| 30 | + |
| 31 | + try { |
| 32 | + // Instantiate Script object |
| 33 | + val cache = Cache(config.osConfig.cacheDir) |
| 34 | + val inputOutputResolver = InputOutputResolver(config.osConfig, cache) |
| 35 | + val sectionResolver = SectionResolver(inputOutputResolver, Parser(), config.scriptingConfig) |
| 36 | + val scriptResolver = ScriptResolver(inputOutputResolver, sectionResolver, config.scriptingConfig) |
| 37 | + |
| 38 | + // Resolve the script (this reads content, parses annotations, etc.) |
| 39 | + // For project generation, we typically don't want to include external preambles like text mode. |
| 40 | + val script = scriptResolver.resolve(scriptFilePathString, preambles = emptyList()) |
| 41 | + infoMsg("Successfully parsed script: ${script.scriptLocation.scriptName}") |
| 42 | + |
| 43 | + // Ensure output directory exists |
| 44 | + if (Files.exists(outputDir)) { |
| 45 | + infoMsg("Output directory '$outputDir' already exists. Files may be overwritten.") |
| 46 | + } else { |
| 47 | + Files.createDirectories(outputDir) |
| 48 | + infoMsg("Successfully created directory: $outputDir") |
| 49 | + } |
| 50 | + |
| 51 | + // 1. Determine Project Properties |
| 52 | + val projectName = outputDir.fileName.toString() |
| 53 | + // Try to use script package name for group, fallback to default |
| 54 | + val projectGroup = if (script.packageName.value.isNotBlank()) script.packageName.value else "com.example" |
| 55 | + val projectVersion = "1.0-SNAPSHOT" // Default version |
| 56 | + |
| 57 | + infoMsg("Project Name: $projectName") |
| 58 | + infoMsg("Project Group: $projectGroup") |
| 59 | + infoMsg("Project Version: $projectVersion") |
| 60 | + |
| 61 | + // 2. Create standard project directories |
| 62 | + val srcMainKotlinPath = projectGroup.replace(".", "/") |
| 63 | + val srcMainKotlinDir = outputDir.resolve("src/main/kotlin").resolve(srcMainKotlinPath) |
| 64 | + val srcMainResourcesDir = outputDir.resolve("src/main/resources") |
| 65 | + val srcTestKotlinDir = outputDir.resolve("src/test/kotlin").resolve(srcMainKotlinPath) |
| 66 | + val srcTestResourcesDir = outputDir.resolve("src/test/resources") |
| 67 | + |
| 68 | + Files.createDirectories(srcMainKotlinDir) |
| 69 | + infoMsg("Successfully created directory: $srcMainKotlinDir") |
| 70 | + Files.createDirectories(srcMainResourcesDir) |
| 71 | + infoMsg("Successfully created directory: $srcMainResourcesDir") |
| 72 | + Files.createDirectories(srcTestKotlinDir) |
| 73 | + infoMsg("Successfully created directory: $srcTestKotlinDir") |
| 74 | + Files.createDirectories(srcTestResourcesDir) |
| 75 | + infoMsg("Successfully created directory: $srcTestResourcesDir") |
| 76 | + |
| 77 | + // 3. Generate settings.gradle.kts |
| 78 | + val settingsFile = outputDir.resolve("settings.gradle.kts") |
| 79 | + val settingsContent = """ |
| 80 | + |rootProject.name = "$projectName" |
| 81 | + |""".trimMargin() |
| 82 | + Files.writeString(settingsFile, settingsContent) |
| 83 | + infoMsg("Successfully generated: $settingsFile") |
| 84 | + |
| 85 | + // 4. Construct build.gradle.kts Content |
| 86 | + val dependenciesString = script.dependencies.joinToString("\n ") { "implementation(\"${it.value}\")" } |
| 87 | + val customReposString = script.repositories.joinToString("\n ") { "maven { url = uri(\"${it.value}\") }" } |
| 88 | + |
| 89 | + // Determine main class name |
| 90 | + val scriptBaseName = scriptFile.nameWithoutExtension.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() } |
| 91 | + val mainClassName = script.entryPoint?.value ?: "${projectGroup}.${scriptBaseName}Kt" |
| 92 | + |
| 93 | + // Kotlin options and compiler options (basic for now) |
| 94 | + // More complex mapping can be done later. For now, just ensure jvmTarget. |
| 95 | + // val joinedCompilerOpts = script.compilerOpts.joinToString(", ") { "\"${it.value}\"" } // Example if needed |
| 96 | + |
| 97 | + val buildGradleKtsContent = """ |
| 98 | + |plugins { |
| 99 | + | kotlin("jvm") version "${config.scriptingConfig.kotlinVersion}" // Use kscript's current Kotlin version |
| 100 | + | application |
| 101 | + |} |
| 102 | + | |
| 103 | + |group = "$projectGroup" |
| 104 | + |version = "$projectVersion" |
| 105 | + | |
| 106 | + |repositories { |
| 107 | + | mavenCentral() |
| 108 | + | $customReposString |
| 109 | + |} |
| 110 | + | |
| 111 | + |dependencies { |
| 112 | + | implementation(kotlin("stdlib")) // Or kotlin("stdlib-jdk8") depending on preference |
| 113 | + | $dependenciesString |
| 114 | + |} |
| 115 | + | |
| 116 | + |application { |
| 117 | + | mainClass.set("$mainClassName") |
| 118 | + | // applicationDefaultJvmArgs = listOf(${script.kotlinOpts.joinToString(", ") { "\"${it.value}\"" }}) // Example |
| 119 | + |} |
| 120 | + | |
| 121 | + |tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { |
| 122 | + | compilerOptions { |
| 123 | + | jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_21) // Match kscript's build |
| 124 | + | // if (listOf($joinedCompilerOpts).isNotEmpty()) { |
| 125 | + | // freeCompilerArgs.addAll(listOf($joinedCompilerOpts)) |
| 126 | + | // } |
| 127 | + | } |
| 128 | + |} |
| 129 | + | |
| 130 | + |java { |
| 131 | + | toolchain { |
| 132 | + | languageVersion.set(JavaLanguageVersion.of(21)) // Match kscript's build |
| 133 | + | } |
| 134 | + |} |
| 135 | + |""".trimMargin() |
| 136 | + |
| 137 | + // 5. Write build.gradle.kts File |
| 138 | + val buildGradleKtsFile = outputDir.resolve("build.gradle.kts") |
| 139 | + Files.writeString(buildGradleKtsFile, buildGradleKtsContent) |
| 140 | + infoMsg("Successfully generated: $buildGradleKtsFile") |
| 141 | + |
| 142 | + // TODO (remaining from original list): |
| 143 | + // 7. Generate .gitignore |
| 144 | + // 8. Add Gradle Wrapper (./gradlew files) |
| 145 | + |
| 146 | + // 6. Place and Transform Script Content |
| 147 | + val targetKtFile = srcMainKotlinDir.resolve("${scriptBaseName}.kt") |
| 148 | + val originalLines = script.resolvedCode.lines() |
| 149 | + val contentLines = mutableListOf<String>() |
| 150 | + |
| 151 | + // Add package declaration |
| 152 | + if (projectGroup.isNotBlank()) { |
| 153 | + contentLines.add("package $projectGroup") |
| 154 | + contentLines.add("") // Add a blank line after package |
| 155 | + } |
| 156 | + |
| 157 | + var firstLineSkipped = false |
| 158 | + for (line in originalLines) { |
| 159 | + if (!firstLineSkipped && line.trim().startsWith("#!/")) { |
| 160 | + firstLineSkipped = true |
| 161 | + continue // Skip shebang |
| 162 | + } |
| 163 | + |
| 164 | + val trimmedLine = line.trim() |
| 165 | + if (trimmedLine.startsWith("@file:DependsOn") || |
| 166 | + trimmedLine.startsWith("@file:Repository") || // Matches Script.kt model |
| 167 | + trimmedLine.startsWith("@file:KotlinOptions") || |
| 168 | + trimmedLine.startsWith("@file:CompilerOptions") || |
| 169 | + trimmedLine.startsWith("@file:EntryPoint")) { |
| 170 | + // Skip kscript-specific file-level annotations |
| 171 | + continue |
| 172 | + } |
| 173 | + contentLines.add(line) |
| 174 | + } |
| 175 | + |
| 176 | + Files.writeString(targetKtFile, contentLines.joinToString("\n")) |
| 177 | + infoMsg("Successfully transformed and placed script content at: $targetKtFile") |
| 178 | + |
| 179 | + // 7. Generate .gitignore |
| 180 | + val gitignoreFile = outputDir.resolve(".gitignore") |
| 181 | + // Simplified .gitignore content to avoid issues with complex multiline strings in the tool |
| 182 | + // In a real scenario, this would be more comprehensive. |
| 183 | + val gitignoreContent = """ |
| 184 | + |/build/ |
| 185 | + |/.gradle/ |
| 186 | + |*.iml |
| 187 | + |.idea/ |
| 188 | + |*~ |
| 189 | + |out/ |
| 190 | + """.trimMargin() |
| 191 | + Files.writeString(gitignoreFile, gitignoreContent) |
| 192 | + infoMsg("Successfully generated: $gitignoreFile") |
| 193 | + |
| 194 | + // 8. Add Gradle Wrapper |
| 195 | + val kscriptProjectRoot = Paths.get(".").toAbsolutePath().normalize() |
| 196 | + val gradlewFileInKscript = kscriptProjectRoot.resolve("gradlew") |
| 197 | + val gradlewBatFileInKscript = kscriptProjectRoot.resolve("gradlew.bat") |
| 198 | + val gradleWrapperDirInKscript = kscriptProjectRoot.resolve("gradle/wrapper") |
| 199 | + |
| 200 | + val gradlewFileInProject = outputDir.resolve("gradlew") |
| 201 | + val gradlewBatFileInProject = outputDir.resolve("gradlew.bat") |
| 202 | + val gradleWrapperDirInProject = outputDir.resolve("gradle/wrapper") |
| 203 | + |
| 204 | + if (Files.exists(gradlewFileInKscript)) { |
| 205 | + Files.copy(gradlewFileInKscript, gradlewFileInProject) |
| 206 | + gradlewFileInProject.toFile().setExecutable(true, false) // ownerOnly = false |
| 207 | + infoMsg("Copied gradlew and set executable.") |
| 208 | + } else { |
| 209 | + warnMsg("gradlew script not found in kscript project root, skipping copy.") |
| 210 | + } |
| 211 | + |
| 212 | + if (Files.exists(gradlewBatFileInKscript)) { |
| 213 | + Files.copy(gradlewBatFileInKscript, gradlewBatFileInProject) |
| 214 | + infoMsg("Copied gradlew.bat.") |
| 215 | + } else { |
| 216 | + warnMsg("gradlew.bat script not found in kscript project root, skipping copy.") |
| 217 | + } |
| 218 | + |
| 219 | + Files.createDirectories(gradleWrapperDirInProject) |
| 220 | + val wrapperJarInKscript = gradleWrapperDirInKscript.resolve("gradle-wrapper.jar") |
| 221 | + val wrapperPropsInKscript = gradleWrapperDirInKscript.resolve("gradle-wrapper.properties") |
| 222 | + |
| 223 | + if (Files.exists(wrapperJarInKscript)) { |
| 224 | + Files.copy(wrapperJarInKscript, gradleWrapperDirInProject.resolve("gradle-wrapper.jar")) |
| 225 | + infoMsg("Copied gradle-wrapper.jar.") |
| 226 | + } else { |
| 227 | + warnMsg("gradle-wrapper.jar not found in kscript project, skipping copy. Project will need 'gradle wrapper' task run manually.") |
| 228 | + } |
| 229 | + |
| 230 | + if (Files.exists(wrapperPropsInKscript)) { |
| 231 | + Files.copy(wrapperPropsInKscript, gradleWrapperDirInProject.resolve("gradle-wrapper.properties")) |
| 232 | + infoMsg("Copied gradle-wrapper.properties.") |
| 233 | + } else { |
| 234 | + warnMsg("gradle-wrapper.properties not found in kscript project, skipping copy. Project will need 'gradle wrapper' task run manually.") |
| 235 | + } |
| 236 | + |
| 237 | + infoMsg("Project generation for '$projectName' is complete.") |
| 238 | + |
| 239 | + } catch (e: Exception) { |
| 240 | + errorMsg("Error during project generation for $scriptFilePathString: ${e.message}") |
| 241 | + e.printStackTrace() // For more detailed debugging during development |
| 242 | + // Consider rethrowing or using a specific return type to indicate failure to the caller |
| 243 | + } |
| 244 | +} |
0 commit comments