Created
July 12, 2013 20:21
-
-
Save rotty3000/5987496 to your computer and use it in GitHub Desktop.
from ruby to java via jrubyc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.liferay.portal.servlet.filters.dynamiccss; | |
| import org.jruby.Ruby; | |
| import org.jruby.RubyObject; | |
| import org.jruby.javasupport.util.RuntimeHelpers; | |
| import org.jruby.runtime.builtin.IRubyObject; | |
| import org.jruby.javasupport.JavaUtil; | |
| import org.jruby.RubyClass; | |
| public class SASSWrapper extends RubyObject { | |
| private static final Ruby __ruby__ = Ruby.getGlobalRuntime(); | |
| private static final RubyClass __metaclass__; | |
| static { | |
| String source = new StringBuilder("require 'compass'\n" + | |
| "require 'fileutils'\n" + | |
| "require 'java'\n" + | |
| "require 'rubygems'\n" + | |
| "require 'sass/plugin'\n" + | |
| "\n" + | |
| "java_package 'com.liferay.portal.servlet.filters.dynamiccss'\n" + | |
| "\n" + | |
| "class SASSWrapper\n" + | |
| " java_signature 'public java.lang.String process(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)'\n" + | |
| " def process(content, cssRealPath, cssThemePath, sassCachePath, debug)\n" + | |
| " if cssRealPath.nil?\n" + | |
| " cssRealPath = 'INPUT'\n" + | |
| " end\n" + | |
| "\n" + | |
| " Compass.add_project_configuration\n" + | |
| " Compass.configuration.project_path ||= cssThemePath\n" + | |
| "\n" + | |
| " load_paths = []\n" + | |
| "\n" + | |
| " if cssThemePath\n" + | |
| " load_paths += [cssThemePath]\n" + | |
| " end\n" + | |
| "\n" + | |
| " if sassCachePath.nil?\n" + | |
| " sassCachePath = Dir.pwd + '/.sass_cache'\n" + | |
| " end\n" + | |
| "\n" + | |
| " load_paths += Compass.configuration.sass_load_paths\n" + | |
| "\n" + | |
| " if debug == \"true\"\n" + | |
| " debug = true\n" + | |
| " else\n" + | |
| " debug = false\n" + | |
| " end\n" + | |
| "\n" + | |
| " if debug\n" + | |
| " puts \"content: #{content}\"\n" + | |
| " puts \"cssRealPath: #{cssRealPath}\"\n" + | |
| " puts \"cssThemePath: #{cssThemePath}\"\n" + | |
| " puts \"sassCachePath: #{sassCachePath}\"\n" + | |
| " end\n" + | |
| "\n" + | |
| " engine = Sass::Engine.new(\n" + | |
| " content,\n" + | |
| " {\n" + | |
| " :cache_location => sassCachePath,\n" + | |
| " :debug_info => debug,\n" + | |
| " :filename => cssRealPath,\n" + | |
| " :full_exception => debug,\n" + | |
| " :line => 0,\n" + | |
| " :load_paths => load_paths,\n" + | |
| " :syntax => :scss,\n" + | |
| " :ugly => true\n" + | |
| " }\n" + | |
| " )\n" + | |
| "\n" + | |
| " begin\n" + | |
| " return engine.render\n" + | |
| " rescue Sass::SyntaxError => e\n" + | |
| " puts e.message\n" + | |
| " puts e.backtrace\n" + | |
| " end\n" + | |
| " end\n" + | |
| "end\n" + | |
| "\n" + | |
| "if ARGV.length < 1\n" + | |
| " return \"Usage: #{$PROGRAM_NAME} content [cssRealPath] [cssThemePath] [sassCachePath] [debug]\"\n" + | |
| "end\n" + | |
| "\n" + | |
| "sassWrapper = SASSWrapper.new()\n" + | |
| "\n" + | |
| "output = sassWrapper.process(ARGV[0], ARGV[1], ARGV[2], ARGV[3], ARGV[4])\n" + | |
| "\n" + | |
| "puts output").toString(); | |
| __ruby__.executeScript(source, "src//com/liferay/portal/servlet/filters/dynamiccss/SASS.rb"); | |
| RubyClass metaclass = __ruby__.getClass("SASSWrapper"); | |
| metaclass.setRubyStaticAllocator(SASSWrapper.class); | |
| if (metaclass == null) throw new NoClassDefFoundError("Could not load Ruby class: SASSWrapper"); | |
| __metaclass__ = metaclass; | |
| } | |
| /** | |
| * Standard Ruby object constructor, for construction-from-Ruby purposes. | |
| * Generally not for user consumption. | |
| * | |
| * @param ruby The JRuby instance this object will belong to | |
| * @param metaclass The RubyClass representing the Ruby class of this object | |
| */ | |
| private SASSWrapper(Ruby ruby, RubyClass metaclass) { | |
| super(ruby, metaclass); | |
| } | |
| /** | |
| * A static method used by JRuby for allocating instances of this object | |
| * from Ruby. Generally not for user comsumption. | |
| * | |
| * @param ruby The JRuby instance this object will belong to | |
| * @param metaclass The RubyClass representing the Ruby class of this object | |
| */ | |
| public static IRubyObject __allocate__(Ruby ruby, RubyClass metaClass) { | |
| return new SASSWrapper(ruby, metaClass); | |
| } | |
| /** | |
| * Default constructor. Invokes this(Ruby, RubyClass) with the classloader-static | |
| * Ruby and RubyClass instances assocated with this class, and then invokes the | |
| * no-argument 'initialize' method in Ruby. | |
| * | |
| * @param ruby The JRuby instance this object will belong to | |
| * @param metaclass The RubyClass representing the Ruby class of this object | |
| */ | |
| public SASSWrapper() { | |
| this(__ruby__, __metaclass__); | |
| RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "initialize"); | |
| } | |
| public java.lang.String process(java.lang.String content, java.lang.String cssRealPath, java.lang.String cssThemePath, java.lang.String sassCachePath, java.lang.String debug) { | |
| IRubyObject ruby_args[] = new IRubyObject[5]; | |
| ruby_args[0] = JavaUtil.convertJavaToRuby(__ruby__, content); | |
| ruby_args[1] = JavaUtil.convertJavaToRuby(__ruby__, cssRealPath); | |
| ruby_args[2] = JavaUtil.convertJavaToRuby(__ruby__, cssThemePath); | |
| ruby_args[3] = JavaUtil.convertJavaToRuby(__ruby__, sassCachePath); | |
| ruby_args[4] = JavaUtil.convertJavaToRuby(__ruby__, debug); | |
| IRubyObject ruby_result = RuntimeHelpers.invoke(__ruby__.getCurrentContext(), this, "process", ruby_args); | |
| return (java.lang.String)ruby_result.toJava(java.lang.String.class); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment