Skip to content

Commit 930fd82

Browse files
committed
feat: Bundle :box:
1 parent 5547519 commit 930fd82

File tree

10 files changed

+106
-9
lines changed

10 files changed

+106
-9
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ phpctl sh echo 'Hello, World!' # To run arbitrary sh commands inside the contain
5959
| `server [port] [directory]` | Runs PHP's built-in web-server (default port is `80` and default directory is current `.`). |
6060

6161
### Useful
62-
| Command | Description |
63-
|-----------------|---------------------------------------------------------------------|
64-
| `phpunit` | Runs [PHPUnit](https://phpunit.de/). |
65-
| `sh [commands]` | Starts an interactive Shell session or runs `sh` commands. |
66-
| `repl` | Starts a PHP REPL session (powered by [PsySH](https://psysh.org/)). |
67-
| `php-cs-fixer` | Runs [PHP-CS-Fixer](https://cs.symfony.com/). |
68-
| `phpstan` | Runs [PHPStan](https://phpstan.org/). |
69-
| `infection` | Runs [Infection](https://infection.github.io/). |
70-
| `box` | Runs [Box](https://github.com/box-project/box). |
62+
| Command | Description |
63+
|-----------------|-----------------------------------------------------------------------|
64+
| `phpunit` | Runs [PHPUnit](https://phpunit.de/). |
65+
| `sh [commands]` | Starts an interactive Shell session or runs `sh` commands. |
66+
| `repl` | Starts a PHP REPL session (powered by [PsySH](https://psysh.org/)). |
67+
| `php-cs-fixer` | Runs [PHP-CS-Fixer](https://cs.symfony.com/). |
68+
| `phpstan` | Runs [PHPStan](https://phpstan.org/). |
69+
| `infection` | Runs [Infection](https://infection.github.io/). |
70+
| `box` | Runs [Box](https://github.com/box-project/box). |
71+
| `bundle` | Bundles a project into an image and ships it as a single binary file. |
7172

7273
### Scaffolders
7374
| Command | Description |

bundle/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ARG FROM=opencodeco/phpctl:php82
2+
FROM $FROM
3+
ARG ENTRYPOINT=php
4+
ENV ENTRYPOINT=$ENTRYPOINT
5+
ARG COMMAND=index.php
6+
ENV COMMAND=$COMMAND
7+
COPY . /usr/src
8+
ENTRYPOINT [ "/usr/src/docker-entrypoint.sh" ]

bundle/bin.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env sh
2+
: '
3+
This script file was generated by the phpctl bundle command.
4+
It is basically a wrapper around the docker run command.
5+
It is not intended to be edited, but feel free to do so (if you know what you are doing).
6+
Move this script file to somewhere in your PATH to make it available as a command.
7+
A good place would be /usr/local/bin.
8+
'
9+
docker run \
10+
-w /workdir \
11+
-v $PWD:/workdir \
12+
--net host \
13+
$(env | awk -F= '/^[[:alpha:]]/{print $1}' | sed 's/^/-e/') \
14+
--rm -it IMAGE $@

bundle/docker-entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env sh
2+
"$ENTRYPOINT" "/usr/src/$COMMAND" $@

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Welcome to the `phpctl` examples page.
55
Feel free to pick one to see how it is done:
66

77
- [box](box/)
8+
- [bundle](bundle/)

examples/bundle/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
IMAGE=docker.io/leocavalcante/phpctl-bundle-example
2+
3+
phpctl-bundle: clean
4+
phpctl bundle $(IMAGE) phpctl-bundle main.php
5+
6+
.PHONY: push
7+
push:
8+
docker push $(IMAGE)
9+
10+
install:
11+
mv phpctl-bundle /usr/local/bin
12+
13+
clean:
14+
rm -rf phpctl-bundle

examples/bundle/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Bundle
2+
3+
This is the example directory for the `bundle` command.
4+
5+
This command is used to bundle a project into an image and ship it as a single binanry file.
6+
7+
The command usage is based on `phpctl bundle [image] [alias] [entryfile]`.
8+
9+
But this directory uses `make` to make life easier.
10+
11+
So just:
12+
```shell
13+
make && make install
14+
```
15+
16+
Then execute:
17+
```shell
18+
phpctl-bundle
19+
```
20+
21+
You should see:
22+
```text
23+
Hello, World!
24+
```
25+
As the output.
26+
27+
The program is `main.php`:
28+
```php
29+
<?php
30+
31+
echo "Hello, World!\n";
32+
33+
```

examples/bundle/main.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo "Hello, World!\n";

src/bundle.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
box() {
22
run -- box $@
33
}
4+
5+
bundle() {
6+
image="$1"
7+
alias="${image##*/}"
8+
alias="${2-alias}"
9+
command="${3-index.php}"
10+
entrypoint="${4-php}"
11+
from="${5-opencodeco/phpctl:php82}"
12+
cp "$PHPCTL_DIR/bundle/Dockerfile" "$PHPCTL_DIR/bundle/docker-entrypoint.sh" .
13+
docker build \
14+
--build-arg FROM="$from" \
15+
--build-arg ENTRYPOINT="$entrypoint" \
16+
--build-arg COMMAND="$command" \
17+
-t "$image" .
18+
rm Dockerfile docker-entrypoint.sh
19+
cp "$PHPCTL_DIR/bundle/bin.sh" "$alias"
20+
sed -i.bak "s#IMAGE#$image#g" "$alias"
21+
rm "$alias.bak"
22+
chmod a+x "$alias"
23+
}

src/help.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ help() {
3131
echo -e "\033[0;32m create [framework] [dir] \033[0m Creates a new project using the given framework (or package)"
3232
echo -e "\033[0;32m init [skeleton] \033[0m Initializes a skeleton configuration (phpunit, php-cs-fixer)"
3333
echo -e "\033[0;32m box \033[0m Runs Box (PHAR builder)"
34+
echo -e "\033[0;32m bundle \033[0m Bundles a PHP project into a Docker image"
3435
echo -e "\033[0;32m help or man \033[0m Displays this help message"
3536
echo -e "\033[0;32m self-update \033[0m Updates phpctl itself"
3637
echo -e "\033[0;32m doctor \033[0m Inspects the current PHP_VERSION and PHPCTL_IMAGE"

0 commit comments

Comments
 (0)