Skip to content

Commit

Permalink
Merge branch 'DIRECTORY_SEPARATOR-and-its-duplicates' into Don't-infe…
Browse files Browse the repository at this point in the history
…r-vendor-directory-from-package-name.-Fix-for-#97
  • Loading branch information
BrianHenryIE committed Feb 7, 2021
2 parents 2902bd4 + 17a4502 commit 6ec5d45
Show file tree
Hide file tree
Showing 28 changed files with 895 additions and 82 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Psalm analysis

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
psalm:
name: Psalm
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Psalm
uses: docker://vimeo/psalm-github-actions
52 changes: 52 additions & 0 deletions .github/workflows/docker-latest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Publish latest to Docker Hub

on:
push:
branches:
- master

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Prepare
id: prep
run: |
DOCKER_IMAGE=coenjacobs/mozart
VERSION=latest
TAGS="${DOCKER_IMAGE}:${VERSION}"
echo ::set-output name=tags::${TAGS}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
target: application
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
10 changes: 8 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name: Lint and test

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4']
php: ['7.3', '7.4', '8.0']
name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v1
Expand Down
94 changes: 94 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Build, tag and attach releases

on:
release:
types: [published]

jobs:
create-phar:
runs-on: ubuntu-latest
name: Create Mozart phar
steps:
- uses: actions/checkout@v1

- name: Install PHP
uses: shivammathur/[email protected]
with:
php-version: 7.3

- name: Install dependencies
run: composer install --no-dev --prefer-dist --no-suggest --no-progress

- name: Create .phar
run: |
wget -O phar-composer.phar https://github.com/clue/phar-composer/releases/download/v1.2.0/phar-composer-1.2.0.phar
mkdir build
mv vendor build/vendor
mv src build/src
mv bin build/bin
mv composer.json build
php -d phar.readonly=off phar-composer.phar build ./build/
- name: Test run mozart
run: php mozart.phar --version

- uses: meeDamian/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
files: mozart.phar
gzip: false
allow_override: true
docker:
runs-on: ubuntu-latest
name: Create Docker tag
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Prepare
id: prep
run: |
DOCKER_IMAGE=coenjacobs/mozart
VERSION=${{ github.event.release.tag_name }}
TAGS="${DOCKER_IMAGE}:${VERSION}"
echo ::set-output name=tags::${TAGS}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: all

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
target: application
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM composer:2.0.8

FROM php:8.0.1-cli-alpine AS base

FROM base as builder
RUN apk update && apk add git
COPY --from=composer /usr/bin/composer /usr/bin/composer
COPY ./composer.json /mozart/
WORKDIR /mozart/
RUN composer install --no-dev -o

FROM base AS application
RUN mkdir project
WORKDIR /project/
COPY --from=builder /mozart/ /mozart/
COPY ./bin/ /mozart/bin/
COPY ./src/ /mozart/src/
79 changes: 74 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
# Mozart [![Build Status](https://api.travis-ci.org/coenjacobs/mozart.png)](https://travis-ci.org/coenjacobs/mozart) [![Latest Stable Version](https://poser.pugx.org/coenjacobs/mozart/v/stable.svg)](https://packagist.org/packages/coenjacobs/mozart) [![License](https://poser.pugx.org/coenjacobs/mozart/license.svg)](https://packagist.org/packages/coenjacobs/mozart)
# Mozart [![Latest Stable Version](https://poser.pugx.org/coenjacobs/mozart/v/stable.svg)](https://packagist.org/packages/coenjacobs/mozart) [![License](https://poser.pugx.org/coenjacobs/mozart/license.svg)](https://packagist.org/packages/coenjacobs/mozart) [![Total Downloads](https://poser.pugx.org/coenjacobs/mozart/downloads)](//packagist.org/packages/coenjacobs/mozart) [![Docker Image Pulls](https://img.shields.io/docker/pulls/coenjacobs/mozart.svg)](https://hub.docker.com/r/coenjacobs/mozart)
Composes all dependencies as a package inside a WordPress plugin. Load packages through Composer and have them wrapped inside your own namespace. Gone are the days when plugins could load conflicting versions of the same package, resulting in hard to reproduce bugs.

This package requires PHP 7.2 or higher in order to run the tool. You can use the resulting files as a bundle, requiring any PHP version you like, even PHP 5.2.
This package requires PHP 7.3 or higher in order to run the tool. You can use the resulting files as a bundle, requiring any PHP version you like, even PHP 5.2.

**Warning:** This package is very experimental and breaking changes are very likely until version 1.0.0 is tagged. Use with caution, always wear a helmet when using this in production environments.

## Installation
Install through Composer, only required in development environments:
Mozart brings its own dependencies to the table and that potentially introduces its own problems (yes, I realise how meta that is, for a package like this). That's why installing Mozart in isolation, either through the Docker container, the available PHAR file or installing Mozart as a global dependency with Composer is prefered. In all cases, the [configuration](#configuration) still needs to be placed in the `composer.json` file of the project iself.

`composer require coenjacobs/mozart --dev`
### Docker
Pull the Docker image from the registry:

```
docker pull coenjacobs/mozart
```

Then you can start the container and run the `mozart compose` command in the container. In a single command:

```
docker run --rm -it -v ${PWD}:/project/ coenjacobs/mozart /mozart/bin/mozart compose
```

Above command automatically adds the current working directory as a volume into the designated directory for the project: `/project/`. In the Docker container, Mozart is installed in the `/mozart/` directory. Using the above command will run Mozart on the current working directory.

