vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.52k stars 2.15k forks source link

V Version Manager #21797

Open enghitalo opened 2 days ago

enghitalo commented 2 days ago

Describe the feature

Something like sdkman or nvm to avoid compatibility problems with packages and in production softwares. maybe: v.mod

Module {
    name: 'v2048',
    description: 'A simple 2048 game written in V.',
    version: '0.0.2',
    license: 'MIT'
    repo_url: 'https://github.com/spytheman/v2048',
    dependencies: [],
+   v_version: '>= 0.4.6 && <= weekly.2024.23'
or
+   v_version: 'latest'
}

.vvmrc

0.4.6

or

latest

Use Case

Build to production

Proposed Solution

When running v <project> or v run <project> it must the get the version in .vvmrc (if it exists), and then run the project with that version of V. If the version is not found in .vvmrc then it should use the latest version of V installed on the system or the in use version of V. If the version in .vvmrc is not installed on the system, it should install that version and then run the project with that version of V, Maybe installing it at /usr/lib/v/<version>/bin/v and/or /usr/local/bin/v<version>.

Other Information

It is need verify if will need have some change how vlib/v/util/version/version.v, cmd/tools/vup.v and @VCURRENTHASH works.

Acknowledgements

Version used

V 0.4.6 209063f

Environment details (OS name and version, etc.)

all

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

enghitalo commented 2 days ago

Something like

#!/bin/bash

# Function to print an error message and exit
function error_exit {
    echo "$1" 1>&2
    exit 1
}

# Function to get the latest installed version of V
function get_latest_installed_version {
    local latest_version=$(ls /usr/lib/v | sort -V | tail -n 1)
    if [[ -z "$latest_version" ]]; then
        error_exit "No V versions found. Please install V."
    fi
    echo "$latest_version"
}

# Function to install the specified version of V
function install_v_version {
    local version=$1
    echo "Installing V version $version..."

    wget "https://github.com/vlang/v/releases/download/$version/v_linux.zip" -O /tmp/v-$version.zip
    mkdir -p "/usr/lib/v/$version/bin"
    unzip -jo /tmp/v-$version.zip v/v -d /usr/lib/v/$version/bin
    ln -sf "/usr/lib/v/$version/bin/v" "/usr/local/bin/v$version"
}

# Determine the project directory
PROJECT_DIR=$(pwd)
if [[ -z "$1" ]]; then
    error_exit "Please specify a project directory or file."
fi

# Check if the specified argument is a directory or file
if [[ -d "$1" ]]; then
    PROJECT_DIR="$1"
elif [[ -f "$1" ]]; then
    PROJECT_DIR=$(dirname "$1")
else
    error_exit "Invalid project directory or file specified."
fi

# Look for .vvmrc file
VVMRC_FILE="$PROJECT_DIR/.vvmrc"
if [[ -f "$VVMRC_FILE" ]]; then
    V_VERSION=$(cat "$VVMRC_FILE" | tr -d '[:space:]')
else
    V_VERSION=$(get_latest_installed_version)
fi

# Check if the specified version is installed
if [[ ! -x "/usr/lib/v/$V_VERSION/bin/v" ]]; then
    install_v_version "$V_VERSION"
fi

# Run the project with the specified version of V
/usr/lib/v/$V_VERSION/bin/v "$@"
JalonSolov commented 2 days ago

I am still against adding things like this until 1.0 or after.

At present, people are still better off always using the latest version of V, even if there was a change that means they need to update their code - often those changes are necessary to avoid more serious issues.