|
1 | 1 | +++ |
2 | | -title = "Getting started" |
| 2 | +title = "Getting Started" |
3 | 3 | weight = 1 |
4 | 4 | +++ |
5 | 5 |
|
6 | 6 | ## Requirements |
7 | 7 |
|
8 | | -Phel requires PHP 8.2 or higher and [Composer](https://getcomposer.org/). |
| 8 | +- PHP 8.2+ |
| 9 | +- [Composer](https://getcomposer.org/) |
9 | 10 |
|
10 | | -## Quick start with a scaffolding template |
| 11 | +## Quick Start |
11 | 12 |
|
12 | | -To get started right away, you can create a new Phel commandline project via Composer's `create-project` command: |
| 13 | +Scaffold a new project: |
13 | 14 |
|
14 | 15 | ```bash |
15 | 16 | composer create-project --stability dev phel-lang/cli-skeleton example-app |
16 | | -``` |
17 | | - |
18 | | -Once the project has been created, start the REPL (read-evaluate-print loop) to try Phel. |
19 | | - |
20 | | -```bash |
21 | 17 | cd example-app |
22 | 18 | composer repl |
23 | 19 | ``` |
24 | 20 |
|
25 | | -> Alternatively to the [phel-lang/cli-skeleton](https://github.com/phel-lang/cli-skeleton), you can also use [phel-lang/web-skeleton](https://github.com/phel-lang/web-skeleton) for a web project. More information can be found in the [README](https://packagist.org/packages/phel-lang/cli-skeleton) of the project. |
26 | | -
|
27 | | - |
28 | | -## Manually initialize a new project using Composer |
| 21 | +> For web projects: [web-skeleton](https://github.com/phel-lang/web-skeleton) |
29 | 22 |
|
30 | | -The easiest way to get started is by setting up a new Composer project. First, create a new directory and initialize a new Composer project. |
| 23 | +## Manual Setup |
31 | 24 |
|
32 | 25 | ```bash |
33 | | -mkdir hello-world |
34 | | -cd hello-world |
| 26 | +mkdir hello-world && cd hello-world |
35 | 27 | composer init |
36 | | -``` |
37 | | - |
38 | | -Next, require Phel as a dependency. |
39 | | - |
40 | | -```bash |
41 | 28 | composer require phel-lang/phel-lang |
| 29 | +mkdir src |
42 | 30 | ``` |
43 | 31 |
|
44 | | -> Optionally, you can create `phel-config.php` at the root of the project: |
45 | | -> ```php |
46 | | -> <?php |
47 | | -> |
48 | | -> return (new \Phel\Config\PhelConfig()) |
49 | | -> ->setSrcDirs(['src']); |
50 | | -> ``` |
51 | | -> Read the docs to see all available [configuration](/documentation/configuration) options for Phel. |
| 32 | +Optional config (`phel-config.php`): |
52 | 33 |
|
53 | | -Then, create a new directory `src` with a file `main.phel` inside this directory. |
54 | | -
|
55 | | -```bash |
56 | | -mkdir src |
| 34 | +```php |
| 35 | +<?php |
| 36 | +return (new \Phel\Config\PhelConfig()) |
| 37 | + ->setSrcDirs(['src']); |
57 | 38 | ``` |
58 | 39 |
|
59 | | -The file `main.phel` contains the actual code of the project. It defines the namespace and prints "Hello, World!". |
| 40 | +Sample Phel file (`src/main.phel`): |
60 | 41 |
|
61 | 42 | ```phel |
62 | | -# inside `src/main.phel` |
63 | 43 | (ns hello-world\main) |
64 | | -
|
65 | 44 | (println "Hello, World!") |
66 | 45 | ``` |
67 | 46 |
|
68 | | -## Running the code |
| 47 | +## Run Code |
| 48 | + |
| 49 | +**From CLI:** |
69 | 50 |
|
70 | | -There are two ways to run the code: from the command line and with a PHP Server. |
| 51 | +```bash |
| 52 | +vendor/bin/phel run src/main.phel |
| 53 | +``` |
71 | 54 |
|
72 | | -### From the Command line |
| 55 | +**With PHP Server:** |
73 | 56 |
|
74 | | -Code can be executed from the command line by calling the `vendor/bin/phel run` command, followed by the file path or namespace: |
| 57 | +```php |
| 58 | +<?php |
| 59 | +require __DIR__ . '/../vendor/autoload.php'; |
| 60 | +\Phel\Phel::run(__DIR__ . '/../', 'hello-world\\main'); |
| 61 | +``` |
75 | 62 |
|
76 | 63 | ```bash |
77 | | -vendor/bin/phel run src/main.phel |
78 | | -# or |
79 | | -vendor/bin/phel run hello-world\\main |
80 | | -# or |
81 | | -vendor/bin/phel run "hello-world\main" |
| 64 | +php -S localhost:8000 ./src/index.php |
82 | 65 | ``` |
83 | 66 |
|
84 | | -The output will be: |
| 67 | +> 📘 [More on running code](/documentation/cli-commands#run-a-script) |
| 68 | +
|
| 69 | +## REPL |
85 | 70 |
|
| 71 | +```bash |
| 72 | +vendor/bin/phel repl |
86 | 73 | ``` |
87 | | -Hello, World! |
| 74 | + |
| 75 | +Try: |
| 76 | + |
| 77 | +```phel |
| 78 | +(def name "World") |
| 79 | +(println "Hello" name) |
88 | 80 | ``` |
89 | 81 |
|
90 | | -### With a PHP Server |
| 82 | +> 📘 [More on REPL](/documentation/repl) |
91 | 83 |
|
92 | | -> Check the [web-skeleton project on GitHub](https://github.com/phel-lang/web-skeleton). |
| 84 | +## Debugging |
93 | 85 |
|
94 | | -The file `index.php` will be executed by the PHP Server. It initializes the Phel Runtime and loads the namespace from the `main.phel` file described above, to start the application. |
| 86 | +```phel |
| 87 | +(php/dump (+ 40 2)) |
| 88 | +``` |
| 89 | + |
| 90 | +Enable temp files in `phel-config-local.php`: |
95 | 91 |
|
96 | 92 | ```php |
97 | | -// src/index.php |
98 | 93 | <?php |
| 94 | +return (require __DIR__ . '/phel-config.php') |
| 95 | + ->setKeepGeneratedTempFiles(true); |
| 96 | +``` |
99 | 97 |
|
100 | | -use Phel\Phel; |
101 | | - |
102 | | -$projectRootDir = __DIR__ . '/../'; |
| 98 | +> 📘 [More on debugging](/documentation/debug) |
103 | 99 |
|
104 | | -require $projectRootDir . 'vendor/autoload.php'; |
| 100 | +## Build & Deploy |
105 | 101 |
|
106 | | -Phel::run($projectRootDir, 'hello-world\\main'); |
| 102 | +```bash |
| 103 | +vendor/bin/phel build |
| 104 | +php out/index.php |
107 | 105 | ``` |
108 | 106 |
|
109 | | -The PHP Server can now be started. |
| 107 | +> 📘 [More on build](/documentation/cli-commands/#build-the-project) |
| 108 | +
|
| 109 | +## Testing |
110 | 110 |
|
111 | 111 | ```bash |
112 | | -# Start server |
113 | | -php -S localhost:8000 ./src/index.php |
| 112 | +vendor/bin/phel test --filter foo |
114 | 113 | ``` |
115 | 114 |
|
116 | | -In the browser, the URL `http://localhost:8000` will now print "Hello, World!". |
117 | | - |
118 | | -> When using a web server, consider building the project to avoid compilation time for each request; so PHP will run the transpiled PHP code instead to gain performance. See more [Buid the project](/documentation/cli-commands/#build-the-project). |
| 115 | +> 📘 [More on testing](/documentation/testing) |
119 | 116 |
|
120 | | -## Launch the REPL |
| 117 | +## Handy Macros |
121 | 118 |
|
122 | | -To try Phel you can run a REPL by executing the `./vendor/bin/phel repl` command. |
| 119 | +```phel |
| 120 | +(when condition (println "if true")) |
| 121 | +(-> {:name "Phel"} (:name) (str "Lang")) |
| 122 | +``` |
123 | 123 |
|
124 | | -> Read more about the [REPL](/documentation/repl) in its own chapter. |
| 124 | +> 📘 [More on macros](/documentation/macros) |
125 | 125 |
|
126 | | -## Editor support |
| 126 | +## Editor Support |
127 | 127 |
|
128 | | -Phel comes with basic support for <a href="https://github.com/phel-lang/phel-vs-code-extension" target="_blank"> |
129 | | -VSCode</a>, <a href="https://github.com/phel-lang/phel-phpstorm-syntax" target="_blank">PhpStorm</a>, a |
130 | | -<a href="https://codeberg.org/mmontone/interactive-lang-tools/src/branch/master/backends/phel" target="_blank"> |
131 | | -Emacs mode with interactive capabilities</a> and a <a href="https://github.com/danirod/phel.vim" target="_blank">Vim |
132 | | -plugin</a> in the making. |
| 128 | +- [VSCode](https://github.com/phel-lang/phel-vs-code-extension) |
| 129 | +- [PhpStorm](https://github.com/phel-lang/phel-phpstorm-syntax) |
| 130 | +- [Emacs](https://codeberg.org/mmontone/interactive-lang-tools) |
| 131 | +- [Vim](https://github.com/danirod/phel.vim) |
0 commit comments