-
Notifications
You must be signed in to change notification settings - Fork 61
/
tent_config.kt
135 lines (112 loc) · 3.6 KB
/
tent_config.kt
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
package org.secfirst.umbrella.data.disk
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.secfirst.umbrella.UmbrellaApplication
import org.secfirst.umbrella.misc.AppExecutors.Companion.ioContext
import org.secfirst.umbrella.misc.deviceLanguage
import java.io.File
private val repoPath = UmbrellaApplication.instance.cacheDir.path + "/repo/"
const val BRANCH_NAME: String = "refs/heads/master"
const val baseUrlRepository = "https://github.com/securityfirst/umbrella-content"
const val ELEMENT_LEVEL = 2
const val SUB_ELEMENT_LEVEL = 3
const val CHILD_LEVEL = 4
fun getDelimiter(fileName: String) = if (fileName == TypeFile.CATEGORY.value) fileName
else fileName.substringBeforeLast("_")
fun isRepository() = File(repoPath).exists()
fun isNotRepository() = !File(repoPath).exists()
fun getPathRepository(): String = repoPath
fun defaultContent(): String = "en"
enum class TypeFile(val value: String) {
CHECKLIST("c"),
FORM("f"),
CATEGORY(".category"),
IMG_CATEGORY("png"),
SEGMENT("s"),
NOUN("")
}
enum class IsoCountry(val value: String) {
ENGLISH("gb"),
CHINESE("zh"),
SPANISH("es"),
ARABIC("ar"),
FARSI("fa"),
RUSSIAN("ru"),
FRENCH("fr")
}
enum class Template(val value: String) {
GLOSSARY("glossary")
}
enum class ExtensionFile(val value: String) {
YML("yml"),
MD("md"),
PNG("png"),
}
fun tentLanguages(): List<String> {
val languages = mutableListOf<String>()
File(repoPath)
.walk()
.filter { !it.path.contains(".git") }
.filter { it.name.length == 2 || it.name.contains("zh", true) }
.filter { it.isDirectory }
.forEach { languages.add(it.name) }
return languages
}
fun defaultTentLanguage(): String {
var value = defaultContent()
tentLanguages().forEach { language ->
if (language == deviceLanguage())
value = deviceLanguage()
}
return value
}
fun getLastCommitID() {
val git = Git.open(File("${getPathRepository()}/.git"))
val head = git.repository.getRef("HEAD")
println("Ref of HEAD: " + head + ": " + head.name + " - " + head.objectId.name)
}
suspend fun validateRepository(repositoryURL: String): Boolean {
var result = false
val repoUrl = "$repositoryURL.git"
withContext(ioContext) {
try {
val db = FileRepositoryBuilder.create(File("/tmp"))
val git = Git.wrap(db)
val lsCmd = git.lsRemote()
lsCmd.setRemote(repoUrl)
if (null != lsCmd.call())
result = true
} catch (exception: Exception) {
result = false
}
}
return result
}
fun getLastDirectory(path: String): String {
val splitPath = path.split("/").filter { it.isNotEmpty() }
return splitPath[splitPath.lastIndex]
}
private fun getSplitPath(path: String) = path.split("/").filter { it.isNotEmpty() }
fun getLevelOfPath(path: String): Int {
val pathSplitted = path.split("/").toMutableList()
if (pathSplitted.size > 1) {
pathSplitted.removeAt(pathSplitted.lastIndex)
}
return pathSplitted.size
}
fun getWorkDirectory(path: String): String {
val splitPath = getSplitPath(path)
var pwd = ""
for (i in 1 until splitPath.size - 1)
pwd += splitPath[i] + "/"
return pwd
}
fun getWorkDirectoryFromImage(path: String): String {
val splitPath = getSplitPath(path)
var pwd = ""
for (i in 0 until splitPath.size - 1)
pwd += splitPath[i] + "/"
return pwd
}
fun basePath(): String = UmbrellaApplication.instance.cacheDir.path