PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/eng/mgmt/gulpfile.js

http://github.com/WindowsAzure/azure-sdk-for-java
JavaScript | 218 lines | 186 code | 29 blank | 3 comment | 36 complexity | 5911518f3e78ac3c9762b9d4543cc8a1 MD5 | raw file
Possible License(s): MIT
  1. var path = require('path');
  2. var gulp = require('gulp');
  3. var args = require('yargs').argv;
  4. var colors = require('colors');
  5. var execa = require('execa');
  6. var pAll = require('p-all');
  7. var os = require('os');
  8. var fs = require('fs');
  9. var shell = require('gulp-shell');
  10. var ghPages = require('gulp-gh-pages');
  11. var argv = require('yargs').argv;
  12. var gulpif = require('gulp-if');
  13. var exec = require('child_process').exec;
  14. const xmlparser = require('fast-xml-parser');
  15. const mappings = require('./api-specs.json');
  16. const defaultSpecRoot = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master";
  17. gulp.task('default', function() {
  18. console.log("Usage: gulp codegen " +
  19. "[--spec-root <swagger specs root>] " +
  20. "[--projects <project names>] " +
  21. "[--autorest <autorest info>] " +
  22. "[--autorest-java <autorest.java info>] " +
  23. "[--debug] " +
  24. "[--parallel <number>] " +
  25. "[--autorest-args <AutoRest arguments>]\n");
  26. console.log("--spec-root");
  27. console.log(`\tRoot location of Swagger API specs, default value is "${defaultSpecRoot}"`);
  28. console.log("--projects");
  29. console.log("\tComma separated projects to regenerate, default is all. List of available project names:");
  30. Object.keys(mappings).forEach(function(i) {
  31. console.log('\t' + i.magenta);
  32. });
  33. console.log("--autorest");
  34. console.log("\tThe version of AutoRest. E.g. 2.0.9, or the location of AutoRest repo, e.g. E:\\repo\\autorest");
  35. console.log("--autorest-java");
  36. console.log("\tOption#1: Path to an autorest.java generator to pass as a --use argument to AutoRest.");
  37. console.log("\tOption#2: The version of AutoRest.Java. E.g. 2.0.9. You can also pass latest or preview.");
  38. console.log("\tUsually you'll only need to provide this and not a --autorest argument in order to work on Java code generation.");
  39. console.log("\tSee https://github.com/Azure/autorest/blob/master/.attic/developer/autorest-extension.md");
  40. console.log("--debug");
  41. console.log("\tFlag that allows you to attach a debugger to the autorest.java generator.");
  42. console.log("--parallel");
  43. console.log("\tSpecifies the maximum number of projects to generate in parallel.");
  44. console.log("\tDefaults to the number of logical CPUs on the system. (On this system, " + os.cpus().length + ")");
  45. console.log("--autorest-args");
  46. console.log("\tPasses additional argument to AutoRest generator");
  47. });
  48. const maxParallelism = parseInt(args['parallel'], 10) || os.cpus().length;
  49. var specRoot = args['spec-root'] || defaultSpecRoot;
  50. var sdkRoot = path.join(process.cwd(), '../..')
  51. var projects = args['projects'];
  52. var autoRestVersion = 'preview'; // default
  53. if (args['autorest'] !== undefined) {
  54. autoRestVersion = args['autorest'];
  55. }
  56. var autoRestJavaVersion = ''; // default
  57. if (args['autorest-java'] !== undefined) {
  58. autoRestJavaVersion = args['autorest-java'];
  59. }
  60. var debug = args['debug'];
  61. var autoRestArgs = args['autorest-args'] || '';
  62. var autoRestExe;
  63. const mgmtPomFilename = 'pom.mgmt.xml'
  64. gulp.task('codegen', function(cb) {
  65. if (autoRestVersion.match(/[0-9]+\.[0-9]+\.[0-9]+.*/) ||
  66. autoRestVersion == 'preview' || autoRestVersion == 'latest') {
  67. autoRestExe = 'autorest --version=' + autoRestVersion;
  68. handleInput(projects, cb);
  69. } else {
  70. autoRestExe = "node " + path.join(autoRestVersion, "src/autorest-core/dist/app.js");
  71. handleInput(projects, cb);
  72. }
  73. });
  74. var handleInput = function(projects, cb) {
  75. console.info(`Generating up to ${maxParallelism} projects in parallel..`);
  76. if (projects === undefined) {
  77. const actions = Object.keys(mappings).map(proj => {
  78. return () => codegen(proj, cb);
  79. });
  80. pAll(actions, { concurrency: maxParallelism });
  81. } else {
  82. const actions = projects.split(",").map(proj => {
  83. return () => {
  84. proj = proj.replace(/\ /g, '');
  85. if (mappings[proj] === undefined) {
  86. console.error('Invalid project name "' + proj + '"!');
  87. process.exit(1);
  88. }
  89. return codegen(proj, cb);
  90. }
  91. });
  92. pAll(actions, { maxParallelism });
  93. }
  94. }
  95. var codegen = function(project, cb) {
  96. if (!args['preserve']) {
  97. deleteMgmtFolders(project);
  98. }
  99. console.log('Generating "' + project + '" from spec file ' + specRoot + '/' + mappings[project].source);
  100. const generatorPath = autoRestJavaVersion == 'preview' || autoRestJavaVersion == 'latest'
  101. || autoRestJavaVersion.match(/^[0-9]+\.[0-9]+\.[0-9a-zA-Z]+$/)
  102. ? `--use=@microsoft.azure/autorest.java@` + autoRestJavaVersion +` `
  103. : (autoRestJavaVersion == '' ? '' : `--use=${path.resolve(args['autorest-java'])} `);
  104. const regenManager = args['regenerate-manager'] ? ' --regenerate-manager=true ' : '';
  105. const genInterface = args['generate-interface'] ? ' --generate-interface=true ' : '';
  106. var apiVersion;
  107. if (mappings[project].apiVersion !== undefined) {
  108. apiVersion = " --api-version='" + mappings[project].apiVersion + "'" + ' ';
  109. } else {
  110. apiVersion = '';
  111. }
  112. var fconfig;
  113. if (mappings[project].fconfig !== undefined) {
  114. fconfig = " --fconfig='" + JSON.stringify(mappings[project].fconfig) + "'" + ' ';
  115. } else {
  116. fconfig = '';
  117. }
  118. // path.join won't work if specRoot is a URL
  119. cmd = autoRestExe + ' ' + specRoot + "/" + mappings[project].source +
  120. ' --java ' +
  121. ' --azure-arm ' +
  122. ' --azure-libraries-for-java-folder=' + sdkRoot + ' ' +
  123. ` --license-header=MICROSOFT_MIT_NO_CODEGEN ` +
  124. generatorPath +
  125. regenManager +
  126. genInterface +
  127. apiVersion +
  128. fconfig +
  129. autoRestArgs;
  130. if (mappings[project].args !== undefined) {
  131. cmd += ' ' + mappings[project].args;
  132. }
  133. if (debug) {
  134. cmd += ' --java.debugger';
  135. }
  136. console.log('Command: ' + cmd);
  137. return execa(cmd, [], { shell: true, stdio: "inherit" });
  138. };
  139. var deleteMgmtFolders = function(project) {
  140. var modules = []
  141. project = project.split('/')[0]
  142. var projectRoot = path.join(sdkRoot, 'sdk', project);
  143. var projectPom = path.join(projectRoot, mgmtPomFilename);
  144. // find all modules from pom
  145. if(fs.existsSync(projectPom)) {
  146. var xml = fs.readFileSync(projectPom, {encoding: 'utf-8'});
  147. var mods = xmlparser.parse(xml).project.modules.module
  148. if (typeof mods === 'string') {
  149. modules.push(mods);
  150. } else {
  151. modules = mods;
  152. }
  153. }
  154. modules.forEach(function(mod, index) {
  155. moduleDir = path.join(projectRoot, mod);
  156. if(fs.lstatSync(moduleDir).isDirectory()) {
  157. deleteFolderRecursive(moduleDir);
  158. }
  159. });
  160. }
  161. var deleteFolderRecursive = function(folder) {
  162. var header = "Code generated by Microsoft (R) AutoRest Code Generator";
  163. if(fs.existsSync(folder)) {
  164. fs.readdirSync(folder).forEach(function(file, index) {
  165. var curPath = folder + "/" + file;
  166. if(fs.lstatSync(curPath).isDirectory() && path.dirname(file) != "test") { // recurse
  167. deleteFolderRecursive(curPath);
  168. } else { // delete file
  169. var content = fs.readFileSync(curPath).toString('utf8');
  170. if (content.indexOf(header) > -1) {
  171. fs.unlinkSync(curPath);
  172. }
  173. }
  174. });
  175. }
  176. };
  177. gulp.task('java:build', shell.task('mvn package javadoc:aggregate -DskipTests=true -q'));
  178. gulp.task('java:stage', ['java:build'], function(){
  179. return gulp.src('./target/site/apidocs/**/*').pipe(gulp.dest('./dist'));
  180. });
  181. /// Top level build entry point
  182. gulp.task('stage', ['java:stage']);
  183. gulp.task('publish', ['stage'], function(){
  184. var options = {};
  185. if(process.env.GH_TOKEN){
  186. options.remoteUrl = 'https://' + process.env.GH_TOKEN + '@github.com/azure/azure-libraries-for-java.git'
  187. }
  188. return gulp.src('./dist/**/*').pipe(gulpif(!argv.dryrun, ghPages(options)));
  189. });