Skip to content

Commit

Permalink
Merge pull request #52 from vaadin/r_1.2.1
Browse files Browse the repository at this point in the history
R 1.2.1
  • Loading branch information
Saulis committed Nov 10, 2015
2 parents 84ead62 + 24572d4 commit afa485a
Show file tree
Hide file tree
Showing 14 changed files with 373 additions and 75 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ $ npm install -g vaadin/gwt-api-generator
```
> If you've installed node and npm using `sudo`, installing packages globally will also require you to use `sudo`. See [http://givan.se/do-not-sudo-npm/](http://givan.se/do-not-sudo-npm/) how to remedy the situation.
- Generating the resourcer for bower packages installed in your bower_components folder
```shell
$ bower install my-web-component
$ gwt-api-generator
```
- Generating the resources for a library
```shell
$ gwt-api-generator --package=PolymerElements/paper-elements
Expand Down
7 changes: 6 additions & 1 deletion bin/gwt-api-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require('coa').Cmd()
.helpful()
.opt()
.name('package')
.req()
.title('Bower package(s) to use. Multiple packages can be defined with: package="foo bar" or package=foo,bar')
.long('package')
.end()
Expand Down Expand Up @@ -38,6 +37,12 @@ require('coa').Cmd()
.title('Version of the generated maven project. (Defaults to package.json.pom.version || pakage.json.version)')
.long('version')
.end()
.opt()
.name('Exclude lib')
.flag()
.title('Do not copy lib java files. Used when the libary .jar is provided (Default: false)')
.long('excludeLib')
.end()
.opt()
.name('pom')
.flag()
Expand Down
48 changes: 34 additions & 14 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ gulp.task('bower:configure', ['clean:resources'], function(done) {
jsonfile.readFile('.bowerrc', function (err, obj) {
if (!err) {
fs.copySync('.bowerrc', globalVar.publicDir + '/.bowerrc');

if(obj.directory) {
globalVar.bowerDir = globalVar.publicDir + '/' + obj.directory;
}
}

done();
});
});

gulp.task('bower:install', ['clean', 'bower:configure'], function() {
return bower({ cmd: 'install', cwd: globalVar.publicDir}, [globalVar.bowerPackages]);
if (globalVar.bowerPackages) {
return bower({ cmd: 'install', cwd: globalVar.publicDir}, [globalVar.bowerPackages]);
} else {
gutil.log('No --package provided. Using package(s) from bower_components folder.');
return gulp.src('./bower_components/**/*', {base: '.'}).pipe(gulp.dest(globalVar.publicDir));
}
});

