Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Pull request for issue #7 #11

Closed
wants to merge 4 commits into from

2 participants

@kastigar

Looks like a bug in less-rhino part. I've found a solution. Also i'm including test-case for this issue.

@softprops
Owner

whoa sweet! A scripted test and everything. Thanks!. I'll check this one out tonight.

@kastigar

Oops, smth goes wrong. Will recreate pull request

@kastigar kastigar closed this
@kastigar kastigar reopened this
@kastigar

Hm. strange pull requesting, it proposes all commits. Last two commits are not for issue #7.
ac042a4 - better error handling. Now it shows correct filename if error occurred. The problem was for imported files, it doesn't display file where error occurred.
1966943 - there are missing resolvers for ls-sbt, according to this

@kastigar kastigar commented on the diff
src/main/scala/less.scala
((6 lines not shown))
private def css(sources: File, less: File, targetDir: File) =
Some(new File(targetDir, IO.relativize(sources, less).get.replace(".less",".css")))
- private def compileSources(compiler: Compiler, mini: Boolean, charset: Charset, out: Logger)(pair: (File, File)) =
- try {
- val (less, css) = pair
- out.debug("Compiling %s" format less)
- compiler.compile(less.getPath, io.Source.fromFile(less)(
- io.Codec(charset)).mkString, mini).fold({ err =>
@kastigar
kastigar added a note

in my case io.codec is missing, but actually it has no sense because of imports. I mean imported files will be read without recoding. So if this feature really required, you should provide correct readFile function into js environment!!!

@softprops Owner

Can you make a pull request with just the path fixes? I don't know how I feel about this change. I had received a bug report before about having utf8 sources compiled to invalid characters without this. I don't want to re-introduce the same issue.

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

yahoo, i understood how to request specified commits. Sorry for inconvenience. Pull request reopened.

@kastigar kastigar closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 2, 2012
  1. @kastigar

    Test-case for issue #7

    kastigar authored
  2. @kastigar
  3. @kastigar

    Better error handling

    kastigar authored
  4. @kastigar

    Add missing resolvers

    kastigar authored
This page is out of date. Refresh to see the latest.
View
4 project/plugins.sbt
@@ -1,3 +1,7 @@
+resolvers += "ls-sbt-resolver-0" at "http://repo.lessis.me"
+
+resolvers += "ls-sbt-resolver-1" at "http://repo.codahale.com"
+
libraryDependencies <+= sbtVersion(v=>
"org.scala-tools.sbt" %% "scripted-plugin" % v
)
View
30 src/main/resources/less-rhino-1.1.5.js
@@ -2436,14 +2436,21 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
var sheetName = name.slice(0, name.lastIndexOf('/') + 1) + sheet.href;
var input = rshellGlobal.readFile(sheetName);
var parser = new less.Parser();
- parser.parse(input, function (e, root) {
- if (e) {
- print("Error: " + e);
- rshellGlobal.quit(1);
- }
- callback(root, sheet, { local: false, lastModified: 0, remaining: remaining });
- });
+ var savedName = name;
+ name = sheetName;
+ try {
+ parser.parse(input, function (e, root) {
+ if (e) { throw e; }
+ callback(root, sheet, { local: false, lastModified: 0, remaining: remaining });
+ });
+ } catch (e) {
+ throw {
+ message: e.message || "Unknown error",
+ filename: name
+ };
+ }
+ name = savedName;
// callback({}, sheet, { local: true, remaining: remaining });
}
@@ -2457,12 +2464,13 @@ function writeFile(filename, content) {
// removed origilnal cmdline argument handling function
// with this function for convenient scoped access to the
// less.Parser function
-function compile(scriptName, code, min) {
+function compile(scriptName, min) {
name = scriptName;
var css = null;
- new less.Parser().parse(code, function (e, root) {
- if(e) { throw e; }
- css = root.toCSS({ compress: min || false })
+
+ loadStyleSheet({ href: scriptName.split("/").pop() }, function(root) {
+ css = root.toCSS({ compress: min || false });
});
+
return css;
}
View
19 src/main/scala/compiler.scala
@@ -48,17 +48,22 @@ abstract class Compiler(src: String) extends ShellEmulation {
val utf8 = Charset.forName("utf-8")
- def compile(name: String, code: String, mini: Boolean = false): Either[String, String] = withContext { ctx =>
+ def compile(name: String, mini: Boolean = false)(f: String => Unit) = withContext { ctx =>
try {
val less = scope.get("compile", scope).asInstanceOf[Callable]
- Right(less.call(ctx, scope, scope, Array(name, code, mini.asInstanceOf[AnyRef])).toString)
+ f(less.call(ctx, scope, scope, Array(name, mini.asInstanceOf[AnyRef])).toString)
} catch {
- case e : JavaScriptException =>
- e.getValue match {
- case v: Scriptable =>
- Left(ScriptableObject.getProperty(v, "message").toString)
- case v => sys.error("unknown exception value type %s" format v)
+ case e : JavaScriptException => e.getValue match {
+ case v: Scriptable => {
+ val fileName = ScriptableObject.getProperty(v, "filename").toString
+ val message = ScriptableObject.getProperty(v, "message").toString
+ sys.error("error occured while compiling %s: %s" format (fileName, message))
}
+ case v => sys.error("error occured while compiling %s: unknown js exception value type %s" format (name, v))
+ }
+ case e => sys.error(
+ "error occured while compiling %s: unexpected exception %s: %s" format (name, e.getClass, e.getMessage)
+ )
}
}
View
25 src/main/scala/less.scala
@@ -17,28 +17,19 @@ object Plugin extends sbt.Plugin {
}
/** name is required as a reference point for importing relative dependencies within less */
- type Compiler = { def compile(name: String, src: String, mini: Boolean): Either[String, String] }
+ type Compiler = { def compile(name: String, mini: Boolean)(f: String => Unit): Unit }
private def css(sources: File, less: File, targetDir: File) =
Some(new File(targetDir, IO.relativize(sources, less).get.replace(".less",".css")))
- private def compileSources(compiler: Compiler, mini: Boolean, charset: Charset, out: Logger)(pair: (File, File)) =
- try {
- val (less, css) = pair
- out.debug("Compiling %s" format less)
- compiler.compile(less.getPath, io.Source.fromFile(less)(
- io.Codec(charset)).mkString, mini).fold({ err =>
@kastigar
kastigar added a note

in my case io.codec is missing, but actually it has no sense because of imports. I mean imported files will be read without recoding. So if this feature really required, you should provide correct readFile function into js environment!!!

@softprops Owner

Can you make a pull request with just the path fixes? I don't know how I feel about this change. I had received a bug report before about having utf8 sources compiled to invalid characters without this. I don't want to re-introduce the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- sys.error(err)
- }, { compiled =>
- IO.write(css, compiled)
- out.debug("Wrote to file %s" format css)
- css
- })
- } catch { case e: Exception =>
- throw new RuntimeException(
- "error occured while compiling %s: %s" format(pair._1, e.getMessage), e
- )
+ private def compileSources(compiler: Compiler, mini: Boolean, charset: Charset, out: Logger)(pair: (File, File)) = {
+ val (less, css) = pair
+ out.debug("Compiling %s" format less)
+ compiler.compile(less.getPath, mini) { compiled =>
+ IO.write(css, compiled)
+ out.debug("Wrote to file %s" format css)
}
+ }
private def compiled(under: File) = (under ** "*.css").get
View
20 src/sbt-test/less-sbt/nested-imports/build.sbt
@@ -0,0 +1,20 @@
+seq(lessSettings:_*)
+
+(LessKeys.filter in (Compile, LessKeys.less)) := "main.less"
+
+InputKey[Unit]("contents") <<= inputTask { (argsTask: TaskKey[Seq[String]]) =>
+ (argsTask, streams) map {
+ (args, out) =>
+ args match {
+ case Seq(given, expected) =>
+ if(IO.read(file(given)).trim.equals(IO.read(file(expected)).trim)) out.log.debug(
+ "Contents match"
+ )
+ else error(
+ "Contents of (%s)\n%s does not match (%s)\n%s" format(
+ given, IO.read(file(given)), expected, IO.read(file(expected))
+ )
+ )
+ }
+ }
+}
View
6 src/sbt-test/less-sbt/nested-imports/fixtures/main.css
@@ -0,0 +1,6 @@
+h1 {
+ color: #001;
+}
+h2 {
+ color: #002;
+}
View
1  src/sbt-test/less-sbt/nested-imports/project/plugins.sbt
@@ -0,0 +1 @@
+addSbtPlugin("me.lessis" %% "less-sbt" % "0.1.5-SNAPSHOT")
View
1  src/sbt-test/less-sbt/nested-imports/src/main/less/imports/lib.less
@@ -0,0 +1 @@
+@import "sub.less";
View
3  src/sbt-test/less-sbt/nested-imports/src/main/less/imports/sub.less
@@ -0,0 +1,3 @@
+h1 {
+ color: #001;
+}
View
2  src/sbt-test/less-sbt/nested-imports/src/main/less/main.less
@@ -0,0 +1,2 @@
+@import "imports/lib.less";
+@import "sub.less";
View
3  src/sbt-test/less-sbt/nested-imports/src/main/less/sub.less
@@ -0,0 +1,3 @@
+h2 {
+ color: #002;
+}
View
5 src/sbt-test/less-sbt/nested-imports/test
@@ -0,0 +1,5 @@
+# imported sub.less should be imported from imports/
+> less
+$ exists target/scala-2.9.1/resource_managed/main/css/main.css
+# note: less expands shorthand hex, i.e. #eee -> #eeeeee
+> contents target/scala-2.9.1/resource_managed/main/css/main.css fixtures/main.css
Something went wrong with that request. Please try again.