Skip to content

Commit

Permalink
Update glift with glift core.
Browse files Browse the repository at this point in the history
  • Loading branch information
artasparks committed Feb 4, 2019
1 parent a332f17 commit 64beb68
Show file tree
Hide file tree
Showing 16 changed files with 3,810 additions and 263 deletions.
63 changes: 63 additions & 0 deletions deps/glift-core/dev/closure-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';


/**
*
*/

const closureCompiler = require('google-closure-compiler').gulp({
// extraArguments: ['-Xms2048m']
});

/**
* defaultCompile passes defaults to the closureCompiler
*
* @param {string} outName whatever defaultCompile produces.
*
* @return {object} whatever closureCompiler returns.
*/
var defaultCompile = function(outName) {
return closureCompiler({
js_output_file: 'glift-core.js',
language_in: 'ECMASCRIPT5_STRICT',
// TODO(kashomon): Turn on ADVANCED_OPTIMIZATIONS when all the right
// functions have been marked @export, where appropriate
// compilation_level: 'ADVANCED_OPTIMIZATIONS',
//
// Note that warning_level=VERBOSE corresponds to:
//
// --jscomp_warning=checkTypes
// --jscomp_error=checkVars
// --jscomp_warning=deprecated
// --jscomp_error=duplicate
// --jscomp_warning=globalThis
// --jscomp_warning=missingProperties
// --jscomp_warning=undefinedNames
// --jscomp_error=undefinedVars
//
// Do some advanced Javascript checks.
// https://github.com/google/closure-compiler/wiki/Warnings
jscomp_error: [
'accessControls',
'checkRegExp',
'checkTypes',
'checkVars',
'const',
'constantProperty',
'deprecated',
'duplicate',
'globalThis',
'missingProperties',
'missingProvide',
'missingReturn',
'undefinedNames',
'undefinedVars',
'visibility',
// We don't turn requires into Errors, because the closure compiler
// reorders the sources based on the requires.
// 'missingRequire',
],
});
};

module.exports = defaultCompile;
105 changes: 105 additions & 0 deletions deps/glift-core/dev/srcgen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';

const fs = require('fs');
const path = require('path');

/**
* Package dev is entirely used for sharing Gulp build-related code across
* various Glift-projects.
*/

//
// Beware! Below lie demons unvanquished.
//

/**
* Takes an ordering array and an ignore glob-array and generates a glob array
* appropriate for gulp. What this does is take an array of paths (both files
* and directorys), and recursively generates directory-based globs.
*
* Glift (and co) have an idiosyncratic way of genserating namespaces.
* Namepsaces are created files with the same name as a directory. For example,
* src/foo should have a file that defines glift.foo = {}; Thus, these files
* must go first before all other files that depend on that namespace.
*
* As an expanded example, consider the following directories:
*
* src/
* foo/
* abc.js
* foo.js
* zed.js
* bar/
* bbc.js
* bar.js
* zod.js
* biff/
* biff.js
* boff.js
*
* So, when called as such:
* jsSrcGlobGen(['src'])
*
* The following array would be produced:
* [
* 'src/*.js', 'src/foo/foo.js', 'src/foo/*.js', 'src/bar/bar.js',
* 'src/bar/biff/biff.js', 'src/bar/biff/*.js'
* ]
*
* Note: for convenience, users may pass in a set of normal node-glob style
* globs that will be appended to the generated globs.
*
* I.e., if jsSrcGlobGen is called with
* jsSrcGlobGen(['src'], ['!src/**' + '/*_test.js'])
*
* Then, assuming the directory structure above, the output array will be
* [ 'src/*.js', ..., !src/**' + ' /*_test.js']
*
* (note: Concatenation is used to avoid comment-breaks).
*/
var jsSrcGlobGen = function(ordering, addGlobs) {
if (typeof ordering !== 'object' || !ordering.length) {
throw new Error(
'Ordering must be a non-empty array of paths. ' +
'Was: ' + (typeof ordering) + ':' + String(ordering));
}

var out = [];
var addGlobs = addGlobs || [];

var rread = function(dirPath) {
var components = dirPath.split(path.sep);
var last = components[components.length - 1];

var nsfile = path.join(dirPath, last + '.js');
if (fs.existsSync(nsfile)) {
out.push(nsfile);
}
out.push(path.join(dirPath, '*.js'));

fs.readdirSync(dirPath).forEach((f) => {
var fpath = path.join(dirPath, f)
var fd = fs.lstatSync(fpath);
if (fd.isDirectory()) {
rread(fpath)
}
});
}

ordering.forEach((fpath) => {
if (!fs.existsSync(fpath)) {
console.warn('Path does not exist: ' + path);
return;
}
var fd = fs.lstatSync(fpath);
if (!fd.isDirectory()) {
out.push(fpath);
} else {
rread(fpath);
}
})

return out.concat(addGlobs);
};

module.exports = jsSrcGlobGen;
86 changes: 86 additions & 0 deletions deps/glift-core/dev/updatehtml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'strict'

const glob = require('glob');
const path = require('path');
const through = require('through2');
const fs = require('fs');

/**
* Package dev is entirely used for sharing Gulp build-related code across
* various Glift-projects.
*/

