Skip to content

Commit 9c9eeae

Browse files
committed
The finished product of rewriting the logic
All of the mentioned features work and it’s easier to modify things thanks to the json file that has information on all the templates.
1 parent edd969e commit 9c9eeae

File tree

2 files changed

+124
-85
lines changed

2 files changed

+124
-85
lines changed

generators/app/index.js

Lines changed: 123 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module.exports = yeoman.Base.extend({
102102
});
103103
})
104104
.then(() => {
105-
this._downloadTemplate(this.answer.templateToUse, listOfUrls[this.answer.templateToUse], this.answer.functionName);
105+
this._downloadTemplate(this.answer.templateToUse, listOfUrls[this.answer.templateToUse], this.answer.functionName, "");
106106
})
107107
.catch(err => {
108108
this.log('There was an error in searching for available templates...');
@@ -125,19 +125,6 @@ module.exports = yeoman.Base.extend({
125125
.get(templatesUrl)
126126
.on('end', () => {
127127
templatesJson = require(path.resolve('./templates.json'));
128-
129-
// this.log(templatesJson[0]);
130-
// output:
131-
// { id: 'BlobTrigger-Batch',
132-
// function: { disabled: false, bindings: [ [Object] ] },
133-
// metadata:
134-
// { name: 'BlobTrigger - Batch',
135-
// description: 'A Batch function that will be run whenever a blob is added to a specified container',
136-
// defaultFunctionName: 'BlobTriggerBatch',
137-
// language: 'Batch',
138-
// category: [ 'Experimental' ],
139-
// userPrompt: [ 'connection', 'path' ] },
140-
// files: { 'run.bat': 'echo OFF\nSET /p input=<%input%\necho Windows Batch script processed blob \'%input%\'' } }
141128

142129
var sortedTemplatesByLanguage = {};
143130

@@ -152,6 +139,8 @@ module.exports = yeoman.Base.extend({
152139
}
153140
}
154141

142+
this.log('There are %d languages available', Object.keys(sortedTemplatesByLanguage).length);
143+
155144
var prompts = [{
156145
type: 'rawlist',
157146
name: 'languageChose',
@@ -177,11 +166,66 @@ module.exports = yeoman.Base.extend({
177166
//------------------------------
178167
//------------------------------
179168
if (this.answer.requestFunctionTemplates == TEMPLATES_BY_EVENT_TYPE) {
180-
this.log('Feature coming soon, just wait on it!');
169+
// templates.json url
170+
var templatesUrl = "https://ahmelsayed.blob.core.windows.net/public/templates.json";
171+
var fileName = "templates.json";
172+
var templatesJson = {};
173+
174+
request
175+
.get(templatesUrl)
176+
.on('end', () => {
177+
templatesJson = require(path.resolve('./templates.json'));
178+
179+
this.log('templatesJson[0]: ' + templatesJson[0]['function'].bindings[0].type);
180+
181+
var sortedTemplatesByEvent = {};
182+
183+
for (var i in templatesJson) {
184+
var tempID = templatesJson[i].id;
185+
var tempEvent = "";
186+
187+
if (templatesJson[i]['function'].bindings[0] !== undefined) {
188+
tempEvent = templatesJson[i]['function'].bindings[0].type;
189+
} else {
190+
tempEvent = "empty";
191+
}
192+
193+
if (sortedTemplatesByEvent.hasOwnProperty(tempEvent)) {
194+
sortedTemplatesByEvent[tempEvent].push(tempID);
195+
} else {
196+
sortedTemplatesByEvent[tempEvent] = new Array(tempID);
197+
}
198+
}
199+
200+
this.log('There are %d event types available', Object.keys(sortedTemplatesByEvent).length);
201+
202+
var prompts = [{
203+
type: 'rawlist',
204+
name: 'eventChose',
205+
message: 'Select an event type...',
206+
choices: Object.keys(sortedTemplatesByEvent),
207+
default: Object.keys(sortedTemplatesByEvent)[0]
208+
}];
209+
210+
return this.prompt(prompts).then(answer => {
211+
this.answer = answer;
212+
this._showRelevantTemplates(sortedTemplatesByEvent[this.answer.eventChose]);
213+
});
214+
})
215+
.on('error', err => {
216+
this.log('There was an error when downloading the templates.json file');
217+
})
218+
.pipe(fs.createWriteStream(path.resolve('./', fileName)));
181219
}
182220
},
183221

184222
_showRelevantTemplates: function(templatesToShow) {
223+
var languageToUse = "";
224+
225+
if (this.answer.languageChose !== undefined) {
226+
languageToUse = this.answer.languageChose;
227+
}
228+
185229
var options = {
186230
uri: 'https://api.github.com/repos/Azure/azure-webjobs-sdk-templates/contents/Templates',
187231
headers: {
@@ -203,6 +247,8 @@ module.exports = yeoman.Base.extend({
203247
}
204248
}
205249

250+
this.log('There are %d templates available', listOfTemplates.length);
251+
206252
var prompts = [{
207253
type: 'list',
208254
name: 'templateToUse',
@@ -221,15 +267,15 @@ module.exports = yeoman.Base.extend({
221267
});
222268
})
223269
.then(() => {
224-
this._downloadTemplate(this.answer.templateToUse, listOfUrls[this.answer.templateToUse], this.answer.functionName);
270+
this._downloadTemplate(this.answer.templateToUse, listOfUrls[this.answer.templateToUse], this.answer.functionName, languageToUse);
225271
})
226272
.catch(err => {
227273
this.log('There was an error in searching for available templates...');
228274
this.log(err);
229275
});;
230276
},
231277

232-
_downloadTemplate: function (templateToUse, urlToUse, functionName, language = "") {
278+
_downloadTemplate: function (templateToUse, urlToUse, functionName, language) {
233279
var languageToUse = language;
234280

235281
var options = {
@@ -245,7 +291,6 @@ module.exports = yeoman.Base.extend({
245291
requestPromise(options)
246292
.then(files => {
247293
var pathToSaveFunction = path.resolve('./', functionName);
248-
var languageOfTemplate = "";
249294

250295
fs.mkdir(pathToSaveFunction, err => {
251296
if (err) {
@@ -254,86 +299,80 @@ module.exports = yeoman.Base.extend({
254299
}
255300
}
256301

257-
this.log('Location for your function:');
258-
this.log(pathToSaveFunction + '\n');
302+
this.log('Location for your function...');
303+
this.log(pathToSaveFunction);
259304

260-
var filesToDownload = {};
305+
var filesInTemplate = {};
261306
for (let i = 0; i < files.length; i++) {
262-
filesToDownload[files[i].name] = files[i].download_url;
307+
filesInTemplate[files[i].name] = files[i].download_url;
263308
}
264309

310+
// Function to download the files
311+
var downloadFile = function (filesToDownload, language) {
312+
for (let file in filesToDownload) {
313+
request
314+
.get(filesToDownload[file])
315+
.on('error', err => {
316+
this.log('There was an error when downloading the file ' + file);
317+
this.log(err);
318+
})
319+
.pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, file)));
320+
}
321+
}.bind(this);
322+
265323
// Verify language if none is given on function call
266-
if (language === "") {
267-
// check metadata.json to verify language
324+
if (languageToUse === "") {
325+
// download metadata.json to verify language
326+
request
327+
.get(filesInTemplate['metadata.json'])
328+
.on('end', () => {
329+
// check the language
330+
languageToUse = languages.resolveLanguage(path.resolve(pathToSaveFunction, "metadata.json"));
331+
332+
// gather the files
333+
var filesToDownload = {};
334+
for (let file in filesInTemplate) {
335+
if (file.indexOf(languagesJSON[languageToUse].fileExtension) >= 0) {
336+
filesToDownload[file] = filesInTemplate[file];
337+
} else if (file === "function.json") {
338+
filesToDownload[file] = filesInTemplate[file];
339+
}
340+
}
341+
342+
// download the rest of the files
343+
downloadFile(filesToDownload, languageToUse);
344+
})
345+
.on('error', err => {
346+
this.log('There was an error when downloading metadata.json');
347+
this.log(err);
348+
})
349+
.pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, 'metadata.json')));
268350
} else {
269-
// download the appropriate files
270-
}
351+
// gather the files
352+
var filesToDownload = {};
353+
for (let file in filesInTemplate) {
354+
this.log('file: ' + file);
355+
if (file.indexOf(languagesJSON[languageToUse].fileExtension) >= 0) {
356+
filesToDownload[file] = filesInTemplate[file];
357+
} else if (file === "metadata.json") {
358+
filesToDownload[file] = filesInTemplate[file];
359+
} else if (file === "function.json") {
360+
filesToDownload[file] = filesInTemplate[file];
361+
}
362+
}
271363

364+
this.log('filesToDownload: ' + filesToDownload);
272365

273-
// for (let i = 0; i < files.length; i++) {
274-
// var fileName = files[i]['name'];
275-
// var fileUrl = files[i]['download_url'];
276-
277-
// if (fileName === "metadata.json") {
278-
// request
279-
// .get(fileUrl)
280-
// .on('end', () => {
281-
// // Verify the language of the template
282-
// languageOfTemplate = languages.resolveLanguage(path.resolve(pathToSaveFunction, "metadata.json"));
283-
// if (languageToUse === "") {
284-
// languageToUse = languageOfTemplate;
285-
286-
// for (let j = 0; j < files.length; j++) {
287-
// var fileName = files[j]['name'];
288-
// var fileUrl = files[j]['download_url'];
289-
290-
// if (fileName.indexOf(languagesJSON[languageToUse].fileExtension) >= 0) {
291-
// request
292-
// .get(fileUrl)
293-
// .on('error', err => {
294-
// this.log('There was an error when downloading the file ' + fileName);
295-
// this.log(err);
296-
// })
297-
// .pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, fileName)));
298-
299-
// this.log('Downloading file ' + fileName + ' to:');
300-
// this.log(path.resolve(pathToSaveFunction, fileName));
301-
// }
302-
303-
// if (fileName === "function.json") {
304-
// request
305-
// .get(fileUrl)
306-
// .on('error', err => {
307-
// this.log('There was an error when downloading the file ' + fileName);
308-
// this.log(err);
309-
// })
310-
// .pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, fileName)));
311-
312-
// this.log('Downloading file ' + fileName + ' to:');
313-
// this.log(path.resolve(pathToSaveFunction, fileName));
314-
// }
315-
// }
316-
// }
317-
// })
318-
// .on('error', err => {
319-
// this.log('There was an error when downloading the file ' + fileName);
320-
// this.log(err);
321-
// })
322-
// .pipe(fs.createWriteStream(path.resolve(pathToSaveFunction, fileName)));
323-
// }
324-
// }
325-
366+
// download the files
367+
downloadFile(filesToDownload, languageToUse);
368+
}
326369
return 1;
327370
});
328-
return this._configureTemplate(pathToSaveFunction);
371+
return 1;
329372
})
330373
.catch(err => {
331374
this.log('There was an error in searching for the files for the template ' + templateToUse);
332375
this.log(err);
333376
});
334-
},
335-
336-
_configureTemplate: function(pathOfTemplate) {
337-
return 1;
338377
}
339378
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "generator-azurefunctions",
3-
"version": "0.0.8",
3+
"version": "0.1.0",
44
"description": "A Yeoman Generator for Azure Functions",
55
"homepage": "",
66
"author": {

0 commit comments

Comments
 (0)