• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jesperancinha / markdowner / #194

pending completion
#194

push

github-ci

web-flow
Merge pull request #149 from jesperancinha/migration-to-kotlin

Migration to kotlin

333 of 333 new or added lines in 13 files covered. (100.0%)

274 of 333 relevant lines covered (82.28%)

8.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/markdowner-filter/src/main/kotlin/org/jesperancinha/parser/markdowner/filter/ReadmeNamingParser.kt
1
package org.jesperancinha.parser.markdowner.filter
2

3
import java.io.*
4
import java.nio.file.DirectoryStream
5
import java.nio.file.Files
6
import java.nio.file.Path
7
import java.util.*
8

9
class ReadmeNamingParser(
28✔
10
     val fileFilterChain: FileFilterChain? = null,
6✔
11
     val templateLocation: Path,
6✔
12
     val isNoEmpty: Boolean = false
10✔
13
) {
14
    /**
15
     * Builds a stream of a Readme marked down texts taking a [Path] as a reference.
16
     * If a Readme.md file already exists, it will return a Stream with it's content.
17
     * If a Readme.md File does not exist then it will create a new stream with the project title as the first header.
18
     *
19
     *
20
     * Special algorithms will determine the name of the project automatically
21
     *
22
     * @param path The path where to search and determine the right text for the markdown Readme file
23
     * @return The calculated Input text stream
24
     * @throws IOException Any IO Exception that may occur
25
     */
26
    @Throws(IOException::class)
27
    fun buildReadmeStream(path: Path): InputStream? {
28
        if (isItATemplatePath(path)) {
8✔
29
            return null
4✔
30
        }
31
        val readmeFile = getReadmePath(path)
8✔
32
        if (readmeFile?.exists() == true) {
18✔
33
            return FileInputStream(readmeFile)
×
34
        }
35
        val packageInfo = findPackageInfo(path)
8✔
36
        return if (noPackageInfo(packageInfo) || isNoEmpty) {
16✔
37
            null
4✔
38
        } else ByteArrayInputStream(("# " + packageInfo?.projectName).toByteArray())
48✔
39
    }
40

41
    /**
42
     * If no package has been found
43
     *
44
     * @param packageInfo The package info
45
     * @return If packageInfo is not null
46
     */
47
    private fun noPackageInfo(packageInfo: PackageInfo?): Boolean {
48
        return Objects.isNull(packageInfo)
6✔
49
    }
50

51
    /**
52
     * Gets the Readme.me from the reference directory path
53
     *
54
     * @param path The directory path
55
     * @return The directory path with the added Readme.md file
56
     */
57
    private fun getReadmePath(path: Path): File? {
58
        val readmePath = path.resolve("Readme.md")
8✔
59
        return readmePath.toFile()
6✔
60
    }
61

62
    /**
63
     * If the path is the actual template. We do not want to change the template itself
64
     *
65
     * @param path The path to test
66
     * @return true if the template path matches the given path, otherwise false
67
     */
68
    private fun isItATemplatePath(path: Path): Boolean {
69
        return (path.toAbsolutePath().toString()
8✔
70
                == templateLocation.toAbsolutePath().toString())
10✔
71
    }
72

73
    /**
74
     * Discovers the package info based on all the chain elements created during chain creation.
75
     * The default configuration of this chain is MAVEN, NPM, GRADLE and SBT.
76
     *
77
     * @param path The Path to be tested
78
     * @return The created package info with the project name and the automated packaging system type [PackageInfo]
79
     * @throws IOException If an input/output exception has occurred
80
     */
81
    @Throws(IOException::class)
82
    private fun findPackageInfo(path: Path): PackageInfo? {
83
        var highestLevel: PackageInfo?
4✔
84
        Files.newDirectoryStream(path).use { stream -> highestLevel = iterateAllPathsInDirectoryStream(stream) }
44✔
85
        return highestLevel
4✔
86
    }
87

88
    /**
89
     * Iterates through all files in the given Directory stream
90
     *
91
     * @param stream Directory stream [DirectoryStream]
92
     * @return The created package info with the project name and the automated packaging system type [PackageInfo]
93
     */
94
    private fun iterateAllPathsInDirectoryStream(stream: DirectoryStream<Path>): PackageInfo? {
95
        var highestLevel: PackageInfo? = null
4✔
96
        for (newPath in stream) {
20✔
97
            if (!Files.isDirectory(newPath)) {
10✔
98
                highestLevel = fileFilterChain?.findHighest(highestLevel, newPath)
26✔
99
            }
100
        }
101
        return highestLevel
4✔
102
    }
103
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc