aboutsummaryrefslogtreecommitdiff
path: root/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt
blob: 9541cdcbba9a0e010edc9d262780c2a8ca9a1e57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package com.github.shyiko.ktlint

import com.github.shyiko.klob.Glob
import com.github.shyiko.ktlint.core.KtLint
import com.github.shyiko.ktlint.core.LintError
import com.github.shyiko.ktlint.core.ParseException
import com.github.shyiko.ktlint.core.Reporter
import com.github.shyiko.ktlint.core.ReporterProvider
import com.github.shyiko.ktlint.core.RuleExecutionException
import com.github.shyiko.ktlint.core.RuleSet
import com.github.shyiko.ktlint.core.RuleSetProvider
import com.github.shyiko.ktlint.internal.EditorConfig
import com.github.shyiko.ktlint.internal.IntellijIDEAIntegration
import com.github.shyiko.ktlint.internal.MavenDependencyResolver
import com.github.shyiko.ktlint.test.DumpAST
import org.eclipse.aether.RepositoryException
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.repository.RepositoryPolicy
import org.eclipse.aether.repository.RepositoryPolicy.CHECKSUM_POLICY_IGNORE
import org.eclipse.aether.repository.RepositoryPolicy.UPDATE_POLICY_NEVER
import org.jetbrains.kotlin.preprocessor.mkdirsOrFail
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileNotFoundException
import java.io.PrintStream
import java.math.BigInteger
import java.net.URLDecoder
import java.nio.file.Path
import java.nio.file.Paths
import java.security.MessageDigest
import java.util.ArrayList
import java.util.Arrays
import java.util.LinkedHashMap
import java.util.NoSuchElementException
import java.util.Scanner
import java.util.ServiceLoader
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.jar.Manifest
import kotlin.system.exitProcess

@Command(
    headerHeading = """An anti-bikeshedding Kotlin linter with built-in formatter
(https://github.com/shyiko/ktlint).

Usage:
  ktlint <flags> [patterns]
  java -jar ktlint <flags> [patterns]

Examples:
  # check the style of all Kotlin files inside the current dir (recursively)
  # (hidden folders will be skipped)
  ktlint

  # check only certain locations (prepend ! to negate the pattern)
  ktlint "src/**/*.kt" "!src/**/*Test.kt"

  # auto-correct style violations
  ktlint -F "src/**/*.kt"

  # custom reporter
  ktlint --reporter=plain?group_by_file
  # multiple reporters can be specified like this
  ktlint --reporter=plain \
    --reporter=checkstyle,output=ktlint-checkstyle-report.xml
  # 3rd-party reporter
  ktlint --reporter=html,artifact=com.gihub.user:repo:master-SNAPSHOT

Flags:""",
    synopsisHeading = "",
    customSynopsis = arrayOf(""),
    sortOptions = false
)
object Main {

    private val DEPRECATED_FLAGS = mapOf(
        "--ruleset-repository" to
            "--repository",
        "--reporter-repository" to
            "--repository",
        "--ruleset-update" to
            "--repository-update",
        "--reporter-update" to
            "--repository-update"
    )
    @Option(names = arrayOf("--android", "-a"), description = arrayOf("Turn on Android Kotlin Style Guide compatibility"))
    private var android: Boolean = false

    // todo: make it a command in 1.0.0 (it's too late now as we might interfere with valid "lint" patterns)
    @Option(names = arrayOf("--apply-to-idea"), description = arrayOf("Update Intellij IDEA settings (global)"))
    private var apply: Boolean = false

    // todo: make it a command in 1.0.0 (it's too late now as we might interfere with valid "lint" patterns)
    @Option(names = arrayOf("--apply-to-idea-project"), description = arrayOf("Update Intellij IDEA project settings"))
    private var applyToProject: Boolean = false

    @Option(names = arrayOf("--color"), description = arrayOf("Make output colorful"))
    private var color: Boolean = false

    @Option(names = arrayOf("--debug"), description = arrayOf("Turn on debug output"))
    private var debug: Boolean = false

