var gulp = require('gulp'),
  connect = require('gulp-connect'),
  rimraf = require('rimraf'),
  $ = require('gulp-load-plugins')();

var iconfont = {
  stylusFileName: '_font.styl'
};

var path = (function () {
  var _path = {
    source: 'app/',
    build: '_publish/'
  };

  _path.sourceHtml = _path.source + 'html/';
  _path.sourceStyle = _path.source + 'style/';
  _path.sourceScript = _path.source + 'script/';
  _path.sourceImage = _path.source + 'images/';
  _path.sourceFont = _path.source + 'font/';

  _path.buildHtml = _path.build;
  _path.buildStyle = _path.build + 'css/';
  _path.buildScript = _path.build + 'js/';
  _path.buildImage = _path.build + 'img/';
  _path.buildFont = _path.build + 'font/';

  return _path;
})();



gulp.task('html', function () {
  return gulp.src(path.sourceHtml + '*.html')
    .pipe($.plumber())
    .pipe($.minifyHtml({
      quotes: true
    }))
    .pipe(gulp.dest(path.buildHtml))
    .pipe(connect.reload());
});



gulp.task('css', function () {
  return gulp.src(path.sourceStyle + 'style.styl')
    .pipe($.plumber())
    .pipe($.stylus({
      import: ['tmp/' + iconfont.stylusFileName]
    }))
    .pipe($.autoprefixer())
    .pipe($.minifyCss())
    .pipe(gulp.dest(path.buildStyle))
    .pipe(connect.reload());
});



gulp.task('png-min', ['png-clone-no-compress'],function () {
  return gulp.src([path.sourceImage + '**/*.png', '!' + path.sourceImage + '**/*-no-compress.png'])
    .pipe($.optipng(['-o2', '-strip all']))
    .pipe(gulp.dest(path.buildImage));
});



gulp.task('png-clone-no-compress', function () {
  return gulp.src([path.sourceImage + '**/*-no-compress.png'])
    .pipe(gulp.dest(path.buildImage));
});



gulp.task('clean:font', function (cb) {
  rimraf(path.buildFont, cb);
});



gulp.task('iconfont', ['clean:font'], function () {
  return gulp.src(path.sourceFont + 'svg/*.svg')
    .pipe($.iconfont({
      fontName: 'myfont',
      // fixedWidth: true,
      centerHorizontally: true,
      normalize: true
    }))
    .on('codepoints', function(codepoints, options) {
      // console.log(codepoints, options);
      gulp.src(path.sourceStyle + '_font.styl-temp')
        .pipe($.consolidate('lodash', {
          glyphs: codepoints,
          fontName: 'myfont',
          fontPath: '../font/',
          className: 'appicon'
        }))
        .pipe($.rename(iconfont.stylusFileName))
        .pipe(gulp.dest(path.sourceStyle + 'tmp/'));
    })
    .pipe(gulp.dest(path.buildFont));
});



gulp.task('server', function () {
  connect.server({
    livereload: true
  });
});

gulp.task('watch', ['build'], function () {
  gulp.watch(path.sourceHtml + '*.html', ['html']);
  gulp.watch(path.sourceScript + '*.js', ['js']);
  gulp.watch(path.sourceStyle + '**/*.styl', ['css']);
});

gulp.task('deploy', ['build', 'png-min'], function () {
  gulp.src(path.build + '**/*')
    .pipe($.ghPages());
});

gulp.task('build', ['iconfont', 'html', 'css']);
gulp.task('dev', ['server', 'watch']);
gulp.task('default', ['build']);

@@ -1276,417 +1276,8 @@
 %0A%0A%0A%0A
-gulp.task('png-min', %5B'png-clone-no-compress'%5D,function () %7B%0A  return gulp.src(%5Bpath.sourceImage + '**/*.png', '!' + path.sourceImage + '**/*-no-compress.png'%5D)%0A    .pipe($.optipng(%5B'-o2', '-strip all'%5D))%0A    .pipe(gulp.dest(path.buildImage));%0A%7D);%0A%0A%0A%0Agulp.task('png-clone-no-compress', function () %7B%0A  return gulp.src(%5Bpath.sourceImage + '**/*-no-compress.png'%5D)%0A    .pipe(gulp.dest(path.buildImage));%0A%7D);%0A%0A%0A%0A
 gulp
@@ -2390,19 +2390,8 @@
 ild'
-, 'png-min'
 %5D, f

remove useless task: png minify