使用git describe嵌入laravel应用程序版本

时间:2015-06-01 16:43:22

标签: git version laravel-5

我想找到一种优雅的方式来嵌入输出:

git describe --long > app\version.tmp

到我的Laravel 5应用程序。请注意,我不需要一种自动化命令的方法。

我想过将输出重定向到一个文件,然后在Laravel的自定义配置文件中读取它。但它对我来说并不优雅。

还有其他建议吗?

注1:

这是我想要使用的:

<?php

return [

    'version' => preg_replace('/[^A-Za-z0-9.\-]/', '',file_get_contents(app_path() . '\version.tmp')),
];

2 个答案:

答案 0 :(得分:1)

对于我的应用程序,我有一个“关于”对话框,用户可以通过菜单访问该对话框。 “关于”对话框显示当前版本号和短git提交哈希值。

我将我的版本信息存储在我的.env文件中,分为两个值:APP_VERSIONAPP_HASH。这些值将呈现到对话框的HTML中。

我通过简短的BASH脚本更新这些值(见下文)。我有一个构建过程,准备应用程序发布(从less重建CSS,最小化我的CSS和JS,复制生产.env文件,以及其他一些东西),所以我在准备时执行此脚本发布应用程序的更新。

这是bash脚本:

#!/bin/bash

# store the production .env file outside my development environment. It will
# be copied when I build the application
#
PRODUCTION=/path/to/production/.env

# store the local (development) .env file in the development environment
# 
LOCAL=./.env

# get the git hash value
# 
CURRENT_GIT_HASH=$(git rev-parse --short HEAD)

# get the new version number from the command line
# 
NEW_VERSION="$1"

# get the old version number from the production .env file
# 
OLD_VERSION=$(grep -P "^APP_VERSION=[0-9]+\.[0-9]+\.[0-9]+$" "$PRODUCTION" | grep -Po "([0-9]+\.[0-9]+\.[0-9]+)")

echo "Current version = $OLD_VERSION"

# check to see if the version number is valid
# 
CHECK=$(echo "$NEW_VERSION" | grep -P "^[0-9]+\.[0-9]+\.[0-9]+$")

# do we have a new version number? if so, check to see if it's changed
# 
if [ -n "$NEW_VERSION" ]; then

    # do we have a valid version number? if not, let the user know
    # 
    if [ -z "$CHECK" ]; then
        echo "Invalid version number: $NEW_VERSION"
    else
        echo "Bumping version to $NEW_VERSION ($CURRENT_GIT_HASH)"

        # update the version number
        #
        sed -ri "s/APP_VERSION=[0-9]+\.[0-9]+\.[0-9]+/APP_VERSION=$NEW_VERSION/" "$PRODUCTION"
        sed -ri "s/APP_VERSION=[0-9]+\.[0-9]+\.[0-9]+/APP_VERSION=$NEW_VERSION/" "$LOCAL"

        # update the version number
        #
        sed -ri "s/APP_HASH=[0-9a-f]+/APP_HASH=$CURRENT_GIT_HASH/" "$PRODUCTION"
        sed -ri "s/APP_HASH=[0-9a-f]+/APP_HASH=$CURRENT_GIT_HASH/" "$LOCAL"

        # tag the current commit with the version number
        #
        git tag -a "bump-$NEW_VERSION" -m "bump version from $OLD_VERSION to $NEW_VERSION"
    fi
fi

这可能比你正在做的要复杂得多,但它的好处是版本#&amp; hash可通过标准env()辅助函数获得。

答案 1 :(得分:0)

与Kryten的回答一样,我在这里使用的是PowerShell:

function setConfigValue( $file, $key, $value ) {
    $content = Get-Content $file
    if ( $content -match "^$key\s*=" ) {
        $content -replace "^$key\s*=.*", "$key=$value" |
        Set-Content $file     
    } else {
        Add-Content $file "$key=$value"
    }
}

$ver = &"git" describe --long | Out-String

setConfigValue ".\.env" "APP_VERSION" $ver