Please note that the Docker image for Mozart is only available starting from the `latest` build of version 0.7.0. The `latest` tag is always the latest build of the `master` branch and not a stable version. You can see [all available tags on Docker Hub](https://hub.docker.com/r/coenjacobs/mozart/tags).

### PHAR (via Phive)
Mozart can be installed via [Phive](https://github.com/phar-io/phive):

```
phive install coenjacobs/mozart --force-accept-unsigned
```

Alternatively, the `mozart.phar` file can be [downloaded from the releases page](https://github.com/coenjacobs/mozart/releases) and then be run from your project directory:

```
php mozart.phar compose
```

### Composer
To install Mozart and its dependencies, without conflicting with the dependencies of your project, it is recommended that you install Mozart as a global package, if you choose to install Mozart via Composer.

#### Global package
Using the `global` command when installing Mozart, it will be installed as a system wide package:

```
composer global require coenjacobs/mozart
```

You can then find the bin file named `mozart` inside your `~/.composer/vendor/bin/` directory and run it from your project directory, referencing the full path to the bin file:

```
~/.composer/vendor/bin/mozart compose
```

#### Development dependency of your project
You can install through Composer in the project itself, only required in development environments:

```
composer require coenjacobs/mozart --dev
```

This gives you a bin file named `mozart` inside your `vendor/bin` directory, after loading the whole package inside your project. Try running `vendor/bin/mozart` to verify it works.

After configuring Mozart properly, the `mozart compose` command does all the magic.
After configuring Mozart properly, the `mozart compose` command does all the magic:

```
vendor/bin/mozart compose
```

## Configuration
Mozart requires little configuration. All you need to do is tell it where the bundled dependencies are going to be stored and what namespace they should be put inside. This configuration needs to be done in the `extra` property of your `composer.json` file:
Expand Down Expand Up @@ -75,3 +130,17 @@ Mozart is designed to install and be forgotten about. Using Composer scripts, th
]
}
```

When using Mozart through its Docker container, you can replace the `"\"vendor/bin/mozart\" compose",` lines with the actual commands you use to [run the Docker container](#docker) for your specific project. Running Mozart from inside the Docker container is really fast and shouldn't take more than a couple seconds.

## Background and philosophy
Mozart is designed to bridge the gap between the WordPress ecosytem and the vast packages ecosystem of PHP as a whole. Since WordPress is such an end-user friendly focussed CMS (for good reasons), there is no place within the ecosystem where an end-user would be burdened with using a developers tool like Composer. Also, since WordPress has to run on basically any hosting infrastructure, running Composer to install packages from the administration panel (trust me, I've tried - it can be done) is a mission impossible to make it happen and compatible with every server out there.

But why make a new tool for this? There are other tools that enable you to do this, right? Yes, there are now. [PHP-Scoper](https://github.com/humbug/php-scoper), for example. PHP-Scoper is a fantastic tool, that does the job right. But, PHP-Scoper wasn't available back when I started the Mozart project. Also, PHP-Scoper has a few limitations (no support for classmap autoloaders, for example) that were and are still quite common within the WordPress ecosystem. Finally, PHP-Scoper can be quite the tool to add to your development flow, while Mozart was designed to be _simple to implement_, specifically tailored for WordPress projects.

The key values of what's important to Mozart:
- Must be able to be easily installable by a developer, preferably in a specific version.
- Distribution must be done through an existing package manager or easily maintained separately.
- Shouldn't add a whole layer of complexity to the development process, i.e. learning a whole new tool/language.

Mozart always has been and always will be geared towards solving the conflicting dependencies problem in the WordPress ecosystem, as efficiently and opinionated as possible. By being opinionated in certain ways and specifically focussed on WordPress projects, Mozart has quickly become easy to understand and implement.
38 changes: 21 additions & 17 deletions bin/mozart
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#!/usr/bin/env php
<?php
call_user_func(function ($version) {
if (is_file($autoload = getcwd() . '/vendor/autoload.php')) {
require $autoload;
} elseif (is_file($autoload = getcwd() . '/../../autoload.php')) {
require $autoload;
}
if( ! Phar::running() ) {
if (is_file($autoload = getcwd() . '/vendor/autoload.php')) {
require $autoload;
} elseif (is_file($autoload = getcwd() . '/../../autoload.php')) {
require $autoload;
}

if (is_file($autoload = __DIR__ . '/../vendor/autoload.php')) {
require($autoload);
} elseif (is_file($autoload = __DIR__ . '/../../../autoload.php')) {
require($autoload);
} else {
fwrite(STDERR,
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
exit(1);
if ( is_file( $autoload = __DIR__ . '/../vendor/autoload.php' ) ) {
require( $autoload );
} elseif ( is_file( $autoload = __DIR__ . '/../../../autoload.php' ) ) {
require( $autoload );
} else {
fwrite( STDERR,
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
exit( 1 );
}
} else {
include 'phar://mozart.phar/vendor/autoload.php';
}

$app = new CoenJacobs\Mozart\Console\Application($version);
$app->run();
}, '0.6.0');
}, '0.8.0');
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prefer-stable": true,
"license": "MIT",
"require": {
"php": "^7.2",
"php": "^7.3|^8.0",
"symfony/console": "^4|^5",
"symfony/finder": "^4|^5",
"league/flysystem": "^1.0"
Expand All @@ -25,7 +25,8 @@
"require-dev": {
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5",
"mheap/phpunit-github-actions-printer": "^1.4"
"mheap/phpunit-github-actions-printer": "^1.4",
"vimeo/psalm": "^4.4"
},
"scripts": {
"lint": [
Expand Down
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
1 change: 1 addition & 0 deletions src/Composer/Autoload/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
interface Autoloader
{
public function processConfig($autoloadConfig);
public function getSearchNamespace();
}
Loading

0 comments on commit 6ec5d45

Please sign in to comment.