Skip to content

Commit

Permalink
Merge pull request #448 from kool-dev/preset-octane
Browse files Browse the repository at this point in the history
Preset for Laravel Octane
  • Loading branch information
fabriciojs authored Dec 6, 2022
2 parents 319901e + 26c3b26 commit acad708
Show file tree
Hide file tree
Showing 31 changed files with 444 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/kool
/kool-cli
/dist/
/.env
Output/
Expand Down
2 changes: 1 addition & 1 deletion commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ $ kool completion fish > ~/.config/fish/completions/kool.fish
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Hidden: true,
RunE: DefaultCommandRunFunction(completion),
}
Expand Down
10 changes: 10 additions & 0 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"kool-dev/kool/core/environment"
"kool-dev/kool/core/presets"
"os"
"path"
"path/filepath"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -59,10 +61,18 @@ func (c *KoolCreate) Execute(args []string) (err error) {

c.Shell().Println("Initializing", preset, "preset...")

if !path.IsAbs(createDirectory) {
if createDirectory, err = filepath.Abs(createDirectory); err != nil {
return
}
}

if err = os.Chdir(createDirectory); err != nil {
return
}

c.env.Set("PWD", createDirectory)

if err = c.parser.Install(preset, c.Shell()); err != nil {
return
}
Expand Down
47 changes: 45 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"kool-dev/kool/core/environment"
"kool-dev/kool/core/parser"
"kool-dev/kool/core/shell"
"os"
"path"
"path/filepath"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -47,6 +49,8 @@ var version string = DEV_VERSION

var rootCmd = NewRootCmd(environment.NewEnvStorage())

var originalWorkingDir = ""

func init() {
AddCommands(rootCmd)
}
Expand All @@ -67,7 +71,7 @@ Complete documentation is available at https://kool.dev/docs`,
Version: version,
DisableAutoGenTag: true,
DisableFlagsInUseLine: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
if verbose := cmd.Flags().Lookup("verbose"); verbose != nil && verbose.Value.String() == "true" {
env.Set("KOOL_VERBOSE", verbose.Value.String())
}
Expand All @@ -76,6 +80,44 @@ Complete documentation is available at https://kool.dev/docs`,
shell.NewShell().Warning("Warning: you are executing a development version of kool.")
hasWarnedDevelopmentVersion = true
}

workDirFlag := cmd.Flags().Lookup("working_dir")
if workDirFlag != nil && workDirFlag.Value.String() != "" {
workDir := workDirFlag.Value.String()

if originalWorkingDir != "" {
// having an original working dir set means we have
// already changed the working dir before and we are in
// a recursive kool call. We need to restore the original
// working dir before changing it again.
if err = os.Chdir(originalWorkingDir); err != nil {
return
}
}

if !path.IsAbs(workDir) {
if workDir, err = filepath.Abs(workDir); err != nil {
return
}
}

if err = os.Chdir(workDir); err != nil {
return
}

if originalWorkingDir == "" {
// we only set the original working dir if it is not set
// yet. This is to avoid overriding the original working
// dir in recursive calls.
if originalWorkingDir, err = os.Getwd(); err != nil {
return
}
}

environment.NewEnvStorage().Set("PWD", workDir)
}

return
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) == 0 {
Expand Down Expand Up @@ -106,7 +148,8 @@ Complete documentation is available at https://kool.dev/docs`,
},
}

cmd.PersistentFlags().Bool("verbose", false, "increases output verbosity")
cmd.PersistentFlags().Bool("verbose", false, "Increases output verbosity")
cmd.PersistentFlags().StringP("working_dir", "w", "", "Changes the working directory for the command")
return
}

Expand Down
222 changes: 222 additions & 0 deletions docs/2-Presets/Laravel+Octane.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
# Start a Laravel Octane Project with Docker in 3 Easy Steps

1. Run `kool create laravel+octane my-project`
2. Update **.env.example**
3. Run `kool run setup`

> Yes, using **kool** + Docker to create and work on new Laravel Octane projects is that easy!
## Requirements

If you haven't done so already, you first need to [install Docker and the kool CLI](/docs/getting-started/installation).

Also, make sure you're running the latest version of **kool**. Run the following command to compare your local version of **kool** with the latest release, and, if a newer version is available, automatically download and install it.

```bash
$ kool self-update
```

> Please note that it helps to have a basic understanding of how Docker and Docker Compose work to use Kool with Docker.
## 1. Run `kool create laravel+octane my-project`

Use the [`kool create PRESET FOLDER` command](/docs/commands/kool-create) to create your new Laravel Octane project:

```bash
$ kool create laravel+octane my-project
```

