4

I'm trying to use PowerShell for a small code generation task. I want the script to generate some java classes and interfaces.

In the script I want to declare a here string variable. For example:

$controllerContent = @"
package controller;

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

@Controller
@EnableWebMvc
public class $completeName {

}
"@

After the declaration I want to pass it to function where I calculate the variable $completeName. But I don't what is the proper way to replace the variable in my string. Do I have to use -replace? Or is there some other way?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Patrick
  • 585
  • 8
  • 22

3 Answers3

4

I usually use a format-string like the answer above, but you could also wrap it in scriptblock and invoke when needed. That wouldn't require any modification to he text itself. Ex:

$completeName = "HelloWorld"

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

}
"@ }

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

Output:

@Controller
@EnableWebMvc
public class HelloWorld {

}

Change the variable:

$completeName = "HelloNorway"

& $controllerContent

Output:

@Controller
@EnableWebMvc
public class HelloNorway {

}
Frode F.
  • 52,376
  • 9
  • 98
  • 114
3

I usually use a format string for such tasks. All you need to do is to replace $completeName with {0}and you can format the string any time:

$controllerContent = @"
package controller;

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

@Controller
@EnableWebMvc
public class {0} {{

}}
"@

Now you can rename the class using:

$controllerContent -f 'MyController'

Note: The only downsite to this is, that you need to escape the curly brackets as shown in my example. So its your choice whether you use a format string or -replace.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
1

Thanks for the help people. For anyone interested, this is the script i came up with...

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
Patrick
  • 585
  • 8
  • 22
  • Some Imports have to be added and i might use this to create test classes also... powershell is really fun. – Patrick Apr 23 '16 at 13:53