Skip to content

Commit

Permalink
Restructure some packages and expose required types and methods in be…
Browse files Browse the repository at this point in the history
…som.*
  • Loading branch information
KacperFKorban committed Aug 3, 2023
1 parent 74f6195 commit 05f8a03
Show file tree
Hide file tree
Showing 31 changed files with 311 additions and 223 deletions.
4 changes: 2 additions & 2 deletions besom-cats/src/main/scala/runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ trait CatsEffectModule extends BesomModule:

def ioRuntime: IORuntime = cats.effect.unsafe.IORuntime.global

protected val rt: Runtime[Eff] = CatsRuntime()(using ioRuntime)
protected lazy val rt: Runtime[Eff] = CatsRuntime()(using ioRuntime)

given Result.ToFuture[Eff] = new Result.ToFuture[IO]:
def eval[A](fa: => IO[A]): () => Future[A] = () => fa.unsafeToFuture()(using ioRuntime)

// override def run(program: Context ?=> Output[Exports]): Future[Unit] = ???

object Pulumi extends CatsEffectModule
export Pulumi.*
export Pulumi.{ *, given }
4 changes: 2 additions & 2 deletions besom-zio/src/main/scala/runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ trait ZIOModule extends BesomModule:
import scala.concurrent.*
override final type Eff[+A] = zio.Task[A]

protected val rt: Runtime[Eff] = ZIORuntime()(using zio.Runtime.default)
protected lazy val rt: Runtime[Eff] = ZIORuntime()(using zio.Runtime.default)

given Result.ToFuture[Eff] = new Result.ToFuture[Task]:
def eval[A](fa: => Task[A]): () => Future[A] = () =>
Expand All @@ -52,4 +52,4 @@ trait ZIOModule extends BesomModule:
// override def run(program: Context ?=> Output[Exports]): Future[Unit] = ???

