-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_script.ps1
More file actions
118 lines (89 loc) · 4.11 KB
/
prepare_script.ps1
File metadata and controls
118 lines (89 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# make sure you have set the execution policy to Unrestricted
# Set-ExecutionPolicy Unrestricted
[CmdletBinding()] Param (
[Parameter (Mandatory,HelpMessage='You must enter the new component name (without .dll extension)')] [string] $componentName,
[Parameter (Mandatory,HelpMessage='You must enter the package name (with NET suffix)')] [string] $packageName
)
$BaseDirectory = ".\"
# JSON configuration filename to use
$BaseConfig = "prepare_settings.json"
$Config = ""
# Load and parse the JSON configuration file
try {
$Config = Get-Content "$BaseDirectory$BaseConfig" -Raw -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | ConvertFrom-Json -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
} catch {
Throw "The Base configuration file is missing!!"
}
# Check the configuration
if (!($Config)) {
Throw "The Base configuration file is missing!"
}
$template = "_ComNameToReplace_"
$package = "_PackageNameToReplace_"
$vsPath = ($Config.Basic.VisualStudioPath)
$srcPath = $template + ".dll"
$newPath = $componentName + ".dll"
$message = "Processing files of " + $componentName
cls
Write-Progress -CurrentOperation $message ( "Please wait a moment ..." )
if (!(Test-Path $vsPath)) {
Throw "Path not exists: " + $vsPath
}
# copy whole template to a new location
New-Item -ItemType directory -Path "$newPath" -Force | Out-Null
Copy-Item -Path "$srcPath\*" -Destination "$newPath\" -Recurse -Force
# remove unnecessary packages
Remove-Item -Path "$newPath\packages\*" -Recurse -Force
# temporary name replacement
$filenames = @(Get-ChildItem -Path "$newPath\*" -Recurse -File -Exclude "Component.snk" | % { $_.FullName })
# generate needed GUIDs
$guidStr1 = [guid]::NewGuid().Guid.ToUpper() <# Solution base GUID #>
$guidStr2 = [guid]::NewGuid().Guid.ToUpper() <# Project MAIN GUID #>
$guidStr3 = [guid]::NewGuid().Guid.ToUpper() <# Project TEST GUID #>
$guidStr4 = [guid]::NewGuid().Guid.ToUpper() <# Component GUID #>
$guidStr5 = [guid]::NewGuid().Guid.ToUpper() <# Interface GUID #>
$guidStr6 = [guid]::NewGuid().Guid.ToUpper() <# Typelib GUID #>
$counter = 0
$maxCount = $filenames.Count
foreach ($file in $filenames)
{
$counter++
Write-Progress -Id 1 -Activity "Updating" -Status 'Progress' -PercentComplete (100*$counter/$maxCount) -CurrentOperation $message
Set-ItemProperty $file -name IsReadOnly -value $false
(Get-Content $file) |
Foreach-object { $_ -replace $template , $componentName } |
<# Template static GUIDs #>
Foreach-object { $_ -replace $Config.Internal.SolutionBaseGUID , $guidStr1 } |
Foreach-object { $_ -replace $Config.Internal.ProjectMainGUID , $guidStr2 } |
Foreach-object { $_ -replace $Config.Internal.ProjectTestGUID , $guidStr3 } |
Foreach-object { $_ -replace $Config.Internal.ComponentGUID , $guidStr4 } |
Foreach-object { $_ -replace $Config.Internal.ComponentGUID , $guidStr5 } |
Foreach-object { $_ -replace $Config.Internal.TypelibGUID , $guidStr6 } |
Set-Content $file
if($File -Like "*.bat") {
(Get-Content $file) |
Foreach-object { $_ -replace $package, $packageName } |
Set-Content $file
}
}
# final work
Get-ChildItem -Path "$newPath\*" -Recurse -File -Exclude "Component.snk" | Rename-Item -NewName { $_.Name.replace($template, $componentName) }
cls
# rebuild dependencies
$fileExe = $vsPath + "\MSBuild\Current\Bin\MSBuild.exe"
if (!(Test-Path $fileExe -PathType Leaf)) {
Throw "File not exists: " + $fileExe
}
& $fileExe -m -t:restore -p:RestorePackagesConfig=true $newPath
# move secret file
$sourceFile = "$newPath\devel_secret.json"
$destinationFile = "$env:userprofile\AppData\Roaming\devel_secret.json"
if (!(Test-Path $destinationFile)) {
Move-Item -Path $sourceFile -Destination $destinationFile -Confirm:$false
}
else {
Move-Item -Path $sourceFile -Destination $destinationFile -Confirm:$true
}
'*' * 80
Write-Host "All done!"
Write-Host "You can move" $componentName".dll folder into" $packageName "package folder."