Skip to content

Commit a1007cc

Browse files
committed
Address feedback
1 parent fe35844 commit a1007cc

File tree

7 files changed

+221
-239
lines changed

7 files changed

+221
-239
lines changed

tools/release/git/executor.ts renamed to tools/release/git/git-client.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import {spawnSync} from 'child_process';
22

33
/**
4-
* Class that can be used to execute Git commands within a given project directory. Relying
5-
* on the working directory of the current process is not good because it's not guaranteed
6-
* that the working directory is always the target project directory (e.g. w/ bazel run).
4+
* Class that can be used to execute Git commands within a given project directory.
5+
*
6+
* Relying on the working directory of the current process is not good because it's not
7+
* guaranteed that the working directory is always the target project directory.
78
*/
8-
export class GitCommandExecutor {
9+
export class GitClient {
910

10-
constructor(public projectDir: string) {}
11+
constructor(public projectDir: string, public remoteGitUrl: string) {}
1112

12-
/** Returns the currently checked out branch for the current working directory. */
13+
/** Gets the currently checked out branch for the project directory. */
1314
getCurrentBranch() {
1415
return spawnSync('git', ['symbolic-ref', '--short', 'HEAD'], {cwd: this.projectDir})
1516
.stdout.toString().trim();
1617
}
1718

18-
/** Returns the commit SHA for the remote repository reference. */
19-
getRemoteCommitSha(remoteRef: string, branchName: string): string {
20-
return spawnSync('git', ['ls-remote', remoteRef, '-h', `refs/heads/${branchName}`],
19+
/** Gets the commit SHA for the specified remote repository branch. */
20+
getRemoteCommitSha(branchName: string): string {
21+
return spawnSync('git', ['ls-remote', this.remoteGitUrl, '-h', `refs/heads/${branchName}`],
2122
{cwd: this.projectDir}).stdout.toString().trim();
2223
}
2324

24-
/** Returns the latest commit SHA for the specified git reference. */
25+
/** Gets the latest commit SHA for the specified git reference. */
2526
getLocalCommitSha(refName: string) {
2627
return spawnSync('git', ['rev-parse', refName], {cwd: this.projectDir})
2728
.stdout.toString().trim();
2829
}
2930

30-
/** Whether the current Git repository has uncommitted changes. */
31+
/** Gets whether the current Git repository has uncommitted changes. */
3132
hasUncommittedChanges(): boolean {
3233
return spawnSync('git', ['diff-index', '--quiet', 'HEAD'], {cwd: this.projectDir}).status !== 0;
3334
}
Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,57 @@
1-
import {parseVersionName, VersionInfo} from '../version-name/parse-version';
2-
import {bold, red, yellow} from 'chalk';
3-
import {ChoiceType, prompt} from 'inquirer';
4-
import {createNewVersion} from '../version-name/create-version';
5-
import {ReleaseType, validateExpectedVersion} from '../version-name/check-version';
1+
import {ChoiceType, prompt, Separator} from 'inquirer';
2+
import {createNewVersion, ReleaseType} from '../version-name/create-version';
3+
import {parseVersionName, Version} from '../version-name/parse-version';
64

75
/** Answers that will be prompted for. */
86
type VersionPromptAnswers = {
9-
releaseType: ReleaseType;
107
versionName: string;
11-
};
12-
13-
/** Available options for selecting a release type. */
14-
const releaseTypeChoices = {
15-
custom: {value: 'custom', name: 'Release w/ custom version.'},
16-
stable: {value: 'stable', name: `Stable release`},
17-
major: {value: 'major', name: 'Major release'},
18-
minor: {value: 'minor', name: 'Minor release'},
19-
patch: {value: 'patch', name: 'Patch release'},
8+
manualCustomVersion: string;
209
};
2110

2211
/**
2312
* Prompts the current user-input interface for a new version name. The new version will be
2413
* validated to be a proper increment of the specified current version.
2514
*/
26-
export async function promptForNewVersion(versionName: string): Promise<VersionInfo> {
27-
const currentVersion = parseVersionName(versionName);
28-
let allowedReleaseTypes: ChoiceType[] = [releaseTypeChoices.custom];
29-
30-
if (!currentVersion) {
31-
console.warn(red(`Cannot parse current project version. This means that we cannot validate ` +
32-
`the new ${bold('custom')} version that will be specified.`));
33-
} else if (currentVersion.suffix) {
34-
console.warn(yellow(`Since the current project version is a ` +
35-
`"${bold(currentVersion.suffix)}", the new version can be either custom or just the ` +
36-
`stable version.`));
37-
allowedReleaseTypes.unshift(releaseTypeChoices.stable);
15+
export async function promptForNewVersion(currentVersion: Version): Promise<Version> {
16+
const versionChoices: ChoiceType[] = [
17+
new Separator(),
18+
{value: 'custom-release', name: 'Release w/ custom version'}
19+
];
20+
21+
if (currentVersion.prereleaseLabel) {
22+
versionChoices.unshift(
23+
createVersionChoice(currentVersion, 'pre-release', 'Pre-release'),
24+
createVersionChoice(currentVersion, 'stable-release', 'Stable release'));
3825
} else {
39-
allowedReleaseTypes.unshift(
40-
releaseTypeChoices.major, releaseTypeChoices.minor, releaseTypeChoices.patch);
26+
versionChoices.unshift(
27+
createVersionChoice(currentVersion, 'major', 'Major release'),
28+
createVersionChoice(currentVersion, 'minor', 'Minor release'),
29+
createVersionChoice(currentVersion, 'patch', 'Patch release'));
4130
}
4231

4332
const answers = await prompt<VersionPromptAnswers>([{
4433
type: 'list',
45-
name: 'releaseType',
34+
name: 'versionName',
4635
message: `What's the type of the new release?`,
47-
choices: allowedReleaseTypes,
36+
choices: versionChoices,
4837
}, {
4938
type: 'input',
50-
name: 'versionName',
51-
message: 'Please provide the new release name:',
52-
default: ({releaseType}) => createVersionSuggestion(releaseType, currentVersion!),
53-
validate: (enteredVersion, {releaseType}) =>
54-
validateNewVersionName(enteredVersion, currentVersion!, releaseType),
39+
name: 'manualCustomVersion',
40+
message: 'Please provide a custom release name:',
41+
validate: enteredVersion =>
42+
!parseVersionName(enteredVersion) ? 'This is not a valid Semver version' : true,
43+
when: ({versionName}) => versionName === 'custom-release'
5544
}]);
5645

57-
return parseVersionName(answers.versionName);
58-
}
59-
60-
/** Creates a suggested version for the expected version type. */
61-
function createVersionSuggestion(releaseType: ReleaseType, currentVersion: VersionInfo) {
62-
// In case the new version is expected to be custom, we can not make any suggestion because
63-
// we don't know the reasoning for a new custom version.
64-
if (releaseType === 'custom') {
65-
return null;
66-
} else if (releaseType === 'stable') {
67-
return createNewVersion(currentVersion!).format();
68-
}
69-
70-
return createNewVersion(currentVersion!, releaseType).format();
46+
return parseVersionName(answers.manualCustomVersion || answers.versionName);
7147
}
7248

73-
/**
74-
* Validates the specified new version by ensuring that the new version is following the Semver
75-
* format and matches the specified target version type.
76-
*/
77-
function validateNewVersionName(newVersionName: string, currentVersion: VersionInfo,
78-
releaseType: ReleaseType) {
79-
const parsedVersion = parseVersionName(newVersionName);
80-
81-
if (!parsedVersion) {
82-
return 'Version does not follow the Semver format.';
83-
}
84-
85-
// In case the release type is custom, we just need to make sure that the new version
86-
// is following the Semver format.
87-
if (releaseType === 'custom') {
88-
return true;
89-
}
90-
91-
if (!validateExpectedVersion(parsedVersion, currentVersion, releaseType)) {
92-
return `Version is not a proper increment for "${releaseType}"`;
93-
}
49+
/** Creates a new choice for selecting a version inside of an Inquirer list prompt. */
50+
function createVersionChoice(currentVersion: Version, releaseType: ReleaseType, message: string) {
51+
const versionName = createNewVersion(currentVersion, releaseType).format();
9452

95-
return true;
53+
return {
54+
value: versionName,
55+
name: `${message} (${versionName})`
56+
};
9657
}

0 commit comments

Comments
 (0)