2016-02-28 06:30:26 +03:00
|
|
|
var gulp = require('gulp');
|
|
|
|
var args = require('yargs').argv;
|
|
|
|
var colors = require('colors');
|
2016-03-01 10:11:50 +03:00
|
|
|
var fs = require('fs');
|
|
|
|
var util = require('util');
|
2016-03-02 06:15:07 +03:00
|
|
|
var path = require('path');
|
2016-02-28 06:30:26 +03:00
|
|
|
var exec = require('child_process').exec;
|
|
|
|
|
|
|
|
var mappings = {
|
|
|
|
'authorization': {
|
|
|
|
'dir': 'authorizationManagement/lib',
|
|
|
|
'source': 'arm-authorization/2015-07-01/swagger/authorization.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
2016-04-15 03:26:42 +03:00
|
|
|
'batch.Management': {
|
|
|
|
'dir': 'batchManagement/lib',
|
|
|
|
'source': 'arm-batch/2015-12-01/swagger/BatchManagement.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
|
|
|
'batch.Service': {
|
|
|
|
'dir': 'batch/lib',
|
|
|
|
'source': 'batch/2016-02-01.3.0/swagger/BatchService.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
2016-03-08 02:02:43 +03:00
|
|
|
'cdn': {
|
|
|
|
'dir': 'cdnManagement/lib',
|
2016-05-12 00:55:20 +03:00
|
|
|
'source': 'arm-cdn/2016-04-02/swagger/cdn.json',
|
2016-03-08 02:02:43 +03:00
|
|
|
'ft': 2
|
2016-02-28 06:30:26 +03:00
|
|
|
},
|
|
|
|
'compute': {
|
|
|
|
'dir': 'computeManagement2/lib',
|
2016-03-15 02:26:47 +03:00
|
|
|
'source': 'arm-compute/compositeComputeClient.json',
|
|
|
|
'ft': 1,
|
|
|
|
'modeler': 'CompositeSwagger'
|
2016-02-28 06:30:26 +03:00
|
|
|
},
|
2016-03-08 02:02:43 +03:00
|
|
|
'datalake.analytics.account': {
|
|
|
|
'dir': 'dataLake.Analytics/lib/account',
|
|
|
|
'source': 'arm-datalake-analytics/account/2015-10-01-preview/swagger/account.json'
|
|
|
|
},
|
|
|
|
'datalake.analytics.job': {
|
|
|
|
'dir': 'dataLake.Analytics/lib/job',
|
2016-04-01 20:55:38 +03:00
|
|
|
'source': 'arm-datalake-analytics/job/2016-03-20-preview/swagger/job.json'
|
2016-03-08 02:02:43 +03:00
|
|
|
},
|
|
|
|
'datalake.analytics.catalog': {
|
|
|
|
'dir': 'dataLake.Analytics/lib/catalog',
|
|
|
|
'source': 'arm-datalake-analytics/catalog/2015-10-01-preview/swagger/catalog.json'
|
|
|
|
},
|
|
|
|
'datalake.store.account': {
|
|
|
|
'dir': 'dataLake.Store/lib/account',
|
|
|
|
'source': 'arm-datalake-store/account/2015-10-01-preview/swagger/account.json'
|
|
|
|
},
|
|
|
|
'datalake.store.filesystem': {
|
|
|
|
'dir': 'dataLake.Store/lib/filesystem',
|
|
|
|
'source': 'arm-datalake-store/filesystem/2015-10-01-preview/swagger/filesystem.json'
|
|
|
|
},
|
2016-05-20 02:40:26 +03:00
|
|
|
'devTestLabs': {
|
|
|
|
'dir': 'devTestLabs/lib',
|
|
|
|
'source': 'arm-devtestlabs/2015-05-21-preview/swagger/DTL.json'
|
|
|
|
},
|
2016-03-08 02:02:43 +03:00
|
|
|
'graph': {
|
|
|
|
'dir': 'graphManagement/lib',
|
|
|
|
'source': 'arm-graphrbac/1.6-internal/swagger/graphrbac.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
2016-02-28 06:30:26 +03:00
|
|
|
'intune': {
|
|
|
|
'dir': 'intune/lib',
|
|
|
|
'source': 'arm-intune/2015-01-14-preview/swagger/intune.json',
|
|
|
|
},
|
|
|
|
'network': {
|
|
|
|
'dir': 'networkManagement2/lib',
|
2016-03-17 04:18:54 +03:00
|
|
|
'source': 'arm-network/2016-03-30/swagger/network.json',
|
2016-02-28 06:30:26 +03:00
|
|
|
'ft': 1
|
|
|
|
},
|
2016-05-19 21:14:36 +03:00
|
|
|
'notificationHubs':{
|
|
|
|
'dir': 'notificationHubsManagement/lib',
|
|
|
|
'source': 'arm-notificationhubs/2014-09-01/swagger/notificationhubs.json'
|
|
|
|
},
|
2016-05-19 03:47:31 +03:00
|
|
|
'powerbiembedded': {
|
|
|
|
'dir': 'powerbiembedded/lib',
|
|
|
|
'source': 'arm-powerbiembedded/2016-01-29/swagger/powerbiembedded.json'
|
|
|
|
},
|
2016-02-28 06:30:26 +03:00
|
|
|
'rediscache': {
|
|
|
|
'dir': 'rediscachemanagement/lib',
|
|
|
|
'source': 'arm-redis/2015-08-01/swagger/redis.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
|
|
|
'resource': {
|
|
|
|
'dir': 'resourceManagement/lib/resource',
|
2016-03-24 01:46:52 +03:00
|
|
|
'source': 'arm-resources/resources/2016-02-01/swagger/resources.json'
|
2016-02-28 06:30:26 +03:00
|
|
|
},
|
|
|
|
'resource.subscription': {
|
|
|
|
'dir': 'resourceManagement/lib/subscription',
|
|
|
|
'source': 'arm-resources/subscriptions/2015-11-01/swagger/subscriptions.json'
|
|
|
|
},
|
2016-03-08 02:02:43 +03:00
|
|
|
'resource.lock': {
|
|
|
|
'dir': 'resourceManagement/lib/lock',
|
|
|
|
'source': 'arm-resources/locks/2015-01-01/swagger/locks.json'
|
2016-02-28 06:30:26 +03:00
|
|
|
},
|
|
|
|
'resource.feature': {
|
|
|
|
'dir': 'resourceManagement/lib/feature',
|
|
|
|
'source': 'arm-resources/features/2015-12-01/swagger/features.json'
|
|
|
|
},
|
2016-04-06 00:20:48 +03:00
|
|
|
'resource.policy': {
|
|
|
|
'dir': 'resourceManagement/lib/policy',
|
2016-04-12 00:56:40 +03:00
|
|
|
'source': 'arm-resources/policy/2016-04-01/swagger/policy.json'
|
2016-04-06 00:20:48 +03:00
|
|
|
},
|
2016-03-08 02:02:43 +03:00
|
|
|
'storage': {
|
|
|
|
'dir': 'storageManagement2/lib',
|
2016-04-12 22:27:28 +03:00
|
|
|
'source': 'arm-storage/2016-01-01/swagger/storage.json',
|
2016-03-08 02:02:43 +03:00
|
|
|
'ft': 2
|
|
|
|
},
|
2016-05-19 21:14:36 +03:00
|
|
|
'servermanagement': {
|
|
|
|
'dir': 'servermanagement/lib',
|
|
|
|
'source': 'arm-servermanagement/2015-07-01-preview/servermanagement.json'
|
|
|
|
},
|
2016-03-08 02:02:43 +03:00
|
|
|
'serviceFabric': {
|
|
|
|
'dir': 'serviceFabric/lib',
|
2016-04-15 03:26:42 +03:00
|
|
|
'source': 'servicefabric/2016-01-28/swagger/servicefabric.json',
|
2016-03-08 02:02:43 +03:00
|
|
|
'language': 'NodeJS'
|
|
|
|
},
|
2016-04-15 03:26:42 +03:00
|
|
|
'traffic':{
|
|
|
|
'dir': 'trafficManagement2/lib',
|
|
|
|
'source': 'arm-trafficmanager/2015-11-01/trafficmanager.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
2016-02-28 06:30:26 +03:00
|
|
|
'website': {
|
|
|
|
'dir': 'websiteManagement2/lib',
|
|
|
|
'source': 'arm-web/2015-08-01/swagger/service.json',
|
|
|
|
'ft': 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-19 21:14:36 +03:00
|
|
|
var defaultAutoRestVersion = '0.17.0-Nightly20160518';
|
2016-03-01 10:11:50 +03:00
|
|
|
var usingAutoRestVersion;
|
2016-02-28 06:30:26 +03:00
|
|
|
var specRoot = args['spec-root'] || "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master";
|
|
|
|
var project = args['project'];
|
2016-03-02 06:15:07 +03:00
|
|
|
var nugetExe = path.join('tools', 'nuget.exe');
|
|
|
|
var autoRestExe = constructAutorestExePath(defaultAutoRestVersion);
|
2016-02-28 06:30:26 +03:00
|
|
|
var nugetSource = 'https://www.myget.org/F/autorest/api/v2';
|
2016-03-01 05:32:11 +03:00
|
|
|
var language = 'Azure.NodeJS';
|
2016-03-01 10:11:50 +03:00
|
|
|
var modeler = 'Swagger';
|
2016-03-01 05:32:11 +03:00
|
|
|
var isWindows = (process.platform.lastIndexOf('win') === 0);
|
2016-03-02 06:15:07 +03:00
|
|
|
function clrCmd(cmd){
|
2016-03-01 05:32:11 +03:00
|
|
|
return isWindows ? cmd : ('mono ' + cmd);
|
|
|
|
};
|
|
|
|
|
2016-03-02 06:15:07 +03:00
|
|
|
function constructAutorestExePath(version) {
|
|
|
|
return path.join('packages', 'autorest.' + version, 'tools', 'AutoRest.exe');
|
|
|
|
}
|
2016-03-01 05:32:11 +03:00
|
|
|
function codegen(project, cb) {
|
2016-03-01 10:11:50 +03:00
|
|
|
var found = false;
|
|
|
|
if (mappings[project].autorestversion) {
|
|
|
|
usingAutoRestVersion = mappings[project].autoRestVersion;
|
|
|
|
} else {
|
|
|
|
usingAutoRestVersion = defaultAutoRestVersion;
|
|
|
|
}
|
2016-03-02 06:15:07 +03:00
|
|
|
autoRestExe = constructAutorestExePath(usingAutoRestVersion);
|
2016-03-01 10:11:50 +03:00
|
|
|
try {
|
|
|
|
fs.statSync(autoRestExe);
|
|
|
|
found = true;
|
|
|
|
} catch (err) {
|
|
|
|
if (!err.message.match(/^ENONET.*/ig)) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
generateProject(project, specRoot, usingAutoRestVersion);
|
|
|
|
} else {
|
2016-03-02 06:15:07 +03:00
|
|
|
var nugetCmd2 = clrCmd(nugetExe) + ' install autorest -Source ' + nugetSource + ' -Version ' + usingAutoRestVersion + ' -o packages';
|
|
|
|
console.log('Downloading Autorest version: ' + nugetCmd2);
|
|
|
|
exec(nugetCmd2, function(err, stdout, stderr) {
|
2016-03-01 10:11:50 +03:00
|
|
|
console.log(stdout);
|
|
|
|
console.error(stderr);
|
|
|
|
generateProject(project, specRoot, usingAutoRestVersion);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateProject(project, specRoot, autoRestVersion) {
|
2016-04-07 21:20:24 +03:00
|
|
|
var currentModeler = modeler;
|
2016-03-01 10:11:50 +03:00
|
|
|
var specPath = specRoot + '/' + mappings[project].source;
|
2016-03-01 05:32:11 +03:00
|
|
|
//servicefabric wants to generate using generic NodeJS.
|
|
|
|
if (mappings[project].language && mappings[project].language.match(/^NodeJS$/ig) !== null) {
|
|
|
|
language = mappings[project].language;
|
|
|
|
}
|
2016-03-01 10:11:50 +03:00
|
|
|
//default Modeler is Swagger. However, some services may want to use CompositeSwaggerModeler
|
|
|
|
if (mappings[project].modeler && mappings[project].modeler.match(/^CompositeSwagger$/ig) !== null) {
|
2016-04-07 21:20:24 +03:00
|
|
|
currentModeler = mappings[project].modeler;
|
2016-03-01 10:11:50 +03:00
|
|
|
}
|
2016-02-28 06:30:26 +03:00
|
|
|
|
2016-03-01 05:32:11 +03:00
|
|
|
console.log(util.format('Generating "%s" from spec file "%s" with language "%s" and AutoRest version "%s".',
|
|
|
|
project, specRoot + '/' + mappings[project].source, language, autoRestVersion));
|
2016-03-02 06:15:07 +03:00
|
|
|
autoRestExe = constructAutorestExePath(autoRestVersion);
|
2016-04-15 03:26:42 +03:00
|
|
|
var cmd = util.format('%s -Modeler %s -CodeGenerator %s -Input %s -outputDirectory lib/services/%s -Header MICROSOFT_MIT_NO_VERSION',
|
2016-04-07 21:20:24 +03:00
|
|
|
autoRestExe, currentModeler, language, specPath, mappings[project].dir);
|
2016-02-28 06:30:26 +03:00
|
|
|
if (mappings[project].ft !== null && mappings[project].ft !== undefined) cmd += ' -FT ' + mappings[project].ft;
|
|
|
|
if (mappings[project].args !== undefined) {
|
|
|
|
cmd = cmd + ' ' + args;
|
|
|
|
}
|
2016-03-08 02:02:43 +03:00
|
|
|
console.log('Executing: ' + clrCmd(cmd));
|
2016-03-01 05:32:11 +03:00
|
|
|
exec(clrCmd(cmd), function(err, stdout, stderr) {
|
2016-02-28 06:30:26 +03:00
|
|
|
console.log(stdout);
|
|
|
|
console.error(stderr);
|
|
|
|
});
|
2016-03-01 05:32:11 +03:00
|
|
|
}
|
2016-02-28 06:30:26 +03:00
|
|
|
|
|
|
|
gulp.task('default', function() {
|
|
|
|
console.log("Usage: gulp codegen [--spec-root <swagger specs root>] [--project <project name>]\n");
|
|
|
|
console.log("--spec-root");
|
|
|
|
console.log("\tRoot location of Swagger API specs, default value is \"https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master\"");
|
|
|
|
console.log("--project\n\tProject to regenerate, default is all. List of available project names:");
|
|
|
|
Object.keys(mappings).forEach(function(i) {
|
|
|
|
console.log('\t' + i.magenta);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('codegen', function(cb) {
|
2016-03-02 06:15:07 +03:00
|
|
|
var nugetCmd = clrCmd(nugetExe) + ' install autorest -Source ' + nugetSource + ' -Version ' + defaultAutoRestVersion + ' -o packages';
|
|
|
|
console.log('Downloading default AutoRest version: ' + nugetCmd);
|
|
|
|
exec(nugetCmd, function(err, stdout, stderr) {
|
2016-02-28 06:30:26 +03:00
|
|
|
console.log(stdout);
|
|
|
|
console.error(stderr);
|
|
|
|
if (project === undefined) {
|
|
|
|
Object.keys(mappings).forEach(function(proj) {
|
|
|
|
codegen(proj, cb);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (mappings[project] === undefined) {
|
|
|
|
console.error('Invalid project name "' + project + '"!');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
codegen(project, cb);
|
|
|
|
}
|
|
|
|
});
|
2016-03-08 01:16:25 +03:00
|
|
|
});
|