使用播放框架将"git describe"的输出放在模板中? [英] Putting output of 'git describe' in template using play framework?

查看:73
本文介绍了使用播放框架将"git describe"的输出放在模板中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的视图中显示"git describe"的输出.我是否需要编写一个插件来更新值并将其设置为整个应用程序?还是有更简单的方法来做到这一点?

I'd like to show the output of 'git describe' in my view. Do I need to write a plug-in that updates a value and sets it application wide? Or is there an easier way to do this?

推荐答案

我刚刚阅读了有关play模块的内容,并决定编写一个( https://github.com/killdashnine/play-git-plugin )查看我是否可以解决我的问题:

I've just read about play modules and decided to write one (https://github.com/killdashnine/play-git-plugin) to see if I could solve my problem:

import java.io.BufferedReader;
import java.io.InputStreamReader;

import play.Logger;
import play.Play;
import play.PlayPlugin;

public class GitPlugin extends PlayPlugin {

        private static String GIT_PLUGIN_PREFIX = "GIT plugin: ";

        @Override
        public void onApplicationStart()  {
                Logger.info(GIT_PLUGIN_PREFIX + "executing 'git describe'");
                final StringBuffer gitVersion = new StringBuffer();
                try {
                        final Process p = Runtime.getRuntime().exec("git describe"); 
                        final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 

                        // wait for process to complete
                        p.waitFor(); 

                        // read the output
                        String line = reader.readLine(); 
                        while(line != null) { 
                                gitVersion.append(line); 
                                line = reader.readLine(); 
                        } 
                }
                catch(Exception e) {
                        Logger.error(GIT_PLUGIN_PREFIX + "unable to execute 'git describe'");
                }

                // set a property for this value
                Play.configuration.setProperty("git.revision", gitVersion.toString());

                Logger.info(GIT_PLUGIN_PREFIX + "revision is " + gitVersion.toString());
        }
}

这将导致:

12:14:46,508 INFO  ~ GIT plugin: executing 'git describe'
12:14:46,513 INFO  ~ GIT plugin: revision is V0-beta-7-gac9af80

在我的控制器中:

    @Before
    static void addDefaults() {
        renderArgs.put("version", Play.configuration.getProperty("git.revision"));
     }

当然,这不是很容易移植,可以改进.可能的改进是允许通过配置文件中的设置运行自定义命令.

Of course this is not very portable and could be improved. Possible improvement would be to allow a custom command to be run via a settings in your configuration file.

这篇关于使用播放框架将"git describe"的输出放在模板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