Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Taylor committed Apr 14, 2023
0 parents commit 4b2706b
Show file tree
Hide file tree
Showing 15 changed files with 1,146 additions and 0 deletions.
99 changes: 99 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Validation Tests

on:
push:
branches:
- 2.x
pull_request:
branches:
- 2.x

jobs:
run:
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 2
matrix:
composer:
- 2
os:
- windows-latest
- ubuntu-latest
php:
- 7.4
- 8.0
- 8.1
- 8.2

name: PHP ${{ matrix.php }}; Composer ${{ matrix.composer }}; ${{ matrix.os }}
steps:
- name: Setup PHP
uses: shivammathur/setup-php@master
with:
php-version: ${{ matrix.php }}
extensions: curl, json, mbstring, pcre
ini-values: memory_limit=512M
tools: composer:v${{ matrix.composer }}

- name: Check PHP Version
run: php -v

- name: Checkout
uses: actions/checkout@master
with:
path: test

- name: Test simply package install (as not required; standalone)
run: composer install --no-progress --no-dev --prefer-dist --optimize-autoloader
working-directory: test

- name: Creating test_app path for dummy project...
run: mkdir test_app

- name: Creating test_app/composer.json...
uses: DamianReeves/[email protected]
with:
path: test_app/composer.json
write-mode: overwrite
contents: |
{
"name": "dummy/dummy",
"description": "Dummy project to test if configuration works",
"require": {
"pantheon-se/go-composer": "@dev"
},
"repositories": [
{
"type": "path",
"url": "../test/"
}
],
"extra": {
"pantheon-se": {
"go-composer": {
}
}
},
"config": {
"optimize-autoloader": true,
"sort-packages": true,
"bin-dir": "bin",
"allow-plugins": {
"pantheon-se/go-composer": true
}
}
}
- name: Trying to install test_app...
run: composer install --no-progress --no-dev --optimize-autoloader -vvv
working-directory: test_app

- name: Test if installed go is working... (Windows)
run: go version
working-directory: .\test_app\bin
if: matrix.os == 'windows-latest'

