Skip to content

Instantly share code, notes, and snippets.

@jeffdyke
Created May 10, 2018 15:44
Show Gist options
  • Save jeffdyke/e9db82a4c0034360312b02dac6ecec44 to your computer and use it in GitHub Desktop.
Save jeffdyke/e9db82a4c0034360312b02dac6ecec44 to your computer and use it in GitHub Desktop.
Failing to find main method
import $ivy.`com.chuusai::shapeless:2.3.3`
import scala.math.Ordering
import scala.collection.immutable.SortedSet
import scala.language.implicitConversions
import scala.language.reflectiveCalls
object ADT {
object Members {
implicit def conv[T](self: this.type)(implicit v: MkMembers[T]): Set[T] = Members[T]
def apply[T](implicit v: MkMembers[T]): Set[T] = v.members.toSet
trait MkMembers[T] { def members: List[T] }
object MkMembers {
import shapeless.{:+:, Coproduct, CNil, Generic, Witness}
implicit def members[T, Repr <: Coproduct](implicit gen: Generic.Aux[T, Repr], v: Aux[T, Repr]): MkMembers[T] =
new MkMembers[T] { def members = v.members }
trait Aux[T, Repr] { def members: List[T] }
object Aux {
implicit def cnilAux[A]: Aux[A, CNil] =
new Aux[A, CNil] { def members = Nil }
implicit def cconsAux[T, L <: T, R <: Coproduct](implicit l: Witness.Aux[L], r: Aux[T, R]): Aux[T, L :+: R] =
new Aux[T, L :+: R] { def members = l.value :: r.members }
}
}
}
implicit def orderingById[T <: {val id: Int}]: Ordering[T] = Ordering.by[T, Int](_.id)
def sortById[T <: {val id: Int}](v: Set[T]): SortedSet[T] = SortedSet(v.toList: _*)
def sortBy[T, U: Ordering](v: Set[T])(f: T => U): SortedSet[T] = SortedSet(v.toList: _*)(Ordering.by(f))
}
import $file.ADT
import ADT.ADT
class Foo(vpc: String, name: String) {
def stop(vpc: String, name: String) = "Sopt called"
def reboot(vpc: String, name: String) = "reboot called"
def start(vpc: String, name: String) = "start called"
def terminate(vpc: String, name: String) = "terminate called"
def attributes(vpc: String, name: String) = "reboot called"
}
object Actions {
sealed trait Action {
val id: Int
def call(vpc: String, env: String, name: String): String
}
case object Create extends Action {
val id = 1
def call(vpc: String, env: String, name: String): String = {
s"$name has been created"
}
}
val actions: Set[Action] = ADT.Members[Action]
def findByName(action: String) = actions.find(_.toString.toLowerCase == action.toLowerCase)
}
@main
def job(action: String, envName: String, server: String): String = {
Actions.findByName(action).map(a =>
a.call(envName, envName, server)).getOrElse(s"Could find action matching $action, possible values ${Actions.actions.map(_.toString.toLowerCase)}")
}
@jeffdyke
Copy link
Author

putting the job function in another file, works perfectly fine. this version fails to find the main method

amm test.sc create foo bar
Compiling /Users/jeff/minimal/test.sc
Script test.sc does not take arguments: "create" "foo" "bar"

@jeffdyke
Copy link
Author

jeffdyke commented May 10, 2018

in the repl it is

jeff-minimal@ import $file.ADT
import $file.$

jeff-minimal@ import ADT.ADT
import ADT.ADT
jeff-minimal@ import $file.test
Compiling /Users/jeff/minimal/test.sc
import $file.$
jeff-minimal@ test.job("create", "foo", "bar")
res3: String = "bar has been created"

@jeffdyke
Copy link
Author

For anyone stumbling upon this, it is not ammonite specific. Imports with shapeless, need to be contained, so if you wanted to use code like this, you'd need to create an entry point, e.g. main.sc that took in arguments for one or more functions, it doesn't import ADT.sc only the files that need it do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment