diff --git a/CHANGELOG.md b/CHANGELOG.md index 666a359..4e9a383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This update introduces minor bug fixes & improvements - Added new constant `%DEFAULT_BUILD_CONFIGURATION%` which points to the project's default build configuration - Added new dynamic constant `%BUILD_OUTPUT_PATH%` which can be used as `%BUILD_OUTPUT_PATH%:release` to show the output path of a specific build configuration + - Refactor CI templates to support dynamic build targets ### Changed - Refactor phpmake template to support dynamic build targets diff --git a/src/ncc/Classes/PhpExtension/Templates/GitHubWorkflowTemplate.php b/src/ncc/Classes/PhpExtension/Templates/GitHubWorkflowTemplate.php index ea0d0fd..4ed2b7a 100644 --- a/src/ncc/Classes/PhpExtension/Templates/GitHubWorkflowTemplate.php +++ b/src/ncc/Classes/PhpExtension/Templates/GitHubWorkflowTemplate.php @@ -58,11 +58,60 @@ class GitHubWorkflowTemplate mkdir($ci_dir, 0777, true); } - IO::fwrite( - $ci_dir . DIRECTORY_SEPARATOR . 'ncc_workflow.yml', - ConstantCompiler::compileConstants($project_manager->getProjectConfiguration(), - IO::fread(__DIR__ . DIRECTORY_SEPARATOR . 'github_ci.yml.tpl') - ) + $template = IO::fread(__DIR__ . DIRECTORY_SEPARATOR . 'github_ci.yml.tpl'); + $default_configuration = $project_manager->getProjectConfiguration()->getBuild()->getDefaultConfiguration(); + $builds = []; + $releases = []; + $downloads = []; + foreach($project_manager->getProjectConfiguration()->getBuild()->getBuildConfigurations() as $build_name) + { + $build_template = IO::fread(__DIR__ . DIRECTORY_SEPARATOR . 'github_ci_build.yml.tpl'); + $build_template = str_replace('%TPL_BUILD_NAME%', $build_name, $build_template); + $build_template = str_replace('%TPL_BUILD_OUTPUT%', + $project_manager->getProjectConfiguration()->getBuild()->getBuildConfiguration($build_name)->getOutput(), + $build_template + ); + $builds[$build_name] = $build_template; + + $download_template = IO::fread(__DIR__ . DIRECTORY_SEPARATOR . 'github_ci_download.yml.tpl'); + $download_template = str_replace('%TPL_BUILD_NAME%', $build_name, $download_template); + $downloads[$build_name] = $download_template; + + $release_template = str_repeat(' ', 12) . $build_name . DIRECTORY_SEPARATOR . basename( + $project_manager->getProjectConfiguration()->getBuild()->getBuildConfiguration($build_name)->getOutput() + ); + $releases[$build_name] = $release_template; + } + + $build_jobs = ''; + foreach($builds as $name => $build_template) + { + $build_jobs .= $build_template . PHP_EOL; + } + + $download_jobs = ''; + foreach($downloads as $name => $download_template) + { + $download_jobs .= $download_template . PHP_EOL; + } + + $release_jobs = ''; + foreach($releases as $name => $release_template) + { + $release_jobs .= $release_template . PHP_EOL; + } + + $template = str_replace('%TPL_BUILDS%', $build_jobs, $template); + $template = str_replace('%TPL_DOWNLOAD_ARTIFACTS%', $download_jobs, $template); + $template = str_replace('%TPL_ARTIFACT_FILES%', $release_jobs, $template); + $template = str_replace('%TPL_BUILD_NAMES%', implode(', ', array_keys($builds)), $template); + $template = str_replace('%TPL_DEFAULT_BUILD_CONFIGURATION%', $default_configuration, $template); + $template = str_replace('%TPL_DEFAULT_ARTIFACT_BUILD_OUTPUT%', $default_configuration . DIRECTORY_SEPARATOR . basename( + $project_manager->getProjectConfiguration()->getBuild()->getBuildConfiguration($default_configuration)->getOutput() + ), $template); + + IO::fwrite($ci_dir . DIRECTORY_SEPARATOR . 'ncc_workflow.yml', + ConstantCompiler::compileConstants($project_manager->getProjectConfiguration(), $template) ); } } \ No newline at end of file diff --git a/src/ncc/Classes/PhpExtension/Templates/github_ci.yml.tpl b/src/ncc/Classes/PhpExtension/Templates/github_ci.yml.tpl index 66ea497..6cdee6b 100644 --- a/src/ncc/Classes/PhpExtension/Templates/github_ci.yml.tpl +++ b/src/ncc/Classes/PhpExtension/Templates/github_ci.yml.tpl @@ -9,56 +9,9 @@ on: workflow_dispatch: jobs: - build: - runs-on: ubuntu-latest - container: - image: php:8.3 - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Install dependencies - run: | - apt update -yqq - apt install git libpq-dev libzip-dev zip make wget gnupg -yqq - - - name: Install phive - run: | - wget -O phive.phar https://phar.io/releases/phive.phar - wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc - gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 - gpg --verify phive.phar.asc phive.phar - chmod +x phive.phar - mv phive.phar /usr/local/bin/phive - - - name: Install phab - run: | - phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C - - - name: Install latest version of NCC - run: | - git clone https://git.n64.cc/nosial/ncc.git - cd ncc - make redist - NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1) - if [ -z "$NCC_DIR" ]; then - echo "NCC build directory not found" - exit 1 - fi - php "$NCC_DIR/INSTALL" --auto - cd .. && rm -rf ncc - - - name: Build project - run: | - ncc build --config release --log-level debug - - - name: Upload build artifacts - uses: actions/upload-artifact@v4 - with: - name: %ASSEMBLY.NAME%_build - path: build/release/%ASSEMBLY.PACKAGE%.ncc +%TPL_BUILDS% + # Checking for phpunit.xml check-phpunit: runs-on: ubuntu-latest outputs: @@ -74,9 +27,55 @@ jobs: else echo "phpunit-exists=false" >> $GITHUB_OUTPUT fi + # Checking for phpdoc.dist.xml + check-phpdoc: + runs-on: ubuntu-latest + outputs: + phpdoc-exists: ${{ steps.check.outputs.phpdoc-exists }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Check for phpdoc.dist.xml + id: check + run: | + if [ -f phpdoc.dist.xml ]; then + echo "phpdoc-exists=true" >> $GITHUB_OUTPUT + else + echo "phpdoc-exists=false" >> $GITHUB_OUTPUT + fi + generate-phpdoc: + needs: [%TPL_DEFAULT_BUILD_CONFIGURATION%, check-phpdoc] + runs-on: ubuntu-latest + container: + image: php:8.3 + if: needs.check-phpdoc.outputs.phpdoc-exists == 'true' + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + apt update -yqq + apt install git libpq-dev libzip-dev zip make wget gnupg -yqq + + - name: Download PHPDocumentor + run: | + wget https://phpdoc.org/phpDocumentor.phar + chmod +x phpDocumentor.phar + + - name: Generate PHPDoc + run: | + php phpDocumentor.phar -d src -t docs + + - name: Upload PHPDoc + uses: actions/upload-artifact@v4 + with: + name: documentation + path: docs test: - needs: [build, check-phpunit] + needs: [%TPL_BUILD_NAMES%, check-phpunit] runs-on: ubuntu-latest container: image: php:8.3 @@ -89,8 +88,8 @@ jobs: - name: Download build artifacts uses: actions/download-artifact@v4 with: - name: %ASSEMBLY.NAME%_build - path: %ASSEMBLY.NAME%_build # Adjust this to download the artifact directly under '%ASSEMBLY.NAME%_build' + name: %TPL_DEFAULT_BUILD_CONFIGURATION% + path: %TPL_DEFAULT_BUILD_CONFIGURATION% - name: Install dependencies run: | @@ -128,15 +127,15 @@ jobs: - name: Install NCC packages run: | - ncc package install --package="%ASSEMBLY.NAME%_build/%ASSEMBLY.PACKAGE%.ncc" --build-source --reinstall -y --log-level debug + ncc package install --package="%TPL_DEFAULT_ARTIFACT_BUILD_OUTPUT%" --build-source --reinstall -y --log-level debug - name: Run PHPUnit tests run: | wget https://phar.phpunit.de/phpunit-11.3.phar php phpunit-11.3.phar --configuration phpunit.xml - release: - needs: [build, test] + upload-artifacts: + needs: [%TPL_BUILD_NAMES%, test] permissions: write-all runs-on: ubuntu-latest container: @@ -147,16 +146,12 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Download build artifacts - uses: actions/download-artifact@v4 - with: - name: %ASSEMBLY.NAME%_build - path: %ASSEMBLY.NAME%_build +%TPL_DOWNLOAD_ARTIFACTS% - name: Upload to GitHub Release uses: softprops/action-gh-release@v1 with: files: | - %ASSEMBLY.NAME%_build/%ASSEMBLY.PACKAGE%.ncc +%TPL_ARTIFACT_FILES% env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/src/ncc/Classes/PhpExtension/Templates/github_ci_build.yml.tpl b/src/ncc/Classes/PhpExtension/Templates/github_ci_build.yml.tpl new file mode 100644 index 0000000..7c64135 --- /dev/null +++ b/src/ncc/Classes/PhpExtension/Templates/github_ci_build.yml.tpl @@ -0,0 +1,49 @@ + %TPL_BUILD_NAME%: + runs-on: ubuntu-latest + container: + image: php:8.3 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + apt update -yqq + apt install git libpq-dev libzip-dev zip make wget gnupg -yqq + + - name: Install phive + run: | + wget -O phive.phar https://phar.io/releases/phive.phar + wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc + gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 + gpg --verify phive.phar.asc phive.phar + chmod +x phive.phar + mv phive.phar /usr/local/bin/phive + + - name: Install phab + run: | + phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C + + - name: Install latest version of NCC + run: | + git clone https://git.n64.cc/nosial/ncc.git + cd ncc + make redist + NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1) + if [ -z "$NCC_DIR" ]; then + echo "NCC build directory not found" + exit 1 + fi + php "$NCC_DIR/INSTALL" --auto + cd .. && rm -rf ncc + + - name: Build project + run: | + ncc build --config %TPL_BUILD_NAME% --log-level debug + + - name: Upload build artifact + uses: actions/upload-artifact@v4 + with: + name: %TPL_BUILD_NAME% + path: %TPL_BUILD_OUTPUT% \ No newline at end of file diff --git a/src/ncc/Classes/PhpExtension/Templates/github_ci_download.yml.tpl b/src/ncc/Classes/PhpExtension/Templates/github_ci_download.yml.tpl new file mode 100644 index 0000000..108d931 --- /dev/null +++ b/src/ncc/Classes/PhpExtension/Templates/github_ci_download.yml.tpl @@ -0,0 +1,5 @@ + - name: Download %TPL_BUILD_NAME% artifact + uses: actions/download-artifact@v4 + with: + name: %TPL_BUILD_NAME% + path: %TPL_BUILD_NAME% \ No newline at end of file