Skip to content

Commit 62ed714

Browse files
committed
Extract common functionality into BaseScriptAction base class
1 parent d5468d3 commit 62ed714

File tree

3 files changed

+81
-56
lines changed

3 files changed

+81
-56
lines changed

apps/rush-lib/src/cli/actions/BaseRushAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { RushConfiguration } from '../../api/RushConfiguration';
1616
import { EventHooksManager } from '../../logic/EventHooksManager';
1717
import { RushCommandLineParser } from './../RushCommandLineParser';
1818

19-
export interface IRushCommandLineActionOptions extends ICommandLineActionOptions {
19+
export interface IBaseRushActionOptions extends ICommandLineActionOptions {
2020
/**
2121
* If true, no locking mechanism will be enforced when this action is run.
2222
* Note this defaults to false (which is a safer assumption in case this value
@@ -46,7 +46,7 @@ export abstract class BaseRushAction extends CommandLineAction {
4646
return this._parser;
4747
}
4848

49-
constructor(options: IRushCommandLineActionOptions) {
49+
constructor(options: IBaseRushActionOptions) {
5050
super(options);
5151

5252
this._parser = options.parser;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { CommandLineParameter } from '@microsoft/ts-command-line';
5+
import { BaseRushAction, IBaseRushActionOptions } from '../actions/BaseRushAction';
6+
import { CommandLineConfiguration } from '../../api/CommandLineConfiguration';
7+
import { ParameterJson } from '../../api/CommandLineJson';
8+
import { RushConstants } from '../../api/RushConstants';
9+
10+
/**
11+
* Constructor parameters for BaseScriptAction
12+
*/
13+
export interface IBaseScriptActionOptions extends IBaseRushActionOptions {
14+
commandLineConfiguration: CommandLineConfiguration;
15+
}
16+
17+
/**
18+
* Base class for command-line actions that are implemented using user-defined scripts.
19+
* These actions and their parameters can be defined via common/config/command-line.json.
20+
*/
21+
export abstract class BaseScriptAction extends BaseRushAction {
22+
protected readonly _commandLineConfiguration: CommandLineConfiguration;
23+
protected readonly customParameters: CommandLineParameter[] = [];
24+
25+
constructor(
26+
options: IBaseScriptActionOptions
27+
) {
28+
super(options);
29+
this._commandLineConfiguration = options.commandLineConfiguration;
30+
}
31+
32+
protected defineScriptParameters(): void {
33+
// Find any parameters that are associated with this command
34+
for (const parameter of this._commandLineConfiguration.parameters) {
35+
let associated: boolean = false;
36+
for (const associatedCommand of parameter.associatedCommands) {
37+
if (associatedCommand === this.actionName) {
38+
associated = true;
39+
}
40+
}
41+
42+
if (associated) {
43+
let customParameter: CommandLineParameter | undefined;
44+
45+
switch (parameter.parameterKind) {
46+
case 'flag':
47+
customParameter = this.defineFlagParameter({
48+
parameterShortName: parameter.shortName,
49+
parameterLongName: parameter.longName,
50+
description: parameter.description
51+
});
52+
break;
53+
case 'choice':
54+
customParameter = this.defineChoiceParameter({
55+
parameterShortName: parameter.shortName,
56+
parameterLongName: parameter.longName,
57+
description: parameter.description,
58+
alternatives: parameter.alternatives.map(x => x.name),
59+
defaultValue: parameter.defaultValue
60+
});
61+
break;
62+
default:
63+
throw new Error(`${RushConstants.commandLineFilename} defines a parameter "${parameter!.longName}"`
64+
+ ` using an unsupported parameter kind "${parameter!.parameterKind}"`);
65+
}
66+
if (customParameter) {
67+
this.customParameters.push(customParameter);
68+
}
69+
}
70+
}
71+
}
72+
}

apps/rush-lib/src/cli/scriptActions/BulkScriptAction.ts

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,26 @@ import {
1010
} from '../../index';
1111

1212
import {
13-
CommandLineParameter,
1413
CommandLineFlagParameter,
1514
CommandLineStringParameter,
1615
CommandLineStringListParameter,
1716
CommandLineParameterKind
1817
} from '@microsoft/ts-command-line';
1918

20-
import { BaseRushAction, IRushCommandLineActionOptions } from '../actions/BaseRushAction';
2119
import { SetupChecks } from '../../logic/SetupChecks';
2220
import { TaskSelector } from '../../logic/TaskSelector';
2321
import { Stopwatch } from '../../utilities/Stopwatch';
2422
import { AlreadyReportedError } from '../../utilities/AlreadyReportedError';
25-
import { CommandLineConfiguration } from '../../api/CommandLineConfiguration';
26-
import { ParameterJson } from '../../api/CommandLineJson';
27-
import { RushConstants } from '../../api/RushConstants';
23+
import { BaseScriptAction, IBaseScriptActionOptions } from './BaseScriptAction';
2824

29-
export interface ICustomRushActionOptions extends IRushCommandLineActionOptions {
25+
export interface IBulkScriptActionOptions extends IBaseScriptActionOptions {
3026
enableParallelism: boolean;
3127
ignoreMissingScript: boolean;
32-
commandLineConfiguration: CommandLineConfiguration;
3328
}
3429

35-
export class BulkScriptAction extends BaseRushAction {
30+
export class BulkScriptAction extends BaseScriptAction {
3631
private _enableParallelism: boolean;
3732
private _ignoreMissingScript: boolean;
38-
private _commandLineConfiguration: CommandLineConfiguration;
3933

4034
private _changedProjectsOnly: CommandLineFlagParameter;
4135
private _fromFlag: CommandLineStringListParameter;
@@ -44,15 +38,12 @@ export class BulkScriptAction extends BaseRushAction {
4438
private _verboseParameter: CommandLineFlagParameter;
4539
private _parallelismParameter: CommandLineStringParameter | undefined;
4640

47-
private _customParameters: CommandLineParameter[] = [];
48-
4941
constructor(
50-
options: ICustomRushActionOptions
42+
options: IBulkScriptActionOptions
5143
) {
5244
super(options);
5345
this._enableParallelism = options.enableParallelism;
5446
this._ignoreMissingScript = options.ignoreMissingScript;
55-
this._commandLineConfiguration = options.commandLineConfiguration;
5647
}
5748

5849
public run(): Promise<void> {
@@ -75,7 +66,7 @@ export class BulkScriptAction extends BaseRushAction {
7566
// Collect all custom parameter values
7667
const customParameterValues: string[] = [];
7768

78-
for (const customParameter of this._customParameters) {
69+
for (const customParameter of this.customParameters) {
7970
customParameter.appendToArgList(customParameterValues);
8071
}
8172

@@ -155,44 +146,7 @@ export class BulkScriptAction extends BaseRushAction {
155146
});
156147
}
157148

158-
// Find any parameters that are associated with this command
159-
for (const parameter of this._commandLineConfiguration.parameters) {
160-
let associated: boolean = false;
161-
for (const associatedCommand of parameter.associatedCommands) {
162-
if (associatedCommand === this.actionName) {
163-
associated = true;
164-
}
165-
}
166-
167-
if (associated) {
168-
let customParameter: CommandLineParameter | undefined;
169-
170-
switch (parameter.parameterKind) {
171-
case 'flag':
172-
customParameter = this.defineFlagParameter({
173-
parameterShortName: parameter.shortName,
174-
parameterLongName: parameter.longName,
175-
description: parameter.description
176-
});
177-
break;
178-
case 'choice':
179-
customParameter = this.defineChoiceParameter({
180-
parameterShortName: parameter.shortName,
181-
parameterLongName: parameter.longName,
182-
description: parameter.description,
183-
alternatives: parameter.alternatives.map(x => x.name),
184-
defaultValue: parameter.defaultValue
185-
});
186-
break;
187-
default:
188-
throw new Error(`${RushConstants.commandLineFilename} defines a parameter "${parameter!.longName}"`
189-
+ ` using an unsupported parameter kind "${parameter!.parameterKind}"`);
190-
}
191-
if (customParameter) {
192-
this._customParameters.push(customParameter);
193-
}
194-
}
195-
}
149+
this.defineScriptParameters();
196150
}
197151

198152
private _mergeToProjects(): string[] {
@@ -238,13 +192,12 @@ export class BulkScriptAction extends BaseRushAction {
238192
}
239193

240194
private _collectTelemetry(stopwatch: Stopwatch, success: boolean): void {
241-
242195
const extraData: { [key: string]: string } = {
243196
command_to: (this._toFlag.values.length > 0).toString(),
244197
command_from: (this._fromFlag.values.length > 0).toString()
245198
};
246199

247-
for (const customParameter of this._customParameters) {
200+
for (const customParameter of this.customParameters) {
248201
switch (customParameter.kind) {
249202
case CommandLineParameterKind.Flag:
250203
case CommandLineParameterKind.Choice:

0 commit comments

Comments
 (0)