Skip to content
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

✨ Add new custom-composer-filename option #261

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ For example:
composer-options: "--ignore-platform-reqs --optimize-autoloader"
```

#### custom-composer-filename

If you have a custom `composer` filename, you may use the `custom-composer-filename`. For example, your `composer` file could be `composer-gh-actions.json` or `composer-staging.json` instead of the default `composer.json`.
You should specify the filename without the extension.

For example:

```yaml
- uses: "ramsey/composer-install@v3"
with:
custom-composer-filename: "composer-gh-actions"
```

#### working-directory

The `working-directory` input parameter allows you to specify a different
Expand Down
14 changes: 10 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ inputs:
Require lock file for install command.
required: false
default: "false"
custom-composer-filename:
description: >-
The custom Composer filename to use (e.g. `composer-gh-actions`, `composer-staging`).
required: false

runs:
using: "composite"
Expand All @@ -51,7 +55,7 @@ runs:
- name: "Determine whether we should ignore caching"
id: "should-cache"
shell: "bash"
run: "${GITHUB_ACTION_PATH}/bin/should_cache.sh \"${{ inputs.ignore-cache }}\""
run: '${GITHUB_ACTION_PATH}/bin/should_cache.sh "${{ inputs.ignore-cache }}"'

- name: "Determine Composer paths"
id: "composer"
Expand All @@ -61,7 +65,8 @@ runs:
${GITHUB_ACTION_PATH}/bin/composer_paths.sh \
"" \
"${{ inputs.working-directory }}" \
"${{ steps.php.outputs.path }}"
"${{ steps.php.outputs.path }}" \
"${{ inputs.custom-composer-filename }}"

- name: "Determine cache key"
id: "cache-key"
Expand All @@ -73,7 +78,7 @@ runs:
"${{ steps.php.outputs.version }}" \
"${{ inputs.dependency-versions }}" \
"${{ inputs.composer-options }}" \
"${{ hashFiles('**/composer.json', '**/composer.lock') }}" \
"${{ hashFiles('**/${{ inputs.custom-composer-filename }}.json', '**/${{ inputs.custom-composer-filename }}.lock') }}" \
"${{ inputs.custom-cache-key }}" \
"${{ inputs.custom-cache-suffix }}" \
"${{ inputs.working-directory }}"
Expand All @@ -97,4 +102,5 @@ runs:
"${{ steps.php.outputs.path }}" \
"${{ steps.composer.outputs.composer_command }}" \
"${{ steps.composer.outputs.lock }}" \
"${{ inputs.require-lock-file }}"
"${{ inputs.require-lock-file }}" \
"${{ inputs.custom-composer-filename }}" \
5 changes: 5 additions & 0 deletions bin/composer_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ php_path="${4:-$(which php)}"
composer_path="${5:-$(which composer)}"
composer_lock="${6}"
require_lock_file="${7}"
custom_composer_filename="${8:-}"

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

if [ -n "${custom_composer_filename}" ]; then
export COMPOSER="${custom_composer_filename}.json"
fi

full_command="${php_path} ${composer_path} ${composer_command} ${composer_options[*]}"
echo "::debug::Using the following Composer command: '${full_command}'"
$full_command
16 changes: 13 additions & 3 deletions bin/composer_paths.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
composer_path="${1:-$(which composer)}"
working_directory="${2:-.}"
php_path="${3:-$(which php)}"
custom_composer_filename="${4:-}"

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

function validate_composer {
"${php_path}" "${composer_path}" validate --no-check-publish --no-check-lock --working-dir "${working_directory}" > /dev/null 2>&1
if [ -n "${custom_composer_filename}" ]; then
export COMPOSER="${custom_composer_filename}.json"
fi

"${php_path}" "${composer_path}" validate --no-check-publish --no-check-lock --working-dir "${working_directory}" > /dev/null 2>&1
}

if ! test_composer; then
Expand All @@ -20,14 +25,19 @@ fi
composer_json="composer.json"
composer_lock="composer.lock"

if [ -n "${custom_composer_filename}" ]; then
composer_json="${custom_composer_filename}.json"
composer_lock="${custom_composer_filename}.lock"
fi

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

composer_json="${working_directory}/composer.json"
composer_lock="${working_directory}/composer.lock"
composer_json="${working_directory}/${composer_json}"
composer_lock="${working_directory}/${composer_lock}"
fi

if [ ! -f "${composer_json}" ]; then
Expand Down
13 changes: 13 additions & 0 deletions tests/expect/composer_install_20.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env -S expect -f

set timeout 3
spawn ../../bin/composer_install.sh "" "" "../fixtures/custom-composer" "" "" "composer-gh-actions.lock" "" "composer-gh-actions"
match_max 100000

expect "::debug::Using the following Composer command: '*/php */composer install --no-interaction --no-progress --ansi --working-dir ../fixtures/custom-composer'"
expect "Installing dependencies"
expect "Generating autoload files"
expect eof

# Clean up
file delete -force ../fixtures/custom-composer/vendor
13 changes: 13 additions & 0 deletions tests/expect/composer_install_21.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env -S expect -f

set timeout 3
spawn ../../bin/composer_install.sh "" "" "../fixtures/custom-composer" "" "" "" "" "composer-gh-actions"
match_max 100000

expect "::debug::Using the following Composer command: '*/php */composer update --no-interaction --no-progress --ansi --working-dir ../fixtures/custom-composer'"
expect "Installing dependencies"
expect "Generating autoload files"
expect eof

# Clean up
file delete -force ../fixtures/custom-composer/vendor
7 changes: 3 additions & 4 deletions tests/expect/composer_paths_05.exp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ set timeout 3
spawn ../../bin/composer_paths.sh "" "../fixtures/no-lock-file"
match_max 100000

expect "::debug::Unable to find composer.lock at '../fixtures/no-lock-file/composer.lock'\r
::debug::Composer path is '*'\r
expect "::debug::Unable to find composer.lock at '../fixtures/no-lock-file/composer.lock'\r"
expect "::debug::Composer path is '*'\r
::debug::Composer version *\r
::debug::Composer cache directory found at '*'\r
::debug::File composer.json found at '../fixtures/no-lock-file/composer.json'\r
::debug::File composer.lock path computed as ''\r
"
::debug::File composer.lock path computed as ''\r"
expect eof

# Verify output variables have been set correctly.
Expand Down
30 changes: 30 additions & 0 deletions tests/expect/composer_paths_11.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env -S expect -f

# For testing outputs variables written to GITHUB_OUTPUT
set gitHubOutputFile composer_paths_11.txt
set ::env(GITHUB_OUTPUT) $gitHubOutputFile

set timeout 3
spawn ../../bin/composer_paths.sh "" "../fixtures/custom-composer" "" "composer-gh-actions"
match_max 100000

expect "::debug::Composer path is '*'\r
::debug::Composer version *\r
::debug::Composer cache directory found at '*'\r
::debug::File composer.json found at '../fixtures/custom-composer/composer-gh-actions.json'\r
::debug::File composer.lock path computed as '../fixtures/custom-composer/composer-gh-actions.lock'\r"
expect eof

# Verify output variables have been set correctly.
set fp [open $gitHubOutputFile r]
set fileData [read $fp]
close $fp

if {[regexp {^composer_command=\S*\s*cache-dir=\S*\s*json=\.\./fixtures/custom-composer/composer-gh-actions\.json\s*lock=\.\./fixtures/custom-composer/composer-gh-actions\.lock\s*$} $fileData] == 0} {
puts "\nExpected output variable does not match. Received:\n"
puts $fileData
exit 1
}

# Clean up
file delete $gitHubOutputFile
8 changes: 8 additions & 0 deletions tests/expect/composer_paths_12.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env -S expect -f

set timeout 3
spawn ../../bin/composer_paths.sh "" "../fixtures/invalid-custom-composer" "" "composer-gh-actions"
match_max 100000

expect "::error title=Invalid composer.json::The composer.json file at '../fixtures/invalid-custom-composer/composer-gh-actions.json' does not validate; run 'composer validate' to check for errors"
expect eof
31 changes: 31 additions & 0 deletions tests/expect/composer_paths_13.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env -S expect -f

# For testing outputs variables written to GITHUB_OUTPUT
set gitHubOutputFile composer_paths_13.txt
set ::env(GITHUB_OUTPUT) $gitHubOutputFile

set timeout 3
spawn ../../bin/composer_paths.sh "" "../fixtures/no-lock-file-custom-composer" "" "composer-gh-actions"
match_max 100000

expect "::debug::Unable to find composer.lock at '../fixtures/no-lock-file-custom-composer/composer-gh-actions.lock'\r"
expect "::debug::Composer path is '*'\r
::debug::Composer version *\r
::debug::Composer cache directory found at '*'\r
::debug::File composer.json found at '../fixtures/no-lock-file-custom-composer/composer-gh-actions.json'\r
::debug::File composer.lock path computed as ''\r"
expect eof

# Verify output variables have been set correctly.
set fp [open $gitHubOutputFile r]
set fileData [read $fp]
close $fp

if {[regexp {^composer_command=\S*\s*cache-dir=\S*\s*json=\.\./fixtures/no-lock-file-custom-composer/composer-gh-actions\.json\s*lock=\s*$} $fileData] == 0} {
puts "\nExpected output variable does not match. Received:\n"
puts $fileData
exit 1
}

# Clean up
file delete $gitHubOutputFile
31 changes: 31 additions & 0 deletions tests/expect/composer_paths_14.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env -S expect -f

# For testing outputs variables written to GITHUB_OUTPUT
set gitHubOutputFile composer_paths_14.txt
set ::env(GITHUB_OUTPUT) $gitHubOutputFile

set timeout 3
spawn ../../bin/composer_paths.sh "" "../fixtures/out-of-sync-lock-custom-composer" "" "composer-gh-actions"
match_max 100000

expect "::debug::Composer path is '*'\r
::debug::Composer version *\r
::debug::Composer cache directory found at '*'\r
::debug::File composer.json found at '../fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.json'\r
::debug::File composer.lock path computed as '../fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.lock'\r
"
expect eof

# Verify output variables have been set correctly.
set fp [open $gitHubOutputFile r]
set fileData [read $fp]
close $fp

if {[regexp {^composer_command=\S*\s*cache-dir=\S*\s*json=\.\./fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.json\s*lock=\.\./fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.lock\s*$} $fileData] == 0} {
puts "\nExpected output variable does not match. Received:\n"
puts $fileData
exit 1
}

# Clean up
file delete $gitHubOutputFile
18 changes: 18 additions & 0 deletions tests/fixtures/custom-composer/composer-gh-actions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ramsey/composer-install-test-with-lock-file",
"description": "Tests composer-install when custom-composer.lock file exists.",
"license": "MIT",
"type": "project",
"authors": [
{
"name": "Ben Ramsey",
"email": "ben@benramsey.com"
}
],
"require": {
"ehime/hello-world": "^1.0.0"
},
"config": {
"allow-plugins": false
}
}
68 changes: 68 additions & 0 deletions tests/fixtures/custom-composer/composer-gh-actions.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "091919c4ac368d87445398fc8c8c2696",
"packages": [
{
"name": "ehime/hello-world",
"version": "1.0.5",
"source": {
"type": "git",
"url": "https://github.com/ehime/hello-world.git",
"reference": "b1c8cdd2c11272d8c5deec7816e51fa5374217c1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ehime/hello-world/zipball/b1c8cdd2c11272d8c5deec7816e51fa5374217c1",
"reference": "b1c8cdd2c11272d8c5deec7816e51fa5374217c1",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"mockery/mockery": "0.8.*",
"phpunit/phpunit": "4.3.5"
},
"type": "library",
"autoload": {
"psr-0": {
"HelloWorld": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jd Daniel",
"email": "dodomeki@gmail.com"
}
],
"description": "Sample Composer project",
"keywords": [
"helloworld",
"sample",
"test"
],
"support": {
"issues": "https://github.com/ehime/hello-world/issues",
"source": "https://github.com/ehime/hello-world/tree/1.0.5"
},
"time": "2015-07-31T17:53:36+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}
1 change: 1 addition & 0 deletions tests/fixtures/invalid-custom-composer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
composer-gh-actions.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "invalid-custom-composer.json",
"description": "Tests composer-install when custom-composer.json is invalid"
}
1 change: 1 addition & 0 deletions tests/fixtures/no-lock-file-custom-composer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
composer-gh-actions.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ramsey/composer-install-test-no-lock-file",
"description": "Tests composer-install when no custom-composer.lock file exists.",
"license": "MIT",
"type": "project",
"authors": [
{
"name": "Ben Ramsey",
"email": "ben@benramsey.com"
}
],
"require": {
"ehime/hello-world": "^1.0.0"
},
"config": {
"allow-plugins": false
}
}
Loading