Skip to content

Commit

Permalink
[fix] add platforms as-is to the output of Assemble and Solve (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
nh13 authored Mar 2, 2023
1 parent a24d734 commit c2e2345
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.condaincubator.condaenvbuilder.api

import com.github.condaincubator.condaenvbuilder.api.CondaStep.Channel
import com.github.condaincubator.condaenvbuilder.api.Platform.Platform
import com.github.condaincubator.condaenvbuilder.api.CondaStep.{Channel, Platform}
import io.circe.Decoder.Result
import io.circe.syntax._
import io.circe.{Decoder, Encoder, HCursor, Json}
Expand Down Expand Up @@ -67,16 +66,17 @@ case class CondaStep(channels: Seq[Channel]=Seq.empty, requirements: Seq[Require
*/
object CondaStep {
type Channel = String
type Platform = String

import Encoders.EncodeRequirement

/** Returns an YAML encoder for [[CondaStep]] */
def encoder: Encoder[CondaStep] = new Encoder[CondaStep] {
final def apply(step: CondaStep): Json = {
val fields = ArrayBuffer[(String, Json)]()
if (step.platforms.nonEmpty) fields.append(("platforms", Json.fromValues(step.platforms.map(_.asJson))))
fields.append(("channels", Json.fromValues(step.channels.map(_.asJson))))
fields.append(("requirements", Json.fromValues(step.requirements.map(_.asJson))))
if (step.platforms.nonEmpty) fields.append(("platforms", Json.fromValues(step.platforms.map(_.asJson))))
Json.obj(fields.toSeq:_*)
}
}
Expand All @@ -88,6 +88,11 @@ object CondaStep {
final def apply(c: HCursor): Decoder.Result[CondaStep] = {
val keys: Seq[String] = c.keys.map(_.toSeq).getOrElse(Seq.empty)

val platformResults: Result[Seq[Platform]] = {
if (keys.contains("platforms")) c.downField("platforms").as[Seq[Platform]]
else Right(Seq.empty)
}

val channelsResults: Result[Seq[String]] = {
if (keys.contains("channels")) c.downField("channels").as[Seq[String]]
else Right(Seq.empty)
Expand All @@ -98,10 +103,6 @@ object CondaStep {
else Right(Seq.empty)
}

val platformResults: Result[Seq[Platform]] = {
if (keys.contains("platforms")) c.downField("platforms").as[Seq[Platform]]
else Right(Seq.empty)
}

for {
channels <- channelsResults
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.github.condaincubator.condaenvbuilder.io

import java.io.PrintWriter
import java.nio.file.Paths
import com.fulcrumgenomics.commons.CommonsDef.{DirPath, FilePath}
import com.fulcrumgenomics.commons.io.Io
import com.fulcrumgenomics.commons.util.{LazyLogging, Logger}
import com.github.condaincubator.condaenvbuilder.cmdline.CondaEnvironmentBuilderTool
import com.github.condaincubator.condaenvbuilder.CondaEnvironmentBuilderDef.PathToYaml
import com.github.condaincubator.condaenvbuilder.api.CodeStep.Command
import com.github.condaincubator.condaenvbuilder.api.{CodeStep, CondaStep, Environment, PipStep}
import com.github.condaincubator.condaenvbuilder.api.CondaStep
import com.github.condaincubator.condaenvbuilder.cmdline.CondaEnvironmentBuilderTool

import java.io.PrintWriter
import java.nio.file.Paths

/** Companion to [[BuildWriter]]. */
object BuildWriter {
Expand Down Expand Up @@ -111,6 +111,12 @@ case class BuildWriter(environment: Environment,

val writer = new PrintWriter(Io.toWriter(environmentYaml))
writer.println(f"name: ${environment.name}")
condaStep.foreach { step =>
if (step.platforms.nonEmpty) {
writer.println("platforms:")
step.platforms.foreach { platform => writer.println(f" - $platform") }
}
}
condaStep.foreach { step =>
if (step.channels.nonEmpty) {
writer.println("channels:")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.github.condaincubator.condaenvbuilder.tools

import java.nio.file.Files
import com.fulcrumgenomics.commons.CommonsDef.DirPath
import com.fulcrumgenomics.commons.io.Io
import com.fulcrumgenomics.sopt.{arg, clp}
import com.github.condaincubator.condaenvbuilder.CondaEnvironmentBuilderDef._
import com.github.condaincubator.condaenvbuilder.api.{Environment, Spec}
import com.github.condaincubator.condaenvbuilder.cmdline.{ClpGroups, CondaEnvironmentBuilderTool}
import com.github.condaincubator.condaenvbuilder.CondaEnvironmentBuilderDef._
import com.github.condaincubator.condaenvbuilder.io.{BuildWriter, SpecParser}
import com.github.condaincubator.condaenvbuilder.api.Spec
import com.github.condaincubator.condaenvbuilder.cmdline.CondaEnvironmentBuilderTool
import com.github.condaincubator.condaenvbuilder.io.BuildWriter

import java.nio.file.Files


@clp(description =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.github.condaincubator.condaenvbuilder.tools

import cats.syntax.either._
import com.fulcrumgenomics.commons.CommonsDef
import com.fulcrumgenomics.commons.CommonsDef.{DirPath, FilePath, SafelyClosable}
import com.fulcrumgenomics.commons.CommonsDef.{DirPath, SafelyClosable}
import com.fulcrumgenomics.commons.io.Io
import com.fulcrumgenomics.commons.util.Logger
import com.fulcrumgenomics.sopt.{arg, clp}
import com.github.condaincubator.condaenvbuilder.api.{CodeStep, CondaStep, Environment, PipStep, Requirement, Spec}
import com.github.condaincubator.condaenvbuilder.cmdline.{ClpGroups, CondaEnvironmentBuilderTool}
import com.github.condaincubator.condaenvbuilder.io.{BuildWriter, SpecParser, SpecWriter}
import com.github.condaincubator.condaenvbuilder.CondaEnvironmentBuilderDef._
import com.github.condaincubator.condaenvbuilder.api.CondaStep.Channel
import com.github.condaincubator.condaenvbuilder.api.CondaStep.{Channel, Platform}
import com.github.condaincubator.condaenvbuilder.api._
import com.github.condaincubator.condaenvbuilder.cmdline.CondaEnvironmentBuilderTool
import com.github.condaincubator.condaenvbuilder.io.BuildWriter
import com.github.condaincubator.condaenvbuilder.cmdline.{ClpGroups, CondaEnvironmentBuilderTool}
import com.github.condaincubator.condaenvbuilder.io.{BuildWriter, SpecParser, SpecWriter}
import io.circe.Decoder.Result
import io.circe.{Decoder, DecodingFailure, HCursor, yaml}

Expand Down Expand Up @@ -201,6 +197,7 @@ object Solve {

private case class CondaEnvironment
(name: String,
platforms: Seq[Platform],
channels: Seq[Channel],
conda: Seq[Requirement],
pip: Seq[Requirement]
Expand All @@ -213,10 +210,11 @@ private object CondaEnvironment {
def decoder: Decoder[CondaEnvironment] = new Decoder[CondaEnvironment] {
final def apply(c: HCursor): Decoder.Result[CondaEnvironment] = {
// Get the keys at this level
val keys = c.keys.map(_.toSeq).getOrElse(Seq.empty)
val nameResult = c.downField("name").as[String]
val channelsResult = if (keys.contains("channels")) c.downField("channels").as[Seq[String]] else Right(Seq.empty[String])
val dependencies = c.downField("dependencies").values.toSeq.flatten
val keys = c.keys.map(_.toSeq).getOrElse(Seq.empty)
val nameResult = c.downField("name").as[String]
val platformsResult = if (keys.contains("platforms")) c.downField("platforms").as[Seq[String]] else Right(Seq.empty[String])
val channelsResult = if (keys.contains("channels")) c.downField("channels").as[Seq[String]] else Right(Seq.empty[String])
val dependencies = c.downField("dependencies").values.toSeq.flatten
val condaResult: Result[Seq[Requirement]] = {
val results = dependencies.filterNot(_.isObject).map(_.as[Requirement])
results.collectFirst { case left: Left[DecodingFailure, Requirement] => left } match {
Expand All @@ -231,15 +229,17 @@ private object CondaEnvironment {

for {
name <- nameResult
platforms <- platformsResult
channels <- channelsResult
conda <- condaResult
pip <- pipResult
} yield {
CondaEnvironment(
name = name,
channels = channels,
conda = conda,
pip = pip
name = name,
platforms = platforms,
channels = channels,
conda = conda,
pip = pip
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ object CondaStepTest extends UnitSpec {
// one channel, one requirement, one platform
(api.CondaStep(channels=Seq("some channel"), requirements=Seq("a==1").reqs, platforms=Seq("linux-32")), {
"""{
| "platforms" : [
| "linux-32"
| ],
| "channels" : [
| "some channel"
| ],
| "requirements" : [
| "a==1"
| ],
| "platforms" : [
| "linux-32"
| ]
|}""".stripMargin
}),
// multiple channels, requirements, and platforms
(api.CondaStep(channels=Seq("channel 1", "channel 2"), requirements=Seq("a==1", "b==2").reqs, platforms=Seq("linux-32", "win-32")), {
"""{
| "platforms" : [
| "linux-32",
| "win-32"
| ],
| "channels" : [
| "channel 1",
| "channel 2"
| ],
| "requirements" : [
| "a==1",
| "b==2"
| ],
| "platforms" : [
| "linux-32",
| "win-32"
| ]
|}""".stripMargin
}),
Expand Down
Loading

0 comments on commit c2e2345

Please sign in to comment.