Skip to content

Commit

Permalink
fix(abigen-plugin): normalize Windows file paths (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtificialPB authored Feb 25, 2025
1 parent 5bdb112 commit e8d53b9
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,32 @@ open class DirectorySourceProvider(project: Project, path: String) : AbiSourcePr
val ret = ArrayList<AbiSource>()

val dir = directory.asFile
dir.walkTopDown().filter(File::isFile).forEach {
val contractName = it.nameWithoutExtension

var destinationPackage = packageOverride
if (destinationPackage == null) {
destinationPackage = it.absolutePath
.substringAfter(dir.absolutePath)
.substringBeforeLast("/")
.removePrefix("/")
.replace("/", ".")
}

ret.add(AbiSource(contractName, destinationPackage, it.toURI().toURL()))
dir.walkTopDown().filter(File::isFile).forEach { file ->
val contractName = file.nameWithoutExtension

val destinationPackage = (packageOverride ?: file.normalizedPath())
.substringAfter(dir.normalizedPath())
.substringBeforeLast("/")
.removePrefix("/")
.replace("/", ".")

ret.add(AbiSource(contractName, destinationPackage, file.toURI().toURL()))
}

return ret
}

/**
* Return normalized absolute path of the file, using `/` as a separator. This is mainly useful
* for normalizing paths on Windows.
*
* Example:
* ```
* from: "C:\\ethers-kt\\ethers-abigen-plugin\\src\\main\\kotlin"
* to: "/ethers-kt/ethers-abigen-plugin/src/main/kotlin"
* ```
* */
private fun File.normalizedPath(): String {
return absolutePath.replace('\\', '/').split(":/").last()
}
}

0 comments on commit e8d53b9

Please sign in to comment.