Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/resolved paths in config view #668

Merged
merged 8 commits into from
Apr 2, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The default `multiple_sep` has been changed from `:` to `;` to avoid conflicts w

* `schema`: Don't require undocumented fields to set default values and add the `links` and `reference` fields to functionality as they were not meant only to be in the project config (PR #636).

* `resource path`: Don't finalize the `path` field of a resource until it's written as part of building a component (PR #668).

* `export json_schema`: Fix minor inconsistencies and make the strict schema stricter by adapting to what Viash will effectively return (PR #666).

# Viash 0.9.0-RC1 (2024-01-26): Restructure platforms into runners and engines
Expand Down
9 changes: 7 additions & 2 deletions src/main/scala/io/viash/config/ConfigMeta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,23 @@ object ConfigMeta {

// change the config object before writing to yaml:
// * substitute 'text' fields in resources with placeholders
// * set 'path' fields to the resourcePath
// * add more info variables
val toWriteConfig = config.copy(
resources = config.resources.map{ res =>
if (res.text.isDefined) {
val textVal = Some(placeholderMap(res))
res.copyResource(text = textVal, parent = None)
} else {
res.copyResource(parent = None)
res.copyResource(parent = None, path = Some(res.resourcePath))
}
},
test_resources = config.test_resources.map { res =>
res.copyResource(parent = None)
if (res.text.isDefined) {
res.copyResource(parent = None)
} else {
res.copyResource(parent = None, path = Some(res.resourcePath))
}
},
build_info = config.build_info.map(_.copy(
output = buildDir.map(_.toString),
Expand Down
31 changes: 24 additions & 7 deletions src/main/scala/io/viash/config/resources/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

// val uri: Option[URI] = path.map(IO.uri)
def uri: Option[URI] = {
path match {
resolvedPath match {
case Some(pat) => {
val patEsc = pat.replaceAll(" ", "%20")
val newPath = parent match {
Expand All @@ -96,6 +96,25 @@
}
}

def resolvedPath: Option[String] = {
Grifs marked this conversation as resolved.
Show resolved Hide resolved
if (this.isInstanceOf[Executable]) {
return path
}
if (path.isEmpty || path.get.contains(":")) {
return path
}

val pathStr = path.get
if (pathStr.startsWith("/")) {
if (parent.isEmpty) {
throw new RuntimeException(s"One of the resources is relative to the package root ($path), but no package config file (_viash.yaml) could be found.")

Check warning on line 110 in src/main/scala/io/viash/config/resources/Resource.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/io/viash/config/resources/Resource.scala#L109-L110

Added lines #L109 - L110 were not covered by tests
}
Some(IO.resolvePathWrtURI(pathStr, parent.get))

Check warning on line 112 in src/main/scala/io/viash/config/resources/Resource.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/io/viash/config/resources/Resource.scala#L112

Added line #L112 was not covered by tests
} else {
path
}
}

private val basenameRegex = ".*/".r
private val getFolderNameRegex = ".*?([^/]+|/)/*$".r

Expand All @@ -109,7 +128,7 @@
if (dest.isDefined) {
dest.get
} else {
getFolderNameRegex.replaceFirstIn(Paths.get(path.get).normalize.toString, "$1")
getFolderNameRegex.replaceFirstIn(Paths.get(resolvedPath.get).normalize.toString, "$1")
}
}
/**
Expand Down Expand Up @@ -160,22 +179,20 @@
if (packageDir.isEmpty) {
throw new RuntimeException(s"One of the resources is relative to the package root ($path), but no package config file (_viash.yaml) could be found.")
}
val pathStr1 = IO.resolvePathWrtURI(pathStr, packageDir.get)
return this.copyResource(
path = Some(pathStr1),
return copyResource(

Check warning on line 182 in src/main/scala/io/viash/config/resources/Resource.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/io/viash/config/resources/Resource.scala#L182

Added line #L182 was not covered by tests
parent = packageDir
)
}

// if the path is relative (which it probably should be),
// set the directory of the config as the parent of this file.
if (!Paths.get(pathStr).isAbsolute) {
return this.copyResource(
return copyResource(
parent = Some(parent)
)
}

return this
this
}

// TODO: This can probably be solved much nicer.
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/viash/helpers/NsExecData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object NsExecData {
def apply(configPath: String, config: Config, runner: Option[Runner], engine: Option[Engine]): NsExecData = {
val configPath_ = Paths.get(configPath)
val dirPath = configPath_.getParent()
val mainScript = config.mainScript.flatMap(s => s.path).map(dirPath.resolve(_))
val mainScript = config.mainScript.flatMap(s => s.resolvedPath).map(dirPath.resolve(_))
apply(
configFullPath = configPath,
absoluteConfigFullPath = configPath_.toAbsolutePath.toString,
Expand Down
5 changes: 3 additions & 2 deletions src/test/scala/io/viash/e2e/build/DockerMeta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import io.viash.config.resources.PlainFile
class DockerMeta extends AnyFunSuite with BeforeAndAfterAll {
Logger.UseColorOverride.value = Some(false)
// which config to test
private val configFile = getClass.getResource(s"/testbash/config.vsh.yaml").getPath
private val configFile = getClass.getResource("/testbash/config.vsh.yaml").getPath
private val parent = getClass.getResource("/").toURI()

// parse config from file
private val config = Config.read(configFile)
private def configAndResources = PlainFile(path = Some(configFile.toString)) :: config.resources
private def configAndResources = PlainFile(path = Some("testbash/config.vsh.yaml"), parent = Some(parent)) :: config.resources

test("Get meta data of a docker", DockerTest) {
// Create temporary folder to copy the files to so we can do a git init in that folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MainConfigInjectSuite extends AnyFunSuite with BeforeAndAfterAll {

val config = Config.read(configFile.toString())

val scriptFile = destPath.resolve(s"$name/" + config.mainScript.get.path.get) // assume all of these things exist
val scriptFile = destPath.resolve(s"$name/" + config.mainScript.get.resolvedPath.get) // assume all of these things exist
assert(scriptFile.toFile().exists, "Check dest script exists")

// inject script
Expand Down
Loading