Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(docs-infra): Add github links to CLI docs #26515

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions aio/tools/transforms/angular-api-package/index.js
Expand Up @@ -34,6 +34,7 @@ module.exports = new Package('angular-api', [basePackage, typeScriptPackage])
.processor(require('./processors/removeInjectableConstructors'))
.processor(require('./processors/processPackages'))
.processor(require('./processors/processNgModuleDocs'))
.processor(require('./processors/fixupRealProjectRelativePath'))


/**
Expand Down
@@ -0,0 +1,14 @@
module.exports = function fixupRealProjectRelativePath(API_DOC_TYPES) {
return {
$runAfter: ['readTypeScriptModules'],
$runBefore: ['processing-docs'],
$process(docs) {
docs.forEach(doc => {
if (API_DOC_TYPES.indexOf(doc.docType) !== -1 && doc.fileInfo && doc.fileInfo.realProjectRelativePath) {
// this is an API doc - so fix up its real path
doc.fileInfo.realProjectRelativePath = 'packages/' + doc.fileInfo.realProjectRelativePath;
}
});
}
};
};
@@ -0,0 +1,36 @@
const testPackage = require('../../helpers/test-package');
const processorFactory = require('./fixupRealProjectRelativePath');
const Dgeni = require('dgeni');

describe('fixupRealProjectRelativePath processor', () => {

it('should be available on the injector', () => {
const dgeni = new Dgeni([testPackage('angular-api-package')]);
const injector = dgeni.configureInjector();
const processor = injector.get('fixupRealProjectRelativePath');
expect(processor.$process).toBeDefined();
expect(processor.$runAfter).toContain('readTypeScriptModules');
expect(processor.$runBefore).toContain('processing-docs');
});

it('should add `packages` segment to the start of `realProjectRelativePath` for API docs', () => {
const processor = processorFactory(['class', 'member']);
const docs = [
{ docType: 'class', fileInfo: { realProjectRelativePath: 'a/b/c' } },
{ docType: 'member', fileInfo: { realProjectRelativePath: 'a/b/c/d' } },
{ docType: 'cli-command', fileInfo: { realProjectRelativePath: 'a/b/c' } },
{ docType: 'class', fileInfo: { } },
{ docType: 'class' },
];
processor.$process(docs);

expect(docs).toEqual([
{ docType: 'class', fileInfo: { realProjectRelativePath: 'packages/a/b/c' } },
{ docType: 'member', fileInfo: { realProjectRelativePath: 'packages/a/b/c/d' } },
{ docType: 'cli-command', fileInfo: { realProjectRelativePath: 'a/b/c' } },
{ docType: 'class', fileInfo: { } },
{ docType: 'class' },
]);
});
});

26 changes: 22 additions & 4 deletions aio/tools/transforms/cli-docs-package/index.js
Expand Up @@ -3,7 +3,9 @@ const Package = require('dgeni').Package;
const basePackage = require('../angular-base-package');
const contentPackage = require('../content-package');
const {CONTENTS_PATH, TEMPLATES_PATH, requireFolder} = require('../config');

const CLI_SOURCE_ROOT = resolve(CONTENTS_PATH, 'cli-src');
const CLI_SOURCE_PATH = resolve(CLI_SOURCE_ROOT, 'node_modules/@angular/cli');
const CLI_SOURCE_HELP_PATH = resolve(CLI_SOURCE_PATH, 'help');

// Define the dgeni package for generating the docs
module.exports = new Package('cli-docs', [basePackage, contentPackage])
Expand All @@ -18,12 +20,11 @@ module.exports = new Package('cli-docs', [basePackage, contentPackage])

// Configure file reading
.config(function(readFilesProcessor, cliCommandFileReader) {
const CLI_SOURCE_PATH = resolve(CONTENTS_PATH, 'cli-src/node_modules/@angular/cli/help');
readFilesProcessor.fileReaders.push(cliCommandFileReader);
readFilesProcessor.sourceFiles = readFilesProcessor.sourceFiles.concat([
{
basePath: CLI_SOURCE_PATH,
include: resolve(CLI_SOURCE_PATH, '*.json'),
basePath: CLI_SOURCE_HELP_PATH,
include: resolve(CLI_SOURCE_HELP_PATH, '*.json'),
fileReader: 'cliCommandFileReader'
},
{
Expand All @@ -42,6 +43,23 @@ module.exports = new Package('cli-docs', [basePackage, contentPackage])
})


.config(function(renderDocsProcessor) {

const cliPackage = require(resolve(CLI_SOURCE_PATH, 'package.json'));
const repoUrlParts = cliPackage.repository.url.replace(/\.git$/, '').split('/');
const version = `v${cliPackage.version}`;
const repo = repoUrlParts.pop();
const owner = repoUrlParts.pop();
const cliVersionInfo = {
gitRepoInfo: { owner, repo },
currentVersion: { raw: version }
};

// Add the cli version data to the renderer, for use in things like github links
renderDocsProcessor.extraData.cliVersionInfo = cliVersionInfo;
})


.config(function(convertToJsonProcessor, postProcessHtml) {
convertToJsonProcessor.docTypes = convertToJsonProcessor.docTypes.concat(['cli-command', 'cli-overview']);
postProcessHtml.docTypes = postProcessHtml.docTypes.concat(['cli-command', 'cli-overview']);
Expand Down
Expand Up @@ -15,6 +15,7 @@ module.exports = function cliCommandFileReader(log) {
name: 'cliCommandFileReader',
defaultPattern: /\.json$/,
getDocs(fileInfo) {
fileInfo.realProjectRelativePath = 'packages/angular/cli/commands/' + fileInfo.relativePath;
try {
const doc = json5.parse(fileInfo.content);
const name = fileInfo.baseName;
Expand All @@ -23,7 +24,6 @@ module.exports = function cliCommandFileReader(log) {
const result = Object.assign(doc, {
content: doc.description,
docType: 'cli-command',
startingLine: 1,
id: `cli-${doc.name}`,
commandAliases: doc.aliases || [],
aliases: computeAliases(doc),
Expand Down
Expand Up @@ -82,11 +82,6 @@ describe('cli-command reader', () => {
]);
});

it('should start at line 1', () => {
const docs = reader.getDocs(fileInfo);
expect(docs[0].startingLine).toEqual(1);
});

it('should extract the short description into the content', () => {
const docs = reader.getDocs(fileInfo);
expect(docs[0].content).toEqual('Add support for a library to your project.');
Expand Down
2 changes: 1 addition & 1 deletion aio/tools/transforms/content-package/readers/content.js
Expand Up @@ -18,7 +18,7 @@ module.exports = function contentFileReader() {
getDocs: function(fileInfo) {

// We return a single element array because content files only contain one document
return [{docType: 'content', content: fileInfo.content, startingLine: 1}];
return [{docType: 'content', content: fileInfo.content}];
}
};
};
Expand Up @@ -37,7 +37,7 @@ describe('contentFileReader', function() {
'project/path/modules/someModule/foo/docs/subfolder/bar.ngdoc', 'A load of content',
'project/path');
expect(fileReader.getDocs(fileInfo)).toEqual([
{docType: 'content', content: 'A load of content', startingLine: 1}
{docType: 'content', content: 'A load of content'}
]);
});
});
Expand Down
2 changes: 2 additions & 0 deletions aio/tools/transforms/templates/cli/cli-command.template.html
@@ -1,6 +1,8 @@
{% import 'lib/cli.html' as cli %}
{% import "../lib/githubLinks.html" as github -%}

<article>
{$ github.githubLinks(doc, cliVersionInfo) $}
{% include 'include/cli-breadcrumb.html' %}
{% include 'include/cli-header.html' %}

Expand Down
@@ -1,3 +1,6 @@
{% extends 'content.template.html' -%}

{% block content %}
<div class="content">
{$ doc.description | marked $}
</div>
Expand All @@ -20,4 +23,5 @@ <h2>Command Overview</h2>
</tr>
{% endfor %}
</tbody>
</table>
</table>
{% endblock %}
4 changes: 3 additions & 1 deletion aio/tools/transforms/templates/content.template.html
Expand Up @@ -2,11 +2,13 @@

{% set relativePath = doc.fileInfo.relativePath %}
{% if doc.title %}{$ ('# ' + doc.title.trim()) | marked $}{% endif %}
{% if 'guide/' in relativePath or 'tutorial/' in relativePath or 'docs.md' in relativePath %}
{% if '/' in relativePath or 'docs.md' in relativePath %}
<div class="github-links">
{$ github.githubEditLink(doc, versionInfo) $}
</div>
{% endif %}
{% block content %}
<div class="content">
{$ doc.description | marked $}
</div>
{% endblock %}
13 changes: 9 additions & 4 deletions aio/tools/transforms/templates/lib/githubLinks.html
Expand Up @@ -3,16 +3,21 @@
{%- endmacro %}

{% macro githubViewHref(doc, versionInfo) -%}
https://github.com/{$ versionInfo.gitRepoInfo.owner $}/{$ versionInfo.gitRepoInfo.repo $}/tree/{$ versionInfo.currentVersion.isSnapshot and versionInfo.currentVersion.SHA or versionInfo.currentVersion.raw $}/packages/{$ doc.fileInfo.realProjectRelativePath $}#L{$ doc.startingLine + 1 $}-L{$ doc.endingLine + 1 $}
{% set githubUrl = 'https://github.com/' + versionInfo.gitRepoInfo.owner + '/' + versionInfo.gitRepoInfo.repo -%}
{% set version = versionInfo.currentVersion.isSnapshot and versionInfo.currentVersion.SHA or versionInfo.currentVersion.raw -%}
{% set lineInfo = doc.startingLine and ('#L' + (doc.startingLine + 1) + '-L' + (doc.endingLine + 1)) or '' -%}
{$ githubUrl $}/tree/{$ version $}/{$ projectRelativePath(doc.fileInfo) $}{$ lineInfo $}
{%- endmacro %}

{% macro githubEditHref(doc, versionInfo) -%}
https://github.com/{$ versionInfo.gitRepoInfo.owner $}/{$ versionInfo.gitRepoInfo.repo $}/edit/master{$ '/packages' if doc.docType !== 'content' $}/{$ projectRelativePath(doc.fileInfo) $}?message=docs
{% macro githubEditHref(doc, versionInfo, pathPrefix) -%}
{% set githubUrl = 'https://github.com/' + versionInfo.gitRepoInfo.owner + '/' + versionInfo.gitRepoInfo.repo -%}
{% set lineInfo = doc.startingLine and ('#L' + (doc.startingLine + 1) + '-L' + (doc.endingLine + 1)) or '' -%}
{$ githubUrl $}/edit/master/{$ projectRelativePath(doc.fileInfo) $}?message=docs
{%- if doc.moduleDoc %}({$ doc.moduleDoc.id.split('/')[0] $})
{%- elseif doc.docType === 'module' %}({$ doc.id.split('/')[0] $})
{%- elseif doc.docType === 'content' %}
{%- else %}(...){%- endif -%}
%3A%20describe%20your%20change...{% if doc.docType !== 'content' %}#L{$ doc.startingLine + 1 $}-L{$ doc.endingLine + 1 $}{% endif %}
%3A%20describe%20your%20change...{$ lineInfo $}
{%- endmacro %}

{% macro githubEditLink(doc, versionInfo) -%}
Expand Down
1 change: 1 addition & 0 deletions scripts/ci/test-aio-tools.sh
Expand Up @@ -10,6 +10,7 @@ source ${thisDir}/_travis-fold.sh
travisFoldStart "test.docs"
(
cd ${PROJECT_ROOT}/aio
yarn setup
yarn tools-test
)
travisFoldEnd "test.docs"