Skip to content

Commit

Permalink
Delete .gitignore check logic
Browse files Browse the repository at this point in the history
- Delete .gitignore check logic in FileService

#9
  • Loading branch information
dohyeon5626 committed Feb 14, 2023
1 parent 411327d commit a65d669
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions src/main/kotlin/com/dohyeon5626/service/impl/FileServiceImpl.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.dohyeon5626.service.impl

import com.dohyeon5626.service.FileService
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.vfs.LocalFileSystem
import java.io.File
import java.nio.file.FileSystems
import kotlin.io.path.Path

class FileServiceImpl: FileService {

private val fileSystem = LocalFileSystem.getInstance()

override fun generateGitKeep(path: String) {
path.takeIf { !isGitIgnorePath("$path/.gitkeep") }
?.let { File(it).listFiles() }
path.let { File(it).listFiles() }
?.apply {
if (isEmpty()) {
createFile("$path/.gitkeep")
Expand All @@ -25,8 +20,7 @@ class FileServiceImpl: FileService {
}

override fun generateGitKeepInAllSubfolder(path: String) {
path.takeIf { !isGitIgnorePath("$path/.gitkeep") }
?.let { File(it).listFiles() }
path.let { File(it).listFiles() }
?.apply {
if (isEmpty()) {
createFile("$path/.gitkeep")
Expand All @@ -49,22 +43,4 @@ class FileServiceImpl: FileService {
fileSystem.refreshAndFindFileByIoFile(file)?.refresh(true, false)
}

private fun isGitIgnorePath(path: String): Boolean {
getProject(path)?.basePath?.let {
return File("${it}/.gitignore").readLines()
.filter { line -> line.isNotEmpty() && !line.startsWith("#") }
.any { ignorePattern -> isGitIgnorePatternMatch(ignorePattern, path.replace("$it/", "")) }
}
return false
}

private fun isGitIgnorePatternMatch(ignorePattern: String, path: String) =
FileSystems.getDefault().getPathMatcher("glob:$ignorePattern").matches(Path(path))

private fun getProject(path: String): Project? {
ProjectManager.getInstance().openProjects.toList()
.forEach { if(it.basePath != null && path.contains(it.basePath!!)) return it }
return null
}

}

0 comments on commit a65d669

Please sign in to comment.