Skip to content

Commit 2da6381

Browse files
✨ Add new composer-filename option (#261)
This adds a new `composer-filename` option to the action, allowing for adding a custom composer filename, which can be helpful for using composer based on different environments. Closes #258 Co-authored-by: Ben Ramsey <ben@ramsey.dev>
1 parent 6a168db commit 2da6381

17 files changed

+346
-10
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ For example:
8585
composer-options: "--ignore-platform-reqs --optimize-autoloader"
8686
```
8787

88+
#### composer-filename
89+
90+
If you have a custom Composer filename, you may use `composer-filename` to change
91+
the filename Composer uses. For example, your Composer file could be
92+
`composer-gh-actions.json` or `composer-staging.json` instead of the default
93+
`composer.json`.
94+
95+
You should specify the filename without the extension, since it will determine
96+
both the JSON and lock filenames to use. The default value is `"composer"`,
97+
which will use `composer.json` and `composer.lock` as the filenames.
98+
99+
For example:
100+
101+
```yaml
102+
- uses: "ramsey/composer-install@v3"
103+
with:
104+
custom-composer-filename: "composer-gh-actions"
105+
```
106+
88107
#### working-directory
89108

90109
The `working-directory` input parameter allows you to specify a different
@@ -180,7 +199,7 @@ even more specific, you can specify a suffix to be added to the cache key via th
180199

181200
#### require-lock-file
182201

183-
By default, if no composer.lock file is found in the working directory
202+
By default, if no composer.lock file is found in the working directory
184203
ramsey/composer-install will invoke `composer update` regardless of the value of
185204
`dependency-versions`.
186205

action.yml

+13-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ inputs:
3939
Require lock file for install command.
4040
required: false
4141
default: "false"
42+
composer-filename:
43+
description: >-
44+
The Composer filename to use, without the extension (e.g. `composer`,
45+
`composer-gh-actions`, `composer-staging`). Please note: this is not a
46+
path. If you need to specify a different path, use `working-directory`.
47+
required: false
48+
default: "composer"
4249

4350
runs:
4451
using: "composite"
@@ -51,7 +58,7 @@ runs:
5158
- name: "Determine whether we should ignore caching"
5259
id: "should-cache"
5360
shell: "bash"
54-
run: "${GITHUB_ACTION_PATH}/bin/should_cache.sh \"${{ inputs.ignore-cache }}\""
61+
run: '${GITHUB_ACTION_PATH}/bin/should_cache.sh "${{ inputs.ignore-cache }}"'
5562

5663
- name: "Determine Composer paths"
5764
id: "composer"
@@ -60,7 +67,8 @@ runs:
6067
${GITHUB_ACTION_PATH}/bin/composer_paths.sh \
6168
"" \
6269
"${{ inputs.working-directory }}" \
63-
"${{ steps.php.outputs.path }}"
70+
"${{ steps.php.outputs.path }}" \
71+
"${{ inputs.composer-filename }}"
6472
6573
- name: "Determine cache key"
6674
id: "cache-key"
@@ -72,7 +80,7 @@ runs:
7280
"${{ steps.php.outputs.version }}" \
7381
"${{ inputs.dependency-versions }}" \
7482
"${{ inputs.composer-options }}" \
75-
"${{ hashFiles('**/composer.json', '**/composer.lock') }}" \
83+
"${{ hashFiles('**/${{ inputs.composer-filename }}.json', '**/${{ inputs.composer-filename }}.lock') }}" \
7684
"${{ inputs.custom-cache-key }}" \
7785
"${{ inputs.custom-cache-suffix }}" \
7886
"${{ inputs.working-directory }}"
@@ -96,4 +104,5 @@ runs:
96104
"${{ steps.php.outputs.path }}" \
97105
"${{ steps.composer.outputs.composer_command }}" \
98106
"${{ steps.composer.outputs.lock }}" \
99-
"${{ inputs.require-lock-file }}"
107+
"${{ inputs.require-lock-file }}" \
108+
"${{ inputs.composer-filename }}" \

bin/composer_install.sh

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ php_path="${4:-$(which php)}"
77
composer_path="${5:-$(which composer)}"
88
composer_lock="${6:-}"
99
require_lock_file="${7:-}"
10+
composer_filename="${8:-composer}"
1011

1112
composer_command="update"
1213
composer_options=("--no-interaction" "--no-progress" "--ansi")
@@ -33,6 +34,10 @@ if [ -n "${working_directory}" ]; then
3334
composer_options+=("--working-dir" "${working_directory}")
3435
fi
3536

37+
COMPOSER="${composer_filename}.json"
38+
export COMPOSER
39+
3640
full_command="${php_path} ${composer_path} ${composer_command} ${composer_options[*]}"
3741
echo "::debug::Using the following Composer command: '${full_command}'"
42+
echo "::debug::The COMPOSER environment variable is '${COMPOSER}'"
3843
$full_command

bin/composer_paths.sh

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,35 @@
33
composer_path="${1:-$(which composer)}"
44
working_directory="${2:-.}"
55
php_path="${3:-$(which php)}"
6+
composer_filename="${4:-composer}"
67

78
function test_composer {
89
"${php_path}" "${composer_path}" --version > /dev/null 2>&1
910
}
1011

1112
function validate_composer {
12-
"${php_path}" "${composer_path}" validate --no-check-publish --no-check-lock --working-dir "${working_directory}" > /dev/null 2>&1
13+
"${php_path}" "${composer_path}" validate --no-check-publish --no-check-lock --working-dir "${working_directory}" > /dev/null 2>&1
1314
}
1415

1516
if ! test_composer; then
1617
echo "::error title=Composer Not Found::Unable to find Composer at '${composer_path}'"
1718
exit 1
1819
fi
1920

20-
composer_json="composer.json"
21-
composer_lock="composer.lock"
21+
composer_json="${composer_filename}.json"
22+
composer_lock="${composer_filename}.lock"
23+
24+
COMPOSER="${composer_json}"
25+
export COMPOSER
2226

2327
if [ -n "${working_directory}" ]; then
2428
if [ ! -d "${working_directory}" ]; then
2529
echo "::error title=Working Directory Not Found::Unable to find working directory at '${working_directory}'"
2630
exit 1
2731
fi
2832

29-
composer_json="${working_directory}/composer.json"
30-
composer_lock="${working_directory}/composer.lock"
33+
composer_json="${working_directory}/${composer_json}"
34+
composer_lock="${working_directory}/${composer_lock}"
3135
fi
3236

3337
if [ ! -f "${composer_json}" ]; then
@@ -53,6 +57,7 @@ echo "::debug::${composer_version}"
5357
echo "::debug::Composer cache directory found at '${cache_dir}'"
5458
echo "::debug::File composer.json found at '${composer_json}'"
5559
echo "::debug::File composer.lock path computed as '${composer_lock}'"
60+
echo "::debug::The COMPOSER environment variable is '${COMPOSER}'"
5661
{
5762
echo "composer_command=${composer_path}"
5863
echo "cache-dir=${cache_dir}"

tests/composer_install.bats

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ teardown() {
2828
''
2929

3030
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
31+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
3132
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
3233
assert_line --partial 'Verifying lock file contents can be installed on current platform'
3334
assert_line --partial 'Generating autoload files'
@@ -48,6 +49,7 @@ teardown() {
4849
''
4950

5051
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable'$"
52+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
5153
assert_line --partial 'Updating dependencies'
5254
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
5355
assert_line --partial 'Generating autoload files'
@@ -68,6 +70,7 @@ teardown() {
6870
''
6971

7072
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi'$"
73+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
7174
assert_line --partial 'Updating dependencies'
7275
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
7376
assert_line --partial 'Generating autoload files'
@@ -88,6 +91,7 @@ teardown() {
8891
''
8992

9093
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
94+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
9195
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
9296
assert_line --partial 'Verifying lock file contents can be installed on current platform'
9397
assert_line --partial 'Generating autoload files'
@@ -108,6 +112,7 @@ teardown() {
108112
''
109113

110114
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
115+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
111116
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
112117
assert_line --partial 'Verifying lock file contents can be installed on current platform'
113118
assert_line --partial 'Generating autoload files'
@@ -128,6 +133,7 @@ teardown() {
128133
''
129134

130135
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader'$"
136+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
131137
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
132138
assert_line --partial 'Verifying lock file contents can be installed on current platform'
133139
assert_line --partial 'Generating optimized autoload files'
@@ -148,6 +154,7 @@ teardown() {
148154
''
149155

150156
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable --ignore-platform-reqs --optimize-autoloader'$"
157+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
151158
assert_line --partial 'Updating dependencies'
152159
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
153160
assert_line --partial 'Generating optimized autoload files'
@@ -168,6 +175,7 @@ teardown() {
168175
''
169176

170177
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader'$"
178+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
171179
assert_line --partial 'Updating dependencies'
172180
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
173181
assert_line --partial 'Generating optimized autoload files'
@@ -188,6 +196,7 @@ teardown() {
188196
''
189197

190198
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader'$"
199+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
191200
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
192201
assert_line --partial 'Verifying lock file contents can be installed on current platform'
193202
assert_line --partial 'Generating optimized autoload files'
@@ -208,6 +217,7 @@ teardown() {
208217
''
209218

210219
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer\.phar install --no-interaction --no-progress --ansi'$"
220+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
211221
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
212222
assert_line --partial 'Verifying lock file contents can be installed on current platform'
213223
assert_line --partial 'Generating autoload files'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bats
2+
3+
setup() {
4+
load 'test_helper/common_setup'
5+
_common_setup
6+
7+
cd "$PROJECT_ROOT/tests/fixtures/custom-composer" || exit 1
8+
}
9+
10+
teardown() {
11+
if [ -d vendor ]; then
12+
rm -rf vendor
13+
fi
14+
15+
git restore composer-gh-actions.lock
16+
17+
cd ../.. || exit 1
18+
}
19+
20+
@test 'installs dependencies using custom composer file and lock file' {
21+
run -0 composer_install.sh \
22+
'' \
23+
'' \
24+
'' \
25+
'' \
26+
'' \
27+
'composer-gh-actions.lock' \
28+
'' \
29+
'composer-gh-actions'
30+
31+
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
32+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
33+
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
34+
assert_line --partial 'Verifying lock file contents can be installed on current platform'
35+
assert_line --partial 'Generating autoload files'
36+
37+
refute_line --partial 'Updating dependencies'
38+
39+
assert_dir_exists "$PROJECT_ROOT/tests/fixtures/custom-composer/vendor"
40+
}
41+
42+
@test 'installs dependencies using custom composer file without lock file' {
43+
run -0 composer_install.sh \
44+
'' \
45+
'' \
46+
'' \
47+
'' \
48+
'' \
49+
'' \
50+
'' \
51+
'composer-gh-actions'
52+
53+
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi'$"
54+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
55+
assert_line --partial 'Updating dependencies'
56+
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
57+
assert_line --partial 'Generating autoload files'
58+
59+
refute_line --partial 'Verifying lock file contents can be installed on current platform'
60+
61+
assert_dir_exists "$PROJECT_ROOT/tests/fixtures/custom-composer/vendor"
62+
}

tests/composer_install_with_different_working_directory.bats

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ teardown() {
2424
''
2525

2626
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --working-dir .*/fixtures/with-lock-file'$"
27+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
2728
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
2829
assert_line --partial 'Verifying lock file contents can be installed on current platform'
2930
assert_line --partial 'Generating autoload files'
@@ -44,6 +45,7 @@ teardown() {
4445
''
4546

4647
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable --working-dir .*/fixtures/with-lock-file'$"
48+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
4749
assert_line --partial 'Updating dependencies'
4850
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
4951
assert_line --partial 'Generating autoload files'
@@ -64,6 +66,7 @@ teardown() {
6466
''
6567

6668
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --working-dir .*/fixtures/with-lock-file'$"
69+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
6770
assert_line --partial 'Updating dependencies'
6871
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
6972
assert_line --partial 'Generating autoload files'
@@ -84,6 +87,7 @@ teardown() {
8487
''
8588

8689
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --working-dir .*/fixtures/with-lock-file'$"
90+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
8791
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
8892
assert_line --partial 'Verifying lock file contents can be installed on current platform'
8993
assert_line --partial 'Generating autoload files'
@@ -104,6 +108,7 @@ teardown() {
104108
''
105109

106110
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
111+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
107112
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
108113
assert_line --partial 'Verifying lock file contents can be installed on current platform'
109114
assert_line --partial 'Generating optimized autoload files'
@@ -124,6 +129,7 @@ teardown() {
124129
''
125130

126131
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
132+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
127133
assert_line --partial 'Updating dependencies'
128134
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
129135
assert_line --partial 'Generating optimized autoload files'
@@ -144,6 +150,7 @@ teardown() {
144150
''
145151

146152
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
153+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
147154
assert_line --partial 'Updating dependencies'
148155
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
149156
assert_line --partial 'Generating optimized autoload files'
@@ -164,6 +171,7 @@ teardown() {
164171
''
165172

166173
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
174+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
167175
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
168176
assert_line --partial 'Verifying lock file contents can be installed on current platform'
169177
assert_line --partial 'Generating optimized autoload files'

tests/composer_install_without_lock_file.bats

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ teardown() {
2626
''
2727

2828
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --working-dir .*/fixtures/no-lock-file'$"
29+
assert_line "::debug::The COMPOSER environment variable is 'composer.json'"
2930
assert_line --partial 'Updating dependencies'
3031
assert_line --partial 'Writing lock file'
3132
assert_line --partial 'Installing dependencies from lock file (including require-dev)'

0 commit comments

Comments
 (0)