diff --git a/components/cores/uapp-java/.project-creation/config.json b/components/cores/uapp-java/.project-creation/config.json index 04ef90e..ca003f1 100644 --- a/components/cores/uapp-java/.project-creation/config.json +++ b/components/cores/uapp-java/.project-creation/config.json @@ -1,7 +1,6 @@ { "files": [ ".pre-commit-config.yaml", - ".project-creation/templates/.devcontainer/devcontainer.json", ".project-creation/templates/.vscode/launch.json" ] } diff --git a/components/cores/uapp-java/.project-creation/test/test_integration.py b/components/cores/uapp-java/.project-creation/test/test_integration.py index 9697cd0..f34b2bc 100644 --- a/components/cores/uapp-java/.project-creation/test/test_integration.py +++ b/components/cores/uapp-java/.project-creation/test/test_integration.py @@ -30,10 +30,6 @@ def is_file(path: str): def test_files_in_place(): os.chdir(os.environ["VELOCITAS_APP_ROOT"]) - # devcontainer - is_dir(".devcontainer") - is_file(".devcontainer/devcontainer.json") - # vscode project is_dir(".vscode") is_file(".vscode/launch.json") diff --git a/components/cores/uapp-java/.project-creation/templates/.devcontainer/devcontainer.json b/components/extensions/devcontainer-setup-java/src/.devcontainer/devcontainer.json similarity index 87% rename from components/cores/uapp-java/.project-creation/templates/.devcontainer/devcontainer.json rename to components/extensions/devcontainer-setup-java/src/.devcontainer/devcontainer.json index deff346..a20b3a3 100644 --- a/components/cores/uapp-java/.project-creation/templates/.devcontainer/devcontainer.json +++ b/components/extensions/devcontainer-setup-java/src/.devcontainer/devcontainer.json @@ -20,12 +20,12 @@ "ghcr.io/devcontainers-contrib/features/pre-commit:2": { "version": "3.5.0" }, - "ghcr.io/devcontainers/features/common-utils:2.3.2": {}, + "ghcr.io/devcontainers/features/common-utils:2.3.2": {} }, // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], - // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "", + "onCreateCommand": "bash .devcontainer/scripts/onCreateCommand.sh", + "postStartCommand": "bash .devcontainer/scripts/postStartCommand.sh", // Configure tool-specific properties. "customizations": { "vscode": { diff --git a/components/extensions/devcontainer-setup-java/src/.devcontainer/scripts/onCreateCommand.sh b/components/extensions/devcontainer-setup-java/src/.devcontainer/scripts/onCreateCommand.sh new file mode 100755 index 0000000..89df682 --- /dev/null +++ b/components/extensions/devcontainer-setup-java/src/.devcontainer/scripts/onCreateCommand.sh @@ -0,0 +1,62 @@ +#!/bin/bash +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# This program and the accompanying materials are made available under the +# terms of the Apache License, Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# SPDX-License-Identifier: Apache-2.0 + +# exit when any command fails +set -e + +# restart Docker connection if in Codespaces +# Workaround according to https://github.com/devcontainers/features/issues/671#issuecomment-1701754897 +if [ "${CODESPACES}" = "true" ]; then + sudo pkill dockerd && sudo pkill containerd + /usr/local/share/docker-init.sh +fi + +echo "#######################################################" +echo "### Run VADF Lifecycle Management ###" +echo "#######################################################" +# needed to get rid of old leftovers +sudo rm -rf ~/.velocitas +velocitas init +velocitas sync + +sudo chmod +x .devcontainer/scripts/*.sh +sudo chown -R $(whoami) $HOME + +if [ "${CODESPACES}" = "true" ]; then + echo "#######################################################" + echo "### Setup Access to Codespaces ###" + echo "#######################################################" + + # Remove the default credential helper + sudo sed -i -E 's/helper =.*//' /etc/gitconfig + + # Add one that just uses secrets available in the Codespace + git config --global credential.helper '!f() { sleep 1; echo "username=${GITHUB_USER}"; echo "password=${MY_GH_TOKEN}"; }; f' +fi + +git config --global --add safe.directory "*" + +echo "#######################################################" +echo "### Install Dependencies ###" +echo "#######################################################" +velocitas exec build-system install + +echo "#######################################################" +echo "### VADF package status ###" +echo "#######################################################" +velocitas upgrade --dry-run + +# Don't let container creation fail if lifecycle management fails +echo "Done!" diff --git a/components/extensions/devcontainer-setup-java/src/.devcontainer/scripts/postStartCommand.sh b/components/extensions/devcontainer-setup-java/src/.devcontainer/scripts/postStartCommand.sh new file mode 100755 index 0000000..e4474e1 --- /dev/null +++ b/components/extensions/devcontainer-setup-java/src/.devcontainer/scripts/postStartCommand.sh @@ -0,0 +1,80 @@ +#!/bin/bash +# Copyright (c) 2024 Contributors to the Eclipse Foundation +# +# This program and the accompanying materials are made available under the +# terms of the Apache License, Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0. +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# SPDX-License-Identifier: Apache-2.0 + +echo "#######################################################" +echo "### Auto-Upgrade CLI ###" +echo "#######################################################" + +ROOT_DIRECTORY=$( realpath "$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/../.." ) +DESIRED_VERSION=$(cat $ROOT_DIRECTORY/.velocitas.json | jq .cliVersion | tr -d '"') + +# Get installed CLI version +INSTALLED_VERSION=v$(velocitas --version | sed -E 's/velocitas-cli\/(\w+.\w+.\w+).*/\1/') + +if [ "$DESIRED_VERSION" = "$INSTALLED_VERSION" ]; then + echo "> Already up to date!" + exit 0 +else + echo "> Checking upgrade to $DESIRED_VERSION" +fi + +AUTHORIZATION_HEADER="" +if [ "${GITHUB_API_TOKEN}" != "" ]; then + AUTHORIZATION_HEADER="-H \"Authorization: Bearer ${GITHUB_API_TOKEN}\"" +fi + +if [ "$DESIRED_VERSION" = "latest" ]; then + CLI_RELEASES_URL=https://api.github.com/repos/eclipse-velocitas/cli/releases/latest +else + CLI_RELEASES_URL=https://api.github.com/repos/eclipse-velocitas/cli/releases/tags/${DESIRED_VERSION} +fi + +CLI_RELEASES=$(curl -s -L \ +-H "Accept: application/vnd.github+json" \ +-H "X-GitHub-Api-Version: 2022-11-28" \ +${AUTHORIZATION_HEADER} \ +${CLI_RELEASES_URL}) + +res=$? +if test "$res" != "0"; then + echo "the curl command failed with exit code: $res" + exit 0 +fi + +DESIRED_VERSION_TAG=$(echo ${CLI_RELEASES} | jq -r .name) + +if [ "$DESIRED_VERSION_TAG" = "null" ] || [ "$DESIRED_VERSION_TAG" = "" ]; then + echo "> Can't find desired Velocitas CLI version: $DESIRED_VERSION. Skipping Auto-Upgrade." + exit 0 +fi + +if [ "$DESIRED_VERSION_TAG" != "$INSTALLED_VERSION" ]; then + echo "> Upgrading CLI..." + if [[ $(arch) == "aarch64" ]]; then + CLI_ASSET_NAME=velocitas-linux-arm64 + else + CLI_ASSET_NAME=velocitas-linux-x64 + fi + CLI_INSTALL_PATH=/usr/bin/velocitas + CLI_DOWNLOAD_URL="https://github.com/eclipse-velocitas/cli/releases/download/${DESIRED_VERSION_TAG}/${CLI_ASSET_NAME}" + + echo "> Downloading Velocitas CLI from ${CLI_DOWNLOAD_URL}" + sudo curl -s -L ${CLI_DOWNLOAD_URL} -o "${CLI_INSTALL_PATH}" + sudo chmod +x "${CLI_INSTALL_PATH}" +else + echo "> Up to date!" +fi + +echo "> Using CLI: $(velocitas --version)" diff --git a/manifest.json b/manifest.json index 507fd59..d254ce2 100644 --- a/manifest.json +++ b/manifest.json @@ -32,6 +32,12 @@ "compatibleCores": [ "uprotocol-core-java" ], + "files": [ + { + "src": "components/extensions/devcontainer-setup-java/src", + "dst": "." + } + ], "mandatory": true } ]