Skip to content

Commit

Permalink
Merge pull request #19 from filinep/0.12
Browse files Browse the repository at this point in the history
Update for sbt 0.12
  • Loading branch information
remeniuk committed May 2, 2014
2 parents ff7a7f2 + 7516e6e commit 582e758
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 88 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Expand Up @@ -2,13 +2,13 @@ organization := "org.netbeans"

name := "sbt-netbeans-plugin"

version := "0.1.4"
version := "0.1.5"

sbtPlugin := true

resolvers ++= Seq(ScalaToolsSnapshots, "Typesafe Repo" at "http://repo.typesafe.com/typesafe")

libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.3"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.4"

publishArtifact in (Compile, packageDoc) := false

Expand Down
132 changes: 66 additions & 66 deletions src/main/scala/netbeans/NetbeansPlugin.scala
Expand Up @@ -6,141 +6,141 @@ import project._
import CommandSupport._

object NetbeansPlugin extends Plugin {

val sbtExecutable = SettingKey[String]("sbt-executable")

/** build.xml template under ./sbt/plugins (overrides the template in plugin jar) */
private val buildXmlTemplateLocation = BuildPaths.defaultGlobalPlugins(BuildPaths.defaultGlobalBase) / "src" / "main" / "resources" / "build.xml"
private val buildXmlTemplateLocation = (state: State) => BuildPaths.getGlobalPluginsDirectory(state, BuildPaths.defaultGlobalBase) / "src" / "main" / "resources" / "build.xml"
/** project.properties template under ./sbt/plugins (overrides the template in plugin jar) */
private val projectFilesTemplateLocation = BuildPaths.defaultGlobalPlugins(BuildPaths.defaultGlobalBase) / "src" / "main" / "resources" / "nbproject"
private val projectFilesTemplateLocation = (state: State) => BuildPaths.getGlobalPluginsDirectory(state, BuildPaths.defaultGlobalBase) / "src" / "main" / "resources" / "nbproject"

/** Extracts Netbeans project files templates from the plugin jar */
private def copyNetbeansFiles(basePath: File)(pluginJarPath: File) =
IO.unzip(pluginJarPath, basePath, "*.xml" | "*.properties")
private def copyNetbeansFiles(basePath: File)(pluginJarPath: File) =
IO.unzip(pluginJarPath, basePath, "*.xml" | "*.properties")

/** Copies packed Netbeans project files templates to the project folder */
private def copyPackedTemplates(libClasspath: Seq[File], dest: File) =
private def copyPackedTemplates(libClasspath: Seq[File], dest: File) =
libClasspath.filter(_.getName.contains("sbt-netbeans-plugin"))
.headOption.map(copyNetbeansFiles(dest))
.headOption.map(copyNetbeansFiles(dest))

/** Copies Netbeans project files templates from ./sbt/plugins */
private def copyUnpackedTemplates(dest: File) = {
buildXmlTemplateLocation.get.map { template =>
IO.copy(Seq(template.asFile -> (dest / "build.xml").asFile), false)
private def copyUnpackedTemplates(dest: File, state: State) = {
buildXmlTemplateLocation(state).get.map { template =>
IO.copy(Seq(template.asFile -> (dest / "build.xml").asFile), false)
}
projectFilesTemplateLocation(state).get.map { template =>
IO.copyDirectory(template.asFile, (dest / "nbproject").asFile, false)
}
projectFilesTemplateLocation.get.map { template =>
IO.copyDirectory(template.asFile, (dest / "nbproject").asFile, false)
}
}

/** Adds sbt-netbeans commands globally */
override lazy val settings = Seq(commands += netbeansCommands)

/** Updates Netbeans project files with SBT project settings */
private[netbeans] def updateNetbeansFiles(projectRef: ProjectRef,
s: State,
private[netbeans] def updateNetbeansFiles(projectRef: ProjectRef,
s: State,
projectFiles: Seq[ProjectContext => NetbeansConfigFile]): State = {

import scalaz.Scalaz._
logger(s).info("Updating Netbeans files for project `%s`..." format(projectRef.project))

s.log.info("Updating Netbeans files for project `%s`..." format(projectRef.project))

ProjectContext.netbeansContext(projectRef, s).map{context =>
projectFiles.map(_(context)).foreach{ projectFile =>
projectFile.validate.fold ({ errors =>
logger(s).error("%s: failed to update %s"
.format(projectRef.project, projectFile.description))
}, { _ =>
projectFile.validate.fold ({ errors =>
s.log.error("%s: failed to update %s"
.format(projectRef.project, projectFile.description))
}, { _ =>
projectFile.store()
logger(s).info("%s: successfully updated %s"
s.log.info("%s: successfully updated %s"
.format(projectRef.project, projectFile.description))
}
}
)
}
}
}

s
}

/** Updates all Netbeans project files */
private[netbeans] def updateAll(projectRef: ProjectRef)(s: State): State =
updateNetbeansFiles(projectRef, s,
Seq((context => AntScript(context.baseDirectory / "build.xml")(context)),
private[netbeans] def updateAll(projectRef: ProjectRef)(s: State): State =
updateNetbeansFiles(projectRef, s,
Seq((context => AntScript(context.baseDirectory / "build.xml")(context)),
(context => ProjectConfiguration(context.baseDirectory / "nbproject" /"project.xml")(context)),
(context => ProjectProperties(context.baseDirectory / "nbproject" / "project.properties")(context)))
)

/** Updates project.properties */
private[netbeans] def updateProjectProperties(projectRef: ProjectRef)(s: State): State =
private[netbeans] def updateProjectProperties(projectRef: ProjectRef)(s: State): State =
updateNetbeansFiles(projectRef, s, Seq((context => ProjectProperties(context.baseDirectory / "nbproject" /"project.properties")(context))))

/** Updates project.xml */
private[netbeans] def updateProjectConfig(projectRef: ProjectRef)(s: State): State =
private[netbeans] def updateProjectConfig(projectRef: ProjectRef)(s: State): State =
updateNetbeansFiles(projectRef, s, Seq((context => ProjectConfiguration(context.baseDirectory / "nbproject" /"project.xml")(context))))

/** Creates empty source/resource directories */
private[netbeans] def createSourceDirectories(projectRef: ProjectRef)(s: State): State = {
private[netbeans] def createSourceDirectories(projectRef: ProjectRef)(s: State): State = {
ProjectContext.netbeansContext(projectRef, s).map{context =>
logger(s).info("Creating empty source directories for project `%s`..." format(projectRef.project))
s.log.info("Creating empty source directories for project `%s`..." format(projectRef.project))

IO.createDirectories(Seq(
context.baseDirectory / "src" / "main" / "scala",
context.baseDirectory / "src" / "test" / "scala",
context.baseDirectory / "src" / "main" / "resources",
context.baseDirectory / "src" / "test" / "resources"
))
))
}
s
}
}

/** Adds Netbeans project files to the SBT project */
private[netbeans] def createNetbeansFiles(projectRef: ProjectRef)(s: State): State = {
val extracted = Project extract s
import extracted._
logger(s).info("Creating Netbeans files for project `%s`" format(projectRef.project))
for{base <- baseDirectory in (projectRef, Compile) get structure.data}{

s.log.info("Creating Netbeans files for project `%s`" format(projectRef.project))
for{base <- baseDirectory in (projectRef, Compile) get structure.data}{
copyPackedTemplates(currentUnit.unit.plugins.classpath,
base)
copyUnpackedTemplates(base)
copyUnpackedTemplates(base, s)
}

if(session.original.filter(_.key.key.label == NetbeansTasks.updateDepTaskKey).isEmpty){
logger(s).info("Writing plugin settings for project `%s`" format(projectRef.project))
NetbeansTasks.writePluginSettings(projectRef, structure)
s.log.info("Writing plugin settings for project `%s`" format(projectRef.project))
NetbeansTasks.writePluginSettings(projectRef, structure)
s.reload
} else s
}

/** Removes Netbeans project files from the SBT project */
private[netbeans] def removeNetbeansFiles(projectRef: ProjectRef)(s: State): State = {
val extracted = Project extract s
import extracted._
logger(s).info("Removing Netbeans files from project `%s`" format(projectRef.project))

s.log.info("Removing Netbeans files from project `%s`" format(projectRef.project))
(baseDirectory in (projectRef, Compile) get structure.data) map { base =>
IO.delete((base / "build.xml" +++ base / "nbproject").get)
}

s
}

type NetbeansCommand = ProjectRef => State => State
lazy val netbeansCommands =

lazy val netbeansCommands =
Command("netbeans")(_ => NetbeansCommands.netbeansConsole) { (state: State, output: Any) =>
output match {
case (cmd: NetbeansCommand, transitive: Boolean) =>
case (cmd: NetbeansCommand, transitive: Boolean) =>
val extracted = Project extract state
val s = cmd(extracted.currentRef)(state)
if(transitive){
logger(state).info("Executing command transitively...")
state.log.info("Executing command transitively...")
(s /: extracted.currentProject.uses) {(_s, _ref) => cmd(_ref)(_s)}
} else s
case other =>
logger(state).error("Failed to process command line: " + other)
state.fail
case other =>
state.log.error("Failed to process command line: " + other)
state.fail
}
}

}
40 changes: 20 additions & 20 deletions src/main/scala/netbeans/NetbeansTasks.scala
Expand Up @@ -13,46 +13,46 @@ import CommandSupport._
import NetbeansPlugin._

object NetbeansTasks {

val exportedSettings = Seq("seq(netbeans.NetbeansTasks.netbeansSettings:_*)")

val updateDepTaskKey = "netbeans-update-dependencies"
val netbeansUpdateDependencies = TaskKey[Any](updateDepTaskKey)

val netbeansUpdateDependencies = TaskKey[Any](updateDepTaskKey)

val netbeansSettings = Seq(
sbtExecutable := "sbt",
netbeansUpdateDependencies <<= updateProjectPropertiesTask triggeredBy(update)
)
)

/** Updates project properties, if project classpath has changed */
def updateProjectPropertiesTask = (baseDirectory, fullClasspath in Compile, thisProjectRef, state) map { (base, classpath, projectRef, s) =>
val pluginCachePath = base / "nbproject" / "netbeans.cache"
logger(s).info("Triggered update dependencies task for proect %s" format(projectRef))

s.log.info("Triggered update dependencies task for proect %s" format(projectRef))

val cachedClasspath = (pluginCachePath.get.map{ path =>
val classpathCache = IO.readLines(path, Charset.defaultCharset).mkString(":")
logger(s).info("Cache file exists: %s" format(classpathCache))
s.log.info("Cache file exists: %s" format(classpathCache))
classpathCache
}).headOption.getOrElse("")

val currentClasspath = classpath.files.mkString(":")

if(currentClasspath != cachedClasspath){
logger(s).info("Updating classpath: %s" format(currentClasspath))
s.log.info("Updating classpath: %s" format(currentClasspath))
IO.write(pluginCachePath, currentClasspath.getBytes)
updateProjectProperties(projectRef)(s)
}
}
}
}

/** Persists plugin settings in SBT project config */
def writePluginSettings(pref: ProjectRef, structure: sbt.Load.BuildStructure) = {
val project = Project.getProject(pref, structure).getOrElse(error("Invalid project reference " + pref))
def writePluginSettings(pref: ProjectRef, structure: sbt.Load.BuildStructure) = {
val project = Project.getProject(pref, structure).getOrElse(sys.error("Invalid project reference " + pref))
val appendTo: File = BuildPaths.configurationSources(project.base).headOption.getOrElse(new File(project.base, "build.sbt"))
val baseAppend = exportedSettings.flatMap("" :: _ :: Nil)
val adjustedLines = if(appendTo.isFile && SessionSettings.hasTrailingBlank(IO readLines appendTo) ) baseAppend else "" +: baseAppend
val adjustedLines = if(appendTo.isFile && !SessionSettings.needsTrailingBlank(IO readLines appendTo) ) baseAppend else "" +: baseAppend
IO.writeLines(appendTo, adjustedLines, append = true)
}

}

0 comments on commit 582e758

Please sign in to comment.