@@ -7,19 +7,23 @@ import com.github.ajalt.clikt.core.CliktCommand
7
7
import com.github.ajalt.clikt.core.Context
8
8
import com.github.ajalt.clikt.core.main
9
9
import com.github.ajalt.clikt.parameters.arguments.argument
10
+ import com.github.ajalt.clikt.parameters.options.default
10
11
import com.github.ajalt.clikt.parameters.options.flag
11
12
import com.github.ajalt.clikt.parameters.options.option
13
+ import com.github.ajalt.clikt.parameters.options.split
12
14
import com.github.ajalt.clikt.parameters.types.path
15
+ import java.io.InputStream
13
16
14
- class Migration (private val replacements : List <Pair <String , String >>) : CliktCommand(" migration_utils" ) {
17
+ class Migration (private val replacements : List <Pair <Regex , String >>) : CliktCommand(" migration_utils" ) {
15
18
val dryRun by option(help = " Perform a dry run (without changing any files)" ).flag()
16
19
val noBackups by option(help = " Do not save backups of changed files as '<filename>.bck'" ).flag()
17
- val directory by argument(name = " project directory" ).path(mustExist = true , canBeDir = true , canBeFile = false )
18
-
19
- val supportedExtensions = setOf (" java" , " kt" )
20
+ val directory by argument(name = " project directory" ).path(mustExist = true , canBeDir = true , canBeFile = true )
21
+ val extensions by option(help = " Provide a list of file extensions to consider. Default: java, kt" ).split(" ," ).default(
22
+ listOf (" kt" , " java" )
23
+ )
20
24
21
25
override fun help (context : Context ): String {
22
- return " This command will scan all files ending with ${supportedExtensions.map { " '. $it ' " }.joinToString( " , " )} " +
26
+ return " This command will scan all files ending with extensions specified in `--extensions=kt,java` " +
23
27
" in <project directory>, and replace package names of classes that have been moved to " +
24
28
" com.pubnub.api.java.* in PubNub Java SDK v10.\n\n " +
25
29
" Perform a --dry-run to print a list of files that will be changed.\n\n " +
@@ -30,7 +34,7 @@ class Migration(private val replacements: List<Pair<String, String>>) : CliktCom
30
34
31
35
override fun run () {
32
36
directory.toFile().walk().forEach { file ->
33
- if (file.extension.lowercase() !in supportedExtensions ) {
37
+ if (file.extension.lowercase() !in this .extensions ) {
34
38
return @forEach
35
39
}
36
40
val newFileContent = file.useLines { lines ->
@@ -51,7 +55,7 @@ class Migration(private val replacements: List<Pair<String, String>>) : CliktCom
51
55
companion object {
52
56
fun replaceLines (
53
57
lines : Sequence <String >,
54
- replacements : List <Pair <String , String >>,
58
+ replacements : List <Pair <Regex , String >>,
55
59
): String? {
56
60
var fileChanged = false
57
61
val newFileContent = lines.fold(StringBuilder ()) { acc: StringBuilder , line: String ->
@@ -70,24 +74,24 @@ class Migration(private val replacements: List<Pair<String, String>>) : CliktCom
70
74
71
75
fun replaceLine (
72
76
line : String ,
73
- replacements : List <Pair <String , String >>,
77
+ replacements : List <Pair <Regex , String >>,
74
78
): String {
75
79
return replacements.fold(line) { lineUnderReplacement, replacement ->
76
80
lineUnderReplacement
77
81
.replace(replacement.first, replacement.second)
78
82
}
79
83
}
80
84
81
- fun loadReplacementsFile (): List <Pair <String , String >> =
82
- this :: class .java.getResourceAsStream( " /replacements.txt " ) !!
85
+ fun loadReplacementsFile (inputStream : InputStream ): List <Pair <Regex , String >> =
86
+ inputStream
83
87
.bufferedReader()
84
88
.readLines()
85
89
.map {
86
90
val split = it.split(' :' )
87
91
require(split.size == 2 )
88
- split[0 ] to split[1 ]
92
+ Regex ( " (?u) " + split[0 ].replace( " . " , " \\ . " ) + " (?! \\ p{Alnum}) " ) to split[1 ]
89
93
}
90
94
}
91
95
}
92
96
93
- fun main (args : Array <String >) = Migration (Migration .loadReplacementsFile()).main(args)
97
+ fun main (args : Array <String >) = Migration (Migration .loadReplacementsFile(Migration :: class .java.getResourceAsStream( " /replacements.txt " ) !! )).main(args)
0 commit comments