Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 在我声明字符串后,是否可以在此处重新计算它们的值?_Function_Powershell_Parameters_Herestring - Fatal编程技术网

Function 在我声明字符串后,是否可以在此处重新计算它们的值?

Function 在我声明字符串后,是否可以在此处重新计算它们的值?,function,powershell,parameters,herestring,Function,Powershell,Parameters,Herestring,我正在尝试将PowerShell用于一个小的代码生成任务。我想让脚本生成一些java类和接口 在脚本中,我想声明一个here字符串变量。例如: $controllerContent = @" package controller; import org.springframework.stereotype.Controller; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Controller

我正在尝试将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
。但是我不知道替换字符串中变量的正确方法是什么。是否必须使用
-替换
?或者有其他方法吗?

我通常使用格式字符串来执行此类任务。您只需将
$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

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

$completeName = "HelloWorld"

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

}
"@ }

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

@Controller
@EnableWebMvc
public class HelloWorld {

}
@Controller
@EnableWebMvc
public class HelloNorway {

}
更改变量:

$completeName = "HelloNorway"

& $controllerContent
输出:

@Controller
@EnableWebMvc
public class HelloWorld {

}
@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

必须添加一些导入,我可能会使用它来创建测试类。。。powershell真的很有趣。