|
| 1 | +package org.enso.pkg |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.io.PrintWriter |
| 5 | + |
| 6 | +import org.apache.commons.io.FileUtils |
| 7 | + |
| 8 | +import scala.io.Source |
| 9 | +import scala.util.Try |
| 10 | + |
| 11 | +object CouldNotCreateDirectory extends Exception |
| 12 | + |
| 13 | +case class Package(root: File, config: Config) { |
| 14 | + |
| 15 | + val sourceDir = new File(root, Package.sourceDirName) |
| 16 | + val configFile = new File(root, Package.configFileName) |
| 17 | + val thumbFile = new File(root, Package.thumbFileName) |
| 18 | + |
| 19 | + def save(): Unit = { |
| 20 | + if (!root.exists) createDirectories() |
| 21 | + if (!sourceDir.exists) createSourceDir() |
| 22 | + saveConfig() |
| 23 | + } |
| 24 | + |
| 25 | + def createDirectories() { |
| 26 | + val created = Try(root.mkdirs).getOrElse(false) |
| 27 | + if (!created) throw CouldNotCreateDirectory |
| 28 | + createSourceDir() |
| 29 | + } |
| 30 | + |
| 31 | + def rename(newName: String): Package = { |
| 32 | + val newPkg = copy(config = config.copy(name = newName)) |
| 33 | + newPkg.save() |
| 34 | + newPkg |
| 35 | + } |
| 36 | + |
| 37 | + def remove(): Unit = { |
| 38 | + FileUtils.deleteDirectory(root) |
| 39 | + } |
| 40 | + |
| 41 | + def move(newRoot: File): Package = { |
| 42 | + val newPkg = copyPackage(newRoot) |
| 43 | + remove() |
| 44 | + newPkg |
| 45 | + } |
| 46 | + |
| 47 | + def copyPackage(newRoot: File): Package = { |
| 48 | + FileUtils.copyDirectory(root, newRoot) |
| 49 | + copy(root = newRoot) |
| 50 | + } |
| 51 | + |
| 52 | + def createSourceDir(): Unit = { |
| 53 | + if (!Try(sourceDir.mkdir).getOrElse(false)) throw CouldNotCreateDirectory |
| 54 | + val lunaCodeSrc = Source.fromResource(Package.mainFileName) |
| 55 | + val writer = new PrintWriter(new File(sourceDir, Package.mainFileName)) |
| 56 | + writer.write(lunaCodeSrc.mkString) |
| 57 | + writer.close() |
| 58 | + lunaCodeSrc.close() |
| 59 | + } |
| 60 | + |
| 61 | + def saveConfig(): Unit = { |
| 62 | + val writer = new PrintWriter(configFile) |
| 63 | + Try(writer.write(config.toYaml)) |
| 64 | + writer.close() |
| 65 | + } |
| 66 | + |
| 67 | + def hasThumb: Boolean = thumbFile.exists |
| 68 | + def name: String = config.name |
| 69 | +} |
| 70 | + |
| 71 | +object Package { |
| 72 | + val configFileName = "package.yaml" |
| 73 | + val sourceDirName = "src" |
| 74 | + val mainFileName = "Main.luna" |
| 75 | + val thumbFileName = "thumb.png" |
| 76 | + |
| 77 | + def create(root: File, config: Config): Package = { |
| 78 | + val pkg = Package(root, config) |
| 79 | + pkg.save() |
| 80 | + pkg |
| 81 | + } |
| 82 | + |
| 83 | + def create(root: File, name: String): Package = { |
| 84 | + val config = Config( |
| 85 | + author = "", |
| 86 | + maintainer = "", |
| 87 | + name = name, |
| 88 | + version = "", |
| 89 | + license = "" |
| 90 | + ) |
| 91 | + create(root, config) |
| 92 | + } |
| 93 | + |
| 94 | + def fromDirectory(root: File): Option[Package] = { |
| 95 | + if (!root.exists()) return None |
| 96 | + val configFile = new File(root, configFileName) |
| 97 | + val source = Try(Source.fromFile(configFile)) |
| 98 | + val result = source.map(_.mkString).toOption.flatMap(Config.fromYaml) |
| 99 | + source.foreach(_.close()) |
| 100 | + result.map(Package(root, _)) |
| 101 | + } |
| 102 | + |
| 103 | + def getOrCreate(root: File): Package = { |
| 104 | + val existing = fromDirectory(root) |
| 105 | + existing.getOrElse(create(root, generateName(root))) |
| 106 | + } |
| 107 | + |
| 108 | + def generateName(file: File): String = { |
| 109 | + val dirname = file.getName |
| 110 | + val startingWithLetter = |
| 111 | + if (!dirname(0).isLetter) "Project" ++ dirname else dirname |
| 112 | + val startingWithUppercase = startingWithLetter.capitalize |
| 113 | + val onlyAlphanumeric = startingWithUppercase.filter(_.isLetterOrDigit) |
| 114 | + onlyAlphanumeric |
| 115 | + } |
| 116 | +} |
0 commit comments