From a48ee68d17cc24eb5a8414e1a677fde295449ffe Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Fri, 11 Jan 2013 16:01:30 -0500 Subject: [PATCH] Additional convenience methods on Project for configuring the .sbt files processed. The Project.setSbtFiles and addSbtFiles methods combined with the current sbt version available from ComponentMangaer.version (suggestions for a better location are welcome), should make it straightforward to load different .sbt files based on the sbt version. Fixes #467. --- main/src/main/scala/sbt/AddSettings.scala | 23 +++++++++++++++++++++++ main/src/main/scala/sbt/Project.scala | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/main/src/main/scala/sbt/AddSettings.scala b/main/src/main/scala/sbt/AddSettings.scala index bf509255a..2d698b874 100644 --- a/main/src/main/scala/sbt/AddSettings.scala +++ b/main/src/main/scala/sbt/AddSettings.scala @@ -34,5 +34,28 @@ object AddSettings def seq(autos: AddSettings*): AddSettings = new Sequence(autos) val allDefaults: AddSettings = seq(userSettings, allPlugins, defaultSbtFiles) + + /** Combines two automatic setting configurations. */ + def append(a: AddSettings, b: AddSettings): AddSettings = (a,b) match { + case (sa: Sequence, sb: Sequence) => seq(sa.sequence ++ sb.sequence : _*) + case (sa: Sequence, _) => seq(sa.sequence :+ b : _*) + case (_, sb: Sequence) => seq(a +: sb.sequence : _*) + case _ => seq(a,b) + } + + def clearSbtFiles(a: AddSettings): AddSettings = tx(a) { + case _: DefaultSbtFiles | _: SbtFiles => None + case x => Some(x) + } getOrElse seq() + + private[sbt] def tx(a: AddSettings)(f: AddSettings => Option[AddSettings]): Option[AddSettings] = a match { + case s: Sequence => + s.sequence.flatMap { b => tx(b)(f) } match { + case Seq() => None + case Seq(x) => Some(x) + case ss => Some(new Sequence(ss)) + } + case x => f(x) + } } diff --git a/main/src/main/scala/sbt/Project.scala b/main/src/main/scala/sbt/Project.scala index 9a53ce434..fa6eb53b9 100755 --- a/main/src/main/scala/sbt/Project.scala +++ b/main/src/main/scala/sbt/Project.scala @@ -107,6 +107,14 @@ sealed trait Project extends ProjectDefinition[ProjectReference] /** Configures how settings from other sources, such as .sbt files, are appended to the explicitly specified settings for this project. */ def autoSettings(select: AddSettings*): Project = copy(auto = AddSettings.seq(select : _*)) + + /** Adds a list of .sbt files whose settings will be appended to the settings of this project. + * They will be appended after the explicit settings and already defined automatic settings sources. */ + def addSbtFiles(files: File*): Project = copy(auto = AddSettings.append(auto, AddSettings.sbtFiles(files: _*)) ) + + /** Sets the list of .sbt files to parse for settings to be appended to this project's settings. + * Any configured .sbt files are removed from this project's list.*/ + def setSbtFiles(files: File*): Project = copy(auto = AddSettings.append( AddSettings.clearSbtFiles(auto), AddSettings.sbtFiles(files: _*)) ) } sealed trait ResolvedProject extends ProjectDefinition[ProjectRef]