我声明它们之后是否可以在这里重新评估字符串?

时间:2016-04-23 09:08:30

标签: function powershell parameters herestring

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

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

$controllerContent = @"
package controller;

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

@Controller
@EnableWebMvc
public class $completeName {

}
"@

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

3 个答案:

答案 0 :(得分:4)

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

$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 {

}

答案 1 :(得分:3)

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

$controllerContent = @"
package controller;

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

@Controller
@EnableWebMvc
public class {0} {{

}}
"@

现在您可以使用以下命令重命名该类:

$controllerContent -f 'MyController'

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

答案 2 :(得分:1)

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

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