2021-04-01 19:28:17 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2021-05-21 12:45:36 -04:00
|
|
|
import {render} from 'ejs';
|
2021-04-01 19:28:17 -04:00
|
|
|
import {join} from 'path';
|
|
|
|
import * as semver from 'semver';
|
2021-05-06 15:30:35 -04:00
|
|
|
import {CommitFromGitLog} from '../../../commit-message/parse';
|
2021-04-01 19:28:17 -04:00
|
|
|
|
|
|
|
import {getCommitsInRange} from '../../../commit-message/utils';
|
|
|
|
import {promptInput} from '../../../utils/console';
|
|
|
|
import {GitClient} from '../../../utils/git/index';
|
2021-05-06 15:30:35 -04:00
|
|
|
import {DevInfraReleaseConfig, getReleaseConfig, ReleaseNotesConfig} from '../../config/index';
|
2021-04-01 19:28:17 -04:00
|
|
|
import {changelogPath} from '../constants';
|
|
|
|
import {RenderContext} from './context';
|
|
|
|
|
2021-05-21 12:45:36 -04:00
|
|
|
import changelogTemplate from './templates/changelog';
|
|
|
|
import githubReleaseTemplate from './templates/github-release';
|
|
|
|
|
2021-04-01 19:28:17 -04:00
|
|
|
/** Gets the path for the changelog file in a given project. */
|
|
|
|
export function getLocalChangelogFilePath(projectDir: string): string {
|
|
|
|
return join(projectDir, changelogPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release note generation. */
|
|
|
|
export class ReleaseNotes {
|
2021-05-06 15:30:35 -04:00
|
|
|
static async fromRange(version: semver.SemVer, startingRef: string, endingRef: string) {
|
|
|
|
return new ReleaseNotes(version, startingRef, endingRef);
|
2021-04-19 14:58:32 -04:00
|
|
|
}
|
|
|
|
|
2021-04-01 19:28:17 -04:00
|
|
|
/** An instance of GitClient. */
|
2021-04-08 15:34:55 -04:00
|
|
|
private git = GitClient.getInstance();
|
2021-04-01 19:28:17 -04:00
|
|
|
/** The RenderContext to be used during rendering. */
|
|
|
|
private renderContext: RenderContext|undefined;
|
|
|
|
/** The title to use for the release. */
|
|
|
|
private title: string|false|undefined;
|
2021-04-19 14:58:32 -04:00
|
|
|
/** A promise resolving to a list of Commits since the latest semver tag on the branch. */
|
2021-05-06 15:30:35 -04:00
|
|
|
private commits: Promise<CommitFromGitLog[]> =
|
|
|
|
this.getCommitsInRange(this.startingRef, this.endingRef);
|
|
|
|
/** The configuration for release notes. */
|
|
|
|
private config: ReleaseNotesConfig = this.getReleaseConfig().releaseNotes;
|
2021-04-01 19:28:17 -04:00
|
|
|
|
2021-05-06 15:30:35 -04:00
|
|
|
protected constructor(
|
|
|
|
public version: semver.SemVer, private startingRef: string, private endingRef: string) {}
|
2021-04-01 19:28:17 -04:00
|
|
|
|
|
|
|
/** Retrieve the release note generated for a Github Release. */
|
|
|
|
async getGithubReleaseEntry(): Promise<string> {
|
2021-05-21 12:45:36 -04:00
|
|
|
return render(githubReleaseTemplate, await this.generateRenderContext(), {rmWhitespace: true});
|
2021-04-01 19:28:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Retrieve the release note generated for a CHANGELOG entry. */
|
|
|
|
async getChangelogEntry() {
|
2021-05-21 12:45:36 -04:00
|
|
|
return render(changelogTemplate, await this.generateRenderContext(), {rmWhitespace: true});
|
2021-04-01 19:28:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt the user for a title for the release, if the project's configuration is defined to use a
|
|
|
|
* title.
|
|
|
|
*/
|
|
|
|
async promptForReleaseTitle() {
|
|
|
|
if (this.title === undefined) {
|
2021-05-06 15:30:35 -04:00
|
|
|
if (this.config.useReleaseTitle) {
|
2021-04-01 19:28:17 -04:00
|
|
|
this.title = await promptInput('Please provide a title for the release:');
|
|
|
|
} else {
|
|
|
|
this.title = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Build the render context data object for constructing the RenderContext instance. */
|
|
|
|
private async generateRenderContext(): Promise<RenderContext> {
|
|
|
|
if (!this.renderContext) {
|
|
|
|
this.renderContext = new RenderContext({
|
|
|
|
commits: await this.commits,
|
2021-04-19 14:58:32 -04:00
|
|
|
github: this.git.remoteConfig,
|
2021-04-01 19:28:17 -04:00
|
|
|
version: this.version.format(),
|
2021-05-06 15:30:35 -04:00
|
|
|
groupOrder: this.config.groupOrder,
|
|
|
|
hiddenScopes: this.config.hiddenScopes,
|
2021-04-01 19:28:17 -04:00
|
|
|
title: await this.promptForReleaseTitle(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.renderContext;
|
|
|
|
}
|
2021-05-06 15:30:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
// These methods are used for access to the utility functions while allowing them to be
|
|
|
|
// overwritten in subclasses during testing.
|
|
|
|
protected async getCommitsInRange(from: string, to?: string) {
|
|
|
|
return getCommitsInRange(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getReleaseConfig(config?: Partial<DevInfraReleaseConfig>) {
|
|
|
|
return getReleaseConfig(config);
|
|
|
|
}
|
2021-04-01 19:28:17 -04:00
|
|
|
}
|