Skip to content

Latest commit

 

History

History
157 lines (102 loc) · 5.41 KB

File metadata and controls

157 lines (102 loc) · 5.41 KB

Step 3: Add Features

You can further customize your codespace by adding container feature, VS Code extensions, VS Code settings, host requirements, and much more.

Let's add the GitHub CLI, extensions to run the python program using VS Code, and a custom script to install some packages when first creating the Codespace.

⌨️ Activity: Add support for the Python

  1. In VS Code, open the Command Palette (CTRL+SHIFT+P) and select the below command.

    Codespaces: Add Dev Container Configuration Files...
    vs code configure dev container command
  2. Select the option Modify your active configuration....

  3. In the list of features, search for and select Python from devcontainers.

    • Instead of the defaults, pick Configure Options.
    • Leave Install Tools set to true.
    • Select Python version: 3.10
  4. Navigate to and open the .devcontainer/devcontainer.json file.

  5. Verify a new entry similar to the below was added.

    "features": {
       "ghcr.io/devcontainers/features/python:1": {
          "installTools": true,
          "version": "3.10"
       }
    },

⌨️ Activity: Add VS Code extensions

  1. In the left navigation, select the Extension tab.

    vs code extensions tab
  2. Search for python and find entries for Python and Python Debugger.

    python extensions for vs code
  3. Right click on each entry and select the Add to devcontainer.json option.

    add to devcontainer config button
  4. Navigate to and open the .devcontainer/devcontainer.json file.

  5. Verify a new entry similar to the below was added.

    "customizations": {
       "vscode": {
          "extensions": [
             "ms-python.python",
             "ms-python.debugpy"
          ]
       }
    },

⌨️ Activity: Add a custom script

The Dev Container specification provides multiple locations to run lifecycle scripts to further customize your Codespace. Let's add the postCreateCommand which runs one time after initial build (or rebuild).

  1. Use the VS Code file explorer to create a script file with the below name.

    .devcontainer/postCreate.sh

    Alternately, run the below terminal command to create it.

    touch .devcontainer/postCreate.sh
  2. Make the script executable by running the below terminal command.

    chmod +x .devcontainer/postCreate.sh
  3. Open the .devcontainer/postCreate.sh file and add the following code, which will install an animation of a steam locomotive.

    #!/bin/bash
    
    sudo apt-get update
    sudo apt-get install sl
    echo "export PATH=\$PATH:/usr/games" >> ~/.bashrc
    echo "export PATH=\$PATH:/usr/games" >> ~/.zshrc
  4. Navigate to and open the .devcontainer/devcontainer.json file.

  5. Create the postCreateCommand entry at the same level (top level) as features, and customizations.

    "postCreateCommand": ".devcontainer/postCreate.sh"
  6. With our new configuration finished, let's commit the changes. Use VS Code's source control tools or the below terminal command.

    git add '.devcontainer/devcontainer.json'
    git add '.devcontainer/postCreate.sh'
    git commit -m 'feat: Add features, extensions, and postCreate script'
    git push
  7. Open the VS Code Command Palette (CTRL+Shift+P) and run the Codespaces: Rebuild Container command. Select the Rebuild option. A full build is not necessary.

    rebuild codespace command
  8. Wait a few minutes for the Codespace to rebuild and VS Code to reconnect.

  9. With the customizations committed, Mona will begin checking your work. Give her a moment to provide feedback and the next learning steps.

Tip

You can also configure your account to install dotfiles, allowing you to combine personal configurations with the project's configuration.

⌨️ Activity: (optional) Verify our customizations

Now that you've rebuilt the codespace, let's verify the python extension, python version, and custom script were adjusted correctly in the Codespace.

  1. Ensure you are in the Codespace.

  2. In the left sidebar, click the extensions tab and verify that the Python extensions are installed and enabled.

    python extensions for vs code
  3. In the left sidebar, select Run and Debug tab and then press the Start Debugging icon. VS Code will open the lower panel and show the run logs.

    run and debug tab pointing to start button
  4. In the lower panel, switch to the TERMINAL tab.

  5. Run the following command to show the installed version of Python. Notice the others are not installed.

    node --version
    dotnet --version
    python --version
    gh --version
  6. Run the following command to show the steam locomotive animation.

    sl