aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/gulpfile.js
blob: 34b567d62ad7c1e7372eae19e5e784ca4dc4686f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Based on the gulpfile provided by angular team
// (https://github.com/angular/ts2dart/blob/master/gulpfile.js)
var gulp = require('gulp');
var ts = require('gulp-typescript');
var typescript = require('typescript');
var gutil = require('gulp-util');
var mochaPhantomJS = require('gulp-mocha-phantomjs');
var tslint = require('gulp-tslint');
var server = require('gulp-server-livereload');
var concat = require('gulp-concat');
var merge = require('merge2');
var gulpFilter = require('gulp-filter');
var vulcanize = require('gulp-vulcanize');
var rename = require('gulp-rename');
var minimist = require('minimist');
var replace = require('gulp-replace');

var options = minimist(process.argv.slice(2), {
  default: {
    p: 8000  // port for gulp server
  }
});

var tsProject = ts.createProject('tsconfig.json', {
  typescript: typescript,
  noExternalResolve: true, // opt-in for faster compilation!
});

var hasError;
var failOnError = true; // Is set to false when watching.

var onError = function(err) {
  hasError = true;
  gutil.log(err.message);
  if (failOnError) {
    process.exit(1);
  }
};

gulp.task('compile.all', function() {
  hasError = false;
  var isComponent = gulpFilter(['components/**/*.js', '!components/**/test/*']);
  var isApp = gulpFilter(['app/**/*.js']);
  var isTest = gulpFilter(['test/**/*', 'components/**/test/*']);

  var srcs = ['components/**/*.ts', 'test/**/*.ts', 'app/**/*.ts',
              'typings/**/*.d.ts', 'bower_components/**/*.d.ts'];

  var tsResult = gulp.src(srcs, {base: '.'})
                     .pipe(ts(tsProject))
                     .on('error', onError);
  return merge([
    // Send concatenated component code to build/component
    tsResult.js
            .pipe(isComponent)
            .pipe(concat('components.js'))
            .pipe(gulp.dest('build')),

    // Duplicate all component code to live next to the ts file
    // (makes polymer imports very clean)
    tsResult.js
            .pipe(isComponent)
            .pipe(gulp.dest('.')),

    tsResult.js
            .pipe(isApp)
            .pipe(gulp.dest('.')),

    // Send all test code to build/test.js
    tsResult.js
            .pipe(isTest)
            .pipe(concat('test.js'))
            .pipe(gulp.dest('build')),

    // Create a unified defintions file at build/all.d.ts
    tsResult.dts
            .pipe(concat('all.d.ts'))
            .pipe(gulp.dest('build')),
  ]);
});

var tslintTask = function(strict) {
  return function(done) {
    if (hasError) {
      done();
      return;
    }
    return gulp.src(['components/**/*.ts', 'test/**/*.ts'])
               .pipe(tslint())
               .pipe(tslint.report('verbose', {
                  emitError: strict,
               }));
 };
};

// Since constructs like console.log are disabled by tslint
// but very useful while developing, create a "permissive"
// version of tslint that warns without erroring, for the
// watch task.
gulp.task('tslint-permissive', [], tslintTask(false));
gulp.task('tslint-strict', [], tslintTask(true));


gulp.task('run-tests', ['compile.all'], function(done) {
  if (hasError) {
    done();
    return;
  }
  return gulp.src('tests.html')
    .pipe(mochaPhantomJS({reporter: 'dot'}));
});

gulp.task('test', ['run-tests', 'tslint-strict']);
gulp.task('watch', ['run-tests', 'tslint-permissive'], function() {
  failOnError = false;
  // Avoid watching generated .d.ts in the build (aka output) directory.
  return gulp.watch(['test/**/*.ts', 'components/**/*.ts'],
          {ignoreInitial: true},
          ['run-tests', 'tslint-permissive']);
});

gulp.task('server', function() {
  gulp.src('.')
      .pipe(server({
        host: '0.0.0.0',
        port: options.p,
        livereload: {
          enable: true,
          port: 27729 + options.p
        },
        directoryListing: true,
      }));
});


var linkRegex = /<link rel="[^"]*" (type="[^"]*" )?href=".*bower_components[^"]*">\n/g;
var scriptRegex = /<script src=".*bower_components[^"]*"><\/script>\n/g;
gulp.task('vulcanize', ['compile.all', 'tslint-strict'], function() {
      gulp.src('app/tf-tensorboard.html')
          .pipe(vulcanize({
            inlineScripts: true,
            inlineCss: true,
            stripComments: true,
            excludes: ['/bower_components/'],
          }))
          // TODO(danmane): Remove this worrysome brittleness when vulcanize
          // fixes https://github.com/Polymer/vulcanize/issues/273
          .pipe(replace(linkRegex, ''))
          .pipe(replace(scriptRegex, ''))
          .pipe(gulp.dest('dist'));

      gulp.src('app/index.html')
          .pipe(vulcanize({
            inlineScripts: true,
            inlineCss: true,
            stripComments: true,
          }))
          .pipe(gulp.dest('dist'));

      gulp.src('app/tf-tensorboard-demo.html')
          .pipe(vulcanize({
            inlineScripts: true,
            inlineCss: true,
            stripComments: true,
          }))
          .pipe(gulp.dest('dist'));
});

gulp.task('serve', ['server']); // alias
gulp.task('default', ['watch']);