Under the hood, this command will run do the same as the standar Laravel preset to get a fresh install of latest Laravel using a customized **kool** Docker image with Swoole: <a href="https://github.com/kool-dev/docker-php-swoole" target="_blank">kooldev/php:8.1-nginx-swoole</a>.

After installing Laravel, `kool create` automatically requires `laravel/octane` and runs `artisan octane:install`. After that you will have the options for including a database or cache service, all of which helps you easily set up the initial tech stack for your project using an interactive wizard.

---

Now, move into your new Laravel Octane with Swoole project:

```bash
$ cd my-project
```

The [`kool preset` command](/docs/commands/kool-preset) auto-generated the following configuration files and added them to your project, which you can modify and extend.

```bash
+docker-compose.yml
+kool.yml
```

> Now's a good time to review the **docker-compose.yml** file and verify the services match the choices you made earlier using the wizard.
## 2. Update .env.example

You need to update some default values in Laravel's **.env.example** file to match the services in your **docker-compose.yml** file.

### Database Services

MySQL 5.7 and 8.0 or MariaDB 10.5

```diff
-DB_HOST=127.0.0.1
+DB_HOST=database
```

PostgreSQL 13.0

```diff
-DB_CONNECTION=mysql
+DB_CONNECTION=pgsql

-DB_HOST=127.0.0.1
+DB_HOST=database

-DB_PORT=3306
+DB_PORT=5432
```

> In order to avoid permission issues with mysql and mariaDB, add a user other than root and a password to your **.env.example** file
```diff
-DB_USERNAME=root
+DB_USERNAME=<some_user>

-DB_PASSWORD=
+DB_PASSWORD=<somepass>
```

### Cache Services

Redis

```diff
-REDIS_HOST=127.0.0.1
+REDIS_HOST=cache
```

Memcached

```diff
-MEMCACHED_HOST=127.0.0.1
+MEMCACHED_HOST=cache
```

## 3. Run `kool run setup`

> Say hello to **kool.yml**, say goodbye to custom shell scripts!
As mentioned above, the [`kool preset` command](/docs/commands/kool-preset) added a **kool.yml** file to your project. Think of **kool.yml** as a super easy-to-use task _helper_. Instead of writing custom shell scripts, add your own scripts to **kool.yml** (under the `scripts` key), and run them with `kool run SCRIPT` (e.g. `kool run artisan`). You can add your own single line commands (see `composer` below), or add a list of commands that will be executed in sequence (see `setup` below).

To help get you started, **kool.yml** comes prebuilt with an initial set of scripts (based on the choices you made earlier using the **preset** wizard), including a script called `setup`, which helps you spin up a project for the first time.

```yaml
scripts:
artisan: kool exec app php artisan
composer: kool exec app composer
mysql: kool exec -e MYSQL_PWD=$DB_PASSWORD database mysql -uroot
node: kool docker kooldev/node:16 node
npm: kool docker kooldev/node:16 npm # or yarn
npx: kool exec app npx

setup:
- kool run before-start
- kool start
- kool run composer install
- kool run artisan key:generate

reset:
- kool run composer install
- kool run artisan migrate:fresh --seed
- kool run yarn install

before-start:
- kool docker kooldev/bash -c "cp .env.example .env"
- kool run yarn install
```
Go ahead and run `kool run setup` to start your Docker environment and finish setting up your project:

```bash
# CAUTION: this script will reset your `.env` file with `.env.example`
$ kool run setup
```

