1+ [CmdletBinding ()]
2+ Param (
3+ [Parameter (Mandatory = $False , Position = 0 )]
4+ [switch ]$PushToStrap
5+ )
6+
7+ function ZipFiles {
8+ [CmdletBinding ()]
9+ Param (
10+ [Parameter (Mandatory = $True , Position = 0 )]
11+ [string ]$ZipFilePath ,
12+
13+ [Parameter (ParameterSetName = " Directory" , Mandatory = $True , Position = 1 )]
14+ [string ]$SourceDir ,
15+
16+ [Parameter (ParameterSetName = " Files" , Mandatory = $True , Position = 1 )]
17+ [Array ]$SourceFiles ,
18+
19+ [Parameter (Mandatory = $False )]
20+ [switch ]$Force
21+
22+ )
23+ Add-Type - Assembly System.IO.Compression.FileSystem
24+ $CompressionLevel = [System.IO.Compression.CompressionLevel ]::Optimal
25+
26+ if (Test-Path $ZipFilePath ) {
27+ if ($Force ) {
28+ $Delete = Remove-Item $ZipFilePath
29+ } else {
30+ Throw " $ZipFilePath exists, use -Force to replace"
31+ }
32+ }
33+
34+ if ($SourceFiles ) {
35+ $TempZipFolder = ' newzip'
36+ $TempZipFullPath = " $ ( $env: temp ) \$TempZipFolder "
37+ $CreateFolder = New-Item - Path $env: temp - Name $TempZipFolder - ItemType Directory
38+ $Copy = Copy-Item $SourceFiles - Destination $TempZipFullPath
39+ $SourceDir = $TempZipFullPath
40+ }
41+
42+ [System.IO.Compression.ZipFile ]::CreateFromDirectory($SourceDir , $ZipFilePath , $CompressionLevel , $false )
43+
44+ $Cleanup = Remove-Item $TempZipFullPath - Recurse
45+ }
46+
47+
48+ $ScriptPath = Split-Path $ ($MyInvocation.MyCommand ).Path
49+ $ModuleName = Split-Path $ScriptPath - Leaf
50+
51+ $SourceDirectory = " src"
52+ $SourcePath = $ScriptPath + " \" + $SourceDirectory
53+ $CmdletPath = $SourcePath + " \" + " cmdlets"
54+ $HelperPath = $SourcePath + " \" + " helpers"
55+ $CsPath = $SourcePath + " \" + " cs"
56+ $OutputFile = $ScriptPath + " \" + " $ModuleName .psm1"
57+ $ManifestFile = $ScriptPath + " \" + " $ModuleName .psd1"
58+ $DllFile = $ScriptPath + " \" + " $ModuleName .dll"
59+ $CsOutputFile = $ScriptPath + " \" + " $ModuleName .cs"
60+
61+ # ##############################################################################
62+ # Create Manifest
63+ $ManifestParams = @ { Path = $ManifestFile
64+ ModuleVersion = ' 1.0'
65+ RequiredAssemblies = @ (" $ModuleName .dll" )
66+ Author = ' Brian Addicks'
67+ RootModule = " $ModuleName .psm1"
68+ PowerShellVersion = ' 4.0'
69+ RequiredModules = @ ()}
70+
71+ New-ModuleManifest @ManifestParams
72+
73+ # ##############################################################################
74+ #
75+
76+ $CmdletHeader = @'
77+ ###############################################################################
78+ ## Start Powershell Cmdlets
79+ ###############################################################################
80+
81+
82+ '@
83+
84+ $HelperFunctionHeader = @'
85+ ###############################################################################
86+ ## Start Helper Functions
87+ ###############################################################################
88+
89+
90+ '@
91+
92+ $Footer = @'
93+ ###############################################################################
94+ ## Export Cmdlets
95+ ###############################################################################
96+
97+ Export-ModuleMember *-*
98+ '@
99+
100+ $FunctionHeader = @'
101+ ###############################################################################
102+ #
103+ '@
104+
105+ # ##############################################################################
106+ # Start Output
107+
108+ $CsOutput = " "
109+
110+ # ##############################################################################
111+ # Add C-Sharp
112+
113+ $AssemblyRx = [regex ] ' ^using\ .+?;'
114+ $NameSpaceStartRx = [regex ] " namespace $ModuleName {"
115+ $NameSpaceStopRx = [regex ] ' ^}$'
116+
117+ $Assemblies = @ ()
118+ $CSharpContent = @ ()
119+
120+ $c = 0
121+ foreach ($f in $ (ls $CsPath )) {
122+ foreach ($l in (gc $f.FullName )) {
123+ $AssemblyMatch = $AssemblyRx.Match ($l )
124+ $NameSpaceStartMatch = $NameSpaceStartRx.Match ($l )
125+ $NameSpaceStopMatch = $NameSpaceStopRx.Match ($l )
126+
127+ if ($AssemblyMatch.Success ) {
128+ $Assemblies += $l
129+ continue
130+ }
131+
132+ if ($NameSpaceStartMatch.Success ) {
133+ $AddContent = $true
134+ continue
135+ }
136+
137+ if ($NameSpaceStopMatch.Success ) {
138+ $AddContent = $false
139+ continue
140+ }
141+
142+ if ($AddContent ) {
143+ $CSharpContent += $l
144+ }
145+ }
146+ }
147+
148+ # $Assemblies | Select -Unique | sort -Descending
149+
150+ $CSharpOutput = $Assemblies | Select - Unique | sort - Descending
151+ $CSharpOutput += " namespace $ModuleName {"
152+ $CSharpOutput += $CSharpContent
153+ $CSharpOutput += ' }'
154+
155+ $CsOutput += [string ]::join(" `n " , $CSharpOutput )
156+ $CsOutput | Out-File $CsOutputFile - Force
157+
158+
159+ Add-Type - ReferencedAssemblies @ (
160+ ([System.Reflection.Assembly ]::LoadWithPartialName(" System.Xml" )).Location,
161+ ([System.Reflection.Assembly ]::LoadWithPartialName(" System.Web" )).Location,
162+ ([System.Reflection.Assembly ]::LoadWithPartialName(" System.Xml.Linq" )).Location
163+ ) - OutputAssembly $DllFile - OutputType Library - TypeDefinition $CsOutput
164+
165+ # ##############################################################################
166+ # Add Cmdlets
167+
168+ $Output = $CmdletHeader
169+
170+ foreach ($l in $ (ls $CmdletPath )) {
171+ $Contents = gc $l.FullName
172+ Write-Verbose $l.FullName
173+ $Output += $FunctionHeader
174+ $Output += $l.BaseName
175+ $Output += " `r`n`r`n "
176+ $Output += [string ]::join(" `n " , $Contents )
177+ $Output += " `r`n`r`n "
178+ }
179+
180+
181+ # ##############################################################################
182+ # Add Helpers
183+
184+ $Output += $HelperFunctionHeader
185+
186+ foreach ($l in $ (ls $HelperPath )) {
187+ $Contents = gc $l.FullName
188+ $Output += $FunctionHeader
189+ $Output += $l.BaseName
190+ $Output += " `r`n`r`n "
191+ $Output += [string ]::join(" `n " , $Contents )
192+ $Output += " `r`n`r`n "
193+ }
194+
195+ # ##############################################################################
196+ # Add Footer
197+
198+ $Output += $Footer
199+
200+ # ##############################################################################
201+ # Output File
202+
203+ $Output | Out-File $OutputFile - Force
204+
205+ if ($PushToStrap ) {
206+ $FilesToZip = ls " $PSScriptRoot \$ModuleName *" - Exclude * .zip
207+ $CreateZip = ZipFiles - ZipFilePath " $PSScriptRoot \$ModuleName .zip" - SourceFiles $FilesToZip - Force
208+ $StageFolder = " \\vmware-host\Shared Folders\Dropbox (Personal)\strap\stages\$ModuleName \"
209+ $Copy = Copy-Item " $PSScriptRoot \$ModuleName .zip" $StageFolder - Force
210+ }
0 commit comments