gulp.task('parse', ['analyze'], function(cb) {
Expand All @@ -76,8 +79,6 @@ gulp.task('parse', ['analyze'], function(cb) {
}
});
}
// Hydrolysis duplicates attributes
helpers.removeDuplicates(item.properties, 'name');
// We don't want to wrap any private api
helpers.removePrivateApi(item.properties, 'name');
});
Expand Down Expand Up @@ -116,7 +117,7 @@ gulp.task('analyze', ['clean:target', 'pre-analyze'], function() {
});
cb(null, file);
})
.catch(function(e){
['catch'](function(e){
gutil.log(e.stack);
cb(null, file);
});
Expand All @@ -134,7 +135,16 @@ function parseTemplate(template, obj, name, dir, suffix) {
// If there is a base .java file we extend it.
var classBase = helpers.camelCase(name) + suffix + "Base";

var prefix = obj.name.split('-')[0].replace(/\./g,'');
// We have to compute the appropriate name-space for the component.
var prefix =
// For events we prefer the first word of the name if they are standard ones.
/^Event/.test(suffix) && /^(polymer|iron|paper|neon)-/.test(name) ? name :
// Otherwise we try the name from its bower.json, then the sub-folder name in
// bower_components, and finally from its name.
obj.bowerData && obj.bowerData.name || obj.path.replace(/.*\/+(.+)\/+[^\/]+/, '$1') || name;
// Then we take the first part before first dash
prefix = prefix.split('-')[0].replace(/\./g,'');

obj.ns = globalVar.ns + '.' + prefix;

var targetPath = globalVar.clientDir + prefix + '/' + dir;
Expand All @@ -161,8 +171,10 @@ function parseTemplate(template, obj, name, dir, suffix) {
gulp.task('generate:elements', ['parse'], function() {
return StreamFromArray(global.parsed,{objectMode: true})
.on('data', function(item) {
if (!helpers.isBehavior(item)) {
parseTemplate('Element', item, item.is, 'element/', 'Element');
if (helpers.isBehavior(item)) {
parseTemplate('Behavior', item, item.is, '', '');
} else {
parseTemplate('Element', item, item.is, '', 'Element');
}
})
});
Expand All @@ -173,7 +185,7 @@ gulp.task('generate:events', ['parse'], function() {
if (item.events) {
item.events.forEach(function(event) {
event.bowerData = item.bowerData;
parseTemplate('ElementEvent', event, event.name, 'element/event/', 'Event');
parseTemplate('ElementEvent', event, event.name, 'event/', 'Event');
});
}
})
Expand Down Expand Up @@ -222,17 +234,25 @@ gulp.task('generate', ['generate:elements-all', 'generate:widgets-all', 'generat
});

gulp.task('copy:lib', function() {
return gulp.src(libDir + '**')
.pipe(gulp.dest(globalVar.clientDirBase));
if (!args.excludeLib) {
return gulp.src(libDir + '**')
.pipe(gulp.dest(globalVar.clientDirBase));
}
});

gulp.task('copy:pom', function() {
var tpl = _.template(fs.readFileSync(tplDir + "pom.template"));
var pom = globalVar.currentDir + "pom.xml";

// Try to get some configuration from a package.json
// otherwise use default values
var pkgFile = globalVar.currentDir + 'package.json';
var pkgContent = fs.readFileSync(pkgFile);
globalVar.pkg = pkgContent ? JSON.parse(pkgContent) : {};
globalVar.pkg = {};
try {
var pkgContent = fs.readFileSync(pkgFile);
globalVar.pkg = JSON.parse(pkgContent);
} catch(ignore) {
}
globalVar.pkg.pom = globalVar.pkg.pom || {};
globalVar.pkg.pom.version = args.version || globalVar.pkg.pom.version || globalVar.pkg.version;

Expand Down
139 changes: 117 additions & 22 deletions lib/com/vaadin/polymer/Polymer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.js.JsType;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
Expand All @@ -14,6 +17,85 @@

public abstract class Polymer {

public static PolymerBase Base;

@JsType
public interface PolymerBase {
/**
* Returns the first node in this element’s local DOM that matches selector.
*/
Element $$(String selector);

/**
* Toggles the named boolean class on the node, adding the class if bool is
* truthy and removing it if bool is falsey.
*/
void toggleClass(String name, boolean b, Element node);

/**
* Toggles the named boolean attribute on the node.
*/
void toggleAttribute(String name, boolean b, Element node);

/**
* Moves a boolean attribute from oldNode to newNode, unsetting the attribute
* (if set) on oldNode and setting it on newNode
*/
void attributeFollows(String name, Element newNode, Element oldNode);

/**
* Moves a class from oldNode to newNode, removing the class (if present)
* on oldNode and adding it to newNode.
*/
void classFollows(String name, Element newNode, Element oldNode);


/**
* Fires a custom event. The options object can contain the following properties:
* node: Node to fire the event on (defaults to this).
* bubbles: Whether the event should bubble. Defaults to true.
* cancelable: Whether the event can be canceled with preventDefault. Defaults to false.
*/
void fire(String type, JavaScriptObject detail, JavaScriptObject options);

/**
* Calls method asynchronously. If no wait time is specified, runs tasks with microtask
* timing (after the current method finishes, but before the next event from the event
* queue is processed). Returns a handle that can be used to cancel the task.
*/
Object async(Function method, int wait);

/**
* Cancels the identified async task.
*/
void cancelAsync(Object handle);

/**
* Applies a CSS transform to the specified node, or host element if no node is
* specified. transform is specified as a string. For example:
* transform('rotateX(90deg)', elm);
*/
void transform(String transform, Element node);

/**
* Transforms the specified node, or host element if no node is specified. For example:
* translate3d('100px', '100px', '100px', elm);
*/
void translate3d(String x, String y, String z, Element node);

/**
* Dynamically imports an HTML document.
*/
void importHref(String href, Function onload, Function onerror);

/**
* Takes a URL relative to the <dom-module> of an imported Polymer element, and returns
* a path relative to the current document. This method can be used, for example,
* to refer to an asset delivered alongside an HTML import.
*/
String resolveUrl(String url);
}

private static Set<String> urlImported = new HashSet<>();

/**
Expand Down Expand Up @@ -53,16 +135,22 @@ private static String absoluteHref(String hrefOrTag) {

private static native void whenPolymerLoaded(Function ok)
/*-{
function done() {
// Set our static reference to Base
@com.vaadin.polymer.Polymer::Base = $wnd.Polymer.Base;
// Polymer dynamic loaded does not remove unresolved
$doc.body.removeAttribute('unresolved');
//
[email protected]::call(*)();
}
if (!$wnd.Polymer) {
var l = $doc.createElement('link');
l.rel = 'import';
l.href = @com.vaadin.polymer.Polymer::absoluteHref(*)('polymer');
l.onload = function(){
[email protected]::call(*)();
};
l.onload = done;
$doc.head.appendChild(l);
} else {
[email protected]::call(*)();
done();
}
}-*/;

Expand All @@ -83,7 +171,7 @@ public static void importHref(String hrefOrTag, final Function ok, final Functio
urlImported.add(href);
whenPolymerLoaded(new Function() {
public Object call(Object arg) {
importHrefImpl(href,ok, err);
Base.importHref(href, ok, err);
return null;
}
});
Expand Down Expand Up @@ -198,22 +286,6 @@ private native static boolean isRegisteredElement(Object e)
return !!e && e.constructor !== $wnd.HTMLElement && e.constructor != $wnd.HTMLUnknownElement;
}-*/;

/**
* Dynamically import a link and monitors when it has been loaded.
*
* This could be done via Polymer importHref, but the method needs a custom element
* instance to be run.
*/
private native static void importHrefImpl(String href, Function onload, Function onerror)
/*-{
console.log("gwt-polymer loading: ", href.replace(/^.*components\//,''), onload, onerror);
$wnd.Polymer.Base.importHref(href, function() {
if (onload) [email protected]::call(*)(href);
}, function() {
if (onerror) [email protected]::call(*)(href);
});
}-*/;

public static void ready(HTMLElement e, Function f) {
whenReady(f, (Element)e);
}
Expand Down Expand Up @@ -275,7 +347,7 @@ function done() {
* run a Function (JsFunction or JavaFunction)
*/
@Deprecated
private static void onReady(Element e, Object f) {
private static void onReady(Element e, Object f) {
whenReady(f, e);
}

Expand Down Expand Up @@ -366,4 +438,27 @@ private static native void reFlow()
}, 50);
}
}-*/;

/**
* Box a native JS array in a Java List. It does not have any performance
* penalty because we directly set the native array of the super ArrayList
* implementation.
*/
public static native <T> List<T> asList(JavaScriptObject o)
/*-{
var l = @java.util.ArrayList::new()();
[email protected]::array = o;
return l;
}-*/;

/**
* UnBox the native JS array in a Java List. It does not have any performance
* penalty because we directly take the native array of the super ArrayList
* implementation.
*/
public static native <T extends JavaScriptObject> JsArray<T> asJsArray(List<?> l)
/*-{
return [email protected]::array;
}-*/;

}
6 changes: 4 additions & 2 deletions lib/com/vaadin/polymer/elemental/Function.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.vaadin.polymer.elemental;

import com.google.gwt.core.client.js.JsType;
import com.google.gwt.core.client.js.JsFunction;

@JsType

@JsFunction
public interface Function {
public Object call(Object arg);
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gwt-api-generator",
"version": "1.0.2",
"version": "1.2.1",
"description": "",
"author": "Vaadin Ltd",
"license": "Apache License 2.0",
Expand Down
Loading

0 comments on commit afa485a

Please sign in to comment.