maestro-native
Version:
Avoka Native Component Build Tools
123 lines (109 loc) • 5.73 kB
JavaScript
const gulp = require('gulp');
const zip = require('gulp-zip');
const watch = require('gulp-watch');
const rename = require("gulp-rename");
const gulpif = require('gulp-if');
const toObject = require("gulp-properties-to-object");
const util = require('gulp-util');
const replace = require('gulp-token-replace');
const debug = require('gulp-debug');
const fs = require('fs');
const path = require('path');
var properties = {};
// strips whitespace from property values
// adds timestamp and date properties
function normalize(object) {
for (var property in object) {
object[property] = object[property].trim ? object[property].trim() : object[property];
}
var now = new Date();
object.TIMESTAMP = Math.floor(now.getTime());
object.TODAY = now.toISOString().slice(0, 10);
}
// 'new' task - generates a new component from the template folder
// run by 'gulp new [-templateName]'
// if no -templateName argument supplied, '-default' is used
// library.properties is used to populate .library-config.xml
// new-component.properties is used to populate component file content and folder name
// - edit this file before running the 'new' task
// - existing output files will be overwritten without warning
gulp.task('new', () => {
gulp.src("*.properties").pipe(toObject(properties)).on('finish', function () {
normalize(properties);
let templateName = process.argv[3] || '-default';
util.log('Generating new component using template: ' + templateName);
gulp.src(['template/' + templateName.substring(1) + '/*.*','*.properties'], { dot: true })
.pipe(gulpif(function (file) { return !file.path.endsWith('.png') }, replace({ global: properties, prefix:'@@', suffix:'@@' })))
.pipe(rename(function (path) {
var componentName = properties['component_filename'];
if (!path.extname.endsWith('xml')) {
path.dirname += "/widgets/" + componentName;
}
path.basename = path.basename.replace('template', componentName);
util.log('Processed: ' + path.dirname + '/' + path.basename);
}))
.pipe(gulp.dest('src'));
});
});
// 'zip' task - packages 'src' folder hierarchy into a Maestro library zip file
gulp.task('zip', () =>
gulp.src("*.properties").pipe(toObject(properties)).on('finish', function () {
normalize(properties);
gulp.src(['src/**/*','!src/**/*.properties'], { dot: true })
.pipe(zip('maestro-lib-' + properties['library_code'] + '.zip'))
.pipe(gulp.dest('dist'));
util.log('Library output: ', util.colors.magenta(__dirname + '\\dist\\maestro-lib-' + properties['library_code'] + '.zip'));
})
);
// 'one' task - packages a single named widget into a Maestro library zip file
// run by 'gulp one -widgetFolderName'
gulp.task('one', () => {
let widgetName = (process.argv[3] || '-error').substring(1).trim();
util.log('Widget: "' + widgetName + '"');
gulp.src(["src/widgets/" + widgetName + "/*.properties"]).pipe(toObject(properties)).on('finish', function () {
normalize(properties);
gulp.src(['src/*.xml','src/widgets/' + widgetName + '/*','!src/**/*.properties'], { dot: true, base: 'src/' })
.pipe(zip('maestro-lib-' + properties['library_code'] + '.zip'))
.pipe(gulp.dest('dist'));
util.log('Library output: ', util.colors.magenta(__dirname + '\\dist\\maestro-lib-' + properties['library_code'] + '.zip'));
})}
);
function getWidgets(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// 'all' task - packages all widgets in the src/ folder into separate Maestro library zip files
// run by 'gulp all'
gulp.task('all', () => {
getWidgets('src/widgets').map(function (widgetName) {
util.log('Widget: "' + widgetName + '"');
gulp.src(["src/widgets/" + widgetName + "/*.properties"]).pipe(toObject(properties)).on('finish', function () {
normalize(properties);
const libraryName = 'maestro-lib-' + (properties['library_code'] || widgetName) + '.zip';
gulp.src(['src/*.xml', 'src/widgets/' + widgetName + '/*', '!src/**/*.properties'], { dot: true, base: 'src/' })
.pipe(zip(libraryName))
.pipe(gulp.dest('dist'));
util.log('Library output: ', util.colors.magenta(__dirname + '\\dist\\' + libraryName));
})
}
)});
// 'watch' task - watches folder hierarchy for changes and runs a task when changes are detected
// watches the src folder hierarchy and runs 'zip' task, or the task you provide with the -taskName option
// watches new-component.properties file and runs 'new' task
gulp.task('watch', function () {
let taskName = (process.argv[3] || '-zip').substring(1).trim();
gulp.watch('src/**/*', [taskName]);
gulp.watch('new-component.properties', ['new']);
});
// creates a redistributable package with all templates
// package can be installed by installing nodejs, unzip package to a folder, run 'npm install', then run gulp new [-templateName]
gulp.task('redist', () =>
gulp.src(['gulpfile.js', 'library.properties', 'new-component.properties', 'package.json', 'template/**'], { base: '.', dot: true }, { dot: true })
.pipe(zip('avoka-native-component-tools.zip'))
.pipe(gulp.dest('.'))
);
gulp.task('default', () => {
util.log('You need to provide a task name. Tasks are new, zip, one, all, redist or watch.');
});