Skip to content

Commit

Permalink
Move to tasks/, see #1459
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Sep 12, 2024
1 parent 40995be commit 6b1e5e5
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 108 deletions.
115 changes: 12 additions & 103 deletions js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,11 @@ const transpiler = new Transpiler( { silent: true } );

// eslint-disable-next-line require-statement-match
const { execSync } = require( 'child_process' );
const getPhetLibs = require( './getPhetLibs.js' );
const getPhetLibs = require( './getPhetLibs' );

module.exports = function( grunt ) {
const packageObject = grunt.file.readJSON( 'package.json' );

// Handle the lack of build.json
let buildLocal;
try {
buildLocal = grunt.file.readJSON( `${process.env.HOME}/.phet/build-local.json` );
}
catch( e ) {
buildLocal = {};
}

const repo = grunt.option( 'repo' ) || packageObject.name;
assert( typeof repo === 'string' && /^[a-z]+(-[a-z]+)*$/u.test( repo ), 'repo name should be composed of lower-case characters, optionally with dashes used as separators' );

Expand Down Expand Up @@ -160,99 +151,7 @@ Minify-specific options:
--minify.stripAssertions=false - During uglification, it will strip assertions.
--minify.stripLogging=false - During uglification, it will not strip logging statements.
`,
wrapTask( async () => {
const buildStandalone = require( './buildStandalone' );
const buildRunnable = require( './buildRunnable' );
const minify = require( './minify' );
const tsc = require( './tsc' );
const reportTscResults = require( './reportTscResults' );
const path = require( 'path' );
const fs = require( 'fs' );
const getPhetLibs = require( './getPhetLibs' );
const phetTimingLog = require( '../../../perennial-alias/js/common/phetTimingLog' );

await phetTimingLog.startAsync( 'grunt-build', async () => {

// Parse minification keys
const minifyKeys = Object.keys( minify.MINIFY_DEFAULTS );
const minifyOptions = {};
minifyKeys.forEach( minifyKey => {
const option = grunt.option( `minify.${minifyKey}` );
if ( option === true || option === false ) {
minifyOptions[ minifyKey ] = option;
}
} );

const repoPackageObject = grunt.file.readJSON( `../${repo}/package.json` );

// Run the type checker first.
const brands = getBrands( grunt, repo, buildLocal );

!grunt.option( 'noTSC' ) && await phetTimingLog.startAsync( 'tsc', async () => {

// We must have phet-io code checked out to type check, since simLauncher imports phetioEngine
// do NOT run this for phet-lib, since it is type-checking things under src/, which is not desirable.
if ( ( brands.includes( 'phet-io' ) || brands.includes( 'phet' ) ) && repo !== 'phet-lib' ) {
const results = await tsc( `../${repo}` );
reportTscResults( results, grunt );
}
else {
grunt.log.writeln( 'skipping type checking' );
}
} );

!grunt.option( 'noTranspile' ) && await phetTimingLog.startAsync( 'transpile', () => {
// If that succeeds, then convert the code to JS
transpiler.transpileRepos( getPhetLibs( repo ) );
} );

// standalone
if ( repoPackageObject.phet.buildStandalone ) {
grunt.log.writeln( 'Building standalone repository' );

const parentDir = `../${repo}/build/`;
if ( !fs.existsSync( parentDir ) ) {
fs.mkdirSync( parentDir );
}

fs.writeFileSync( `${parentDir}/${repo}.min.js`, await buildStandalone( repo, minifyOptions ) );

// Build a debug version
minifyOptions.minify = false;
minifyOptions.babelTranspile = false;
minifyOptions.uglify = false;
minifyOptions.isDebug = true;
fs.writeFileSync( `${parentDir}/${repo}.debug.js`, await buildStandalone( repo, minifyOptions, true ) );

if ( repoPackageObject.phet.standaloneTranspiles ) {
for ( const file of repoPackageObject.phet.standaloneTranspiles ) {
fs.writeFileSync( `../${repo}/build/${path.basename( file )}`, minify( grunt.file.read( file ) ) );
}
}
}
else {

const localPackageObject = grunt.file.readJSON( `../${repo}/package.json` );
assert( localPackageObject.phet.runnable, `${repo} does not appear to be runnable` );
grunt.log.writeln( `Building runnable repository (${repo}, brands: ${brands.join( ', ' )})` );

// Other options
const allHTML = true; // Always build this artifact
const encodeStringMap = grunt.option( 'encodeStringMap' ) !== false;
const compressScripts = !!grunt.option( 'compressScripts' );
const profileFileSize = !!grunt.option( 'profileFileSize' );
const localesOption = grunt.option( 'locales' ) || 'en'; // Default back to English for now

for ( const brand of brands ) {
grunt.log.writeln( `Building brand: ${brand}` );

await phetTimingLog.startAsync( 'build-brand-' + brand, async () => {
await buildRunnable( repo, minifyOptions, allHTML, brand, localesOption, buildLocal, encodeStringMap, compressScripts, profileFileSize );
} );
}
}
} );
} )
wrapExecSync( `node ../chipper/js/grunt/tasks/build.js --repo ${repo}` )
);

grunt.registerTask( 'generate-used-strings-file',
Expand Down Expand Up @@ -323,6 +222,15 @@ Minify-specific options:

const getPhetLibs = require( './getPhetLibs' );

// Handle the lack of build.json
let buildLocal;
try {
buildLocal = grunt.file.readJSON( `${process.env.HOME}/.phet/build-local.json` );
}
catch( e ) {
buildLocal = {};
}

const brands = getBrands( grunt, repo, buildLocal );

const lintReturnValue = await lint( getPhetLibs( repo, brands ), {
Expand Down Expand Up @@ -743,6 +651,7 @@ Updates the normal automatically-generated files for this repository. Includes:
].forEach( forwardToPerennialGrunt );
};

// TODO: https://github.com/phetsims/chipper/issues/1459 move to getBrands.js
const getBrands = ( grunt, repo, buildLocal ) => {

// Determine what brands we want to build
Expand Down
118 changes: 118 additions & 0 deletions js/grunt/tasks/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2024, University of Colorado Boulder

/**
* @author Sam Reid (PhET Interactive Simulations)
*/

const buildStandalone = require( '../buildStandalone' );
const buildRunnable = require( '../buildRunnable' );
const minify = require( '../minify' );
const tsc = require( '../tsc' );
const reportTscResults = require( '../reportTscResults' );
const path = require( 'path' );
const fs = require( 'fs' );
const getPhetLibs = require( '../getPhetLibs' );
const phetTimingLog = require( '../../../../perennial-alias/js/common/phetTimingLog' );

const grunt = require( 'grunt' );
const getRequiredArg = require( './getRequiredArg' );
const assert = require( 'assert' );
const Transpiler = require( '../../common/Transpiler' );
const getBrands = require( './getBrands' );

const repo = getRequiredArg( '--repo' );

const transpiler = new Transpiler( { silent: true } );

( async () => {
await phetTimingLog.startAsync( 'grunt-build', async () => {

// Parse minification keys
const minifyKeys = Object.keys( minify.MINIFY_DEFAULTS );
const minifyOptions = {};
minifyKeys.forEach( minifyKey => {
const option = grunt.option( `minify.${minifyKey}` );
if ( option === true || option === false ) {
minifyOptions[ minifyKey ] = option;
}
} );

const repoPackageObject = grunt.file.readJSON( `../${repo}/package.json` );

// Handle the lack of build.json
let buildLocal;
try {
buildLocal = grunt.file.readJSON( `${process.env.HOME}/.phet/build-local.json` );
}
catch( e ) {
buildLocal = {};
}

// Run the type checker first.
const brands = getBrands( grunt, repo, buildLocal );

!grunt.option( 'noTSC' ) && await phetTimingLog.startAsync( 'tsc', async () => {

// We must have phet-io code checked out to type check, since simLauncher imports phetioEngine
// do NOT run this for phet-lib, since it is type-checking things under src/, which is not desirable.
if ( ( brands.includes( 'phet-io' ) || brands.includes( 'phet' ) ) && repo !== 'phet-lib' ) {
const results = await tsc( `../${repo}` );
reportTscResults( results, grunt );
}
else {
grunt.log.writeln( 'skipping type checking' );
}
} );

!grunt.option( 'noTranspile' ) && await phetTimingLog.startAsync( 'transpile', () => {
// If that succeeds, then convert the code to JS
transpiler.transpileRepos( getPhetLibs( repo ) );
} );

// standalone
if ( repoPackageObject.phet.buildStandalone ) {
grunt.log.writeln( 'Building standalone repository' );

const parentDir = `../${repo}/build/`;
if ( !fs.existsSync( parentDir ) ) {
fs.mkdirSync( parentDir );
}

fs.writeFileSync( `${parentDir}/${repo}.min.js`, await buildStandalone( repo, minifyOptions ) );

// Build a debug version
minifyOptions.minify = false;
minifyOptions.babelTranspile = false;
minifyOptions.uglify = false;
minifyOptions.isDebug = true;
fs.writeFileSync( `${parentDir}/${repo}.debug.js`, await buildStandalone( repo, minifyOptions, true ) );

if ( repoPackageObject.phet.standaloneTranspiles ) {
for ( const file of repoPackageObject.phet.standaloneTranspiles ) {
fs.writeFileSync( `../${repo}/build/${path.basename( file )}`, minify( grunt.file.read( file ) ) );
}
}
}
else {

const localPackageObject = grunt.file.readJSON( `../${repo}/package.json` );
assert( localPackageObject.phet.runnable, `${repo} does not appear to be runnable` );
grunt.log.writeln( `Building runnable repository (${repo}, brands: ${brands.join( ', ' )})` );

// Other options
const allHTML = true; // Always build this artifact
const encodeStringMap = grunt.option( 'encodeStringMap' ) !== false;
const compressScripts = !!grunt.option( 'compressScripts' );
const profileFileSize = !!grunt.option( 'profileFileSize' );
const localesOption = grunt.option( 'locales' ) || 'en'; // Default back to English for now

for ( const brand of brands ) {
grunt.log.writeln( `Building brand: ${brand}` );

await phetTimingLog.startAsync( 'build-brand-' + brand, async () => {
await buildRunnable( repo, minifyOptions, allHTML, brand, localesOption, buildLocal, encodeStringMap, compressScripts, profileFileSize );
} );
}
}
} );
} )();
40 changes: 40 additions & 0 deletions js/grunt/tasks/getBrands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024, University of Colorado Boulder

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const assert = require( 'assert' );

const getBrands = ( grunt, repo, buildLocal ) => {

// Determine what brands we want to build
assert( !grunt.option( 'brand' ), 'Use --brands={{BRANDS}} instead of brand' );

const localPackageObject = grunt.file.readJSON( `../${repo}/package.json` );
const supportedBrands = localPackageObject.phet.supportedBrands || [];

let brands;
if ( grunt.option( 'brands' ) ) {
if ( grunt.option( 'brands' ) === '*' ) {
brands = supportedBrands;
}
else {
brands = grunt.option( 'brands' ).split( ',' );
}
}
else if ( buildLocal.brands ) {
// Extra check, see https://github.com/phetsims/chipper/issues/640
assert( Array.isArray( buildLocal.brands ), 'If brands exists in build-local.json, it should be an array' );
brands = buildLocal.brands.filter( brand => supportedBrands.includes( brand ) );
}
else {
brands = [ 'adapted-from-phet' ];
}

// Ensure all listed brands are valid
brands.forEach( brand => assert( supportedBrands.includes( brand ), `Unsupported brand: ${brand}` ) );

return brands;
};

module.exports = getBrands;
4 changes: 1 addition & 3 deletions js/grunt/tasks/output-js-all.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const Transpiler = require( '../../common/Transpiler' );
const transpiler = new Transpiler();
const transpiler = new Transpiler( { silent: true } );

transpiler.transpileAll();
2 changes: 1 addition & 1 deletion js/grunt/tasks/output-js-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getRequiredArg = require( './getRequiredArg' );
* @author Sam Reid (PhET Interactive Simulations)
*/
const Transpiler = require( '../../common/Transpiler' );
const transpiler = new Transpiler();
const transpiler = new Transpiler( { silent: true } );

const repos = getRequiredArg( '--repos' );

Expand Down
2 changes: 1 addition & 1 deletion js/grunt/tasks/output-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getRequiredArg = require( './getRequiredArg' );
* @author Sam Reid (PhET Interactive Simulations)
*/
const Transpiler = require( '../../common/Transpiler' );
const transpiler = new Transpiler();
const transpiler = new Transpiler( { silent: true } );

const repo = getRequiredArg( '--repo' );

Expand Down

0 comments on commit 6b1e5e5

Please sign in to comment.