Skip to content

Commit

Permalink
Deleted todo file and instructions from coffeescript-sbt-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Gurnell committed Mar 2, 2011
1 parent 7531c2f commit 97ed4cf
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 114 deletions.
55 changes: 7 additions & 48 deletions README.markdown
@@ -1,58 +1,17 @@
closure-sbt-plugin
==================

This is a [simple-build-tool](http://simple-build-tool.googlecode.com/) plugin for compiling Javascript using Google's [Closure compiler](http://code.google.com/p/closure-compiler/). It was originally forked from [this CoffeeScript plugin](https://github.com/rubbish/coffee-script-sbt-plugin). Most of the project is shameless searching-and-replacing. There are areas (such as the README below) where I haven't even gotten around to that yet.
This is a [simple-build-tool](http://simple-build-tool.googlecode.com/) plugin for compiling Javascript using Google's [Closure compiler](http://code.google.com/p/closure-compiler/). It was originally forked from [this CoffeeScript plugin](https://github.com/rubbish/coffee-script-sbt-plugin). Most of the project is shameless searching-and-replacing.

## Usage ##

Run the`compile-coffee-script` task to compile all your CoffeeScript files under `src/main/cofffee-script/` into JavaScript. For web projects the files are compiled to under `src/main/webapp/javascript`, for other projects they will compiled to `src/main/generated-javascript/`. The file structure under the source directory will be maintained.

The source and output paths can be configured via coffeeScriptDirectoryPathFinder and coffeeScriptCompiledOuputDirectory properties. i.e.

class Project(info: ProjectInfo) extends DefaultProject(info) {
override def coffeeScriptDirectoryPathFinder = "src" / "main" / "custom-coffee-script"
override def coffeeScriptCompiledOuputDirectory = "src/main/custom-generated-javascript"
...
}

See sbt's documentation on [Paths](http://code.google.com/p/simple-build-tool/wiki/Paths) for more information on the coffeeScriptDirectoryPathFinder property.

CoffeeScript files are also added the watchPaths so that triggered actions will also be invoked when any CoffeeScript file changes.

## Setup ##

1. In your plugin definition file, add a dependency on coffee-script-sbt-plugin

i.e. in `project/plugins/Plugins.scala`

import sbt._

class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
val coffeeScriptSbtRepo = "coffeeScript sbt repo" at "http://repo.coderlukes.com"
val coffeeScript = "org.coffeescript" % "coffee-script-sbt-plugin" % "0.1.0"
}

2. In your project file, mixin the ClosureCompile trait

i.e., in `project/build/Project.scala`

import sbt._
import org.coffeescript.ClosureCompile

class YourProject(info: ProjectInfo) extends DefaultProject(info) with ClosureCompile {
// ....
}

3. Optionally change the compile action to also include `closure-compile`

override def compileAction = super.compileAction dependsOn(compileCoffeeScript)

## Roadmap ##

See [TODO.org](TODO.org) for more information.
More instructions to follow.

## LICENSE ##

Copyright (c) 2011 Untyped Ltd

Based on the CoffeeScript SBT plugin:

https://github.com/rubbish/coffee-script-sbt-plugin
Copyright (c) 2010 Luke Amdor

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
32 changes: 0 additions & 32 deletions TODO.org

This file was deleted.

2 changes: 1 addition & 1 deletion project/build.properties
Expand Up @@ -4,5 +4,5 @@ project.organization=com.untyped
project.name=closure-sbt-plugin
sbt.version=0.7.4
project.version=0.1
build.scala.versions=2.8.1
build.scala.versions=2.7.7
project.initialize=false
16 changes: 12 additions & 4 deletions project/build/Project.scala
@@ -1,17 +1,25 @@
import sbt._

import java.io.File

class Project(info: ProjectInfo) extends PluginProject(info) with test.ScalaScripted {

val closure = "com.google.javascript" % "closure-compiler" % "r606"
val closure = "com.google.javascript" % "closure-compiler" % "r706"

override def scriptedSbt = "0.7.4"
override def scriptedBufferLog = false

override def testAction = testNoScripted

lazy val default = scripted dependsOn(publishLocal) describedAs("Publishes locally and tests against example projects")

// val publishTo = Resolver.file("local-s3-repo.coderlukes.com-mirror",
// new java.io.File(Path.userHome.toString, "code/repo.coderlukes.com"))

val publishTo = {
val host = System.getenv("DEFAULT_REPO_HOST")
val path = System.getenv("DEFAULT_REPO_PATH")
val user = System.getenv("DEFAULT_REPO_USER")
val keyfile = new File(System.getenv("DEFAULT_REPO_KEYFILE"))

Resolver.sftp("Default Repo", host, path).as(user, keyfile)
}

}
39 changes: 15 additions & 24 deletions src/main/scala/com/untyped/ClosureCompile.scala
Expand Up @@ -4,15 +4,14 @@ import sbt._
import scala.io.Source
import java.io.File

import com.google.javascript.jscomp.{ Compiler, CompilerOptions, JSSourceFile }

trait ClosureCompile extends BasicScalaProject {

protected def srcRoot = "src" / "main" / "javascript"


lazy val manifestPaths = (srcRoot ##) ** "*.jstarget"
lazy val srcPaths = (srcRoot ##) ** "*.js"

override def watchPaths = super.watchPaths +++ srcPaths
override def watchPaths = super.watchPaths +++ manifestPaths +++ srcPaths

protected def desRoot = {
if (mainArtifact.extension == "war") {
Expand All @@ -26,26 +25,18 @@ trait ClosureCompile extends BasicScalaProject {
"Compiles Javascript files."

def compileJsTasks = task { None } dependsOn ({
srcPaths.get.map { srcPath =>
val srcFile = srcPath.asFile
val desFile = new File(desRoot, "\\.js$".r.replaceFirstIn(srcPath.relativePath, ".js"))

log.info("Src = %s" format (srcPath.toString))
log.info("Des = %s" format (Path.fromFile(desFile).toString))

fileTask(Path.fromFile(desFile) from srcPath) {
log.info("Compiling %s" format (srcPath.relativePath))

val compiler = new Compiler
val externs = List[JSSourceFile]().toArray
val sources = List[JSSourceFile](JSSourceFile.fromFile(srcFile)).toArray
val options = new CompilerOptions

val result = compiler.compile(externs, sources, options)

new File(desFile.getParent).mkdirs()

FileUtilities.write(desFile, compiler.toSource, log)
manifestPaths.get.map { manifestPath =>

val desPath = Path.fromFile(new File(
desRoot, "\\.jstarget$".r.replaceFirstIn(manifestPath.relativePath, ".js")))

val manifest: JsManifest = new JsManifest(manifestPath, desPath)

log.info("Manifest " + manifestPath)
log.info("Destination " + desPath)

fileTask(desPath from manifestPath) {
manifest.compile(log)
}
}
}.toSeq: _*)
Expand Down
19 changes: 14 additions & 5 deletions src/main/scala/com/untyped/JsManifest.scala
Expand Up @@ -3,6 +3,7 @@ package com.untyped
import sbt._
import scala.io.Source
import java.io.File
import java.net.URL

import com.google.javascript.jscomp.{ Compiler, CompilerOptions, JSSourceFile }

Expand All @@ -29,14 +30,19 @@ class JsManifest(val manifestPath: Path, val desPath: Path) {
def isFile(line: String): Boolean =
!isSkippable(line) && !isUrl(line)

def urlToCode(url: URL): String =
Source.fromInputStream(url.openStream).mkString

def urlToFilename(url: URL): String =
url.getPath.split("/").lastOption.getOrElse("unknown.js")

def lineToSources(line: String): List[JSSourceFile] = {
if(isUrl(line)) {
Nil
val url = new URL(line)
List(JSSourceFile.fromCode(urlToCode(url), urlToFilename(url)))
} else if(isFile(line)) {
List(JSSourceFile.fromFile(Path.fromString(rootDir, line).asFile))
} else {
Nil
}
} else Nil
}

def externs(log: Logger): Array[JSSourceFile] = Nil.toArray
Expand All @@ -47,9 +53,12 @@ class JsManifest(val manifestPath: Path, val desPath: Path) {
def compile(log: Logger): Option[String] = {
val compiler = new Compiler
val options = new CompilerOptions

log.info("Source files " + sources(log).toString)

val result = compiler.compile(externs(log), sources(log), options)

log.info("Source " + compiler.toSource)
log.info("Compiled code " + compiler.toSource)

new File(desPath.asFile.getParent).mkdirs()

Expand Down

0 comments on commit 97ed4cf

Please sign in to comment.