> As you can see in **kool.yml**, the `setup` script will do the following in sequence: copy your updated **.env.example** file to **.env**; start your Docker environment; use Composer to install vendor dependencies; generate your `APP_KEY` (in `.env`); and then build your Node packages and assets.
Once `kool run setup` finishes, you should be able to access your new site at [http://localhost](http://localhost) and see the Laravel welcome page. Hooray!

Verify your Docker container is running using the [`kool status` command](/docs/commands/kool-status).

Run `kool logs app` to see the logs from the running `app` container.

> Use `kool logs` to see the logs from all running containers. Add the `-f` option after `kool logs` to follow the logs (i.e. `kool logs -f app`).
---

### Run Commands in Docker Containers

Use [`kool exec`](/docs/commands/kool-exec) to execute a command inside a running service container:

```bash
# kool exec [OPTIONS] SERVICE COMMAND [--] [ARG...]

$ kool exec app ls
```

Try `kool run artisan --help` to execute the `kool exec app php artisan --help` command in your running `app` container and print out information about Laravel's CLI commands.

### Open Sessions in Docker Containers

Similar to SSH, if you want to open a Bash session in your `app` container, run `kool exec app bash`, where `app` is the name of the service container in **docker-compose.yml**. If you prefer, you can use `sh` instead of `bash` (`kool exec app sh`).

```bash
$ kool exec app bash
bash-5.1#

$ kool exec app sh
/app #
```

### Connect to Docker Database Container

You can easily start a new SQL client session inside your running `database` container by executing `kool run mysql` (MySQL) or `kool run psql` (PostgreSQL) in your terminal. This runs the single-line `mysql` or `psql` script included in your **kool.yml**.

### Access Private Repos and Packages in Docker Containers

If you need your `app` container to use your local SSH keys to pull private repositories and/or install private packages (which have been added as dependencies in your `composer.json` or `package.json` file), you can simply add `$HOME/.ssh:/home/kool/.ssh:delegated` under the `volumes` key of the `app` service in your **docker-compose.yml** file. This maps a `.ssh` folder in the container to the `.ssh` folder on your host machine.

```diff
volumes:
- .:/app:delegated
+ - $HOME/.ssh:/home/kool/.ssh:delegated
```

## Staying kool

When it's time to stop working on the project:

```bash
$ kool stop
```

And when you're ready to start work again:

```bash
$ kool start
```

## Additional Presets

We have more presets to help you start projects with **kool** in a standardized way across different frameworks.

- **[AdonisJs](/docs/2-Presets/AdonisJs.md)**
- **[CodeIgniter](/docs/2-Presets/CodeIgniter.md)**
- **[Express.js](/docs/2-Presets/ExpressJS.md)**
- **[Hugo](/docs/2-Presets/Hugo.md)**
- **[NestJS](/docs/2-Presets/NestJS.md)**
- **[Next.js](/docs/2-Presets/NextJS.md)**
- **[Node.js](/docs/2-Presets/NodeJS.md)**
- **[Nuxt.js](/docs/2-Presets/NuxtJS.md)**
- **[PHP](/docs/2-Presets/PHP.md)**
- **[Symfony](/docs/2-Presets/Symfony.md)**
- **[WordPress](/docs/2-Presets/WordPress.md)**

Missing a preset? **[Make a request](https://github.com/kool-dev/kool/issues/new)**, or contribute by opening a Pull Request. Go to [https://github.com/kool-dev/kool/tree/main/presets](https://github.com/kool-dev/kool/tree/main/presets) and browse the code to learn more about how presets work.
2 changes: 1 addition & 1 deletion docs/2-Presets/Laravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Use the [`kool create PRESET FOLDER` command](/docs/commands/kool-create) to cre
$ kool create laravel my-project
```

Under the hood, this command will run `composer create-project --no-install --no-scripts --prefer-dist laravel/laravel my-project` using a customized **kool** Docker image: <a href="https://github.com/kool-dev/docker-php" target="_blank">kooldev/php:7.4</a>.
Under the hood, this command will run `composer create-project --no-install --no-scripts --prefer-dist laravel/laravel my-project` using a customized **kool** Docker image: <a href="https://github.com/kool-dev/docker-php" target="_blank">kooldev/php:8.1</a>.

After installing Laravel, `kool create` automatically runs the `kool preset laravel` command, which helps you easily set up the initial tech stack for your project using an interactive wizard.

Expand Down
5 changes: 3 additions & 2 deletions docs/4-Commands/0-kool.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ kool
### Options

```
-h, --help help for kool
--verbose increases output verbosity
-h, --help help for kool
--verbose Increases output verbosity
-w, --working_dir string Changes the working directory for the command
```

### SEE ALSO
Expand Down
3 changes: 2 additions & 1 deletion docs/4-Commands/kool-completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ kool completion [bash|zsh|fish|powershell]
### Options inherited from parent commands

```
--verbose increases output verbosity
--verbose Increases output verbosity
-w, --working_dir string Changes the working directory for the command
```

### SEE ALSO
Expand Down
3 changes: 2 additions & 1 deletion docs/4-Commands/kool-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ kool create PRESET FOLDER
### Options inherited from parent commands

```
--verbose increases output verbosity
--verbose Increases output verbosity
-w, --working_dir string Changes the working directory for the command
```

### SEE ALSO
Expand Down
3 changes: 2 additions & 1 deletion docs/4-Commands/kool-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ kool docker [OPTIONS] IMAGE [COMMAND] [--] [ARG...]
### Options inherited from parent commands

```
--verbose increases output verbosity
--verbose Increases output verbosity
-w, --working_dir string Changes the working directory for the command
```

### SEE ALSO
Expand Down
Loading

0 comments on commit acad708

Please sign in to comment.