Friday, December 10, 2010

Setting Node properties using SlingPostServlet

SlingPostServlet is handy


To have a multi-value property that has one value, use propname@TypeHint


curl -u admin:admin -F'foo=bar' -F'foo@TypeHint=String[]' http://localhost:8888/some/path


More stuff here: http://www.unc.edu/home/adamc/post-servlet.html


And here's script that lets you set up a test page if you are using Day CQ and ExtJS


#!/bin/bash

if (( $# < 1 ))
then
    echo "Creates /apps/sandbox/*"
    echo "Usage: $0 project_name [host] [cred]"
    echo "ex, $0 test-project localhost:4502 admin:admin"
    exit 1
fi

name="$1"
host="localhost:4502"
cred="admin:admin"

if (( $# >= 2 ))
then
    host="$2"
fi

if (( $# >= 3 ))
then
    cred="$3"
fi

path="/apps/sandbox/$name"
left="http://${host}${path}"
category="sandbox.$name"

function createComponent() {
curl -s -u "$cred" \
    -F'jcr:primaryType=cq:Component' \
    -F"sling:resourceType=$path" \
    "$left" > /dev/null 2>&1
}

function uploadJsp() {
curl -s -u "$cred" \
    -T - "$left/html.jsp" > /dev/null 2>&1 <<HEREDOC
<%@include file="/libs/foundation/global.jsp"%>
<%@page import="com.day.cq.widget.HtmlLibraryManager"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>$path</title>
    <%
    final HtmlLibraryManager manager = sling.getService(HtmlLibraryManager.class);
    if (manager != null) {
        manager.writeCssInclude(slingRequest, out, "$category");
        manager.writeJsInclude(slingRequest, out, "$category");
    } else {
        out.write("BAD");
    }
    %>
</head>
<body>
    <h1>Sandbox $name</h1>
</body>
</html>
HEREDOC
}

function createWidget() {
curl -s -u "$cred" \
    -F'jcr:primaryType=cq:ClientLibraryFolder' \
    -F'sling:resourceType=widgets/clientlib' \
    -F"categories=$category" \
    -F'categories@TypeHint=String[]' \
    -F'dependencies=cq.widgets' \
    -F'dependencies@TypeHint=String[]' \
    "$left/widgets" > /dev/null 2>&1
}

function uploadJsTxt() {
curl -s -u "$cred" \
    -T - "$left/widgets/js.txt" > /dev/null 2>&1 <<HEREDOC
#base=source
script.js
HEREDOC
}

function createSource() {
curl -s -u "$cred" \
    -F'jcr:primaryType=nt:folder' \
    "$left/widgets/source" > /dev/null 2>&1
}

function uploadJs() {
curl -s -u "$cred" \
    -T - "$left/widgets/source/script.js" > /dev/null 2>&1 <<HEREDOC
CQ.Ext.onReady(function() {
    //entry point
    CQ.Ext.Msg.alert('Sandbox $name', 'Hello World!');
});
HEREDOC
}

echo -n "creating cq:Component $path... "
createComponent || exit 1
echo "DONE"

echo -n "uploading html.jsp... "
uploadJsp || exit 1
echo "DONE"

echo -n "creating widgets... "
createWidget || exit 1
echo "DONE"

echo -n "uploading js.txt... "
uploadJsTxt || exit 1
echo "DONE"

echo -n "creating source directory... "
createSource || exit 1
echo "DONE"

echo -n "uploading script.js... "
uploadJs || exit 1
echo "DONE"

echo "You may visit: $left.html"

Thursday, December 9, 2010

reading extjs docs locally

  1. download extjs
  2. unzip the file
  3. cd extjs-3.1.1/docs
  4. python -m SimpleHTTPServer 8080
  5. http://localhost:8080
or, download Twisted and  twistd -n web --port=8080 --path=.

Thursday, December 2, 2010

ats on windows

to install ats on windows, you need cygwin.

in cygwin you need:

  • g++
  • make
  • gmp
  • maybe automake

then,

cd ats-x.x.x
./configure --prefix=/cygdrive/c/opt/ats
make

before make install, you need to modify Makefile.

  • find install: target
  • quote cd "$(abstopsrcdir)" && ..

and in ~/.bashrc, you need something like:

OPT_DIR=/cygdrive/c/home/opt
export ATSHOME="${OPT_DIR}/ats/lib/ats-anairiats-0.2.2"
export ATSHOMERELOC="ATS-0.2.2"
export PATH="$ATSHOME/bin:$PATH"

and atscc is available

Sunday, August 8, 2010

lua readline tab completion

#!/bin/bash

LUA_HOME="$HOME/opt/lua"
LUA_PATH="$LUA_PATH;$LUA_HOME/?.lua" LUA_CPATH="$LUA_CPATH;$LUA_HOME/?.so" "$LUA_HOME/bin/lua" -lreadline -lcomplete $*

where readline and complete are from  http://lua-users.org/wiki/CompleteWithReadline

use readline.c and complete.lua. You don't have to patch lua.c.

read http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders to know how to use LUA_PATH and LUA_CPATH. question mark (?) in LUA_PATH or LUA_CPATH is replaced with foo when you do require("foo"). And, when you do require("foo.bar"),  it searches for foo/bar.lua or foo/bar.so if LUA_(C)PATH.

Friday, February 26, 2010

LLVM build on MinGW

building llvm-2.6

  1. download and install MinGW
  2. download and install cmake
  3. download llvm
  4. tar xvzf llvm-2.6.tar.gz
  5. mkdir llvm-cmake
  6. cd llvm-cmake
  7. cmake -G "MinGW Makefiles" ..\llvm-2.6
  8. it'll fail
  9. open CMakeCache.txt
  10. write CMAKE_C_COMPILER_ENVVAR:FILEPATH=C:/MinGW/bin/gcc.exe
  11. cmake -G "MinGW Makefiles" ..\llvm-2.6
  12. mingw32-make

For llvm 2.7,

  1. unzip llvm-2.7
  2. unzip clang source under llvm-27/tools/clang
  3. cd llvm-cmake
  4. cmake -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX="/home/opt/llvm" ..\llvm-2.7
  5. mingw32-make DESTDIR=C: install

Tuesday, February 16, 2010

v8 on mingw

  1. install python
  2. install scons
  3. install svn
  4. checkout v8 svn checkout http://v8.googlecode.com/svn/trunk/ v8
  5. edit Sconstruct and comment out -Werror (maybe there's a way to disable it from command line) vim v8/SConstruct
  6. cd v8
  7. scons
  8. g++ -Iinclude samples\shell.cc libv8.a -lwinmm -lws2_32 -o v8.exe

Saturday, December 12, 2009

adds openconsole right click context:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Folder\shell]

[HKEY_CURRENT_USER\Software\Classes\Folder\shell\openconsole]

[HKEY_CURRENT_USER\Software\Classes\Folder\shell\openconsole\command]
@="\"C:\\home\\opt\\Console2\\bin\\release\\Console.exe\" -d \"%L\""

adds openbash right click context (needs msys and Console2 tab named "Bash" where shell is set to C:\msys\1.0\bin\bash.exe):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Folder\shell]

[HKEY_CURRENT_USER\Software\Classes\Folder\shell\openbash]

[HKEY_CURRENT_USER\Software\Classes\Folder\shell\openbash\command]
@="\"C:\\home\\opt\\Console2\\bin\\release\\Console.exe\" -t Bash -d \"%L\""

Tuesday, October 13, 2009

elb-create-lb

elastic load balancing gotcha:
elb-create-lb TheNameMustBeginWithCapitalLetterAndShouldNotIncludeDotsOrUnderscore..
For example, elb-create-lb ExampleCom is ok
elb-create-lb example.com is not.

Friday, September 18, 2009

windows batch file to convert videos to mp3

save this as tomp3.bat
@echo off

setlocal

set filename=%~n1
set filepath=%~df1
set dirname=%~dp1
set dumpfile=%dirname%\audiodump.wav
set outfile=%dirname%\%filename%.mp3
mplayer -ao pcm:fast -af volnorm -vo null -vc null "%filepath%" -dumpfile "%dumpfile%"
lame -h "%dumpfile%" -o "%outfile%"
del "%dumpfile%"
pause
endlocal
and drag and drop the movie to the batch file

Saturday, August 22, 2009

clojure tab completion on windows

windows sucks because it does.

so, clji.bat:

@echo off
setlocal
set CLOJURE_HOME=%~dp0\clojure
set CLASS_PATH=%CLOJURE_HOME%\jline-0.9.94.jar;%CLOJURE_HOME%\clojure.jar;%CLOJURE_HOME%\clojure-contrib.jar
java -Djline.words="%CLOJURE_HOME%\all_names.txt" -cp "%CLASS_PATH%" jline.ConsoleRunner clojure.main %*
endlocal

clj.bat:

@echo off
setlocal
set CLOJURE_HOME=%~dp0\clojure
set CLASS_PATH=%CLOJURE_HOME%\jline-0.9.94.jar;%CLOJURE_HOME%\clojure.jar;%CLOJURE_HOME%\clojure-contrib.jar
IF (%1)==() (
    java -Djline.words="%CLOJURE_HOME%\all_names.txt" -cp "%CLASS_PATH%" jline.ConsoleRunner clojure.main
) ELSE (
    java -Djline.words="%CLOJURE_HOME%\all_names.txt" -cp "%CLASS_PATH%" clojure.main %1 -- %*
)
endlocal

clji.bat always starts repl. clj.bat starts repl when no argument is passed. otherwise, it runs the first argument (should be .clj script). it expects there's clojure/ directory at the same level of the batch files. the directory should have some jar files (clojure.jar, clojure-contrib.jar, jline..) and also clojure/ contains allnames.txt that is generated by allnames.clj:

(def all-names (concat (map (fn [p] (keys (ns-publics (find-ns p))))
    '(clojure.core clojure.set clojure.xml clojure.zip))))

(println (apply str
    (interleave (reduce concat all-names)
                (repeat "\n"))))

to run it:

$ clj all_names.clj > all_names.txt

now, modified ConsoleRunner.java (from http://jline.sf.net) that'll actually use all_names.txt:

/*
 * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 */
package jline;

import java.io.*;
import java.util.*;

/**
 *  <p>
 *  A pass-through application that sets the system input stream to a
 *  {@link ConsoleReader} and invokes the specified main method.
 *  </p>
 *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
 */
public class ConsoleRunner {
    public static final String HISTORY = "jline.history";
    public static final String WORDS = "jline.words";

    public static String[] getWords(String wordsFile) {
        List result = new ArrayList();
        try {
            BufferedReader input = new BufferedReader(
                new FileReader(wordsFile)
            );
            try {
                String line = null;
                while (null != (line = input.readLine())) {
                    result.add(line);
                }
            } finally {
                input.close();
            }
        } catch (FileNotFoundException err) {
            err.printStackTrace();
        } catch (IOException err) {
            err.printStackTrace();
        }
        String[] array = new String[result.size()];
        result.toArray(array);
        return array;
    }

    public static void main(final String[] args) throws Exception {
        String historyFileName = null;

        List argList = new ArrayList(Arrays.asList(args));

        if (argList.size() == 0) {
            usage();

            return;
        }

        historyFileName = System.getProperty(ConsoleRunner.HISTORY, null);

        // invoke the main() method
        String mainClass = (String) argList.remove(0);

        // setup the inpout stream
        ConsoleReader reader = new ConsoleReader();

        if (historyFileName != null) {
            reader.setHistory(new History (new File
                (System.getProperty("user.home"),
                    ".jline-" + mainClass
                        + "." + historyFileName + ".history")));
        } else {
            reader.setHistory(new History(new File
                (System.getProperty("user.home"),
                    ".jline-" + mainClass + ".history")));
        }

        String wordsFileName = System.getProperty(WORDS, null);
        if (null != wordsFileName) {
            //List list = new ArrayList();
            //list.add(new SimpleCompletor(getWords(wordsFileName)));
            //list.add(new FileNameCompletor());
            //list.add(new NullCompletor());
            Completor completor =
                new MultiCompletor(new Completor[] {
                    new SimpleCompletor(getWords(wordsFileName))
                    , new FileNameCompletor()
                });
            reader.addCompletor(
                new ArgumentCompletor(new Completor[] {
                    completor
                    , completor
                }, new SExpr())
            );
        } else {
        }

        String completors = System.getProperty
            (ConsoleRunner.class.getName() + ".completors", "");
        List completorList = new ArrayList();


        for (StringTokenizer tok = new StringTokenizer(completors, ",");
            tok.hasMoreTokens();) {
            completorList.add
                ((Completor) Class.forName(tok.nextToken()).newInstance());
        }

        if (completorList.size() > 0) {
            reader.addCompletor(new ArgumentCompletor(completorList));
        }

        //reader.setCompletionHandler(new CandidateListCompletionHandler());

        ConsoleReaderInputStream.setIn(reader);

        try {
            Class.forName(mainClass).
                getMethod("main", new Class[] { String[].class }).
                invoke(null, new Object[] { argList.toArray(new String[0]) });
        } finally {
            // just in case this main method is called from another program
            ConsoleReaderInputStream.restoreIn();
        }
    }

    private static void usage() {
        System.out.println("Usage: \n   java " + "[-Djline.history='name'] "
            + "[-Djline.words=<path to words file for autocompletion>] "
            + ConsoleRunner.class.getName()
            + " <target class name> [args]"
            + "\n\nThe -Djline.history option will avoid history"
            + "\nmangling when running ConsoleRunner on the same application."
            + "\n\nargs will be passed directly to the target class name.");
    }
}

class SExpr
        extends ArgumentCompletor.AbstractArgumentDelimiter {
    public boolean isDelimiterChar(String buffer, int pos) {
        String c = buffer.substring(pos, pos+1);
        return c.matches("[^\\w-*<>=?]");
    }
}

if -Djline.words=path/to/file/that/has/word/list is passed, it'll use that file to do tab completion. it's quick and dirty. i don't get how jline works. somehow, i had to pass completor (new MultiCompletor(...)) twice to ArgumentCompletor so that tab completion works into middle of line. Also, SExpr is argument delimiter that considers things other than [\w-*<>=?] a delimiter.

so, run mvn package to build jline jar. then put jline jar file under clojure/. then clj.bat and clji.bat would work.

i always spend time to set up development environment then lose interest :P