-
-
Notifications
You must be signed in to change notification settings - Fork 262
Add new --matrix option to multiply commands #526
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
Open
cdrini
wants to merge
6
commits into
open-cli-tools:main
Choose a base branch
from
cdrini:517/feature/matrix-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f656f5d
Add new --matrix option to multiply commands
cdrini f4dbb85
Merge branch 'main' into 517/feature/matrix-option
paescuj a95b8bf
Update test to use vitest
paescuj 661431b
Change --matrix option to require name
cdrini 71a3349
Rename matrices option to matrix
cdrini cbed67a
Fix some --matrix edge cases
cdrini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add new --matrix option to multiply commands
- Loading branch information
commit f656f5d4c06690f7441a2b8a224241218204d5b0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { CommandInfo } from '../command'; | ||
| import { combinations, ExpandMatrices } from './expand-matrices'; | ||
|
|
||
| const createCommandInfo = (command: string): CommandInfo => ({ | ||
| command, | ||
| name: '', | ||
| }); | ||
|
|
||
| describe('ExpandMatrices', () => { | ||
| it('should replace placeholders with matrix values', () => { | ||
| const matrices = [ | ||
| ['a', 'b'], | ||
| ['1', '2'], | ||
| ]; | ||
| const expandMatrices = new ExpandMatrices(matrices); | ||
| const commandInfo = createCommandInfo('echo {1} and {2}'); | ||
|
|
||
| const result = expandMatrices.parse(commandInfo); | ||
|
|
||
| expect(result).toEqual([ | ||
| { command: 'echo a and 1', name: '' }, | ||
| { command: 'echo a and 2', name: '' }, | ||
| { command: 'echo b and 1', name: '' }, | ||
| { command: 'echo b and 2', name: '' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle escaped placeholders', () => { | ||
| const matrices = [['a', 'b']]; | ||
| const expandMatrices = new ExpandMatrices(matrices); | ||
| const commandInfo = createCommandInfo('echo \\{1} and {1}'); | ||
|
|
||
| const result = expandMatrices.parse(commandInfo); | ||
|
|
||
| expect(result).toEqual([ | ||
| { command: 'echo {1} and a', name: '' }, | ||
| { command: 'echo {1} and b', name: '' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should replace placeholders with empty string if index is out of bounds', () => { | ||
| const matrices = [['a']]; | ||
| const expandMatrices = new ExpandMatrices(matrices); | ||
| const commandInfo = createCommandInfo('echo {2}'); | ||
|
|
||
| const result = expandMatrices.parse(commandInfo); | ||
|
|
||
| expect(result).toEqual([{ command: 'echo ', name: '' }]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('combinations', () => { | ||
| it('should return all possible combinations of the given dimensions', () => { | ||
| const dimensions = [ | ||
| ['a', 'b'], | ||
| ['1', '2'], | ||
| ]; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([ | ||
| ['a', '1'], | ||
| ['a', '2'], | ||
| ['b', '1'], | ||
| ['b', '2'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle single dimension', () => { | ||
| const dimensions = [['a', 'b']]; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([['a'], ['b']]); | ||
| }); | ||
|
|
||
| it('should handle empty dimensions', () => { | ||
| const dimensions: string[][] = []; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([[]]); | ||
| }); | ||
|
|
||
| it('should handle dimensions with empty arrays', () => { | ||
| const dimensions = [['a', 'b'], []]; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should handle dimensions with multiple empty arrays', () => { | ||
| const dimensions = [[], []]; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should handle dimensions with some empty arrays', () => { | ||
| const dimensions = [['a', 'b'], [], ['x', 'y']]; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should handle dimensions with all empty arrays', () => { | ||
| const dimensions = [[], [], []]; | ||
|
|
||
| const result = combinations(dimensions); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { quote } from 'shell-quote'; | ||
|
|
||
| import { CommandInfo } from '../command'; | ||
| import { CommandParser } from './command-parser'; | ||
|
|
||
| /** | ||
| * Replace placeholders with new commands for each combination of matrices. | ||
| */ | ||
| export class ExpandMatrices implements CommandParser { | ||
| private _bindings: string[][]; | ||
|
|
||
| constructor(private readonly matrices: readonly string[][]) { | ||
|
cdrini marked this conversation as resolved.
Outdated
|
||
| this.matrices = matrices; | ||
|
cdrini marked this conversation as resolved.
Outdated
|
||
| this._bindings = combinations(matrices); | ||
| } | ||
|
|
||
| parse(commandInfo: CommandInfo) { | ||
| return this._bindings.map((binding) => this.replacePlaceholders(commandInfo, binding)); | ||
| } | ||
|
|
||
| private replacePlaceholders(commandInfo: CommandInfo, binding: string[]): CommandInfo { | ||
| const command = commandInfo.command.replace( | ||
| /\\?\{([0-9]*)?\}/g, | ||
| (match, placeholderTarget) => { | ||
| // Don't replace the placeholder if it is escaped by a backslash. | ||
| if (match.startsWith('\\')) { | ||
| return match.slice(1); | ||
| } | ||
|
|
||
| let index = 0; | ||
| if (placeholderTarget && !isNaN(placeholderTarget)) { | ||
|
cdrini marked this conversation as resolved.
Outdated
|
||
| index = parseInt(placeholderTarget, 10) - 1; | ||
| } | ||
|
|
||
| // Replace numeric placeholder if value exists in additional arguments. | ||
| if (index < binding.length) { | ||
| return quote([binding[index]]); | ||
| } | ||
|
|
||
| // Replace placeholder with empty string | ||
| // if value doesn't exist in additional arguments. | ||
| return ''; | ||
| }, | ||
| ); | ||
|
|
||
| return { ...commandInfo, command }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns all possible combinations of the given dimensions. | ||
| */ | ||
| export function combinations(dimensions: readonly string[][]): string[][] { | ||
| return dimensions.reduce( | ||
| (acc, dimension) => { | ||
| return acc.flatMap((accItem) => | ||
| dimension.map((dimensionItem) => accItem.concat(dimensionItem)), | ||
| ); | ||
| }, | ||
| [[]] as string[][], | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.