1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var rxjs_1 = require('rxjs');
|
12 | var core_1 = require('@angular/core');
|
13 | var http_1 = require('@angular/http');
|
14 | var patch_parser_1 = require('./patch-parser');
|
15 | function capitalizeFirstLetter(message) {
|
16 | return message.charAt(0).toUpperCase() + message.slice(1);
|
17 | }
|
18 | function prepareSummary(message) {
|
19 | return capitalizeFirstLetter(message.trim());
|
20 | }
|
21 | function parseOutStepNumberAndComment(arr) {
|
22 | arr.forEach(function (parsedPatch) {
|
23 | var splitMessage = parsedPatch.message.split(':');
|
24 | if (splitMessage.length > 1) {
|
25 | var stepNumber = splitMessage[0].split(' ')[1];
|
26 | if (!stepNumber) {
|
27 | stepNumber = splitMessage[0].split(' ')[0];
|
28 | }
|
29 | parsedPatch.stepNumber = stepNumber.trim();
|
30 | if (splitMessage[1]) {
|
31 | parsedPatch.summary = prepareSummary(splitMessage[1]);
|
32 | }
|
33 | }
|
34 | });
|
35 | return arr;
|
36 | }
|
37 | function doMapping(parsedData) {
|
38 | var stepToPatch = {};
|
39 | parsedData.forEach(function (parsedPatch) {
|
40 | stepToPatch[parsedPatch.stepNumber] = parsedPatch;
|
41 | });
|
42 | return stepToPatch;
|
43 | }
|
44 | var cache = {};
|
45 | var TutorialRegistryCache = (function () {
|
46 | function TutorialRegistryCache(http) {
|
47 | this.http = http;
|
48 | }
|
49 | TutorialRegistryCache.prototype.load = function (id, revision, tutorialData) {
|
50 | var _this = this;
|
51 | if (cache[id + '_' + revision]) {
|
52 | return rxjs_1.Observable.of(cache[id + '_' + revision]);
|
53 | }
|
54 | else {
|
55 | var rootUrl = 'https://api.github.com/repos/' + tutorialData.gitHub + '/commits?path=/.tortilla/manuals/templates/root.tmpl&sha=' + revision;
|
56 | console.log('Will load root URL: ', rootUrl);
|
57 | return this.http.get(rootUrl)
|
58 | .map(function (res) { return res.json(); })
|
59 | .map(function (res) { return res && res[0] && res[0].sha ? res[0].sha : null; })
|
60 | .flatMap(function (firstCommitId) {
|
61 | if (!firstCommitId) {
|
62 | throw new Error('Unable to find first commit with root.tmpl modification!');
|
63 | }
|
64 | var url = 'https://github.com/' + tutorialData.gitHub + '/compare/' + firstCommitId + '...' + revision + '.patch';
|
65 | console.log('Will load path from URL: ', url);
|
66 | return _this.http.get(url);
|
67 | })
|
68 | .map(function (res) { return res['text'](); })
|
69 | .map(patch_parser_1.parseMultiPatch)
|
70 | .map(parseOutStepNumberAndComment)
|
71 | .map(doMapping)
|
72 | .map(function (parsedTutorial) {
|
73 | var cacheItem = {
|
74 | steps: parsedTutorial,
|
75 | tutorial: tutorialData
|
76 | };
|
77 | cache[id + '_' + revision] = cacheItem;
|
78 | return cacheItem;
|
79 | });
|
80 | }
|
81 | };
|
82 | TutorialRegistryCache = __decorate([
|
83 | core_1.Injectable(),
|
84 | __metadata('design:paramtypes', [http_1.Http])
|
85 | ], TutorialRegistryCache);
|
86 | return TutorialRegistryCache;
|
87 | }());
|
88 | exports.TutorialRegistryCache = TutorialRegistryCache;
|
89 |
|
\ | No newline at end of file |