object Pulumi extends ZIOModule
export Pulumi.*
export Pulumi.{ *, given }
32 changes: 16 additions & 16 deletions codegen/src/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ object CodeGen {

val commonImportedIdentifiers = Seq(
"besom.util.NotProvided",
"besom.internal.Output",
"besom.internal.Context"
"besom.types.Output",
"besom.types.Context"
)

class TypeMapper(
Expand Down Expand Up @@ -58,18 +58,18 @@ object CodeGen {
case StringType => t"String"
case IntegerType => t"Int"
case NumberType => t"Double"
case UrnType => t"besom.util.Types.URN"
case ResourceIdType => t"besom.util.Types.ResourceId"
case UrnType => t"besom.types.URN"
case ResourceIdType => t"besom.types.ResourceId"
case ArrayType(elemType) => t"scala.collection.immutable.List[${asScalaType(elemType, asArgsType)}]"
case MapType(elemType) => t"scala.Predef.Map[String, ${asScalaType(elemType, asArgsType)}]"
case unionType: UnionType =>
unionType.oneOf.map(asScalaType(_, asArgsType)).reduce{ (t1, t2) => t"$t1 | $t2"}
case namedType: NamedType =>
namedType.typeUri match {
case "pulumi.json#/Archive" =>
t"besom.internal.Archive"
t"besom.types.Archive"
case "pulumi.json#/Asset" =>
t"besom.internal.Asset"
t"besom.types.Asset"
case "pulumi.json#/Any" =>
t"besom.types.PulumiAny"
case "pulumi.json#/Json" =>
Expand Down Expand Up @@ -277,12 +277,12 @@ object CodeGen {
val argsClassName = Type.Name(argsClassCoordinates.className).syntax

val baseFileImports = makeImportStatements(commonImportedIdentifiers ++ Seq(
"besom.internal.Decoder"
"besom.types.Decoder"
))

val argsFileImports = makeImportStatements(commonImportedIdentifiers ++ Seq(
"besom.internal.Encoder",
"besom.internal.ArgsEncoder"
"besom.types.Encoder",
"besom.types.ArgsEncoder"
))

val objectProperties = {
Expand Down Expand Up @@ -417,23 +417,23 @@ object CodeGen {
val baseFileImports = {
val conditionalIdentifiers =
if (isProvider)
Seq("besom.internal.ProviderResource")
Seq("besom.types.ProviderResource")
else
Seq("besom.internal.CustomResource")
Seq("besom.types.CustomResource")
val unconditionalIdentifiers = Seq(
"besom.internal.ResourceDecoder",
"besom.internal.CustomResourceOptions",
"besom.util.NonEmptyString",
"besom.types.ResourceDecoder",
"besom.types.CustomResourceOptions",
"besom.types.NonEmptyString",
)
makeImportStatements(commonImportedIdentifiers ++ conditionalIdentifiers ++ unconditionalIdentifiers)
}

val argsFileImports = {
val conditionalIdentifiers =
if (isProvider)
Seq("besom.internal.ProviderArgsEncoder")
Seq("besom.types.ProviderArgsEncoder")
else
Seq("besom.internal.ArgsEncoder")
Seq("besom.types.ArgsEncoder")
makeImportStatements(commonImportedIdentifiers ++ conditionalIdentifiers)
}

Expand Down
28 changes: 28 additions & 0 deletions core/src/main/scala/besom/aliases.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package besom

object aliases:
type Output[+A] = besom.internal.Output[A]
object Output extends besom.internal.OutputFactory
object OutputExtensions extends besom.internal.OutputExtensionsFactory
export OutputExtensions.*
type Context = besom.internal.Context
type Config = besom.internal.Config
object Config extends besom.internal.ConfigFactory
type Logger = besom.internal.logging.UserLoggerFactory
object Logger extends besom.internal.logging.UserLoggerFactory
type NonEmptyString = besom.util.NonEmptyString
object NonEmptyString extends besom.util.NonEmptyStringFactory
object NonEmptyStringExtensions extends besom.util.NonEmptyStringExtensionsFactory
export NonEmptyStringExtensions.*
type Decoder[A] = besom.internal.Decoder[A]
type Encoder[A] = besom.internal.Encoder[A]
type ArgsEncoder[A] = besom.internal.ArgsEncoder[A]
type ProviderArgsEncoder[A] = besom.internal.ProviderArgsEncoder[A]
type ResourceDecoder[A <: besom.internal.Resource] = besom.internal.ResourceDecoder[A]
type ProviderResource = besom.internal.ProviderResource
type CustomResource = besom.internal.CustomResource
type CustomResourceOptions = besom.internal.CustomResourceOptions
object CustomResourceOptions extends besom.internal.CustomResourceOptionsFactory
type ComponentBase = besom.internal.ComponentBase
type ComponentResource = besom.internal.ComponentResource
type RegistersOutputs[A <: ComponentResource & Product] = besom.internal.RegistersOutputs[A]
6 changes: 3 additions & 3 deletions core/src/main/scala/besom/future.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import scala.concurrent.*

trait FutureMonadModule extends BesomModule:
override final type Eff[+A] = scala.concurrent.Future[A]
given ExecutionContext = scala.concurrent.ExecutionContext.global
protected val rt: Runtime[Future] = FutureRuntime()
given ExecutionContext = scala.concurrent.ExecutionContext.global
protected lazy val rt: Runtime[Future] = FutureRuntime()

given Result.ToFuture[Eff] = new Result.ToFuture[Future]:
def eval[A](fa: => Future[A]): () => Future[A] = () => fa

object Pulumi extends FutureMonadModule
export Pulumi.*
export Pulumi.{ *, given }
42 changes: 21 additions & 21 deletions core/src/main/scala/besom/internal/BesomModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ package besom.internal

import com.google.protobuf.struct.*
import besom.internal.logging.{LocalBesomLogger => logger, BesomLogger}
import besom.util.NonEmptyString
import besom.util.Types.ResourceType

trait BesomModule extends BesomSyntax:
trait EffectBesomModule extends BesomSyntax:
type Eff[+A]

protected val rt: Runtime[Eff]

object Output extends OutputFactory
protected lazy val rt: Runtime[Eff]

def run(program: Context ?=> Output[Exports]): Unit =
val everything: Result[Unit] = Result.scoped {
for
_ <- BesomLogger.setupLogger()
runInfo <- RunInfo.fromEnv
_ <- logger.info(s"Besom starting up in ${if runInfo.dryRun then "dry run" else "live"} mode.")
taskTracker <- TaskTracker()
monitor <- Result.resource(Monitor(runInfo.monitorAddress))(_.close())
engine <- Result.resource(Engine(runInfo.engineAddress))(_.close())
_ <- logger.info(s"Established connections to monitor and engine, spawning streaming pulumi logger.")
logger <- Result.resource(BesomLogger(engine, taskTracker))(_.close())
config <- Config(runInfo.project)
_ <- BesomLogger.setupLogger()
runInfo <- RunInfo.fromEnv
_ <- logger.info(s"Besom starting up in ${if runInfo.dryRun then "dry run" else "live"} mode.")
taskTracker <- TaskTracker()
monitor <- Result.resource(Monitor(runInfo.monitorAddress))(_.close())
engine <- Result.resource(Engine(runInfo.engineAddress))(_.close())
_ <- logger.info(s"Established connections to monitor and engine, spawning streaming pulumi logger.")
logger <- Result.resource(BesomLogger(engine, taskTracker))(_.close())
config <- Config(runInfo.project)
featureSupport <- FeatureSupport(monitor)
_ <- logger.info(s"Resolved feature support, spawning context and executing user program.")
ctx <- Result.resource(Context(runInfo, taskTracker, monitor, engine, logger, featureSupport, config))(
_.close()
)
userOutputs <- program(using ctx).map(_.toResult).getValueOrElse(Result.pure(Struct()))
- <- Stack.registerStackOutputs(runInfo, userOutputs)(using ctx)
_ <- ctx.waitForAllTasks
ctx <- Result.resource(Context(runInfo, taskTracker, monitor, engine, logger, featureSupport, config))(_.close())
userOutputs <- program(using ctx).map(_.toResult).getValueOrElse(Result.pure(Struct()))
_ <- Stack.registerStackOutputs(runInfo, userOutputs)(using ctx)
_ <- ctx.waitForAllTasks
yield ()
}

rt.unsafeRunSync(everything.run(using rt)) match
case Left(err) => throw err
case Right(_) => sys.exit(0)

trait BesomModule extends EffectBesomModule {
export besom.types.{ *, given }
export besom.util.interpolator.{ *, given }
export besom.util.{ -> }
}
4 changes: 2 additions & 2 deletions core/src/main/scala/besom/internal/BesomSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package besom.internal
import besom.internal.logging.BesomLogger
import scala.reflect.Typeable
import besom.util.NonEmptyString
import besom.util.Types.ResourceType
import besom.util.Types.URN
import besom.types.ResourceType
import besom.types.URN

/*
* This trait is the main export point that exposes Besom specific functions and types to the user.
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/besom/internal/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,6 @@ object Config:
configSecretKeys <- Result.evalTry(Env.getConfigSecretKeys(EnvConfigSecretKeys))
config <- Config(projectName, configMap, configSecretKeys)
yield config

trait ConfigFactory:
def apply(projectName: NonEmptyString)(using Context): Output[Config] = Output(Config(projectName))
3 changes: 2 additions & 1 deletion core/src/main/scala/besom/internal/Context.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package besom.internal

import com.google.protobuf.struct.*
import besom.util.*, Types.*
import besom.util.*
import besom.types.{ ResourceType, URN, Label, ProviderType }
import besom.internal.logging.*
import scala.annotation.implicitNotFound
import besom.internal.ComponentResource
Expand Down
16 changes: 13 additions & 3 deletions core/src/main/scala/besom/internal/Output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,19 @@ class Output[+A] private[internal] (using private[besom] val ctx: Context)(
/** These factory methods should be the only way to create Output instances in user code!
*/
trait OutputFactory:
def eval[F[_]: Result.ToFuture, A](value: F[A])(using ctx: Context): Output[A] = Output.eval(value)
def apply[A](value: A)(using ctx: Context): Output[A] = Output(value)
def secret[A](value: A)(using ctx: Context): Output[A] = Output.secret(value)
def eval[F[_]: Result.ToFuture, A](value: F[A])(using Context): Output[A] = Output.eval(value)
def apply[A](value: A)(using Context): Output[A] = Output(value)
def secret[A](value: A)(using Context): Output[A] = Output.secret(value)
def sequence[A](coll: List[Output[A]])(using Context): Output[List[A]] = Output.sequence(coll)
def traverse[A, B](coll: List[A])(f: A => Output[B])(using Context): Output[List[B]] =
sequence(coll.map(f))
trait OutputExtensionsFactory:
implicit final class OutputSequenceOps[A](coll: List[Output[A]]):
def sequence(using Context): Output[List[A]] =
Output.sequence(coll)
implicit final class OutputTraverseOps[A](coll: List[A]):
def traverse[B](f: Any => Output[B])(using Context): Output[List[B]] =
coll.map(f).sequence

object Output:
// should be NonEmptyString
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/besom/internal/RawResourceResult.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package besom.internal

import com.google.protobuf.struct.Struct
import besom.util.Types.*
import besom.types.*

case class RawResourceResult(urn: URN, id: Option[ResourceId], data: Struct, dependencies: Map[String, Set[Resource]])

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/besom/internal/Resource.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package besom.internal

import besom.util.NonEmptyString
import besom.util.Types.*
import besom.types.*
import com.google.protobuf.struct.*
import scala.quoted.*
import scala.deriving.Mirror
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/besom/internal/ResourceDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.google.protobuf.struct.{Struct, Value}
import scala.quoted.*
import scala.deriving.Mirror
import besom.internal.logging.*
import besom.util.Types.*
import besom.types.{ Label, URN, ResourceId }
import org.checkerframework.checker.units.qual.C

trait ResourceDecoder[A <: Resource]: // TODO rename to something more sensible
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/besom/internal/ResourceOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.google.protobuf.struct.*
import pulumirpc.resource.*
import pulumirpc.resource.RegisterResourceRequest.PropertyDependencies

import besom.util.*, Types.*
import besom.util.*
import besom.types.*
import besom.internal.logging.*
import fansi.Str

Expand Down
37 changes: 37 additions & 0 deletions core/src/main/scala/besom/internal/ResourceOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ final case class StackReferenceResourceOptions private[internal] (
CommonResourceOptions:
export common.*

trait CustomResourceOptionsFactory:
def apply(using Context)(
parent: Resource | NotProvided = NotProvided,
dependsOn: Output[List[Resource]] = Output(List.empty[Resource]),
deletedWith: Resource | NotProvided = NotProvided,
protect: Boolean = false,
ignoreChanges: List[String] = List.empty,
version: NonEmptyString | NotProvided = NotProvided, // TODO? UGLY AF
provider: ProviderResource | NotProvided = NotProvided,
customTimeouts: String | NotProvided = NotProvided, // CustomTimeouts // TODO
// resourceTransformations: List[ResourceTransformation], // TODO
// aliases: List[Output[Alias]], // TODO
urn: String | NotProvided = NotProvided, // TODO better type
replaceOnChanges: List[String] = List.empty, // TODO?
retainOnDelete: Boolean = false,
pluginDownloadUrl: String | NotProvided = NotProvided,
deleteBeforeReplace: Boolean = false,
additionalSecretOutputs: List[String] = List.empty,
importId: NonEmptyString | NotProvided = NotProvided
): CustomResourceOptions = CustomResourceOptions.apply(
parent = parent,
dependsOn = dependsOn,
deletedWith = deletedWith,
protect = protect,
ignoreChanges = ignoreChanges,
version = version,
provider = provider,
customTimeouts = customTimeouts,
urn = urn,
replaceOnChanges = replaceOnChanges,
retainOnDelete = retainOnDelete,
pluginDownloadUrl = pluginDownloadUrl,
deleteBeforeReplace = deleteBeforeReplace,
additionalSecretOutputs = additionalSecretOutputs,
importId = importId
)

object CustomResourceOptions:
def apply(using Context)(
parent: Resource | NotProvided = NotProvided,
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/besom/internal/ResourceState.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package besom.internal

import besom.util.*, Types.*
import besom.util.*
import besom.types.*

sealed trait ResourceState:
def children: Set[Resource]
Expand Down
17 changes: 0 additions & 17 deletions core/src/main/scala/besom/internal/assets.scala

This file was deleted.

5 changes: 2 additions & 3 deletions core/src/main/scala/besom/internal/codecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import scala.deriving.Mirror
import com.google.protobuf.struct.*, Value.Kind
import besom.internal.ProtobufUtil.*
import scala.util.*
import besom.util.*, Types.Label
import besom.util.*
import besom.types.*
import Asset.*
import Archive.*
import org.checkerframework.checker.units.qual.s
import org.checkerframework.checker.units.qual.m
import besom.util.Types.URN
import besom.util.Types.ResourceId

object Constants:
final val UnknownValue = "04da6b54-80e4-46f7-96ec-b56ff0331ba9"
Expand Down
Loading

0 comments on commit 05f8a03

Please sign in to comment.