phetsims / kite

A library for creating, manipulating and displaying 2D shapes in JavaScript.
MIT License
12 stars 6 forks source link

Harvest useful tasks from old Gruntfile #66

Closed samreid closed 7 years ago

samreid commented 7 years ago

In #65 I forgot to harvest the old Gruntfile for useful tasks.

samreid commented 7 years ago

It looks like the only task that was left behind is this one:

  grunt.registerTask( 'generate-svgPath-parser',
    'Uses js/parser/svgPath.pegjs to generate js/parser/svgPath.js',
    function() {
      var pegInput = fs.readFileSync( 'js/parser/svgPath.pegjs', 'utf8' );
      var source = pegjs.buildParser( pegInput ).toSource();

      // replace fixed strings at the start/end with our prefix/suffix, so that it will work nicely with require.js
      var prefix = '// NOTE: Generated from svgPath.pegjs using PEG.js, with added kite namespace and require.js compatibility.\n' +
                   '// See svgPath.pegjs for more documentation, or run \'grunt generate-svgPath-parser\' to regenerate.\n' +
                   '\n' +
                   'define( function( require ) {\n' +
                   '  var kite = require( \'KITE/kite\' );\n';
      var suffix = '  kite.register( \'svgPath\', result );\n' +
                   '  return kite.svgPath;\n' +
                   '} );\n';
      var toStripFromStart = '(function(){';
      var toStrimFromEnd = '  return result;\n})()';

      var startIndex = source.indexOf( toStripFromStart );
      if ( startIndex !== 0 ) {
        throw new Error( 'Could not find string to strip from the beginning of the PEG.js output' );
      }
      source = prefix + source.substring( startIndex + toStripFromStart.length );

      var endIndex = source.lastIndexOf( toStrimFromEnd );
      if ( endIndex === -1 ) {
        throw new Error( 'Could not find string to strip from the end of the PEG.js output' );
      }
      source = source.substring( 0, endIndex ) + suffix;

      // write the output
      fs.writeFileSync( 'js/parser/svgPath.js', source, 'utf8' );

      console.log( 'Please reformat the generated svgPath.js before checking in!' );
    } );

@jonathanolson should I resurrect it?

jonathanolson commented 7 years ago

That's one we'll need. If the code needs to live in chipper, would that mean peg-js would need to be in chipper's package.json, or the sim's package.json?

samreid commented 7 years ago

If the code needs to live in chipper

We could experiment with augmenting kite/Gruntfile.js globals before deferring to main gruntfile...

samreid commented 7 years ago

The strategy used in https://github.com/phetsims/dot/issues/64 will work well here.

samreid commented 7 years ago

I restored the path parser generator above, @jonathanolson can you please take it for a test drive?

jonathanolson commented 7 years ago

Looks great, thanks!