Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

chore(docs): improve version picker #15385

Merged
merged 2 commits into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ module.exports = function(grunt) {

grunt.registerTask('minify', ['bower', 'clean', 'build', 'minall']);
grunt.registerTask('webserver', ['connect:devserver']);
grunt.registerTask('package', ['bower', 'validate-angular-files', 'clean', 'buildall', 'minall', 'collect-errors', 'docs', 'copy', 'write', 'compress']);
grunt.registerTask('package', ['bower', 'validate-angular-files', 'clean', 'buildall', 'minall', 'collect-errors', 'write', 'docs', 'copy', 'compress']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the version.json file is generated by the write grunt task. So we need it to run before the docs task, which now relies upon it.

grunt.registerTask('ci-checks', ['ddescribe-iit', 'merge-conflict', 'eslint']);
grunt.registerTask('default', ['package']);
};
1 change: 0 additions & 1 deletion docs/app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ angular.module('docsApp', [
'ngSanitize',
'ngAnimate',
'DocsController',
'versionsData',
'pagesData',
'navData',
'directives',
Expand Down
10 changes: 5 additions & 5 deletions docs/app/src/docs.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

angular.module('DocsController', [])
angular.module('DocsController', ['currentVersionData'])

.controller('DocsController', [
'$scope', '$rootScope', '$location', '$window', '$cookies',
'NG_PAGES', 'NG_NAVIGATION', 'NG_VERSION',
'NG_PAGES', 'NG_NAVIGATION', 'CURRENT_NG_VERSION',
function($scope, $rootScope, $location, $window, $cookies,
NG_PAGES, NG_NAVIGATION, NG_VERSION) {
NG_PAGES, NG_NAVIGATION, CURRENT_NG_VERSION) {

$scope.navClass = function(navItem) {
return {
Expand Down Expand Up @@ -58,8 +58,8 @@ angular.module('DocsController', [])
Initialize
***********************************/

$scope.versionNumber = NG_VERSION.full;
$scope.version = NG_VERSION.full + ' ' + NG_VERSION.codeName;
$scope.versionNumber = CURRENT_NG_VERSION.full;
$scope.version = CURRENT_NG_VERSION.full + ' ' + CURRENT_NG_VERSION.codeName;
$scope.loading = 0;


Expand Down
63 changes: 34 additions & 29 deletions docs/app/src/versions.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
'use strict';
/* global console */

angular.module('versions', [])
angular.module('versions', ['currentVersionData', 'allVersionsData'])

.controller('DocsVersionsCtrl', ['$scope', '$location', '$window', 'NG_VERSIONS', function($scope, $location, $window, NG_VERSIONS) {
$scope.docs_version = NG_VERSIONS[0];
$scope.docs_versions = NG_VERSIONS;
.directive('versionPicker', function() {
return {
restrict: 'E',
scope: true,
controllerAs: '$ctrl',
controller: ['$location', '$window', 'CURRENT_NG_VERSION', 'ALL_NG_VERSIONS',
/** @this VersionPickerController */
function VersionPickerController($location, $window, CURRENT_NG_VERSION, ALL_NG_VERSIONS) {

for (var i = 0, minor = NaN; i < NG_VERSIONS.length; i++) {
var version = NG_VERSIONS[i];
if (version.isSnapshot) {
version.isLatest = true;
continue;
}
// NaN will give false here
if (minor <= version.minor) {
continue;
}
version.isLatest = true;
minor = version.minor;
}
var versionStr = CURRENT_NG_VERSION.isSnapshot ? 'snapshot' : CURRENT_NG_VERSION.version;

$scope.getGroupName = function(v) {
return v.isLatest ? 'Latest' : ('v' + v.major + '.' + v.minor + '.x');
this.versions = ALL_NG_VERSIONS;
this.selectedVersion = find(ALL_NG_VERSIONS, function(value) { return value.version.version === versionStr; });

this.jumpToDocsVersion = function(value) {
var currentPagePath = $location.path().replace(/\/$/, '');
$window.location = value.docsUrl + currentPagePath;
};
}],
template:
'<div class="picker version-picker">' +
' <select ng-options="v as v.label group by v.group for v in $ctrl.versions"' +
' ng-model="$ctrl.selectedVersion"' +
' ng-change="$ctrl.jumpToDocsVersion($ctrl.selectedVersion)"' +
' class="docs-version-jump">' +
' </select>' +
'</div>'
};

$scope.jumpToDocsVersion = function(version) {
var currentPagePath = $location.path().replace(/\/$/, ''),
url = '';
if (version.isOldDocsUrl) {
url = version.docsUrl;
} else {
url = version.docsUrl + currentPagePath;
function find(collection, matcherFn) {
for (var i = 0, ii = collection.length; i < ii; ++i) {
if (matcherFn(collection[i])) {
return collection[i];
}
}
$window.location = url;
};
}]);
}
});
9 changes: 7 additions & 2 deletions docs/app/test/docsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ describe('DocsController', function() {
angular.module('fake', [])
.value('$cookies', {})
.value('NG_PAGES', {})
.value('NG_NAVIGATION', {})
.value('NG_VERSION', {});
.value('NG_NAVIGATION', {});

angular.module('currentVersionData', [])
.value('CURRENT_NG_VERSION', {});

angular.module('allVersionsData', [])
.value('ALL_NG_VERSIONS', {});

beforeEach(module('fake', 'DocsController'));
beforeEach(inject(function($rootScope, $controller) {
Expand Down
106 changes: 90 additions & 16 deletions docs/config/processors/versions-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _ = require('lodash');
var exec = require('shelljs').exec;
var semver = require('semver');

/**
* @dgProcessor generateVersionDocProcessor
Expand All @@ -12,23 +13,96 @@ module.exports = function generateVersionDocProcessor(gitData) {
return {
$runAfter: ['generatePagesDataProcessor'],
$runBefore: ['rendering-docs'],
// the blacklist is to remove rogue builds that are in npm but not on code.angularjs.org
blacklist: ['1.3.4-build.3588'],
$process: function(docs) {

var versionDoc = {
docType: 'versions-data',
id: 'versions-data',
template: 'versions-data.template.js',
outputPath: 'js/versions-data.js',
currentVersion: gitData.version
};

versionDoc.versions = _(gitData.versions)
.filter(function(version) { return version.major > 0; })
.push(gitData.version)
.reverse()
.value();

docs.push(versionDoc);
var blacklist = this.blacklist;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the meaning of the blacklist? Can you please add a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. The main reason was that we appear to have npm published a random rogue release, 1.3.4-build.3588, which does not have an associated folder on code.angularjs.org

var currentVersion = require('../../../build/version.json');
var output = exec('npm info angular versions --json', { silent: true }).stdout;
var allVersions = processAllVersionsResponse(JSON.parse(output));

docs.push({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call this current-version-data? This makes it clearer what's the difference from version-data.js

Copy link
Contributor Author

@petebacondarwin petebacondarwin Nov 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I agree that the visual difference between versions and version is not that obvious

docType: 'current-version-data',
id: 'current-version-data',
template: 'angular-service.template.js',
outputPath: 'js/current-version-data.js',
ngModuleName: 'currentVersionData',
serviceName: 'CURRENT_NG_VERSION',
serviceValue: currentVersion
});

docs.push({
docType: 'allversions-data',
id: 'allversions-data',
template: 'angular-service.template.js',
outputPath: 'js/all-versions-data.js',
ngModuleName: 'allVersionsData',
serviceName: 'ALL_NG_VERSIONS',
serviceValue: allVersions
});


function processAllVersionsResponse(versions) {

var latestMap = {};

versions = versions
.filter(function(versionStr) {
return blacklist.indexOf(versionStr) === -1;
})
.map(function(versionStr) {
return semver.parse(versionStr);
})
.filter(function(version) {
return version && version.major > 0;
})
.map(function(version) {
var key = version.major + '.' + version.minor;
var latest = latestMap[key];
if (!latest || version.compare(latest) > 0) {
latestMap[key] = version;
}
return version;
})
.map(function(version) {
return makeOption(version);
})
.reverse();

var latest = sortObject(latestMap, reverse(semver.compare))
.map(function(version) { return makeOption(version, 'Latest'); });

return [makeOption({version: 'snapshot'}, 'Latest', 'master')]
.concat(latest)
.concat(versions);
}

function makeOption(version, group, label) {
return {
version: version,
label: label || 'v' + version.raw,
group: group || 'v' + version.major + '.' + version.minor,
docsUrl: createDocsUrl(version)
};
}

function createDocsUrl(version) {
var url = 'https://code.angularjs.org/' + version.version + '/docs';
// Versions before 1.0.2 had a different docs folder name
if (version.major === 1 && version.minor === 0 && version.patch < 2) {
url += '-' + version.version;
}
return url;
}

function reverse(fn) {
return function(left, right) { return -fn(left, right); };
}

function sortObject(obj, cmp) {
return Object.keys(obj).map(function(key) { return obj[key]; }).sort(cmp);
}
}
};
};
3 changes: 2 additions & 1 deletion docs/config/services/deployments/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = function debugDeployment(getVersion) {
'components/lunr.js-' + getVersion('lunr.js') + '/lunr.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/prettify.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/lang-css.js',
'js/versions-data.js',
'js/current-version-data.js',
'js/all-versions-data.js',
'js/pages-data.js',
'js/nav-data.js',
'js/docs.js'
Expand Down
3 changes: 2 additions & 1 deletion docs/config/services/deployments/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = function defaultDeployment(getVersion) {
'components/lunr.js-' + getVersion('lunr.js') + '/lunr.min.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/prettify.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/lang-css.js',
'js/versions-data.js',
'js/current-version-data.js',
'js/all-versions-data.js',
'js/pages-data.js',
'js/nav-data.js',
'js/docs.min.js'
Expand Down
3 changes: 2 additions & 1 deletion docs/config/services/deployments/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module.exports = function jqueryDeployment(getVersion) {
'components/lunr.js-' + getVersion('lunr.js') + '/lunr.min.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/prettify.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/lang-css.js',
'js/versions-data.js',
'js/current-version-data.js',
'js/all-versions-data.js',
'js/pages-data.js',
'js/nav-data.js',
'js/docs.min.js'
Expand Down
3 changes: 2 additions & 1 deletion docs/config/services/deployments/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ module.exports = function productionDeployment(getVersion) {
'components/lunr.js-' + getVersion('lunr.js') + '/lunr.min.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/prettify.js',
'components/google-code-prettify-' + getVersion('google-code-prettify') + '/src/lang-css.js',
'js/versions-data.js',
'js/current-version-data.js',
'https://code.angularjs.org/snapshot/docs/js/all-versions-data.js',
'js/pages-data.js',
'js/nav-data.js',
'js/docs.min.js'
Expand Down
4 changes: 4 additions & 0 deletions docs/config/templates/angular-service.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

angular.module('{$ doc.ngModuleName $}', [])
.value('{$ doc.serviceName $}', {$ doc.serviceValue | json $});
8 changes: 1 addition & 7 deletions docs/config/templates/indexPage.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,7 @@ <h4 class="search-results-group-heading">{{ key }}</h4>
<section class="sup-header">
<div class="container main-grid main-header-grid">
<div class="grid-left">
<div ng-controller="DocsVersionsCtrl" class="picker version-picker">
<select ng-options="v as (v.isSnapshot ? v.branch : ('v' + v.version)) group by getGroupName(v) for v in docs_versions"
ng-model="docs_version"
ng-change="jumpToDocsVersion(docs_version)"
class="docs-version-jump">
</select>
</div>
<version-picker></version-picker>
</div>
<div class="grid-right">
<ul class="nav-breadcrumb">
Expand Down
6 changes: 0 additions & 6 deletions docs/config/templates/versions-data.template.js

This file was deleted.

10 changes: 5 additions & 5 deletions lib/versions/version-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var getGitRepoInfo = function() {
* @return {String} The codename if found, otherwise null/undefined
*/
var getCodeName = function(tagName) {
var gitCatOutput = shell.exec('git cat-file -p ' + tagName, {silent:true}).output;
var gitCatOutput = shell.exec('git cat-file -p ' + tagName, {silent:true}).stdout;
var tagMessage = gitCatOutput.match(/^.*codename.*$/mg)[0];
var codeName = tagMessage && tagMessage.match(/codename\((.*)\)/)[1];
if (!codeName) {
Expand All @@ -69,7 +69,7 @@ var getCodeName = function(tagName) {
* @return {String} The build segment of the version
*/
function getBuild() {
var hash = shell.exec('git rev-parse --short HEAD', {silent: true}).output.replace('\n', '');
var hash = shell.exec('git rev-parse --short HEAD', {silent: true}).stdout.replace('\n', '');
return 'sha.' + hash;
}

Expand All @@ -87,7 +87,7 @@ var getTaggedVersion = function() {
var gitTagResult = shell.exec('git describe --exact-match', {silent:true});

if (gitTagResult.code === 0) {
var tag = gitTagResult.output.trim();
var tag = gitTagResult.stdout.trim();
var version = semver.parse(tag);

if (version && checkBranchPattern(version.version, currentPackage.branchPattern)) {
Expand All @@ -113,7 +113,7 @@ var getPreviousVersions = function() {
var query = NO_REMOTE_REQUESTS ? 'git tag' : 'git ls-remote --tags ' + repo_url;
var tagResults = shell.exec(query, {silent: true});
if (tagResults.code === 0) {
return _(tagResults.output.match(/v[0-9].*[0-9]$/mg))
return _(tagResults.stdout.match(/v[0-9].*[0-9]$/mg))
.map(function(tag) {
var version = semver.parse(tag);
return version;
Expand Down Expand Up @@ -159,7 +159,7 @@ var getCdnVersion = function() {
{silent: true});
if (cdnResult.code === 0) {
// --write-out appends its content to the general request response, so extract it
var statusCode = cdnResult.output.split('\n').pop().trim();
var statusCode = cdnResult.stdout.split('\n').pop().trim();
if (statusCode === '200') {
cdnVersion = version;
}
Expand Down
Loading