@@ -46,116 +46,98 @@ param(
4646)
4747
4848function Parse-URLForFile {
49- [cmdletbinding ()]
50- param ($URL )
49+ [cmdletbinding ()]
50+ param ($URL )
5151 # This will need work. Assume leaf is file. If CGI exists in leaf, assume it is after the file
5252 $FileName = $URL.split (' /' )[-1 ]
53- if ($FileName -match ' \?' )
54- {
53+ if ($FileName -match ' \?' ) {
5554 $FileName.split (' ?' )[0 ]
5655 }
57- else
58- {
56+ else {
5957 $FileName
6058 }
6159 Write-Verbose " Parsed file name [$FileName ] from `$ URL"
6260}
6361
6462# Extract data from Dependency
65- $DependencyName = $Dependency.DependencyName
66- $Name = $Dependency.Name
67- $Target = $Dependency.Target
68- $Source = $Dependency.Source
69-
70- # Pick the URL
71- if ($Source )
72- {
73- $URL = $Source
74- }
75- else
76- {
77- $URL = $DependencyName
78- }
79- Write-Verbose " Using URL: $URL "
63+ $DependencyName = $Dependency.DependencyName
64+ $Name = $Dependency.Name
65+ $Target = $Dependency.Target
66+ $Source = $Dependency.Source
67+
68+ # Pick the URL
69+ if ($Source ) {
70+ $URL = $Source
71+ }
72+ else {
73+ $URL = $DependencyName
74+ }
75+ Write-Verbose " Using URL: $URL "
8076
8177# Act on target path....
82- $ToInstall = $False # Anti pattern
83- $TargetParent = Split-Path $Target - Parent
84- $PathToAdd = $Target
85- if ( (Test-Path $TargetParent ) -and -not (Test-Path $Target ))
86- {
87- # They gave us a full path, don't parse the file name, use this!
88- $Path = $Target
89- $ToInstall = $True
90- Write-Verbose " Found parent [$TargetParent ], not target [$Target ], assuming this is target file path"
78+ $ToInstall = $False # Anti pattern
79+ $TargetParent = Split-Path $Target - Parent
80+ $PathToAdd = $Target
81+ if ( (Test-Path $TargetParent ) -and -not (Test-Path $Target )) {
82+ # They gave us a full path, don't parse the file name, use this!
83+ $Path = $Target
84+ $ToInstall = $True
85+ Write-Verbose " Found parent [$TargetParent ], not target [$Target ], assuming this is target file path"
86+ }
87+ elseif (Test-Path $Target - PathType Leaf) {
88+ # File exists. We should download to temp spot, compare hashes, take action as appropriate.
89+ # For now, skip the file.
90+ Write-Verbose " Skipping existing file [$Target ]"
91+ if ($PSDependAction -contains ' Test' ) {
92+ return $True
9193 }
92- elseif (Test-Path $Target - PathType Leaf)
93- {
94- # File exists. We should download to temp spot, compare hashes, take action as appropriate.
95- # For now, skip the file.
96- Write-Verbose " Skipping existing file [$Target ]"
97- if ($PSDependAction -contains ' Test' )
98- {
99- return $True
100- }
101- $PathToAdd = Split-Path $Target - Parent
94+ $PathToAdd = Split-Path $Target - Parent
95+ }
96+ elseif (-not (Test-Path $Target )) {
97+ # They gave us something that doesn't look like a new container for a new or existing file. Wat?
98+ Write-Error " Could not find target path [$Target ]"
99+ if ($PSDependAction -contains ' Test' ) {
100+ return $False
102101 }
103- elseif (-not (Test-Path $Target ))
104- {
105- # They gave us something that doesn't look like a new container for a new or existing file. Wat?
106- Write-Error " Could not find target path [$Target ]"
107- if ($PSDependAction -contains ' Test' )
108- {
109- return $False
110- }
102+ }
103+ else {
104+ Write-Verbose " [$Target ] is a container, creating path to file"
105+ # We have a target container, now find the name
106+ If ($Name ) {
107+ # explicit name
108+ $FileName = $Name
111109 }
112- else
113- {
114- Write-Verbose " [$Target ] is a container, creating path to file"
115- # We have a target container, now find the name
116- If ($Name )
117- {
118- # explicit name
119- $FileName = $Name
120- }
121- else
122- {
123- $FileName = Parse- URLForFile - URL $URL
124- }
125- $Path = Join-Path $Target $FileName
110+ else {
111+ $FileName = Parse- URLForFile - URL $URL
112+ }
113+ $Path = Join-Path $Target $FileName
126114
127- if (Test-Path $Path - PathType Leaf)
128- {
129- Write-Verbose " Skipping existing file [$Path ]"
130- if ($PSDependAction -contains ' Test' )
131- {
132- return $True
133- }
134- }
135- else
136- {
137- $ToInstall = $True
115+ if (Test-Path $Path - PathType Leaf) {
116+ Write-Verbose " Skipping existing file [$Path ]"
117+ if ($PSDependAction -contains ' Test' ) {
118+ return $True
138119 }
139120 }
140- Write-Verbose " Using [$Path ] as `$ Target"
141-
142- # No dependency found, return false if we're testing alone...
143- if ( $PSDependAction -contains ' Test' -and $PSDependAction.count -eq 1 )
144- {
145- return $False
121+ else {
122+ $ToInstall = $True
146123 }
147- Write-Verbose " Downloading [$URL ] to [$Path ]"
124+ }
125+ Write-Verbose " Using [$Path ] as `$ Target"
126+
127+ # No dependency found, return false if we're testing alone...
128+ if ( $PSDependAction -contains ' Test' -and $PSDependAction.count -eq 1 ) {
129+ return $False
130+ }
131+ Write-Verbose " Downloading [$URL ] to [$Path ]"
148132
149- if ($PSDependAction -contains ' Install' -and $ToInstall )
150- {
133+ if ($PSDependAction -contains ' Install' -and $ToInstall ) {
151134 # Future considerations:
152- # Should we check for existing? And if we find it, still download file, and compare sha256 hash, replace if it does not match?
153- # We should consider credentials at some point, but PSD1 does not lend itself to securely storing passwords
135+ # Should we check for existing? And if we find it, still download file, and compare sha256 hash, replace if it does not match?
136+ # We should consider credentials at some point, but PSD1 does not lend itself to securely storing passwords
154137 Get-WebFile - URL $URL - Path $Path
155138}
156139
157- if ($Dependency.AddToPath )
158- {
140+ if ($Dependency.AddToPath ) {
159141 Write-Verbose " Setting PATH to`n $ ( $PathToAdd , $env: PATH -join ' ;' | Out-String ) "
160142 Add-ToItemCollection - Reference Env:\Path - Item $PathToAdd
161143}
0 commit comments