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-03-08 02:02:43 +03:00
|
|
|
'cdn': {
|
|
|
|
'dir': 'cdnManagement/lib',
|
|
|
|
'source': 'arm-cdn/2015-06-01/swagger/cdn.json',
|
|
|
|
'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',
|
|
|
|
'source': 'arm-datalake-analytics/job/2015-11-01-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'
|
|
|
|
},
|
|
|
|
'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',
|
|
|
|
'source': 'arm-network/2015-06-15/swagger/network.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
|
|
|
'rediscache': {
|
|
|
|
'dir': 'rediscachemanagement/lib',
|
|
|
|
'source': 'arm-redis/2015-08-01/swagger/redis.json',
|
|
|
|
'ft': 1
|
|
|
|
},
|
|
|
|
'resource': {
|
|
|
|
'dir': 'resourceManagement/lib/resource',
|
|
|
|
'source': 'arm-resources/resources/2015-11-01/swagger/resources.json'
|
|
|
|
},
|
|
|
|
'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-03-08 02:02:43 +03:00
|
|
|
'storage': {
|
|
|
|
'dir': 'storageManagement2/lib',
|
|
|
|
'source': 'arm-storage/2015-06-15/swagger/storage.json',
|
|
|
|
'ft': 2
|
|
|
|
},
|
|
|
|
'serviceFabric': {
|
|
|
|
'dir': 'serviceFabric/lib',
|
|
|
|
'source': 'arm-servicefabric/2016-01-28/swagger/servicefabric.json',
|
|
|
|
'language': 'NodeJS'
|
|
|
|
},
|
2016-02-28 06:30:26 +03:00
|
|
|
'website': {
|
|
|
|
'dir': 'websiteManagement2/lib',
|
|
|
|
'source': 'arm-web/2015-08-01/swagger/service.json',
|
|
|
|
'ft': 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-15 02:26:47 +03:00
|
|
|
var defaultAutoRestVersion = '0.16.0-Nightly20160313';
|
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) {
|
|
|
|
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) {
|
|
|
|
modeler = 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);
|
2016-03-01 10:11:50 +03:00
|
|
|
var cmd = util.format('%s -Modeler %s -CodeGenerator %s -Input %s -outputDirectory lib/services/%s -Header MICROSOFT_MIT',
|
|
|
|
autoRestExe, modeler, 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
|
|
|
});
|