Skip to content

Commit

Permalink
Closes #31 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetuska authored Jan 23, 2022
1 parent ee44577 commit e9336c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/NpmPublishPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import dev.petuska.npm.publish.task.NpmPackTask
import dev.petuska.npm.publish.task.NpmPackageAssembleTask
import dev.petuska.npm.publish.task.NpmPublishTask
import dev.petuska.npm.publish.util.Builder
import dev.petuska.npm.publish.util.toCamelCase
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.Copy
import org.gradle.util.internal.GUtil
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBinaryMode
Expand Down Expand Up @@ -149,7 +149,7 @@ class NpmPublishPlugin : Plugin<Project> {
}
processResourcesTask
}
val upperName = GUtil.toCamelCase(pub.name)
val upperName = pub.name.toCamelCase()

val assembleTaskName = "assemble${upperName}NpmPublication"
val packTaskName = "pack${upperName}NpmPublication"
Expand All @@ -175,7 +175,7 @@ class NpmPublishPlugin : Plugin<Project> {
packTask.dependsOn(npmPackTask)
packTask.enabled = true
repositories.map { repo ->
val upperRepoName = GUtil.toCamelCase(repo.name)
val upperRepoName = repo.name.toCamelCase()
val publishTaskName = "publish${upperName}NpmPublicationTo$upperRepoName"
tasks.findByName(publishTaskName)
?: tasks.create(publishTaskName, NpmPublishTask::class.java, pub, repo).also { task ->
Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/util/stringUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.petuska.npm.publish.util

import java.util.Locale
import java.util.regex.Pattern

private val WORD_SEPARATOR = Pattern.compile("\\W+")

fun String.toCamelCase(lower: Boolean = false): String {
val builder = StringBuilder()
val matcher = WORD_SEPARATOR.matcher(this)
var pos = 0
var first = true
while (matcher.find()) {
var chunk: String = subSequence(pos, matcher.start()).toString()
pos = matcher.end()
if (chunk.isEmpty()) {
continue
}
if (lower && first) {
chunk = chunk.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
first = false
} else {
chunk = chunk.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}
builder.append(chunk)
}
var rest: String = subSequence(pos, length).toString()
rest = if (lower && first) {
rest.replaceFirstChar { it.lowercase(Locale.getDefault()) }
} else {
rest.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}
builder.append(rest)
return builder.toString()
}

0 comments on commit e9336c8

Please sign in to comment.