    // todo: this should have been a command, not a flag (consider changing in 1.0.0)
    @Option(names = arrayOf("--format", "-F"), description = arrayOf("Fix any deviations from the code style"))
    private var format: Boolean = false

    @Option(names = arrayOf("--install-git-pre-commit-hook"), description = arrayOf(
        "Install git hook to automatically check files for style violations on commit"
    ))
    private var installGitPreCommitHook: Boolean = false

    @Option(names = arrayOf("--limit"), description = arrayOf(
        "Maximum number of errors to show (default: show all)"
    ))
    private var limit: Int = -1
        get() = if (field < 0) Int.MAX_VALUE else field

    @Option(names = arrayOf("--print-ast"), description = arrayOf(
        "Print AST (useful when writing/debugging rules)"
    ))
    private var printAST: Boolean = false

    @Option(names = arrayOf("--relative"), description = arrayOf(
        "Print files relative to the working directory " +
            "(e.g. dir/file.kt instead of /home/user/project/dir/file.kt)"
    ))
    private var relative: Boolean = false

    @Option(names = arrayOf("--reporter"), description = arrayOf(
        "A reporter to use (built-in: plain (default), plain?group_by_file, json, checkstyle). " +
        "To use a third-party reporter specify either a path to a JAR file on the filesystem or a" +
        "<groupId>:<artifactId>:<version> triple pointing to a remote artifact (in which case ktlint will first " +
        "check local cache (~/.m2/repository) and then, if not found, attempt downloading it from " +
        "Maven Central/JCenter/JitPack/user-provided repository)\n" +
        "e.g. \"html,artifact=com.github.username:ktlint-reporter-html:master-SNAPSHOT\""
    ))
    private var reporters = ArrayList<String>()

    @Option(names = arrayOf("--repository"), description = arrayOf(
        "An additional Maven repository (Maven Central/JCenter/JitPack are active by default) " +
            "(value format: <id>=<url>)"
    ))
    private var repositories = ArrayList<String>()
    @Option(names = arrayOf("--ruleset-repository", "--reporter-repository"), hidden = true)
    private var repositoriesDeprecated = ArrayList<String>()

    @Option(names = arrayOf("--repository-update", "-U"), description = arrayOf(
        "Check remote repositories for updated snapshots"
    ))
    private var forceUpdate: Boolean? = null
    @Option(names = arrayOf("--ruleset-update", "--reporter-update"), hidden = true)
    private var forceUpdateDeprecated: Boolean? = null

    @Option(names = arrayOf("--ruleset", "-R"), description = arrayOf(
        "A path to a JAR file containing additional ruleset(s) or a " +
        "<groupId>:<artifactId>:<version> triple pointing to a remote artifact (in which case ktlint will first " +
        "check local cache (~/.m2/repository) and then, if not found, attempt downloading it from " +
        "Maven Central/JCenter/JitPack/user-provided repository)"
    ))
    private var rulesets = ArrayList<String>()

    @Option(names = arrayOf("--skip-classpath-check"), description = arrayOf("Do not check classpath for pottential conflicts"))
    private var skipClasspathCheck: Boolean = false

    @Option(names = arrayOf("--stdin"), description = arrayOf("Read file from stdin"))
    private var stdin: Boolean = false

    @Option(names = arrayOf("--verbose", "-v"), description = arrayOf("Show error codes"))
    private var verbose: Boolean = false

    @Option(names = arrayOf("--version"), description = arrayOf("Print version information"))
    private var version: Boolean = false

    @Option(names = arrayOf("--help", "-h"), help = true, hidden = true)
    private var help: Boolean = false

    @Option(names = arrayOf("-y"), hidden = true)
    private var forceApply: Boolean = false

    @Parameters(hidden = true)
    private var patterns = ArrayList<String>()

    private val workDir = File(".").canonicalPath
    private fun File.location() = if (relative) this.toRelativeString(File(workDir)) else this.path

