You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

docsite.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var bower = require('gulp-bower');
  2. var config = require('./config');
  3. var common = require('./common');
  4. var gulp = require('gulp');
  5. var fs = require('fs-extra');
  6. var replace = require('gulp-replace');
  7. var gutil = require('gulp-util');
  8. var zip = require('gulp-zip');
  9. var args = require('yargs').argv;
  10. var addsrc = require('gulp-add-src');
  11. var stagingBasePath = config.paths.staging.cdn;
  12. var docPath = config.paths.staging.doc;
  13. var version = config.version;
  14. var host = config.toolsHost;
  15. var permalink = config.permalink;
  16. var stagingPath = stagingBasePath + '/' + version;
  17. var modify = require('gulp-modify');
  18. var rootZip = 'target/';
  19. var fileZip = 'docsite.zip';
  20. gulp.task('cdn:docsite:clean', function() {
  21. fs.removeSync(docPath);
  22. fs.removeSync(rootZip + fileZip);
  23. });
  24. gulp.task('cdn:docsite:bower_components', ['cdn:stage-bower_components'], function() {
  25. gutil.log('Copying bower components from ' + stagingPath + ' to ' + docPath + '/bower_components');
  26. return gulp.src([stagingPath + '/**'])
  27. // Temporary patch until #180 is fixed:
  28. // https://github.com/webcomponents/webcomponentsjs/issues/180
  29. .pipe(modify({
  30. fileModifier: function(file, contents) {
  31. if (/webcomponents-lite.*js/.test(file.path)) {
  32. contents = contents.replace(/(if ?\()(\w+\.log)(\))/mg, '$1$2 && $2.split$3');
  33. }
  34. return contents;
  35. }
  36. }))
  37. .pipe(gulp.dest(docPath + '/bower_components'));
  38. });
  39. gulp.task('cdn:docsite:core-elements', function() {
  40. return gulp.src('doc/*')
  41. .pipe(gulp.dest(docPath));
  42. });
  43. gulp.task('cdn:docsite:core-elements-integrations', function() {
  44. return getDocModifyTask('demo/**', docPath + '/integrations');
  45. });
  46. var doctasks = ['cdn:docsite:core-elements', 'cdn:docsite:core-elements-integrations'];
  47. config.coreElements.forEach(function (n) {
  48. var task = 'cdn:docsite:' + n;
  49. doctasks.push(task);
  50. gulp.task(task, ['cdn:docsite:bower_components'], function(done) {
  51. var elementDocsite = docPath + '/' + n;
  52. var elementDemo = stagingPath + '/' + n + '/demo/**';
  53. return getDocModifyTask(elementDemo, elementDocsite, n);
  54. });
  55. });
  56. function getDocModifyTask(sourceFiles, targetFolder, n) {
  57. fs.mkdirsSync(targetFolder);
  58. gutil.log('Generating site documentation from ' + sourceFiles + ' into ' + targetFolder);
  59. return gulp.src([sourceFiles, '!**/*-embed.html'])
  60. // Remove bad tags
  61. .pipe(replace(/^.*<(!doctype|\/?html|\/?head|\/?body|meta|title).*>.*\n/img, ''))
  62. // Uncomment metainfo, and enclose all the example in {% raw %} ... {% endraw %} to avoid liquid conflicts
  63. // We use gulp-modify instead of replace in order to handle the github url for this file
  64. .pipe(modify({
  65. fileModifier: function(file, contents) {
  66. var re = new RegExp(".*/" + n + "/");
  67. var gh = 'https://github.com/vaadin/' + n + '/edit/master/' + file.path.replace(re, '');
  68. return contents.replace(/^.*<!--[ \n]+([\s\S]*?title:[\s\S]*?)[ \n]+-->.*\n([\s\S]*)/img,
  69. '---\n$1\nsourceurl: ' + gh + '\n---\n{% raw %}\n$2\n{% endraw %}');
  70. }
  71. }))
  72. .pipe(replace(/^.*<section>[\s\S]*?table-of-contents[\s\S]*?<\/section>.*\n/im, ''))
  73. // Add ids to headers, so as site configures permalinks
  74. .pipe(replace(/<h(\d+)>(.*)(<\/h\d+>)/img, function($0, $1, $2, $3){
  75. var id = $2.trim().toLowerCase().replace(/[^\w]+/g,'_');
  76. return '<h' + $1 + ' id="' + id + '">' + $2 + $3;
  77. }))
  78. // Remove webcomponents polyfill since it's added at top of the site
  79. .pipe(replace(/^.*<script.*?\/webcomponents.*\.js[\"'].*?<\/script>\s*?\n?/img, ''))
  80. // embed files are displayed as iframe, we don't remove above fragments like body or polyfill
  81. .pipe(addsrc(sourceFiles + '/*-embed.html'))
  82. // Remove Analytics
  83. .pipe(replace(/^.*<script.*?ga\.js[\"'].*?<\/script>\s*?\n?/img, ''))
  84. // Adjust bowerComponents variable in common.html
  85. .pipe(replace(/(bowerComponents *= *)'..\/..\/'/, "$1'../bower_components/'"))
  86. // Adjust location of the current component in bower_components (..)
  87. .pipe(replace(/(src|href)=("|')\.\.(\/\w)/mg, '$1=$2../bower_components/' + n + '$3'))
  88. // Adjust location of dependencies in bower_components (../..)
  89. .pipe(replace(/(src|href)=("|')(.*?)\.\.\/\.\.\//mg, '$1=$2../bower_components/'))
  90. // Remove references to demo.css file
  91. .pipe(replace(/^.*<link.*demo.css.*\n/im, ''))
  92. // Remove table of contents
  93. .pipe(replace(/^.*table-of-contents.html.*\n/im, ''))
  94. .pipe(gulp.dest(targetFolder));
  95. }
  96. gulp.task('cdn:docsite:zip', doctasks, function() {
  97. var src = docPath + '/**/*';
  98. gutil.log("Creating docsite zip " + docPath + " -> " + rootZip + fileZip);
  99. return gulp.src(src)
  100. .pipe(zip(fileZip))
  101. .pipe(gulp.dest(rootZip));
  102. });
  103. gulp.task('cdn:docsite:upload', ['cdn:docsite:clean', 'cdn:docsite:zip'], function(done) {
  104. common.checkArguments(['cdnUsername', 'cdnDestination']);
  105. gutil.log('Uploading docsite (scp): ' + rootZip + fileZip + ' -> ' + args.cdnUsername + '@' + host + ':' + args.cdnDestination + version);
  106. require('scp2').scp(rootZip + fileZip, {
  107. host: host,
  108. username: args.cdnUsername,
  109. privateKey: config.paths.privateKey(),
  110. path: args.cdnDestination + version
  111. }, function(err) {
  112. done(err);
  113. });
  114. });
  115. gulp.task('cdn:docsite', ['cdn:docsite:upload']);