diff --git a/README.md b/README.md index f6e617e..bb747a0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Lenvi: Laravel & Legacy Environment Simplified -This playbook is built for Laravel development but can also run legacy PHP apps, offering a powerful, centralized environment on Debian, Ubuntu, or WSL2—similar to Homestead but without the virtual machine overhead. +This playbook is built for Laravel development but can also run legacy PHP apps, offering a powerful, centralized environment with multi-php support on Debian, Ubuntu, or WSL2; similar to Homestead but without the virtual machine overhead. ## Prerequisites @@ -19,7 +19,7 @@ This playbook is built for Laravel development but can also run legacy PHP apps, Clone the repository and go to the project directory. ```bash -git clone https://git.marmattheo.com/marito/Lenvi.git && cd Lenvi +git clone https://git.marmattheo.com/marito/Lenvi.git ~/Lenvi && cd ~/Lenvi ``` ### 2\. Configure: Define Your Projects diff --git a/roles/composer/tasks/main.yml b/roles/composer/tasks/main.yml index df223c2..d51f5aa 100644 --- a/roles/composer/tasks/main.yml +++ b/roles/composer/tasks/main.yml @@ -1,25 +1,18 @@ --- -- name: "Install yq (YAML processor) for the smart composer script" +- name: "Install yq (YAML processor for our smart wrappers)" ansible.builtin.get_url: url: "https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64" dest: /usr/local/bin/yq mode: '0755' -- name: "Download the Composer installer" +- name: "Download the Composer installer script" ansible.builtin.get_url: url: https://getcomposer.org/installer - dest: /tmp/composer-installer.php + dest: /tmp/composer-setup.php mode: '0755' -- name: "Install Composer globally (as composer.phar)" +- name: "Install Composer executable globally" ansible.builtin.command: - cmd: php /tmp/composer-installer.php --install-dir=/usr/local/bin --filename=composer.phar - creates: /usr/local/bin/composer.phar - -- name: "Deploy the smart Composer wrapper script" - ansible.builtin.template: - src: composer-wrapper.sh.j2 - dest: /usr/local/bin/composer - owner: root - group: root - mode: '0755' \ No newline at end of file + cmd: php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer + args: + creates: /usr/local/bin/composer \ No newline at end of file diff --git a/roles/composer/templates/composer-wrapper.sh.j2 b/roles/composer/templates/composer-wrapper.sh.j2 deleted file mode 100644 index 01380d1..0000000 --- a/roles/composer/templates/composer-wrapper.sh.j2 +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -# -# Lenvi Smart Composer Wrapper (v2) -# -# This script automatically detects the required PHP version for a project -# by reading a central Lenvi.yaml file and uses the correct PHP binary. - -# --- The real composer binary and default PHP --- -COMPOSER_PHAR="/usr/local/bin/composer.phar" -DEFAULT_PHP_BINARY="/usr/bin/php" -PHP_BINARY="" - -# --- The EXACT path to the configuration file, baked in by Ansible --- -LENVI_CONFIG_FILE="{{ playbook_dir }}/Lenvi.yaml" - -# Check if the central config file actually exists -if [[ -f "$LENVI_CONFIG_FILE" ]]; then - # We have the config file, now find the PHP version for the current project directory - CURRENT_PROJECT_ROOT=$(pwd) - PHP_VERSION=$(yq eval '.sites[] | select(.project_root == "'"$CURRENT_PROJECT_ROOT"'") | .php_version' "$LENVI_CONFIG_FILE") - - if [[ -n "$PHP_VERSION" && -x "/usr/bin/php$PHP_VERSION" ]]; then - # Version found and the corresponding PHP binary exists - PHP_BINARY="/usr/bin/php$PHP_VERSION" - echo "Lenvi: Project configuration found. Using PHP $PHP_VERSION..." >&2 - fi -fi - -# If no specific PHP binary was found after checking, use the system default -if [[ -z "$PHP_BINARY" ]]; then - echo "Lenvi: No specific project configuration found for this directory. Using default system PHP." >&2 - PHP_BINARY="$DEFAULT_PHP_BINARY" -fi - -# Execute the real Composer with the determined PHP version and pass all arguments -exec "$PHP_BINARY" "$COMPOSER_PHAR" "$@" \ No newline at end of file diff --git a/roles/php/tasks/main.yml b/roles/php/tasks/main.yml index 8bcaf5f..86439c8 100644 --- a/roles/php/tasks/main.yml +++ b/roles/php/tasks/main.yml @@ -23,4 +23,20 @@ enabled: yes state: started loop: "{{ php_versions_to_install }}" - when: php_versions_to_install is defined and php_versions_to_install | length > 0 \ No newline at end of file + when: php_versions_to_install is defined and php_versions_to_install | length > 0 + +- name: "Deploy the smart PHP wrapper script" + ansible.builtin.template: + src: php-wrapper.sh.j2 + dest: /usr/local/bin/php + owner: root + group: root + mode: '0755' + +- name: "Deploy the smart Artisan wrapper script" + ansible.builtin.template: + src: artisan-wrapper.sh.j2 + dest: /usr/local/bin/artisan + owner: root + group: root + mode: '0755' \ No newline at end of file diff --git a/roles/php/templates/artisan-wrapper.sh.j2 b/roles/php/templates/artisan-wrapper.sh.j2 new file mode 100644 index 0000000..54dfdf1 --- /dev/null +++ b/roles/php/templates/artisan-wrapper.sh.j2 @@ -0,0 +1,14 @@ +#!/bin/bash +# Lenvi Smart Artisan Wrapper +# This script executes 'artisan' with the project-specific PHP version. + +# Find the php wrapper script which contains all the logic. +PHP_WRAPPER="/usr/local/bin/php" + +# Check if an artisan file exists in the current directory before trying to run it. +if [ -f "artisan" ]; then + exec "$PHP_WRAPPER" "artisan" "$@" +else + echo "Error: 'artisan' file not found in the current directory." >&2 + exit 1 +fi \ No newline at end of file diff --git a/roles/php/templates/php-wrapper.sh.j2 b/roles/php/templates/php-wrapper.sh.j2 new file mode 100644 index 0000000..910ef73 --- /dev/null +++ b/roles/php/templates/php-wrapper.sh.j2 @@ -0,0 +1,22 @@ +#!/bin/bash +# Lenvi Smart PHP Wrapper +# This script executes any 'php' command with the project-specific PHP version. + +DEFAULT_PHP_BINARY="/usr/bin/php" +PHP_BINARY="" +LENVI_CONFIG_FILE="{{ playbook_dir }}/Lenvi.yaml" + +if [[ -f "$LENVI_CONFIG_FILE" ]]; then + CURRENT_PROJECT_ROOT=$(pwd) + PHP_VERSION=$(yq eval '.sites[] | select(.project_root == "'"$CURRENT_PROJECT_ROOT"'") | .php_version' "$LENVI_CONFIG_FILE") + + if [[ -n "$PHP_VERSION" && -x "/usr/bin/php$PHP_VERSION" ]]; then + PHP_BINARY="/usr/bin/php$PHP_VERSION" + fi +fi + +if [[ -z "$PHP_BINARY" ]]; then + PHP_BINARY="$DEFAULT_PHP_BINARY" +fi + +exec "$PHP_BINARY" "$@" \ No newline at end of file