Skip to content

Commit

Permalink
Fixed #4 and #5: added gulpfile processing, and changed generate xqy …
Browse files Browse the repository at this point in the history
…to be used per file remotely
  • Loading branch information
grtjn committed May 20, 2015
1 parent ddd85dc commit 7826ca2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
xml/
node_modules/
xml/
79 changes: 79 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*jshint node: true */

'use strict';

var gulp = require('gulp');

var argv = require('yargs').argv;
var fs = require('fs');
var request = require('request');
var through = require('through2');
var typescript = require('gulp-tsc');

var onError = function(err) {
if (err) {
console.log(err);
}
};

var options = {
mlHost: argv['ml-host'] || 'ml8-ml1',
mlPort: argv['ml-port'] || '8000',
mlUser: argv['ml-user'] || 'admin',
mlPass: argv['ml-pass'] || 'admin'
};


gulp.task('generate', function(){
var query;

fs.readFile('qconsole/generate-definitions.xqy', null, function(err, data) {
query = data.toString();
});

return gulp.src(['xml/**/*.xml'])
.pipe(through.obj(function (file, enc, cb) {

console.log(file.path);

var outFile = file.path.replace('/xml/', '/ts/').replace('.xml', '.d.ts');

var xml = file.contents.toString();
var escapedXml = xml.replace(/\\/gm, '\\\\').replace(/"/gm, '\\"').replace(/(\n\r|\r\n|\n|\r)/gm, '\\n').replace(/\t/gm, '\\t');

request({
method: 'POST',
url: 'http://' + options.mlHost + ':' + options.mlPort + '/v1/eval',
form: {
xquery: query,
vars: '{ xml: "'+ escapedXml +'"}'
},
auth: {
user: options.mlUser,
pass: options.mlPass,
sendImmediately: false
}
}, function(err, httpResponse, body) {
// get rid of multipart response wrapping
body = body.replace(/^([^\r]*\r\n){5}/, '').replace(/\r\n[^\r]*\r\n$/, '');

if (err || httpResponse.statusCode !== 200) {
console.log('FAILED!');
console.log(body);
//console.log(escapedXml);
} else {
fs.writeFile(outFile, body, onError);
}
});

cb(null, file);
}));
});

gulp.task('validate', function(){
gulp.src(['ts/**/*.ts'])
.pipe(typescript());
});

// Default Task
gulp.task('default', ['generate', 'validate']);
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
"typescript",
"marklogic"
],
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"gulp": "^3.8.11",
"gulp-tsc": "^0.9.5",
"yargs": "^3.9.1",
"request": "^2.55.0",
"through2": "^0.6.5"
},
"engine": "node >= 0.4.1",
"scripts": {
Expand Down
75 changes: 54 additions & 21 deletions qconsole/generate-definitions.xqy
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
xquery version "1.0-ml";

(:
let $docs-dir := "/Users/gjosten/Projects/github-grtjn/marklogic-typescript-definitions/xml/"
let $docs-files := xdmp:filesystem-directory($docs-dir)/*[ends-with(*:filename, ".xml")]/*:filename
for $file in $docs-files
let $xml := xdmp:document-get(concat($docs-dir, $file))
:)

declare variable $xml external;
let $xml := xdmp:unquote($xml)

let $definition := xdmp:xslt-eval(
<xsl:stylesheet version="2.0" exclude-result-prefixes="#all" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:apidoc="http://marklogic.com/xdmp/apidoc" xmlns:local="local">
Expand All @@ -25,7 +30,7 @@ let $definition := xdmp:xslt-eval(
<xsl:text>&#32;&#32;&#32;&#32;</xsl:text>
<xsl:value-of select="local:fixName(@name)"/>
<xsl:text>(</xsl:text>
<xsl:for-each select="apidoc:params/apidoc:param">
<xsl:for-each select="apidoc:params/apidoc:param[not(@class) or @class = 'javascript']">
<xsl:value-of select="local:fixName(@name)"/>
<xsl:if test="string(@optional) = 'true'">?</xsl:if>
<xsl:text>: </xsl:text>
Expand All @@ -38,6 +43,8 @@ let $definition := xdmp:xslt-eval(
</xsl:template>

<xsl:template match="/">
<xsl:variable name="root" select="."/>

<xsl:text>// Type definitions for </xsl:text><xsl:value-of select="apidoc:module/@name"/><xsl:text>&#10;</xsl:text>
<xsl:text>// Definitions: </xsl:text><xsl:value-of select="base-uri(.)"/><xsl:text>&#10;&#10;</xsl:text>

Expand All @@ -49,44 +56,70 @@ let $definition := xdmp:xslt-eval(
<xsl:value-of select="apidoc:module/@name"/>
<xsl:text> {{&#10;&#10;</xsl:text>

<xsl:text> interface </xsl:text>
<xsl:value-of select="apidoc:module/@lib"/>
<xsl:text> {{&#10;&#10;</xsl:text>
<xsl:for-each select="distinct-values(((apidoc:module/@lib, 'xdmp')[1], //apidoc:function[empty(@http-verb)]/@lib))">
<xsl:variable name="lib" select="."/>
<xsl:text> interface </xsl:text>
<xsl:value-of select="local:fixName($lib)"/>
<xsl:text> {{&#10;&#10;</xsl:text>

<xsl:apply-templates select="//apidoc:function"/>
<xsl:apply-templates select="$root//apidoc:function[@lib = $lib][empty(@http-verb)][not(@class) or @class = 'javascript']"/>

<xsl:text>&#10; }}&#10;}}&#10;</xsl:text>
<xsl:text>&#10; }}&#10;</xsl:text>
</xsl:for-each>

<xsl:text>}}&#10;</xsl:text>
</xsl:template>

<xsl:function name="local:fixType">
<xsl:param name="type"/>


<xsl:variable name="quantifier" select="replace($type, '^.+([?+*])?$', '$1')"/>
<xsl:variable name="type" select="replace($type, '^(.+)[?+*]$', '$1')"/>

<xsl:choose>
<xsl:when test="$type = ('binary()', 'document-node()', 'element()', 'node()')">Node</xsl:when>
<xsl:when test="$type = 'empty-sequence()'">void</xsl:when>
<xsl:when test="$type = 'item()'">any</xsl:when>
<xsl:when test="matches($type, 'int|long', 'i')">number</xsl:when>
<xsl:when test="$type = ('item()', 'xs:anyURI', 'xs:string', 'xs:time', 'xs:unsignedLong', '(cts:order|xs:string)')">String</xsl:when>
<xsl:when test="$type = 'json:array'">Array</xsl:when>
<xsl:when test="$type = ('json:object', 'map:map', '(element()|map:map)', 'xdmp:function')">Object</xsl:when>
<xsl:when test="$type = ('function()', 'function(*)')">() => any</xsl:when>
<xsl:when test="$type = 'xs:dateTime'">Date</xsl:when>
<xsl:when test="$type = ('xs:decimal', 'xs:double', 'xs:float', 'xs:integer', 'xs:long', 'xs:positiveInteger', 'xs:unsignedInt')">Number</xsl:when>
<xsl:when test="matches($type, '^(schema-)?element\([^)]+\)$')">Node</xsl:when>
<!-- generic fallback -->
<xsl:otherwise>
<xsl:value-of select="translate(replace($type, '^([^:]+:)?(.+)$', '$2'), '?+*', '')"/>
<!--xsl:variable name="local-name" select="replace($type, '^([^:]+:)?(.+)$', '$2')"/>
<xsl:value-of select="concat(upper-case(substring($local-name, 1, 1)), substring($local-name, 2))"/-->
<xsl:text>Object</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

<xsl:function name="local:fixName">
<xsl:param name="name"/>

<xsl:analyze-string select="$name" regex="-([a-zA-Z])">

<xsl:matching-substring>
<xsl:value-of select="upper-case(regex-group(1))"/>
</xsl:matching-substring>
<xsl:choose>
<xsl:when test="$name = 'default'">defaultVal</xsl:when>
<xsl:when test="$name = 'function'">functionArg</xsl:when>
<xsl:otherwise>
<xsl:analyze-string select="$name" regex="-([a-zA-Z])">

<xsl:matching-substring>
<xsl:value-of select="upper-case(regex-group(1))"/>
</xsl:matching-substring>

<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="translate(., '-', '')"/>
</xsl:non-matching-substring>

</xsl:analyze-string>
</xsl:analyze-string>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

</xsl:stylesheet>, $xml)

return xdmp:save(concat(replace($docs-dir, 'xml/$', 'ts/'), replace($file, '\.xml$', ''), '.d.ts'), $definition)

(:
return xdmp:save(concat(replace($docs-dir, 'xml/$', 'ts/'), replace($file, '\.xml$', ''), '.d.ts'), $definition)
:)
return $definition

0 comments on commit 7826ca2

Please sign in to comment.