Commit 83bc2bf8 authored by Romain Reuillon's avatar Romain Reuillon
Browse files

[Plugin] enh: add imports to omr files

parent c90844f6
......@@ -464,7 +464,7 @@ lazy val modifierHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.modifie
lazy val jsonHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.json", imports = Seq("*")) dependsOn(openmoleDSL, json, replication % "test") settings (
libraryDependencies += Libraries.scalatest) settings (pluginSettings: _*)
lazy val omrHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.omr", imports = Seq("*")) dependsOn(openmoleDSL, jsonHook, openmoleBuildInfo, replication % "test") settings(
lazy val omrHook = OsgiProject(pluginDir, "org.openmole.plugin.hook.omr", imports = Seq("*")) dependsOn(openmoleDSL, jsonHook, openmoleBuildInfo, project, replication % "test") settings(
libraryDependencies += Libraries.scalatest, libraryDependencies += Libraries.circe) settings (pluginSettings: _*)
......
......@@ -103,9 +103,6 @@ package object extension {
type RandomProvider = org.openmole.tool.random.RandomProvider
type PrototypeSet = org.openmole.core.context.PrototypeSet
type ScriptSourceData = org.openmole.core.workflow.tools.ScriptSourceData
val ScriptSourceData = org.openmole.core.workflow.tools.ScriptSourceData
type Time = squants.Time
type Information = squants.information.Information
......
......@@ -105,17 +105,17 @@ object Imports {
(1 to imp.stableIdentifier.size).map { i
val part = imp.stableIdentifier.take(i)
val path = toFile(directory, part.dropRight(1) ++ part.lastOption.map(_ + Project.scriptExtension))
ImportedFile(part, path)
ImportedFile(imp, part, path)
}.find { importedFile Project.isScript(importedFile.file) }
def matchingFileInSelector(imp: Import): Seq[ImportedFile] =
imp match {
case Import(stableIdentifier, Alias(from, _))
val file = toFile(directory, stableIdentifier) / (from + Project.scriptExtension)
if (file.exists) Seq(ImportedFile(stableIdentifier, file)) else Seq.empty
if (file.exists) Seq(ImportedFile(imp, stableIdentifier, file)) else Seq.empty
case Import(stableIdentifier, WildCard)
listScripts(toFile(directory, stableIdentifier)) map {
script ImportedFile(stableIdentifier, script)
script ImportedFile(imp, stableIdentifier, script)
}
case _ Seq.empty
}
......@@ -148,7 +148,11 @@ object Imports {
case class WildCardBut(names: String*) extends ImportSelector
case object WildCard extends ImportSelector
case class ImportedFile(stableIdentifier: Seq[String], file: File)
object ImportedFile {
def identifier(importedFile: ImportedFile) = importedFile.stableIdentifier.mkString(".")
}
case class ImportedFile(`import`: Import, stableIdentifier: Seq[String], file: File)
case class SourceFile(file: File, importedFiles: Seq[ImportedFile])
def toFile(dir: File, path: Seq[String]) =
......
......@@ -28,7 +28,6 @@ import org.openmole.core.fileservice.FileService
import org.openmole.core.project
import org.openmole.core.services._
import org.openmole.core.workflow.composition.DSL
import org.openmole.core.workflow.tools.ScriptSourceData
import org.openmole.core.workspace.TmpDirectory
import org.openmole.tool.hash._
......@@ -146,7 +145,7 @@ object Project {
|new $traitName {
|
|$functionPrototype = {
|implicit def _scriptSourceData = ${classOf[ScriptSourceData.ScriptData].getCanonicalName}(File(\"\"\"$workDirectory\"\"\"), File(\"\"\"$script\"\"\"))
|implicit def _scriptSourceData = ${ScriptSourceData.applySource(workDirectory, script, Imports.importedFiles(script).flatMap(_.importedFiles))}
|import ${Project.uniqueName(script)}._imports._""".stripMargin
def scriptFooter =
......
package org.openmole.core.project
import org.openmole.core.project.Imports.{ ImportedFile }
import org.openmole.tool.file._
object ScriptSourceData {
implicit def defaultData = NoData
case class ImportData(`import`: String, script: File)
case class ScriptData(workDirectory: File, script: File, imports: Seq[ImportData]) extends ScriptSourceData {
val content = if (script.exists()) script.content else ""
}
case object NoData extends ScriptSourceData
def applySource(workDirectory: File, script: File, imports: Seq[ImportedFile]) = {
def tq = "\"\"\""
def importData(i: ImportedFile) = s"""${classOf[ScriptSourceData.ImportData].getCanonicalName}($tq${ImportedFile.identifier(i)}$tq, File($tq${i.file}$tq))"""
s"""${classOf[ScriptSourceData.ScriptData].getCanonicalName}(File($tq$workDirectory$tq), File($tq$script$tq), Seq(${imports.map(importData).mkString(",")}))"""
}
def importsContent(scriptData: ScriptSourceData) =
scriptData match {
case NoData Seq()
case s: ScriptData s.imports
}
def scriptContent(scriptData: ScriptSourceData) =
scriptData match {
case NoData ""
case s: ScriptData s.content
}
}
sealed trait ScriptSourceData
\ No newline at end of file
package org.openmole.core.workflow.tools
import org.openmole.tool.file._
object ScriptSourceData {
implicit def defaultData = NoData
case class ScriptData(workDirectory: File, script: File) extends ScriptSourceData {
val content = if (script.exists()) script.content else ""
}
case object NoData extends ScriptSourceData
def scriptContent(scriptData: ScriptSourceData) =
scriptData match {
case NoData ""
case s: ScriptData s.content
}
}
sealed trait ScriptSourceData
\ No newline at end of file
......@@ -3,11 +3,12 @@ package org.openmole.plugin.hook.omr
import org.openmole.core.dsl._
import org.openmole.core.dsl.extension._
import org.openmole.core.workflow.format.CSVOutputFormat
import org.openmole.core.workflow.hook.FromContextHook
import org.openmole.core.project._
import org.openmole.plugin.tool.json._
import io.circe._
import org.openmole.core.exception.InternalProcessingError
import org.openmole.core.workflow.format.OutputFormat.{ PlainContent, SectionContent }
import io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
object OMROutputFormat {
......@@ -18,6 +19,7 @@ object OMROutputFormat {
def omrVersionField = "version"
def omrVersion = "0.1"
def scriptField = "script"
def importsField = "imports"
def openMOLEVersionField = "openmole-version"
implicit def outputFormat[MD](implicit encoder: Encoder[MD], methodData: MethodData[MD], scriptData: ScriptSourceData): OutputFormat[OMROutputFormat, MD] = new OutputFormat[OMROutputFormat, MD] {
......@@ -41,12 +43,18 @@ object OMROutputFormat {
.add(omrVersionField, Json.fromString(omrVersion))
.add(openMOLEVersionField, Json.fromString(org.openmole.core.buildinfo.version.value))
if (format.script) o2.add(scriptField, Json.fromString(ScriptSourceData.scriptContent(scriptData))) else o2
if (format.script) {
case class OMRImport(`import`: String, content: String)
o2.add(scriptField, Json.fromString(ScriptSourceData.scriptContent(scriptData)))
.add(importsField, Json.fromValues(ScriptSourceData.importsContent(scriptData).map(i OMRImport(i.`import`, i.script.content).asJson)))
}
else o2
}
}
directory.withLockInDirectory {
import io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
content match {
case PlainContent(variables, name)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment