是否可以在我声明后重新评估此处的字符串?

Is it possible to reevalute here strings after I declare them?

我正在尝试使用 PowerShell 执行小型代码生成任务。我希望脚本生成一些 java 类 和接口。

在脚本中我想在这里声明一个字符串变量。例如:

$controllerContent = @"
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Controller
@EnableWebMvc
public class $completeName {

}
"@

声明后我想将它传递给计算变量 $completeName 的函数。但我不知道替换字符串中变量的正确方法是什么。我必须使用 -replace 吗?或者有其他方法吗?

我通常使用 格式 字符串来完成此类任务。您需要做的就是将 $completeName 替换为 {0} 并且您可以随时格式化字符串:

$controllerContent = @"
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Controller
@EnableWebMvc
public class {0} {{

}}
"@

现在您可以重命名 class 使用:

$controllerContent -f 'MyController'

注意:唯一的缺点是,您需要像我的示例中所示那样转义大括号。因此,您可以选择使用 格式 字符串还是 -replace.

我通常使用类似上面答案的格式字符串,但您也可以将其包装在脚本块中并在需要时调用。这不需要对文本本身进行任何修改。例如:

$completeName = "HelloWorld"

$controllerContent = { @"
@Controller
@EnableWebMvc
public class $completeName {

}
"@ }

& $controllerContent
#Save the string:  $str = & $controlledContent

输出:

@Controller
@EnableWebMvc
public class HelloWorld {

}

更改变量:

$completeName = "HelloNorway"

& $controllerContent

输出:

@Controller
@EnableWebMvc
public class HelloNorway {

}

感谢大家的帮助。对于任何感兴趣的人,这是我想出的脚本...

Param(
    [Parameter(Mandatory=$True)]
    [string]$name
)
$sourceDir = "..."
$classNameGeneration = {param($name,$type)Return "$name"+  ($type.Substring(0,1)).ToUpper()+($type.Substring(1))}
$interfaceNameGeneration = {param($name,$type)Return "$name"+($type.Substring(0,1)).ToUpper()+($type.Substring(1))+"IF"}

$controllerName = & $classNameGeneration -name $name -type controller
$serviceInterfaceName = & $interfaceNameGeneration -name $name -type service
$serviceName = & $classNameGeneration -name $name -type service
$daoInterfaceName = & $interfaceNameGeneration -name $name -type dao
$daoName = & $classNameGeneration -name $name -type dao

function create($name, $type, $content) {
    $dir = $sourceDir+"$type"
    $fileName = $name+".java"
    $filePath = "$dir$fileName"
    cd $dir
    New-Item $filePath -ItemType file
    Set-Content $filePath $content
}

"[INFO]create controller class..."
$controllerContent = @"
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.beans.factory.annotation.Autowired;

@Controller
@EnableWebMvc
public class {0} {{

@Autowired
private {1} service;

}}
"@
$controllerContent = $controllerContent -f $controllerName,$serviceInterfaceName
create $controllerName controller $controllerContent  

"[INFO]Create service interface..."
$serviceInterfaceContent = @"
package service;

public interface {0} {{

}}
"@
$serviceInterfaceContent = $serviceInterfaceContent -f $serviceInterfaceName
create $serviceInterfaceName service $serviceInterfaceContent 

"[INFO]Create service class..."
$serviceContent = @"
package service;

import org.springframework.beans.factory.annotation.Autowired;

public class {0} implements {1} {{

@Autowired
private {2} dao;
}}
"@
$serviceContent = $serviceContent -f $serviceName,$serviceInterfaceName,$daoInterfaceName
create $serviceName service $serviceContent 
"[INFO]Create dao interface..."
$daoInterfaceContent = @"
package dao;

public interface {0} {{

}}
"@
$daoInterfaceContent = $daoInterfaceContent -f $daoInterfaceName
create $daoInterfaceName dao $daoInterfaceContent
"[INFO]Create dao class..."
$daoContent = @"
package dao;

public class {0} implements {1} {{

}}
"@
$daoContent = $daoContent -f $daoName,$daoInterfaceName
create $daoName dao $daoContent