forked from enso-org/developer-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preliminary package structure and API (enso-org#3)
- Loading branch information
1 parent
6f84b8a
commit c23209b
Showing
5 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def Main: | ||
None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.enso.pkg | ||
|
||
import java.io.File | ||
import java.io.PrintWriter | ||
|
||
import io.circe.yaml | ||
import io.circe.generic.auto._ | ||
import io.circe.syntax._ | ||
import io.circe.yaml.syntax._ | ||
|
||
import scala.io.Source | ||
import scala.util.Try | ||
|
||
case class Config( | ||
author: String, | ||
maintainer: String, | ||
name: String, | ||
version: String, | ||
license: String) { | ||
def toYaml: String = this.asJson.asYaml.spaces4 | ||
} | ||
|
||
object Config { | ||
def fromYaml(yamlString: String): Option[Config] = { | ||
yaml.parser.parse(yamlString).flatMap(_.as[Config]).toOption | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.enso.pkg | ||
|
||
import java.io.File | ||
|
||
object Main extends App { | ||
override def main(args: Array[String]): Unit = { | ||
Package.getOrCreate( | ||
new File("/Users/marcinkostrzewa/765Luna__$%%$#Project") | ||
) | ||
Package.getOrCreate( | ||
new File("/Users/marcinkostrzewa/proper_%%$##%#project") | ||
) | ||
Package.getOrCreate(new File("/Users/marcinkostrzewa/Properproject")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.enso.pkg | ||
|
||
import java.io.File | ||
import java.io.PrintWriter | ||
|
||
import org.apache.commons.io.FileUtils | ||
|
||
import scala.io.Source | ||
import scala.util.Try | ||
|
||
object CouldNotCreateDirectory extends Exception | ||
|
||
case class Package(root: File, config: Config) { | ||
|
||
val sourceDir = new File(root, Package.sourceDirName) | ||
val configFile = new File(root, Package.configFileName) | ||
val thumbFile = new File(root, Package.thumbFileName) | ||
|
||
def save(): Unit = { | ||
if (!root.exists) createDirectories() | ||
if (!sourceDir.exists) createSourceDir() | ||
saveConfig() | ||
} | ||
|
||
def createDirectories() { | ||
val created = Try(root.mkdirs).getOrElse(false) | ||
if (!created) throw CouldNotCreateDirectory | ||
createSourceDir() | ||
} | ||
|
||
def rename(newName: String): Package = { | ||
val newPkg = copy(config = config.copy(name = newName)) | ||
newPkg.save() | ||
newPkg | ||
} | ||
|
||
def remove(): Unit = { | ||
FileUtils.deleteDirectory(root) | ||
} | ||
|
||
def move(newRoot: File): Package = { | ||
val newPkg = copyPackage(newRoot) | ||
remove() | ||
newPkg | ||
} | ||
|
||
def copyPackage(newRoot: File): Package = { | ||
FileUtils.copyDirectory(root, newRoot) | ||
copy(root = newRoot) | ||
} | ||
|
||
def createSourceDir(): Unit = { | ||
if (!Try(sourceDir.mkdir).getOrElse(false)) throw CouldNotCreateDirectory | ||
val lunaCodeSrc = Source.fromResource(Package.mainFileName) | ||
val writer = new PrintWriter(new File(sourceDir, Package.mainFileName)) | ||
writer.write(lunaCodeSrc.mkString) | ||
writer.close() | ||
lunaCodeSrc.close() | ||
} | ||
|
||
def saveConfig(): Unit = { | ||
val writer = new PrintWriter(configFile) | ||
Try(writer.write(config.toYaml)) | ||
writer.close() | ||
} | ||
|
||
def hasThumb: Boolean = thumbFile.exists | ||
def name: String = config.name | ||
} | ||
|
||
object Package { | ||
val configFileName = "package.yaml" | ||
val sourceDirName = "src" | ||
val mainFileName = "Main.luna" | ||
val thumbFileName = "thumb.png" | ||
|
||
def create(root: File, config: Config): Package = { | ||
val pkg = Package(root, config) | ||
pkg.save() | ||
pkg | ||
} | ||
|
||
def create(root: File, name: String): Package = { | ||
val config = Config( | ||
author = "", | ||
maintainer = "", | ||
name = name, | ||
version = "", | ||
license = "" | ||
) | ||
create(root, config) | ||
} | ||
|
||
def fromDirectory(root: File): Option[Package] = { | ||
if (!root.exists()) return None | ||
val configFile = new File(root, configFileName) | ||
val source = Try(Source.fromFile(configFile)) | ||
val result = source.map(_.mkString).toOption.flatMap(Config.fromYaml) | ||
source.foreach(_.close()) | ||
result.map(Package(root, _)) | ||
} | ||
|
||
def getOrCreate(root: File): Package = { | ||
val existing = fromDirectory(root) | ||
existing.getOrElse(create(root, generateName(root))) | ||
} | ||
|
||
def generateName(file: File): String = { | ||
val dirname = file.getName | ||
val startingWithLetter = | ||
if (!dirname(0).isLetter) "Project" ++ dirname else dirname | ||
val startingWithUppercase = startingWithLetter.capitalize | ||
val onlyAlphanumeric = startingWithUppercase.filter(_.isLetterOrDigit) | ||
onlyAlphanumeric | ||
} | ||
} |