/**
* A function to update the HTML files. The idea is that updateHtmlFiles takes a
* glob of files and treats them as templates. It goes through and add
* sources to these files then outputs them to the specified outDir
*
* @param {string} filesGlob The glob of html files.
* @param {string} header The header marker to indicate where to dump the JS
* sources.
* @param {string} footer The footer marker to indicate where to dump the JS
* sources.
* @param {string} outDir the output dir for the templated files.
* @param {string} template the template to use.
*
* @return an object stream
* Note: this gets the 'srcs' as part of the Vinyl file stream.
*/
var updateHtmlFiles = function(params) {
var files = glob.sync(params.filesGlob);
var header = params.header;
var footer = params.footer;
var regexp = new RegExp(`(${header})(.|\n)*(${footer})`, 'g')
var outDir = params.outDir;

var dirHeader = params.dirHeader;
var all = [];
var template = params.template || '<script type="text/javascript" src="%s"></script>';

return through.obj(function(file, enc, cb) {
all.push(file);
cb();
}, function(cb) {
var htmldir = path.dirname(files[0])

var tags = [];
var lastdir = null
all.forEach((f) => {
var relpath = path.relative(htmldir, f.path)

var dir = path.dirname(f.path)
if (dir !== lastdir) {
tags.push(dirHeader.replace('%s', path.relative(htmldir, dir)))
lastdir = dir
}

tags.push(template.replace('%s', relpath))
this.push(f)
})

var text = tags.join('\n');

if (!fs.existsSync(outDir)){
fs.mkdirSync(outDir);
}

files.forEach((fname) => {
var parsedPath = path.parse(fname)
var outPath = path.join(outDir, parsedPath.base)
if (!fs.existsSync(outPath)) {
// First we write the template files.
var contents = fs.readFileSync(fname, {encoding: 'UTF-8'})
fs.writeFileSync(outPath, contents)
}
// Then, read from the newly-written file and overwrite the template
// sections.
var contents = fs.readFileSync(outPath, {encoding: 'UTF-8'})
var replaced = contents.replace(regexp, '$1\n' + text + '\n$3')
fs.writeFileSync(outPath, replaced)
});

cb();
})
};

module.exports = updateHtmlFiles;
27 changes: 27 additions & 0 deletions deps/glift-core/flattener/flattener.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ glift.flattener = {};
* - clearMarks: Whether to clear all the marks from the diagram. Note: this
* only affects marks and labels that are in the SGF and doesn't affect
* next-move-path labels (since that's the whole point of a next-moves-path.)
* - ignoreLabels: Whether to ignore any label-mark suggestions. This has the
* effect of clearing all labels
*
* Options for problems
* - problemConditions: determine how to evaluate whether or not a position is
Expand All @@ -57,6 +59,7 @@ glift.flattener = {};
* showKoLocation: (boolean|undefined),
* problemConditions: (!glift.rules.ProblemConditions|undefined),
* clearMarks: (boolean|undefined),
* ignoreLabels: (boolean|undefined)
* }}
*/
glift.flattener.Options;
Expand Down Expand Up @@ -211,6 +214,10 @@ glift.flattener.flatten = function(movetreeInitial, opt_options) {
glift.flattener.markKo_(markMap, goban.getKo());
}

// Optionally clear all the labels in the map.
if (options.ignoreLabels) {
glift.flattener.clearLabels_(markMap);
}

// Finally! Generate the intersections double-array.
var board = glift.flattener.board.create(cropping, stoneMap, markMap);
Expand Down Expand Up @@ -319,6 +326,8 @@ glift.flattener.stoneMap_ = function(goban, nextStones) {


/**
* Tracker for labels and symbols overlayed on stones
*
* Example value:
* {
* marks: {
Expand Down Expand Up @@ -623,3 +632,21 @@ glift.flattener.markKo_ = function(markMap, koLocation) {
}
}
};


/**
* Clear all the labels from a mark map.
*
* @param {!glift.flattener.MarkMap} markMap
* @private
*/
glift.flattener.clearLabels_ = function(markMap) {
var marks = markMap.marks;
for (var key in marks) {
var symbol = marks[key];
if (symbol === glift.flattener.symbols.TEXTLABEL) {
delete marks[key];
}
}
markMap.labels = {};
}
19 changes: 19 additions & 0 deletions deps/glift-core/flattener/flattener_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@
deepEqual(i.textLabel(), '2');
});

test('Simple next moves + ignoring labeling', function() {
var basicSgf = '(;GB[1];B[aa]C[zo];W[ab]C[zed])';
var mt = glift.rules.movetree.getFromSgf(basicSgf);
var f = flattener.flatten(mt, {
nextMovesPath: [0,0],
ignoreLabels: true,
});

var i = f.board().getIntBoardPt(toPt('aa'));
deepEqual(i.stone(), symb.BSTONE);
deepEqual(i.mark(), symb.EMPTY);
deepEqual(i.textLabel(), null);

i = f.board().getIntBoardPt(toPt('ab'));
deepEqual(i.stone(), symb.WSTONE);
deepEqual(i.mark(), symb.EMPTY);
deepEqual(i.textLabel(), null);
});

test('Init position', function() {
var basicSgf = '(;GB[1];B[aa]C[zo];W[ab]C[zed])';
var mt = glift.rules.movetree.getFromSgf(basicSgf);
Expand Down
2 changes: 1 addition & 1 deletion deps/glift-core/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ glift.global = {
*
* Not yet stable.
*/
'core-version': '0.9.1'
'core-version': '0.9.2'
};
Loading

0 comments on commit 64beb68

Please sign in to comment.