azure-sdk-for-node/gulpfile.js

285 строки
9.7 KiB
JavaScript
Исходник Обычный вид История

2016-02-28 06:30:26 +03:00
var gulp = require('gulp');
var args = require('yargs').argv;
var colors = require('colors');
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 = {
'analysisservices': {
'dir': 'analysisServices/lib',
2016-10-27 15:25:20 +03:00
'source': 'arm-analysisservices/2016-05-16/swagger/analysisservices.json',
},
2016-02-28 06:30:26 +03:00
'authorization': {
'dir': 'authorizationManagement/lib',
'source': 'arm-authorization/2015-07-01/swagger/authorization.json',
'ft': 1
},
'batch.Management': {
'dir': 'batchManagement/lib',
2017-02-03 00:43:21 +03:00
'source': 'arm-batch/2017-01-01/swagger/BatchManagement.json',
'ft': 1
},
'batch.Service': {
'dir': 'batch/lib',
2017-02-03 00:43:21 +03:00
'source': 'batch/2017-01-01.4.0/swagger/BatchService.json',
'ft': 1
},
'cdn': {
'dir': 'cdnManagement/lib',
2016-10-28 04:51:27 +03:00
'source': 'arm-cdn/2016-10-02/swagger/cdn.json',
'ft': 2
2016-02-28 06:30:26 +03:00
},
'compute': {
'dir': 'computeManagement2/lib',
'source': 'arm-compute/compositeComputeClient.json',
'ft': 1,
'modeler': 'CompositeSwagger'
2016-02-28 06:30:26 +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',
'source': 'arm-datalake-analytics/job/2016-03-20-preview/swagger/job.json'
},
'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'
},
'devtestlabs': {
'dir': 'devTestLabs/lib',
'source': 'arm-devtestlabs/2016-05-15/swagger/DTL.json'
},
2016-07-29 03:52:05 +03:00
'dns': {
'dir': 'dnsManagement/lib',
'source': 'arm-dns/2016-04-01/swagger/dns.json'
},
'eventhub': {
'dir': 'eventHubManagement/lib',
'source': 'arm-eventhub/2015-08-01/swagger/EventHub.json'
},
'graph': {
'dir': 'graphManagement/lib',
'source': 'arm-graphrbac/compositeGraphRbacManagementClient.json',
'ft': 1,
'modeler': 'CompositeSwagger'
},
2016-02-28 06:30:26 +03:00
'intune': {
'dir': 'intune/lib',
'source': 'arm-intune/2015-01-14-preview/swagger/intune.json',
},
'insights': {
'dir': 'insights/lib',
'source': 'insights/compositeInsightsClient.json',
'ft': 1,
'modeler': 'CompositeSwagger'
},
'insights.management': {
'dir': 'insightsManagement/lib',
'source': 'arm-insights/compositeInsightsManagementClient.json',
'ft': 1,
'modeler': 'CompositeSwagger'
},
2016-06-16 22:09:15 +03:00
'iothub': {
'dir': 'iothub/lib',
'source': 'arm-iothub/2016-02-03/swagger/iothub.json',
'ft': 1
},
'keyvault': {
'dir': 'keyvault/lib',
'source': 'keyvault/2015-06-01/swagger/keyvault.json',
'ft': 1
},
'keyvaultmanagement': {
'dir': 'keyVaultManagement/lib',
'source': 'arm-keyvault/2015-06-01/swagger/keyvault.json',
'ft': 1
},
2016-02-28 06:30:26 +03:00
'network': {
'dir': 'networkManagement2/lib',
'source': 'arm-network/2016-09-01/swagger/network.json',
2016-02-28 06:30:26 +03:00
'ft': 1
},
'notificationHubs':{
'dir': 'notificationHubsManagement/lib',
'source': 'arm-notificationhubs/2016-03-01/swagger/notificationhubs.json'
},
'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/2016-04-01/swagger/redis.json',
2016-02-28 06:30:26 +03:00
'ft': 1
},
'resource': {
'dir': 'resourceManagement/lib/resource',
'source': 'arm-resources/resources/2016-09-01/swagger/resources.json'
2016-02-28 06:30:26 +03:00
},
'resource.subscription': {
'dir': 'resourceManagement/lib/subscription',
'source': 'arm-resources/subscriptions/2016-06-01/swagger/subscriptions.json'
2016-02-28 06:30:26 +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
},
2016-10-13 09:51:19 +03:00
'resource.link': {
'dir': 'resourceManagement/lib/link',
'source': 'arm-resources/links/2016-09-01/swagger/links.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'
},
'resource.policy': {
'dir': 'resourceManagement/lib/policy',
'source': 'arm-resources/policy/2016-04-01/swagger/policy.json'
},
'servermanagement': {
'dir': 'servermanagement/lib',
'source': 'arm-servermanagement/2015-07-01-preview/servermanagement.json'
},
'servicebus.management': {
'dir': 'serviceBusManagement2/lib',
'source': 'arm-servicebus/2015-08-01/swagger/servicebus.json'
},
'serviceFabric': {
'dir': 'serviceFabric/lib',
'source': 'servicefabric/2016-01-28/swagger/servicefabric.json',
'language': 'NodeJS'
},
'storage': {
'dir': 'storageManagement2/lib',
'source': 'arm-storage/2016-12-01/swagger/storage.json',
2016-12-15 09:49:22 +03:00
'ft': 2,
2016-12-15 09:51:56 +03:00
'ClientName': 'StorageManagementClient'
},
'traffic':{
'dir': 'trafficManagerManagement2/lib',
'source': 'arm-trafficmanager/2015-11-01/swagger/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
}
};
2017-02-03 00:43:21 +03:00
var defaultAutoRestVersion = '1.0.0-Nightly20170201';
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';
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-02 06:15:07 +03:00
}
2016-03-01 05:32:11 +03:00
function codegen(project, cb) {
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);
try {
fs.statSync(autoRestExe);
found = true;
} catch (err) {
if (!err.message.match(/^ENONET.*/ig)) {
cb(err);
}
}
if (found) {
generateProject(project, specRoot, usingAutoRestVersion);
} else {
var nugetCmd2 = clrCmd(nugetExe) + ' install Autorest -Source ' + nugetSource + ' -Version ' + usingAutoRestVersion + ' -o packages';
2016-03-02 06:15:07 +03:00
console.log('Downloading Autorest version: ' + nugetCmd2);
exec(nugetCmd2, function(err, stdout, stderr) {
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;
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;
}
//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-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);
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;
2016-12-15 09:49:22 +03:00
if (mappings[project].ClientName !== null && mappings[project].ClientName !== undefined) cmd += ' -ClientName ' + mappings[project].ClientName;
2016-02-28 06:30:26 +03:00
if (mappings[project].args !== undefined) {
cmd = cmd + ' ' + args;
}
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) {
var nugetCmd = clrCmd(nugetExe) + ' install Autorest -Source ' + nugetSource + ' -Version ' + defaultAutoRestVersion + ' -o packages';
2016-03-02 06:15:07 +03:00
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
});