forked from Azure/azure-libraries-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
191 lines (164 loc) · 6.78 KB
/
gulpfile.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var path = require('path');
var gulp = require('gulp');
var args = require('yargs').argv;
var colors = require('colors');
var execa = require('execa');
var pAll = require('p-all');
var os = require('os');
var fs = require('fs');
var shell = require('gulp-shell');
var ghPages = require('gulp-gh-pages');
var argv = require('yargs').argv;
var gulpif = require('gulp-if');
var exec = require('child_process').exec;
const mappings = require('./api-specs.json');
const defaultSpecRoot = "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master";
async function defaultInfo() {
console.log("Usage: gulp codegen " +
"[--spec-root <swagger specs root>] " +
"[--projects <project names>] " +
"[--autorest <autorest info>] " +
"[--autorest-java <autorest.java info>] " +
"[--debug] " +
"[--parallel <number>] " +
"[--autorest-args <AutoRest arguments>]\n");
console.log("--spec-root");
console.log(`\tRoot location of Swagger API specs, default value is "${defaultSpecRoot}"`);
console.log("--projects");
console.log("\tComma separated projects to regenerate, default is all. List of available project names:");
Object.keys(mappings).forEach(function(i) {
console.log('\t' + i.magenta);
});
console.log("--autorest");
console.log("\tThe version of AutoRest. E.g. 2.0.9, or the location of AutoRest repo, e.g. E:\\repo\\autorest");
console.log("--autorest-java");
console.log("\tPath to an autorest.java generator to pass as a --use argument to AutoRest.");
console.log("\tUsually you'll only need to provide this and not a --autorest argument in order to work on Java code generation.");
console.log("\tSee https://github.com/Azure/autorest/blob/master/docs/developer/autorest-extension.md");
console.log("--debug");
console.log("\tFlag that allows you to attach a debugger to the autorest.java generator.");
console.log("--parallel");
console.log("\tSpecifies the maximum number of projects to generate in parallel.");
console.log("\tDefaults to the number of logical CPUs on the system. (On this system, " + os.cpus().length + ")");
console.log("--autorest-args");
console.log("\tPasses additional argument to AutoRest generator");
}
const maxParallelism = parseInt(args['parallel'], 10) || os.cpus().length;
var specRoot = args['spec-root'] || defaultSpecRoot;
var projects = args['projects'];
var autoRestVersion = '2.0.4417'; // default
if (args['autorest'] !== undefined) {
autoRestVersion = args['autorest'];
}
autorestJava = '@microsoft.azure/[email protected]'; // default
if (args['autorest-java'] !== undefined) {
autorestJava = args['autorest-java'];
}
var debug = args['debug'];
var autoRestArgs = args['autorest-args'] || '';
var autoRestExe;
async function generate(cb) {
if (autoRestVersion.match(/[0-9]+\.[0-9]+\.[0-9]+.*/) ||
autoRestVersion == 'latest') {
autoRestExe = 'autorest --version=' + autoRestVersion;
handleInput(projects, cb);
} else {
autoRestExe = "node " + path.join(autoRestVersion, "src/autorest-core/dist/app.js");
handleInput(projects, cb);
}
}
function handleInput(projects, cb) {
console.info(`Generating up to ${maxParallelism} projects in parallel..`);
if (projects === undefined) {
const actions = Object.keys(mappings).map(proj => {
return () => codegen(proj, cb);
});
pAll(actions, { concurrency: maxParallelism });
} else {
const actions = projects.split(",").map(proj => {
return () => {
proj = proj.replace(/\ /g, '');
if (mappings[proj] === undefined) {
console.error('Invalid project name "' + proj + '"!');
process.exit(1);
}
return codegen(proj, cb);
}
});
pAll(actions, { maxParallelism });
}
}
function codegen(project, cb) {
if (!args['preserve']) {
const sourcesToDelete = path.join(
mappings[project].dir,
'/src/main/java/',
mappings[project].package.replace(/\./g, '/'));
deleteFolderRecursive(sourcesToDelete);
}
console.log('Generating "' + project + '" from spec file ' + specRoot + '/' + mappings[project].source);
var generator = '--fluent';
if (mappings[project].fluent !== null && mappings[project].fluent === false) {
generator = '';
}
const generatorPath = `--use=${autorestJava} `
const regenManager = args['regenerate-manager'] ? ' --regenerate-manager=true ' : '';
const outDir = path.resolve(mappings[project].dir);
// path.join won't work if specRoot is a URL
cmd = autoRestExe + ' ' + specRoot + "/" + mappings[project].source +
' --java ' +
' --azure-arm ' +
generator +
` --namespace=${mappings[project].package} ` +
` --java.output-folder=${outDir} ` +
` --license-header=MICROSOFT_MIT_NO_CODEGEN ` +
generatorPath +
regenManager +
autoRestArgs;
if (mappings[project].args !== undefined) {
cmd += ' ' + mappings[project].args;
}
if (debug) {
cmd += ' --java.debugger';
}
console.log('Command: ' + cmd);
return execa(cmd, [], { shell: true, stdio: "inherit" });
}
function deleteFolderRecursive(path) {
var header = "Code generated by Microsoft (R) AutoRest Code Generator";
if(fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index) {
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
var content = fs.readFileSync(curPath).toString('utf8');
if (content.indexOf(header) > -1) {
fs.unlinkSync(curPath);
}
}
});
}
}
async function prepareBuild() {
return shell.task('mvn package javadoc:aggregate -DskipTests -q');
}
async function prepareStage() {
return gulp.src('./target/site/apidocs/**/*').pipe(gulp.dest('./dist'));
}
async function preparePublish() {
var options = {};
if(process.env.GH_TOKEN){
options.remoteUrl = 'https://' + process.env.GH_TOKEN + '@github.com/azure/azure-libraries-for-java.git'
}
return gulp.src('./dist/**/*').pipe(gulpif(!argv.dryrun, ghPages(options)));
}
const build = prepareBuild;
const stage = gulp.series(build, prepareStage);
const publish = gulp.series(stage, preparePublish);
//exports task
exports.default = defaultInfo;
exports.codegen = generate;
exports.build = build;
exports.stage = stage;
exports.publish = publish;