Files
sbt/main/ProjectNavigation.scala
T

71 lines
2.3 KiB
Scala
Raw Normal View History

2011-01-22 14:01:59 -05:00
/* sbt -- Simple Build Tool
* Copyright 2008, 2009, 2010, 2011 Mark Harrah
*/
package sbt
import ProjectNavigation._
2011-02-26 23:56:30 -05:00
import Project.updateCurrent
2011-03-02 06:46:28 -05:00
import Keys.sessionSettings
2011-01-22 14:01:59 -05:00
import complete.{DefaultParsers, Parser}
import DefaultParsers._
import java.net.URI
object ProjectNavigation
{
def command(s: State): Parser[() => State] =
2011-03-02 06:46:28 -05:00
if(s get sessionSettings isEmpty) failure("No project loaded") else (new ProjectNavigation(s)).command
2011-01-22 14:01:59 -05:00
}
final class ProjectNavigation(s: State)
{
2011-01-25 22:22:11 -05:00
val extracted = Project extract s
import extracted.{currentRef, structure, session}
2011-01-22 14:01:59 -05:00
2011-01-25 07:24:52 -05:00
def setProject(nuri: URI, nid: String) =
{
val neval = if(currentRef.build == nuri) session.currentEval else mkEval(nuri)
2011-03-02 06:46:28 -05:00
updateCurrent(s.put(sessionSettings, session.setCurrent(nuri, nid, neval)))
2011-01-25 07:24:52 -05:00
}
2011-02-02 19:29:03 -05:00
def mkEval(nuri: URI) = Load.lazyEval(structure.units(nuri).unit)
2011-01-22 14:01:59 -05:00
def getRoot(uri: URI) = Load.getRootProject(structure.units)(uri)
def apply(action: Option[ResolvedReference]): State =
2011-01-22 14:01:59 -05:00
action match
{
case None => show(); s
case Some(BuildRef(uri)) => changeBuild(uri)
case Some(ProjectRef(uri, id)) => selectProject(uri, id)
2011-01-22 14:01:59 -05:00
/* else if(to.forall(_ == '.'))
if(to.length > 1) gotoParent(to.length - 1, nav, s) else s */ // semantics currently undefined
}
def show(): Unit = s.log.info(currentRef.project + " (in build " + currentRef.build + ")")
def selectProject(uri: URI, to: String): State =
2011-01-22 14:01:59 -05:00
if( structure.units(uri).defined.contains(to) )
setProject(uri, to)
else
fail("Invalid project name '" + to + "' in build " + uri + " (type 'projects' to list available projects).")
2011-01-22 14:01:59 -05:00
def changeBuild(newBuild: URI): State =
2011-01-22 14:01:59 -05:00
if(structure.units contains newBuild)
setProject(newBuild, getRoot(newBuild))
else
fail("Invalid build unit '" + newBuild + "' (type 'projects' to list available builds).")
2011-01-22 14:01:59 -05:00
def fail(msg: String): State =
{
s.log.error(msg)
2011-01-22 14:01:59 -05:00
s.fail
}
import complete.Parser._
import complete.Parsers._
val parser: Parser[Option[ResolvedReference]] =
2011-01-22 14:01:59 -05:00
{
val reference = Act.resolvedReference(structure.index.keyIndex, currentRef.build, success(()))
val root = token('/' ^^^ rootRef)
success(None) | some( token(Space) ~> (root | reference) )
2011-01-22 14:01:59 -05:00
}
def rootRef = ProjectRef(currentRef.build, getRoot(currentRef.build))
2011-01-22 15:07:59 -05:00
val command: Parser[() => State] = Command.applyEffect(parser)(apply)
2011-01-22 14:01:59 -05:00
}