有什么办法可以在Groovy中自动设置字符串中的Windows路径? [英] Is there any way to automatically setting windows path in a string in groovy?

查看:76
本文介绍了有什么办法可以在Groovy中自动设置字符串中的Windows路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目根目录是:

D:/Project/Node_Project

我正在使用gradle插件在项目根目录中临时安装nodejs,以便在构建thoject时可以在项目中运行一些nodejs命令。插件如下:

I am using a gradle plugin to install nodejs temporarily in my project root directory so that some nodejs command can run in the project while the thoject builds. The plugin is as below:

plugins {
    id "com.github.node-gradle.node" version "2.2.4"
}
node {
    download = true
    version = "10.10.0"
    distBaseUrl = 'https://nodejs.org/dist'
    workDir = file("${project.buildDir}/nodejs")
}

因此,nodejs已安装在项目中的以下位置:

So, nodejs is getting installed inside the project in the location:

D:/Project/Node_Project/build/nodejs/node-v10.10.0-win-x64

现在,我正在使用 .execute(String []要在环境变量处设置的路径,要执行的文件的字符串路径,位于项目根目录中)方法,用于运行具有节点依赖关系的Windows命令。下面的代码:

Now, I am using a .execute(String[] "path to set at environment variable", String path of file to be executed which is in the project root directory) method to run a windows command with node dependency. Code below:

cmd = "node connect.js"
def process = cmd.execute(["PATH=${project.projectDir}/build/nodejs/node-v10.10.0-win-x64"],null)

在上述.execute方法中,有没有一种方法可以自动填充 build / nodejs / node-v10.10.0-win-x64 部分字符串,而不是将其硬编码到方法中?
类似的东西:

In the above .execute method, is there a way to auto-populate the "build/nodejs/node-v10.10.0-win-x64" part of the string instead of hardcoding it into the method? Something like:

def process = cmd.execute(["PATH=${project.projectDir}/.*"],null)

.execute方法的语法:
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html#execute(java.lang.String [],%20java.io.File)

Syntax of .execute method: https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html#execute(java.lang.String[],%20java.io.File)

所有代码都在 build.gradle文件中。

All the codes are inside "build.gradle" file. Please help!

推荐答案

我问为什么你不只是编写 NodeTask ,但我了解您例如,您可以在后台运行它,而您不能这样做。

I asked why you don't just write a task of type NodeTask, but I understand that you like to run a it in the background, which you can't do with that.

您可以列出目录的内容,并将其用作命令的一部分。但是,您也可以从插件提供的扩展名中获取它。

You could list the content of a directory and use that as part of the command. But you could also just grab it from the extension provided by the plugin.

这没有记录,并且可能会在该插件的将来版本中中断,但是您可以做一些事情像这样(Groovy DSL):

This is not documented and it might break in future releases of the plugin, but you can do something like this (Groovy DSL):

task connectJS {
    dependsOn nodeSetup
    doFirst {
        def connectProcess = "$node.variant.nodeExec $projectDir/src/js/connect.js".execute()
        // Blocking readers (if async, pipe to a log file instead)
        connectProcess.in.eachLine { logger.info(it) }
        connectProcess.err.eachLine { logger.err(it) }
    }
}

这篇关于有什么办法可以在Groovy中自动设置字符串中的Windows路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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