Skip to content

Commit 5c02e20

Browse files
Add semver sort subcommand for sorting semantic versions (#133)
* Initial plan * Initial planning for sort subcommand feature Co-authored-by: justinmchase <[email protected]> * Implement sort subcommand with comprehensive tests Co-authored-by: justinmchase <[email protected]> * Add README documentation for sort command Co-authored-by: justinmchase <[email protected]> * Address PR feedback: handle empty versions gracefully Co-authored-by: justinmchase <[email protected]> * Update test to capture build metadata behavior and update semver to 1.0.6 Co-authored-by: justinmchase <[email protected]> * Add sort command to action.yml and docker-action tests, update copilot instructions Co-authored-by: justinmchase <[email protected]> * Remove special case logic for prerelease in none increment Co-authored-by: justinmchase <[email protected]> * Revert unintended test changes to version files Co-authored-by: justinmchase <[email protected]> * Update value input description to include sort command Co-authored-by: justinmchase <[email protected]> * Add tests for build metadata in None increment Co-authored-by: justinmchase <[email protected]> * Split version strings by whitespace to handle space-separated versions Co-authored-by: justinmchase <[email protected]> * Remove --desc option, keep only --asc since descending is default Co-authored-by: justinmchase <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: justinmchase <[email protected]> Co-authored-by: Justin Chase <[email protected]>
1 parent 92a6449 commit 5c02e20

File tree

12 files changed

+435
-94
lines changed

12 files changed

+435
-94
lines changed

.github/copilot-instructions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,33 @@ The GitHub Actions workflow requires:
200200

201201
**NEVER CANCEL operations - they complete very quickly. Set timeouts of 60+
202202
seconds minimum for safety.**
203+
204+
## Adding New Subcommands
205+
206+
When adding a new subcommand to the CLI, you MUST also update the following
207+
files:
208+
209+
1. **action.yml**: Add the new subcommand to the `inputs.command.options` list
210+
- This makes the command available in the GitHub Action
211+
- Keep the list alphabetically sorted for consistency
212+
213+
2. **.github/workflows/checks.yml**: Add an integration test step in the
214+
`docker-action` job
215+
- Add a step that tests the new subcommand using the action
216+
- Follow the naming pattern: `- name: <Command Name>`
217+
- Provide appropriate test values via the `with:` section
218+
- This ensures the command works correctly in the Docker action context
219+
220+
3. **README.md**: Update the usage documentation
221+
- Add the new command to the commands list
222+
- Provide usage examples
223+
- Document any command-specific options or flags
224+
225+
4. **src/commands/mod.ts**: Export the new command
226+
5. **main.ts**: Register the new command with yargs
227+
228+
**Example**: When adding the `sort` subcommand:
229+
230+
- Added `- sort` to action.yml command options
231+
- Added a "Sort" test step in docker-action job with test values
232+
- Updated README with sort command documentation and examples

.github/workflows/checks.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ jobs:
141141
command: lte
142142
value: 1.2.3
143143
compare-to: 1.2.3
144+
- name: Sort
145+
uses: ./
146+
with:
147+
command: sort
148+
value: 2.0.0 1.0.0 3.0.0
144149

145150
docker:
146151
runs-on: ubuntu-latest

README.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ deno task install
4747
semver <command>
4848

4949
Commands:
50-
semver get Get the version
51-
semver set <value> Set the version
52-
semver inc Increment the version
53-
semver parse [value] Parse the version and print
54-
semver cmp <v1> <v2> Compare v1 to v2 and return -1/0/1
55-
semver gt <v1> <v2> Return 0 if v1 is greater than v2, else 1
56-
semver gte <v1> <v2> Return 0 if v1 is greater than or equal to v2, else 1
57-
semver lt <v1> <v2> Return 0 if v1 is less than v2, else 1
58-
semver lte <v1> <v2> Return 0 if v1 is less than or equal to v2, else 1
59-
semver eq <v1> <v2> Return 0 if v1 is equal to v2, else 1
50+
semver get Get the version
51+
semver set <value> Set the version
52+
semver inc Increment the version
53+
semver parse [value] Parse the version and print
54+
semver cmp <v1> <v2> Compare v1 to v2 and return -1/0/1
55+
semver gt <v1> <v2> Return 0 if v1 is greater than v2, else 1
56+
semver gte <v1> <v2> Return 0 if v1 is greater than or equal to v2, else 1
57+
semver lt <v1> <v2> Return 0 if v1 is less than v2, else 1
58+
semver lte <v1> <v2> Return 0 if v1 is less than or equal to v2, else 1
59+
semver eq <v1> <v2> Return 0 if v1 is equal to v2, else 1
60+
semver sort [versions..] Sort semantic versions
6061

6162
Options:
6263
--help Show help [boolean]
@@ -74,6 +75,10 @@ command will create the `VERSION` file if it doesn't already exist.
7475
The `parse` command accepts a version string as input and parses and prints that
7576
version as output if it is valid.
7677
78+
The `sort` command accepts one or more version strings and outputs them in
79+
sorted order (descending by default, one version per line). Use the `-a` flag
80+
for ascending order, or read versions from stdin using `--`.
81+
7782
#### examples
7883
7984
```sh
@@ -97,6 +102,28 @@ semver get # 1.2.3
97102
semver parse 1.0.0 # {"major":1,"minor":1,"patch":0,"prerelease":[],"build":[]}
98103
```
99104
105+
```sh
106+
# sort versions in descending order (default)
107+
semver sort 2.0.0 1.0.0 3.0.0
108+
# 3.0.0
109+
# 2.0.0
110+
# 1.0.0
111+
```
112+
113+
```sh
114+
# sort versions in ascending order
115+
semver sort -a 2.0.0 1.0.0 3.0.0
116+
# 1.0.0
117+
# 2.0.0
118+
# 3.0.0
119+
```
120+
121+
```sh
122+
# sort versions from stdin
123+
cat versions.txt | semver sort --
124+
# (sorted output)
125+
```
126+
100127
### Incrementing
101128
102129
When calling the command `inc` the `VERSION` file will be updated based on the

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ inputs:
2020
- gte
2121
- lt
2222
- lte
23+
- sort
2324
sub-command:
2425
type: choice
2526
description: "The kind of increment (major|minor|patch|none) for (get|inc) commands"
@@ -39,7 +40,7 @@ inputs:
3940
required: false
4041
value:
4142
type: string
42-
description: The Version (for set, parse, eq, cmp, gt, gte, lt, lte commands)
43+
description: The Version (for set, parse, eq, cmp, gt, gte, lt, lte commands) or space-separated versions (for sort command)
4344
required: false
4445
compare-to:
4546
type: string

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"@std/fmt": "jsr:@std/fmt@^1.0.8",
66
"json5": "npm:json5@^2.2.3",
77
"jsonc-parser": "npm:jsonc-parser@^3.2.1",
8-
"semver": "jsr:@std/semver@^1.0.3",
8+
"semver": "jsr:@std/semver@^1.0.6",
99
"path": "jsr:@std/path@^1.0.6",
1010
"assert": "jsr:@std/assert@^1.0.6",
1111
"testing/bdd": "jsr:@std/testing@^1.0.3/bdd",

deno.lock

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
lte,
1313
parse,
1414
set,
15+
sort,
1516
} from "./src/commands/mod.ts";
1617
import { getContext } from "./src/context.ts";
1718
import { ApplicationError } from "./src/errors/application.error.ts";
@@ -34,6 +35,7 @@ try {
3435
.command(lt)
3536
.command(lte)
3637
.command(eq)
38+
.command(sort)
3739
.strictOptions()
3840
.strictCommands()
3941
.demandCommand(1)

src/commands/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from "./gte.ts";
88
export * from "./lt.ts";
99
export * from "./lte.ts";
1010
export * from "./eq.ts";
11+
export * from "./sort.ts";

0 commit comments

Comments
 (0)