    private fun usage() =
        ByteArrayOutputStream()
            .also { CommandLine.usage(this, PrintStream(it), CommandLine.Help.Ansi.OFF) }
            .toString()
            .replace(" ".repeat(32), " ".repeat(30))

    private fun parseCmdLine(args: Array<String>) {
        try {
            CommandLine.populateCommand(this, *args)
            repositories.addAll(repositoriesDeprecated)
            if (forceUpdateDeprecated != null && forceUpdate == null) {
                forceUpdate = forceUpdateDeprecated
            }
        } catch (e: Exception) {
            System.err.println("Error: ${e.message}\n\n${usage()}")
            exitProcess(1)
        }
        if (help) {
            println(usage())
            exitProcess(0)
        }
        args.forEach { arg ->
            if (arg.startsWith("--") && arg.contains("=")) {
                val flag = arg.substringBefore("=")
                val alt = DEPRECATED_FLAGS[flag]
                if (alt != null) {
                    System.err.println("$flag flag is deprecated and will be removed in 1.0.0 (use $alt instead)")
                }
            }
        }
    }

    @JvmStatic
    fun main(args: Array<String>) {
        parseCmdLine(args)
        if (version) {
            println(getImplementationVersion())
            exitProcess(0)
        }
        if (installGitPreCommitHook) {
            installGitPreCommitHook()
            if (!apply) {
                exitProcess(0)
            }
        }
        if (apply || applyToProject) {
            applyToIDEA()
            exitProcess(0)
        }
        if (printAST) {
            printAST()
            exitProcess(0)
        }
        val start = System.currentTimeMillis()
        // load 3rd party ruleset(s) (if any)
        val dependencyResolver = lazy(LazyThreadSafetyMode.NONE) { buildDependencyResolver() }
        if (!rulesets.isEmpty()) {
            loadJARs(dependencyResolver, rulesets)
        }
        // standard should go first
        val ruleSetProviders = ServiceLoader.load(RuleSetProvider::class.java)
            .map { it.get().id to it }
            .sortedBy { if (it.first == "standard") "\u0000${it.first}" else it.first }
        if (debug) {
            ruleSetProviders.forEach { System.err.println("[DEBUG] Discovered ruleset \"${it.first}\"") }
        }
        val reporter = loadReporter(dependencyResolver)
        // load .editorconfig
        val userData = (
            EditorConfig.of(workDir)
                ?.also { editorConfig ->
                    if (debug) {
                        System.err.println("[DEBUG] Discovered .editorconfig (${
                            generateSequence(editorConfig) { it.parent }.map { it.path.parent.toFile().location() }.joinToString()
                        })")
                        System.err.println("[DEBUG] ${editorConfig.mapKeys { it.key }} loaded from .editorconfig")
                    }
                }
                ?: emptyMap<String, String>()
            ) + mapOf("android" to android.toString())
        val tripped = AtomicBoolean()
        data class LintErrorWithCorrectionInfo(val err: LintError, val corrected: Boolean)
        fun process(fileName: String, fileContent: String): List<LintErrorWithCorrectionInfo> {
            if (debug) {
                System.err.println("[DEBUG] Checking ${if (fileName != "<text>") File(fileName).location() else fileName}")
            }
            val result = ArrayList<LintErrorWithCorrectionInfo>()
            val localUserData = if (fileName != "<text>") userData + ("file_path" to fileName) else userData
            if (format) {
                val formattedFileContent = try {
                    format(fileName, fileContent, ruleSetProviders.map { it.second.get() }, localUserData) { err, corrected ->
                        if (!corrected) {
                            result.add(LintErrorWithCorrectionInfo(err, corrected))
                            tripped.set(true)
                        }
                    }
                } catch (e: Exception) {
                    result.add(LintErrorWithCorrectionInfo(e.toLintError(), false))
                    tripped.set(true)
                    fileContent // making sure `cat file | ktlint --stdint > file` is (relatively) safe
                }
                if (stdin) {
                    println(formattedFileContent)
                } else {
                    if (fileContent !== formattedFileContent) {
                        File(fileName).writeText(formattedFileContent, charset("UTF-8"))
                    }
                }
            } else {
                try {
                    lint(fileName, fileContent, ruleSetProviders.map { it.second.get() }, localUserData) { err ->
                        result.add(LintErrorWithCorrectionInfo(err, false))
                        tripped.set(true)
                    }
                } catch (e: Exception) {
                    result.add(LintErrorWithCorrectionInfo(e.toLintError(), false))
                    tripped.set(true)
                }
            }
            return result
        }
        val (fileNumber, errorNumber) = Pair(AtomicInteger(), AtomicInteger())
        fun report(fileName: String, errList: List<LintErrorWithCorrectionInfo>) {
            fileNumber.incrementAndGet()
            val errListLimit = minOf(errList.size, maxOf(limit - errorNumber.get(), 0))
            errorNumber.addAndGet(errListLimit)
            reporter.before(fileName)
            errList.head(errListLimit).forEach { (err, corrected) ->
                reporter.onLintError(fileName, err, corrected)
            }
            reporter.after(fileName)
        }
        reporter.beforeAll()
        if (stdin) {
            report("<text>", process("<text>", String(System.`in`.readBytes())))
        } else {
            fileSequence()
                .takeWhile { errorNumber.get() < limit }
                .map { file -> Callable { file to process(file.path, file.readText()) } }
                .parallel({ (file, errList) -> report(file.location(), errList) })
        }
        reporter.afterAll()
        if (debug) {
            System.err.println("[DEBUG] ${
                System.currentTimeMillis() - start
            }ms / $fileNumber file(s) / $errorNumber error(s)")
        }
        if (tripped.get()) {
            exitProcess(1)
        }
    }

    private fun getImplementationVersion() = javaClass.`package`.implementationVersion
        // JDK 9 regression workaround (https://bugs.openjdk.java.net/browse/JDK-8190987, fixed in JDK 10)
        // (note that version reported by the fallback might not be null if META-INF/MANIFEST.MF is
        // loaded from another JAR on the classpath (e.g. if META-INF/MANIFEST.MF wasn't created as part of the build))
        ?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
            ?.let { stream ->
                Manifest(stream).mainAttributes.getValue("Implementation-Version")
            }

    private fun loadReporter(dependencyResolver: Lazy<MavenDependencyResolver>): Reporter {
        data class ReporterTemplate(val id: String, val artifact: String?, val config: Map<String, String>, var output: String?)
        val tpls = (if (reporters.isEmpty()) listOf("plain") else reporters)
            .map { reporter ->
                val split = reporter.split(",")
                val (reporterId, rawReporterConfig) = split[0].split("?", limit = 2) + listOf("")
                ReporterTemplate(
                    reporterId,
                    split.lastOrNull { it.startsWith("artifact=") }?.let { it.split("=")[1] },
                    mapOf("verbose" to verbose.toString(), "color" to color.toString()) + parseQuery(rawReporterConfig),
                    split.lastOrNull { it.startsWith("output=") }?.let { it.split("=")[1] }
                )
            }
            .distinct()
        val reporterLoader = ServiceLoader.load(ReporterProvider::class.java)
        val reporterProviderById = reporterLoader.associate { it.id to it }.let { map ->
            val missingReporters = tpls.filter { !map.containsKey(it.id) }.mapNotNull { it.artifact }.distinct()
            if (!missingReporters.isEmpty()) {
                loadJARs(dependencyResolver, missingReporters)
                reporterLoader.reload()
                reporterLoader.associate { it.id to it }
            } else map
        }
        if (debug) {
            reporterProviderById.forEach { (id) -> System.err.println("[DEBUG] Discovered reporter \"$id\"") }
        }
        fun ReporterTemplate.toReporter(): Reporter {
            val reporterProvider = reporterProviderById[id]
            if (reporterProvider == null) {
                System.err.println("Error: reporter \"$id\" wasn't found (available: ${
                    reporterProviderById.keys.sorted().joinToString(",")
                })")
                exitProcess(1)
            }
            if (debug) {
                System.err.println("[DEBUG] Initializing \"$id\" reporter with $config" +
                    (output?.let { ", output=$it" } ?: ""))
            }
            val stream = if (output != null) {
                File(output).parentFile?.mkdirsOrFail(); PrintStream(output, "UTF-8")
            } else if (stdin) System.err else System.out
            return reporterProvider.get(stream, config)
                .let { reporter ->
                    if (output != null)
                        object : Reporter by reporter {
                            override fun afterAll() {
                                reporter.afterAll()
                                stream.close()
                            }
                        }
                    else reporter
                }
        }
        return Reporter.from(*tpls.map { it.toReporter() }.toTypedArray())
    }

    private fun Exception.toLintError(): LintError = this.let { e ->
        when (e) {
            is ParseException ->
                LintError(e.line, e.col, "",
                    "Not a valid Kotlin file (${e.message?.toLowerCase()})")
            is RuleExecutionException -> {
                if (debug) {
                    System.err.println("[DEBUG] Internal Error (${e.ruleId})")
                    e.printStackTrace(System.err)
                }
                LintError(e.line, e.col, "", "Internal Error (${e.ruleId}). " +
                    "Please create a ticket at https://github.com/shyiko/ktlint/issue " +
                    "(if possible, provide the source code that triggered an error)")
            }
            else -> throw e
        }
    }

    private fun printAST() {
        fun process(fileName: String, fileContent: String) {
            if (debug) {
                System.err.println("[DEBUG] Analyzing ${if (fileName != "<text>") File(fileName).location() else fileName}")
            }
            try {
                lint(fileName, fileContent, listOf(RuleSet("debug", DumpAST(System.out, color))), emptyMap()) {}
            } catch (e: Exception) {
                if (e is ParseException) {
                    throw ParseException(e.line, e.col, "Not a valid Kotlin file (${e.message?.toLowerCase()})")
                }
                throw e
            }
        }
        if (stdin) {
            process("<text>", String(System.`in`.readBytes()))
        } else {
            for (file in fileSequence()) {
                process(file.path, file.readText())
            }
        }
    }

    private fun fileSequence() =
        when {
            patterns.isEmpty() ->
                Glob.from("**/*.kt", "**/*.kts")
                    .iterate(Paths.get(workDir), Glob.IterationOption.SKIP_HIDDEN)
            else ->
                Glob.from(*patterns.map { expandTilde(it) }.toTypedArray())
                    .iterate(Paths.get(workDir))
        }
            .asSequence()
            .map(Path::toFile)

    private fun installGitPreCommitHook() {
        if (!File(".git").isDirectory) {
            System.err.println(".git directory not found. " +
                "Are you sure you are inside project root directory?")
            exitProcess(1)
        }
        val hooksDir = File(".git", "hooks")
        hooksDir.mkdirsOrFail()
        val preCommitHookFile = File(hooksDir, "pre-commit")
        val expectedPreCommitHook = ClassLoader.getSystemClassLoader()
            .getResourceAsStream("ktlint-git-pre-commit-hook${if (android) "-android" else ""}.sh").readBytes()
        // backup existing hook (if any)
        val actualPreCommitHook = try { preCommitHookFile.readBytes() } catch (e: FileNotFoundException) { null }
        if (actualPreCommitHook != null && !actualPreCommitHook.isEmpty() && !Arrays.equals(actualPreCommitHook, expectedPreCommitHook)) {
            val backupFile = File(hooksDir, "pre-commit.ktlint-backup." + hex(actualPreCommitHook))
            System.err.println(".git/hooks/pre-commit -> $backupFile")
            preCommitHookFile.copyTo(backupFile, overwrite = true)
        }
        // > .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
        preCommitHookFile.writeBytes(expectedPreCommitHook)
        preCommitHookFile.setExecutable(true)
        System.err.println(".git/hooks/pre-commit installed")
    }

    private fun applyToIDEA() {
        try {
            val workDir = Paths.get(".")
            if (!forceApply) {
                val fileList = IntellijIDEAIntegration.apply(workDir, true, android, applyToProject)
                System.err.println("The following files are going to be updated:\n\n\t" +
                    fileList.joinToString("\n\t") +
                    "\n\nDo you wish to proceed? [y/n]\n" +
                    "(in future, use -y flag if you wish to skip confirmation)")
                val scanner = Scanner(System.`in`)
                val res = generateSequence {
                        try { scanner.next() } catch (e: NoSuchElementException) { null }
                    }
                    .filter { line -> !line.trim().isEmpty() }
                    .first()
                if (!"y".equals(res, ignoreCase = true)) {
                    System.err.println("(update canceled)")
                    exitProcess(1)
                }
            }
            IntellijIDEAIntegration.apply(workDir, false, android, applyToProject)
        } catch (e: IntellijIDEAIntegration.ProjectNotFoundException) {
            System.err.println(".idea directory not found. " +
                "Are you sure you are inside project root directory?")
            exitProcess(1)
        }
        System.err.println("(updated)")
        System.err.println("\nPlease restart your IDE")
        System.err.println("(if you experience any issues please report them at https://github.com/shyiko/ktlint)")
    }

    private fun hex(input: ByteArray) = BigInteger(MessageDigest.getInstance("SHA-256").digest(input)).toString(16)

    // a complete solution would be to implement https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html
    // this implementation takes care only of the most commonly used case (~/)
    private fun expandTilde(path: String) = path.replaceFirst(Regex("^~"), System.getProperty("user.home"))

    private fun <T> List<T>.head(limit: Int) = if (limit == size) this else this.subList(0, limit)

    private fun buildDependencyResolver(): MavenDependencyResolver {
        val mavenLocal = File(File(System.getProperty("user.home"), ".m2"), "repository")
        mavenLocal.mkdirsOrFail()
        val dependencyResolver = MavenDependencyResolver(
            mavenLocal,
            listOf(
                RemoteRepository.Builder(
                    "central", "default", "http://repo1.maven.org/maven2/"
                ).setSnapshotPolicy(RepositoryPolicy(false, UPDATE_POLICY_NEVER,
                    CHECKSUM_POLICY_IGNORE)).build(),
                RemoteRepository.Builder(
                    "bintray", "default", "http://jcenter.bintray.com"
                ).setSnapshotPolicy(RepositoryPolicy(false, UPDATE_POLICY_NEVER,
                    CHECKSUM_POLICY_IGNORE)).build(),
                RemoteRepository.Builder(
                    "jitpack", "default", "http://jitpack.io").build()
            ) + repositories.map { repository ->
                val colon = repository.indexOf("=").apply {
                    if (this == -1) { throw RuntimeException("$repository is not a valid repository entry " +
                        "(make sure it's provided as <id>=<url>") }
                }
                val id = repository.substring(0, colon)
                val url = repository.substring(colon + 1)
                RemoteRepository.Builder(id, "default", url).build()
            },
            forceUpdate == true
        )
        if (debug) {
            dependencyResolver.setTransferEventListener { e ->
                System.err.println("[DEBUG] Transfer ${e.type.toString().toLowerCase()} ${e.resource.repositoryUrl}" +
                    e.resource.resourceName + (e.exception?.let { " (${it.message})" } ?: ""))
            }
        }
        return dependencyResolver
    }

    // fixme: isn't going to work on JDK 9
    private fun loadJARs(dependencyResolver: Lazy<MavenDependencyResolver>, artifacts: List<String>) {
        (ClassLoader.getSystemClassLoader() as java.net.URLClassLoader)
            .addURLs(artifacts.flatMap { artifact ->
                if (debug) {
                    System.err.println("[DEBUG] Resolving $artifact")
                }
                val result = try {
                    dependencyResolver.value.resolve(DefaultArtifact(artifact)).map { it.toURI().toURL() }
                } catch (e: IllegalArgumentException) {
                    val file = File(expandTilde(artifact))
                    if (!file.exists()) {
                        System.err.println("Error: $artifact does not exist")
                        exitProcess(1)
                    }
                    listOf(file.toURI().toURL())
                } catch (e: RepositoryException) {
                    if (debug) {
                        e.printStackTrace()
                    }
                    System.err.println("Error: $artifact wasn't found")
                    exitProcess(1)
                }
                if (debug) {
                    result.forEach { url -> System.err.println("[DEBUG] Loading $url") }
                }
                if (!skipClasspathCheck) {
                    if (result.any { it.toString().substringAfterLast("/").startsWith("ktlint-core-") }) {
                        System.err.println("\"$artifact\" appears to have a runtime/compile dependency on \"ktlint-core\".\n" +
                            "Please inform the author that \"com.github.shyiko:ktlint*\" should be marked " +
                            "compileOnly (Gradle) / provided (Maven).\n" +
                            "(to suppress this warning use --skip-classpath-check)")
                    }
                    if (result.any { it.toString().substringAfterLast("/").startsWith("kotlin-stdlib-") }) {
                        System.err.println("\"$artifact\" appears to have a runtime/compile dependency on \"kotlin-stdlib\".\n" +
                            "Please inform the author that \"org.jetbrains.kotlin:kotlin-stdlib*\" should be marked " +
                            "compileOnly (Gradle) / provided (Maven).\n" +
                            "(to suppress this warning use --skip-classpath-check)")
                    }
                }
                result
            })
    }

    private fun parseQuery(query: String) = query.split("&")
        .fold(LinkedHashMap<String, String>()) { map, s ->
            if (!s.isEmpty()) {
                s.split("=", limit = 2).let { e -> map.put(e[0],
                    URLDecoder.decode(e.getOrElse(1) { "true" }, "UTF-8")) }
            }
            map
        }

    private fun lint(
        fileName: String,
        text: String,
        ruleSets: Iterable<RuleSet>,
        userData: Map<String, String>,
        cb: (e: LintError) -> Unit
    ) =
        if (fileName.endsWith(".kt", ignoreCase = true)) {
            KtLint.lint(text, ruleSets, userData, cb)
        } else {
            KtLint.lintScript(text, ruleSets, userData, cb)
        }

    private fun format(
        fileName: String,
        text: String,
        ruleSets: Iterable<RuleSet>,
        userData: Map<String, String>,
        cb: (e: LintError, corrected: Boolean) -> Unit
    ): String =
        if (fileName.endsWith(".kt", ignoreCase = true)) {
            KtLint.format(text, ruleSets, userData, cb)
        } else {
            KtLint.formatScript(text, ruleSets, userData, cb)
        }

    private fun java.net.URLClassLoader.addURLs(url: Iterable<java.net.URL>) {
        val method = java.net.URLClassLoader::class.java.getDeclaredMethod("addURL", java.net.URL::class.java)
        method.isAccessible = true
        url.forEach { method.invoke(this, it) }
    }

    private fun <T> Sequence<Callable<T>>.parallel(
        cb: (T) -> Unit,
        numberOfThreads: Int = Runtime.getRuntime().availableProcessors()
    ) {
        val q = ArrayBlockingQueue<Future<T>>(numberOfThreads)
        val pill = object : Future<T> {

            override fun isDone(): Boolean { throw UnsupportedOperationException() }
            override fun get(timeout: Long, unit: TimeUnit): T { throw UnsupportedOperationException() }
            override fun get(): T { throw UnsupportedOperationException() }
            override fun cancel(mayInterruptIfRunning: Boolean): Boolean { throw UnsupportedOperationException() }
            override fun isCancelled(): Boolean { throw UnsupportedOperationException() }
        }
        val consumer = Thread(Runnable {
            while (true) {
                val future = q.poll(Long.MAX_VALUE, TimeUnit.NANOSECONDS)
                if (future === pill) {
                    break
                }
                cb(future.get())
            }
        })
        consumer.start()
        val executorService = Executors.newCachedThreadPool()
        for (v in this) {
            q.put(executorService.submit(v))
        }
        q.put(pill)
        executorService.shutdown()
        consumer.join()
    }
}