Skip to content

Commit 91ec9be

Browse files
authored
Merge pull request #28 from r7kamura/command-auto-detection
Make `command` optional
2 parents 6a0f1d7 + df833d1 commit 91ec9be

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

.github/workflows/bump-request.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@ jobs:
1919
- uses: actions/checkout@v3
2020
- uses: ./
2121
with:
22-
command: |
23-
sed -i -r "s/([0-9]+\.[0-9]+\.[0-9]+)/$VERSION/" VERSION
2422
release_type: ${{ inputs.release_type }}

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ jobs:
2828
steps:
2929
- uses: r7kamura/bump-request@v0
3030
with:
31-
command: |
32-
npm version --no-git-commit-hooks --no-git-tag-version "$VERSION"
3331
release_type: ${{ inputs.release_type }}
3432
```
3533
@@ -60,24 +58,20 @@ This action simply creates a pull request and does nothing more. If you want to
6058

6159
### `command`
6260

63-
Shell command for modifying files that contain versions such as package.json, Catgo.toml, etc. Note that the next version is passed as an environment variable `VERSION`.
61+
Shell command for modifying files that contain versions such as package.json, Catgo.toml, etc.
62+
For supported types of packages, this will be handled automatically without specifying this,
63+
but if you are dealing with packages that are not, you will need to specify this.
6464

65-
- required
65+
Currently, the following types of packages are supported:
6666

67-
NPM package example:
67+
- crate
68+
- gem
69+
- npm
70+
- plain (where version is managed by VERSION file)
6871

69-
```yaml
70-
command: |
71-
npm version --no-git-commit-hooks --no-git-tag-version "$VERSION"
72-
```
73-
74-
Ruby gem example:
72+
If you have a well-known package that is missing, please send us a pull request.
7573

76-
```yaml
77-
command: |
78-
sed -i -r "s/[0-9]+\.[0-9]+\.[0-9]+/$VERSION/" lib/my_ruby_gem/version.rb
79-
bundle install
80-
```
74+
Note that the next version value is passed as an environment variable `VERSION` on running this command.
8175

8276
### `release_type`
8377

action.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Create a pull request that bumps version.
33
inputs:
44
command:
55
description: Shell command for modifying files that contain versions such as package.json, Catgo.toml, etc.
6-
required: true
6+
required: false
77
github_token:
88
description: GitHub access token.
99
required: false
@@ -27,12 +27,17 @@ runs:
2727
GITHUB_TOKEN: ${{ inputs.github_token || github.token }}
2828
shell: bash
2929
id: detect_next_version
30-
- name: Run command
30+
- if: inputs.command
3131
run: |
3232
${{ inputs.command }}
3333
env:
3434
VERSION: ${{ steps.detect_next_version.outputs.version }}
3535
shell: bash
36+
- if: ${{ !inputs.command }}
37+
run: deno run --allow-all src/changeVersion.ts
38+
env:
39+
BUMP_REQUEST_VERSION: ${{ steps.detect_next_version.outputs.version }}
40+
shell: bash
3641
- run: deno run --allow-all src/createPullRequest.ts
3742
env:
3843
BUMP_REQUEST_VERSION: ${{ steps.detect_next_version.outputs.version }}

src/changeVersion.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { exec } from "./deps.ts";
2+
import { fs } from "./deps.ts";
3+
4+
const version = Deno.env.get("BUMP_REQUEST_VERSION")!;
5+
6+
if (checkCratePackage()) {
7+
changeCrateVersion(version);
8+
} else if (checkGemPackage()) {
9+
changeGemVersion(version);
10+
} else if (checkNpmPackage()) {
11+
changeNpmVersion(version);
12+
} else if (checkPlainPackage()) {
13+
changePlainVersion(version);
14+
} else {
15+
throw new Error("Unsupported package type.");
16+
}
17+
18+
function checkCratePackage() {
19+
return fs.existsSync("Cargo.toml");
20+
}
21+
22+
function checkGemPackage() {
23+
return fs.expandGlobSync("lib/**/version.rb").next().value !== undefined;
24+
}
25+
26+
function checkNpmPackage() {
27+
return fs.existsSync("package.json");
28+
}
29+
30+
function checkPlainPackage() {
31+
return fs.existsSync("VERSION");
32+
}
33+
34+
function changeCrateVersion(version: string) {
35+
replaceContent(
36+
"Cargo.toml",
37+
/version = "[^"]+"/,
38+
`version = "${version}"`,
39+
);
40+
}
41+
42+
function changeGemVersion(version: string) {
43+
replaceContent(
44+
fs.expandGlobSync("lib/**/version.rb").next().value.path,
45+
/VERSION = "[^"]+"/,
46+
`VERSION = "${version}"`,
47+
);
48+
exec.exec("bundle", ["install"]);
49+
}
50+
51+
function changeNpmVersion(version: string) {
52+
exec.exec("npm", [
53+
"version",
54+
"--no-git-commit-hooks",
55+
"--no-git-tag-version",
56+
version,
57+
]);
58+
}
59+
60+
function changePlainVersion(version: string) {
61+
replaceContent(
62+
"VERSION",
63+
/.+/,
64+
version,
65+
);
66+
}
67+
68+
function replaceContent(
69+
filePath: string,
70+
pattern: RegExp,
71+
replacement: string,
72+
) {
73+
const content = Deno.readTextFileSync(filePath);
74+
const updatedContent = content.replace(pattern, replacement);
75+
Deno.writeTextFileSync(filePath, updatedContent);
76+
}

src/deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * as exec from "https://esm.sh/@actions/exec@1.1.1?dts";
33
export * as github from "https://esm.sh/@actions/github@6.0.0?dts";
44
export * as semver from "https://esm.sh/semver@7.6.2?dts";
55
export * as asserts from "https://deno.land/std@0.65.0/testing/asserts.ts";
6+
export * as fs from "https://deno.land/std@0.224.0/fs/mod.ts";

0 commit comments

Comments
 (0)