- name: Test if installed go is working... (Not Windows)
run: go version
working-directory: ./test_app/bin
if: matrix.os != 'windows-latest'
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
composer.phar
/vendor/
composer.lock
.idea
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Marius Büscher

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[![License](https://img.shields.io/packagist/l/pantheon-se/go-composer)](LICENSE) [![Packagist Version](https://img.shields.io/packagist/v/pantheon-se/go-composer)](https://packagist.org/packages/pantheon-se/go-composer) [![Tests](https://github.com/pantheon-se/go-composer/actions/workflows/test.yml/badge.svg?branch=2.x)](https://github.com/Pantheon-SE/go-composer/actions)

# Go Composer

> Composer Plugin to install other utilities via Composer with Go.
Based on [node-composer by mariusbuescher](https://github.com/mariusbuescher/node-composer), this Composer plugin will install a version of Go into your vendor/bin directory so that they are available to use during your Composer builds. This plugin helps automate the download of the binaries which are linked to the bin-directory specified in your composer.json.

Once installed, you can then use Go in your composer-scripts.

## Setup

Simply install the plugin, and the latest Go will be installed - **no other configurations are necessary**. Optionally, you can specify the `go-version` in your composer.json extra configs to declare a specific version of Go.

**Example composer.json**

```json
{
"name": "my/project",
"type": "project",
"license": "MIT",
"require": {
"pantheon-se/go-composer": "*"
},
"extra": {
"pantheon-se": {
"go-composer": {
"go-version": true
}
}
},
"config": {
"allow-plugins": {
"pantheon-se/go-composer": true
}
}
}
```

## Configuration

There are three parameters you can configure:
- Go version (`go-version`)
- The download url template for the Go binary archives (`go-download-url`).

In the Node download url, replace the following placeholders with your specific needs:

- version: `${version}`
- type of your os: `${osType}`
- system architecture: `${architecture}`
- file format `${format}`

**Example composer.json with specific versions of Go**

```json
{
"extra": {
"pantheon-se": {
"go-composer": {
"go-version": "1.20.2",
"go-download-url": "https://go.dev/dl/go${version}.${osType}-${architecture}.${format}"
}
}
}
}
```
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "pantheon-se/go-composer",
"type": "composer-plugin",
"description": "Installs Go into vendor/bin",
"license": "MIT",
"keywords": [
"go",
"php"
],
"homepage": "https://github.com/pantheon-se/go-composer",
"authors": [
{
"name": "Marius Büscher",
"email": "[email protected]"
},
{
"name": "Kyle Taylor",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"PantheonSalesEngineering\\GoComposer\\": "src/"
}
},
"require": {
"php": ">=7.1",
"composer/composer": ">=2.0",
"composer-plugin-api": ">=2.0",
"symfony/process": ">=3.3",
"ext-zip": "*",
"ext-json": "*"
},
"extra": {
"class": "PantheonSalesEngineering\\GoComposer\\GoComposerPlugin"
}
}
25 changes: 25 additions & 0 deletions src/ArchitectureMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PantheonSalesEngineering\GoComposer;

class ArchitectureMap
{
/**
* @var array
*/
private static $map = array(
'x86_64' => 'x64',
'amd64' => 'x64',
'i586' => 'x86',
);

/**
* @param string $phpArchitecture
* @return string
*/
public static function getGoArchitecture(string $phpArchitecture): string
{
$lowercaseArchitecture = strtolower($phpArchitecture);
return static::$map[$lowercaseArchitecture] ?? $phpArchitecture;
}
}
120 changes: 120 additions & 0 deletions src/BinLinker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace PantheonSalesEngineering\GoComposer;

use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;

/**
* Class to symlink bin files
*
* @package PantheonSalesEngineering\GoComposer
*/
class BinLinker
{
/**
* Composer vendor bin dir
*
* @var string
*/
protected $vendorBinDir;

/**
* Filesystem instance for operations with files
*
* @var SymfonyFilesystem
*/
protected $filesystem;

/**
* Os type
*
* @var string
*/
private $osType;

/**
* Filesystem constructor.
*
* @param string $vendorBinDir Where all bin files should be kept?
* @param string $osType OS type
*/
public function __construct(string $vendorBinDir, string $osType)
{
$this->vendorBinDir = $vendorBinDir;
$this->osType = strtolower($osType);
$this->filesystem = new SymfonyFilesystem();
}

/**
* Links bin item
*
* @param string $from Path from where to create symlink
* @param string $to Path to where create symlink
*/
public function linkBin(string $from, string $to)
{
if ($this->osType === 'win') {
$this->filesystem->dumpFile(
$to . '.bat',
$this->generateBatchCode($from)
);
} else {
// Symlink into bin
$this->filesystem->symlink($from, $to);
}
}

/**
* Generates batch code
*
* @param string $from Path from where to create symlink
*
* @return string
*/
protected function generateBatchCode(string $from): string
{
$binPath = dirname(
$this->filesystem->makePathRelative($from, $this->vendorBinDir)
);
$caller = basename($from);

$binPath = str_replace('/', '\\', $binPath);

return "@ECHO OFF". PHP_EOL .
"setlocal DISABLEDELAYEDEXPANSION". PHP_EOL .
"set EXE_PATH=%~dp0\\".trim(ProcessExecutor::escape($binPath), '"\'').PHP_EOL.
"set GO_PATH=%EXE_PATH%\\vendor".PHP_EOL.
"set Path=%EXE_PATH%;%Path%;".PHP_EOL.
"{$caller} %*".PHP_EOL;
}

/**
* Unlinks bin item
*
* @param string $to Path to unlink
*
* @return bool
*/
public function unlinkBin(string $to): bool
{
// Windows
if ($this->osType === 'win') {
if ($this->filesystem->exists($to . '.bat')) {
$this->filesystem->remove($to . '.bat');
return true;
}
return false;
}

// Non-windows
if ($this->filesystem->exists($to)) {
$this->filesystem->remove($to);
return true;
}
return false;
}

}
Loading

0 comments on commit 4b2706b

Please sign in to comment.