Skip to content

Instantly share code, notes, and snippets.

@identy
Created January 24, 2016 15:23
Show Gist options
  • Save identy/e38e159e8d9e03eb1243 to your computer and use it in GitHub Desktop.
Save identy/e38e159e8d9e03eb1243 to your computer and use it in GitHub Desktop.
gulp cordova tasks for vscode
/* ------------------------------------------ */
/*
id :: gwt.gulp
develop :: gulp tasks for vscode
authors :: Sigfrido Rodríguez and Thomas Roux,
*/
/* ------------------------------------------ */
/*---------- Declare gulp variables ----------*/
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var csslint = require('gulp-csslint');
//var compass = require('gulp-compass');
var connect = require('gulp-connect'), path = require('path');
var livereload = require('gulp-livereload');
var clean = require('gulp-clean');
var usemin = require('gulp-usemin');
var useref = require('gulp-useref');
var rev = require('gulp-rev');
var uglify = require('gulp-uglify');
var minifyCss = require('gulp-minify-css');
var minifyHtml = require('gulp-minify-html');
var imagemin = require('gulp-imagemin');
var watch = require('gulp-watch');
var cordova = require("cordova-lib").cordova;
var cordova_raw = cordova.raw;
var packages = require('./package.json');
const create = require('gulp-cordova-create');
const plugin = require('gulp-cordova-plugin');
const android = require('gulp-cordova-build-android');
const ios = require('gulp-cordova-build-ios');
var taco = require("taco-team-build");
var bower = require("gulp-bower");
var ts = require('gulp-typescript');
var less = require('gulp-less');
var sass = require('gulp-sass');
var gulpif = require('gulp-if');
var rename = require('gulp-rename');
var gutil = require('gulp-util');
var merge = require('merge2');
var es = require('event-stream');
var _ = require('lodash');
var $ = require('gulp-load-plugins')({
camelize: true
});
// run a command in a shell
var exec = require('child_process').exec;
/*---------- Declare paths ----------*/
/* ¿ vscode env */
var environment = {
title: 'financial',
deploy: './release/',
paths: {
src: {
sass: './www/styles/sass/**/*.scss',
css: './www/styles/*.css',
scripts: './www/scripts/**/*.js',
images: './www/images/*',
html: './www/*.html'
},
device: {
plugins: './plugins',
platforms: './plaforms',
source: './release',
production: './production',
},
dist: {
css: './release/styles',
scripts: './release/scripts',
images: './release/images',
html: './release'
}
}
};
/*---------- Tasks ----------*/
// CSS Lint
gulp.task('csslint', function () {
return gulp.src(environment.paths.src.css)
.pipe(csslint())
.pipe(csslint.reporter());
});
// JS Lint
gulp.task('jshint', function () {
return gulp.src(environment.paths.src.scripts)
.pipe(jshint())
//.pipe(jshint.reporter('jshint-stylish'));
.pipe(jshint.reporter('default'));
});
// usemin : Concat & Minify Scripts un add revision to avoid cache
gulp.task('use.package', function (done) {
return gulp.src(environment.paths.src.html)
.pipe(usemin({
//css: [ rev() ],
//css: [minifyCss(), 'concat', rev()],
//css: [ function() { return minifyCss({keepSpecialComments: 0}); }, 'concat', rev],
css: [minifyCss(), 'concat', rev()],
//html: [ function () {return minifyHtml({ empty: true });} ],
html: [minifyHtml({ empty: true, quotes: true })],
//js: [ jshint.reporter('default'), uglify(), rev() ],
//js: [ uglify().on('error', function(e) { console.log(e); }), rev() ],
//js: [ uglify().on('error', gutil.log), rev() ],
js: [$.ngmin(), $.uglify().on('error', gutil.log), $.rev()],
inlinejs: [uglify()],
modernizr: [uglify()],
inlinecss: [minifyCss(), 'concat']
}))
.pipe(gulp.dest(environment.paths.dist.html))
.on('end', done);
});
// useref : Concat & Minify Scripts un add revision to avoid cache
gulp.task('ref.package', function (done) {
return gulp.src(environment.paths.src.html)
.pipe(useref())
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(gulp.dest(environment.paths.dist.html))
.on('end', done);
});
// Minify Images
gulp.task('images.package', function () {
return gulp.src(environment.paths.src.images)
.pipe(imagemin({
optimizationLevel: 5,
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(environment.paths.dist.images));
});
// Clean Build Folder
gulp.task('clean.package', function () {
//return gulp.src([environment.paths.dist.html], {read: false}).pipe(clean());
return gulp.src(environment.paths.dist.html, {
read: false
}).pipe($.clean());
});
// Compass
/*
gulp.task('compass', function(){
return gulp.src(environment.paths.src.sass)
.pipe(compass({
config_file: './config.rb',
css: './www/styles/css',
sass: './www/styles/sass'
}))
.pipe(gulp.dest(environment.paths.src.css));
});
*/
// Create Server
/*
gulp.task('connect', connect.server({
root: [ path.join(__dirname)],
port: 1337,
//livereload: true,
open: {
file: 'index.html',
browser: 'Google Chrome'
}
}));
*/
// Watch Files For Changes & Livereload
gulp.task('watch', function () {
var server = livereload();
//gulp.watch(environment.paths.src.sass, ['compass']);
gulp.watch([environment.paths.src.css, environment.paths.src.scripts, environment.paths.src.images, environment.paths.src.html]).on('change', function (file) {
server.changed(file.path);
});
});
// device
gulp.task('device.run', function (cb) {
return cordova_raw.run({ platforms: ['android'], options: ['--device'] });
});
gulp.task('device.emulate', function () {
return cordova_raw.emulate({ platforms: ['android'] });
});
gulp.task('device.release', function () {
return cordova_raw.build({ platforms: ['android'], options: ['--release'] });
});
// cordova init
gulp.task('cordova:init', function () {
gulp.src('./package.json')
.pipe(cordova())
});
gulp.task('cordova.build', function (done) {
cordova.build({
"platforms": ["android"],
"options": {
argv: ["--release", "--gradleArg=--no-daemon"]
}
}, done);
});
gulp.task('cordova.plugins.packages', (done) => {
return gulp.src(environment.paths.device.plugins)
.pipe(plugin(packages.plugins))
.pipe(gulp.dest(environment.paths.device.plugins))
.on('end', done);
});
gulp.task('cordova.plugins', (done) => {
return gulp.src(environment.paths.device.plugins)
//.pipe(create())
//.pipe(plugin('org.apache.cordova.camera', 'latest'))
//.pipe(plugin('plugin.google.maps', {variables: {'API_KEY_FOR_ANDROID': '...', 'API_KEY_FOR_IOS': '...'}}))
.pipe(plugin({
'cordova-plugin-camera': 'latest',
'cordova-plugin-console': 'latest',
'cordova-plugin-device': 'latest',
'cordova-plugin-device-orientation': 'latest',
'cordova-plugin-device-motion': 'latest',
'cordova-plugin-dialogs': 'latest',
'cordova-plugin-file': 'latest',
'cordova-plugin-file-transfer': 'latest',
'cordova-plugin-geolocation': 'latest',
'cordova-plugin-globalization': 'latest',
'cordova-plugin-inappbrowser': 'latest',
'cordova-plugin-keyboard': 'latest',
'cordova-plugin-media': 'latest',
'cordova-plugin-media-capture': 'latest',
'cordova-plugin-navigationbar': 'latest',
'cordova-plugin-network-information': 'latest',
'cordova-plugin-splashscreen': 'latest',
'cordova-plugin-statusbar': 'latest',
'cordova-plugin-whitelist': 'latest',
'cordova-plugin-googlemaps': { version: 'latest', variables: { 'API_KEY_FOR_ANDROID': '', 'API_KEY_FOR_IOS': '' } },
'de.sitewaerts.cordova.documentviewer': 'latest',
'ionic-plugin-keyboard': 'latest',
'phonegap-plugin-push': 'latest',
'msopentech.azure.NotificationHub': 'latest'
}))
.pipe(gulp.dest(environment.paths.device.plugins))
.on('end', done);
});
gulp.task('cordova.plaforms', function () {
return gulp.src(environment.paths.device.platforms)
//.pipe(create())
.pipe(android({ storeFile: './key.keystore', keyAlias: '', release: true }))
.pipe(gulp.dest(environment.paths.device.platforms));
});
gulp.task("taco.run", function () {
taco.setupCordova().done(function (cordova) {
cordova.run({ platforms: ["android"], options: ["--nobuild"] }, function () {
// continue processing after run is complete
});
});
});
gulp.task("taco.build", function () {
//return taco.buildProject("android", ["--release", "--gradleArg=--no-daemon"])
return taco.buildProject(["android", "ios", "windows"], ["--gradleArg=--no-daemon"])
.then(function () { return taco.packageProject("android"); });
});
gulp.task("taco.build.release", function () {
//return taco.buildProject("android", ["--release", "--gradleArg=--no-daemon"])
return taco.buildProject(["android", "ios", "windows"], { android: ["--release", "--ant", "--gradleArg=--no-daemon"], windows: ["--release"] })
.then(function () { return taco.packageProject("android"); });
});
gulp.task("taco.plugins", function () {
taco.setupCordova().done(function (cordova) {
cordova.plugin("add", "org.apache.cordova.camera", function () {
// Continue processing after camera plugin has been added
});
});
});
// Optional Manifest file
gulp.task('manifest.package', function () {
gulp.src([environment.paths.dist.html + '/*'])
.pipe($.manifest({
hash: true,
preferOnline: true,
network: ['http://*', 'https://*', '*'],
filename: 'app.manifest',
exclude: ['app.manifest', 'index.html']
}))
.pipe(gulp.dest(environment.paths.dist.html));
});
/*
gulp.task('code.watch-less', function() {
gulp.watch('*.less', ['less']);
})
*/
/*
gulp.task('code.watch-sass', function() {
gulp.watch('*.sass', ['sass']);
})
*/
/*
gulp.task('code.watch-typescript', function() {
gulp.watch('*.ts', ['typescript']);
})
*/
/*
gulp.task('code.less.build', function() {
gulp.src('*.less')
.pipe(less())
.pipe(gulp.dest(function(f) {
return f.base;
}))
});
gulp.task('code.sass.build', function(done) {
gulp.src('*.scss')
//.pipe(sass())
.pipe(sass.sync().on('error', sass.logError))
//.pipe(gulp.dest(function(f) { return f.base; }))
.pipe(gulp.dest('./release/style'))
.pipe(minifyCss())
.pipe(rename({ extname: '.min.css' }))
.on('end', done);
});
gulp.task('code.typscript.merge', function() {
var tsResult = gulp.src('*.ts')
.pipe(ts({
declarationFiles: true,
noExternalResolve: true,
noImplicitAny: true,
out: 'main.js'
}));
return merge([
tsResult.dts.pipe(gulp.dest('release/definitions')),
tsResult.js.pipe(gulp.dest('release/scripts'))
]);
});
gulp.task('package.images.optimize', function () {
"use strict";
var DEST = 'release/images';
gulp.src('www/images')
.pipe(changed(DEST))
.pipe(imageResize({width: 1080}))
.pipe(imagemin({optimizationLevel: 4, progressive: true, interlace: true}))
.pipe(gulp.dest('release/images'));
});
*/
function failure(fail) {
console.log(fail.toString());
this.emit('end');
}
/*---------- Environements Tasks ----------*/
// Default Task
//gulp.task('default',['develop']);
gulp.task('default', ['production']);
//gulp.task('default', ['use.package'], function() {})
// Build Task
gulp.task('build', ['production']);
//gulp.task('build', ['use.package'], function() {});
// Development tasks
gulp.task('develop', ['connect', 'watch']);
// Production tasks
//gulp.task('production', ['taco.build','clean.package','jshint','images.package','use.package']);
gulp.task('production', ['clean.package', 'images.package', 'use.package']);
gulp.task('manifest', ['manifest.package'], function () {
//gulp.watch(environment.paths.src.html, ['build.package']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment