-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build flows rewrite to be independent of powershell and bash (#245
- Loading branch information
1 parent
6b68be0
commit 6b27ef8
Showing
40 changed files
with
1,219 additions
and
857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const AdmZip = require('adm-zip'); | ||
const childProcess = require('child_process'); | ||
const path = require('path'); | ||
|
||
const Messenger = require('@src/view/messenger'); | ||
|
||
class AbstractBuildFlow { | ||
/** | ||
* Constructor | ||
* @param {Object} options | ||
* @param {String} options.cwd working directory for build | ||
* @param {String} options.src source directory | ||
* @param {String} options.buildFile full path for zip file to generate | ||
* @param {Boolean} options.doDebug debug flag | ||
*/ | ||
constructor({ cwd, src, buildFile, doDebug }) { | ||
this.cwd = cwd; | ||
this.src = src; | ||
this.buildFile = buildFile; | ||
this.stdio = doDebug ? 'inherit' : 'pipe'; | ||
this.doDebug = !!doDebug; | ||
this.isWindows = process.platform === 'win32'; | ||
this.defaultZipFileDate = new Date(1990, 1, 1); | ||
} | ||
|
||
/** | ||
* Creates build zip file | ||
* @param {Object} options | ||
* @param {Object} options.filter filter to apply to exclude files from zip | ||
* @param {Function} callback | ||
*/ | ||
createZip(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
const filter = options.filter || (() => false); | ||
|
||
this.debug(`Zipping source files and dependencies to ${this.buildFile}.`); | ||
const zip = new AdmZip(); | ||
const zipFileName = path.basename(this.buildFile); | ||
|
||
// adding files | ||
zip.addLocalFolder(this.cwd, '', (entry) => entry !== zipFileName && !filter(entry)); | ||
// setting create timestamp to the same value to allow consistent hash | ||
zip.getEntries().forEach(e => { e.header.time = this.defaultZipFileDate; }); | ||
|
||
zip.writeZip(this.buildFile, callback); | ||
} | ||
|
||
/** | ||
* Modifies build zip file | ||
* @param {Object} options | ||
* @param {Object} options.entryProcessor function to apply to each zip file entry | ||
* @param {Function} callback | ||
*/ | ||
modifyZip(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
const zip = new AdmZip(this.buildFile); | ||
|
||
const entryProcessor = options.entryProcessor || (() => {}); | ||
|
||
zip.getEntries().forEach(e => { | ||
// setting create timestamp to the same value to allow consistent hash | ||
e.header.time = this.defaultZipFileDate; | ||
entryProcessor(e); | ||
}); | ||
zip.writeZip(this.buildFile, callback); | ||
} | ||
|
||
/** | ||
* Executes shell command | ||
* @param {String} cmd command | ||
*/ | ||
execCommand(cmd) { | ||
childProcess.execSync(cmd, { cwd: this.cwd, stdio: this.stdio }); | ||
} | ||
|
||
/** | ||
* Outputs debug message | ||
* @param {String} message message | ||
*/ | ||
debug(message) { | ||
if (this.doDebug) { | ||
Messenger.getInstance().debug(message); | ||
} | ||
} | ||
} | ||
|
||
module.exports = AbstractBuildFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
const AbstractBuildFlow = require('./abstract-build-flow'); | ||
|
||
class CustomBuildFlow extends AbstractBuildFlow { | ||
/** | ||
* If this file exists than the build flow can handle build | ||
*/ | ||
static get manifest() { return process.platform === 'win32' ? 'build.ps1' : 'build.sh'; } | ||
|
||
static get _customScriptPath() { return path.join(process.cwd(), 'hooks', CustomBuildFlow.manifest); } | ||
|
||
/** | ||
* Returns true if the build flow can handle the build | ||
*/ | ||
static canHandle() { | ||
return fs.existsSync(CustomBuildFlow._customScriptPath); | ||
} | ||
|
||
/** | ||
* Constructor | ||
* @param {Object} options | ||
* @param {String} options.cwd working directory for build | ||
* @param {String} options.src source directory | ||
* @param {String} options.buildFile full path for zip file to generate | ||
* @param {Boolean} options.doDebug debug flag | ||
*/ | ||
constructor({ cwd, src, buildFile, doDebug }) { | ||
super({ cwd, src, buildFile, doDebug }); | ||
} | ||
|
||
/** | ||
* Executes build | ||
* @param {Function} callback | ||
*/ | ||
execute(callback) { | ||
this.debug(`Executing custom hook script ${CustomBuildFlow._customScriptPath}.`); | ||
let command = `${CustomBuildFlow._customScriptPath} "${this.buildFile}" ${this.doDebug}`; | ||
if (this.isWindows) { | ||
const powerShellPrefix = 'PowerShell.exe -Command'; | ||
const doDebug = this.doDebug ? '$True' : '$False'; | ||
command = `${powerShellPrefix} "& {& '${CustomBuildFlow._customScriptPath}' '${this.buildFile}' ${doDebug} }"`; | ||
} | ||
this.execCommand(command); | ||
callback(); | ||
} | ||
} | ||
|
||
module.exports = CustomBuildFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
const AbstractBuildFlow = require('./abstract-build-flow'); | ||
|
||
class JavaMvnBuildFlow extends AbstractBuildFlow { | ||
/** | ||
* If this file exists than the build flow can handle build | ||
*/ | ||
static get manifest() { return 'pom.xml'; } | ||
|
||
/** | ||
* Returns true if the build flow can handle the build | ||
*/ | ||
static canHandle({ src }) { | ||
return fs.existsSync(path.join(src, JavaMvnBuildFlow.manifest)); | ||
} | ||
|
||
/** | ||
* Constructor | ||
* @param {Object} options | ||
* @param {String} options.cwd working directory for build | ||
* @param {String} options.src source directory | ||
* @param {String} options.buildFile full path for zip file to generate | ||
* @param {Boolean} options.doDebug debug flag | ||
*/ | ||
constructor({ cwd, src, buildFile, doDebug }) { | ||
super({ cwd, src, buildFile, doDebug }); | ||
} | ||
|
||
/** | ||
* Executes build | ||
* @param {Function} callback | ||
*/ | ||
execute(callback) { | ||
this.debug(`Building skill artifacts based on the ${JavaMvnBuildFlow.manifest}.`); | ||
this.execCommand('mvn clean org.apache.maven.plugins:maven-assembly-plugin:2.6:assembly ' | ||
+ '-DdescriptorId=jar-with-dependencies package'); | ||
const targetFolderPath = path.join(this.cwd, 'target'); | ||
const jarFileName = fs.readdirSync(targetFolderPath).find(fileName => fileName.endsWith('jar-with-dependencies.jar')); | ||
const jarFilePath = path.join(targetFolderPath, jarFileName); | ||
this.debug(`Renaming the jar file ${jarFilePath} to ${this.buildFile}.`); | ||
fs.moveSync(jarFilePath, this.buildFile, { overwrite: true }); | ||
|
||
this.modifyZip({ entryProcessor: this._removeCommentsFromPomProperties }, callback); | ||
} | ||
|
||
_removeCommentsFromPomProperties(entry) { | ||
// removing comment to allow consistent hashing | ||
if (entry.entryName.includes('pom.properties')) { | ||
const data = entry.getData().toString().replace(/^#.*\n?/mg, ''); | ||
entry.setData(data); | ||
} | ||
} | ||
} | ||
|
||
module.exports = JavaMvnBuildFlow; |
Oops, something went wrong.