-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathoptimize-images.ps1
More file actions
106 lines (86 loc) · 3.34 KB
/
optimize-images.ps1
File metadata and controls
106 lines (86 loc) · 3.34 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
[cmdletbinding()]
param(
[Parameter(Position=0)]
$folderToOptimize = ($pwd),
[Parameter(Position=1)]
[switch]
$force = $false,
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
<#
.SYNOPSIS
If nuget is in the tools
folder then it will be downloaded there.
#>
function Get-Nuget(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
process{
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
'Downloading nuget.exe' | Write-Verbose
(New-Object System.Net.WebClient).DownloadFile($nugetDownloadUrl, $nugetDestPath)
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
}
}
# return the path of the file
$nugetDestPath
}
}
<#
.SYNOPSIS
If the image optimizer in the .ools
folder then it will be downloaded there.
#>
function GetImageOptimizer(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
process{
if(!(Test-Path $toolsDir)){
New-Item $toolsDir -ItemType Directory | Out-Null
}
$imgOptimizer = (Get-ChildItem -Path $toolsDir -Include 'ImageCompressor.Job.exe' -Recurse)
if(!$imgOptimizer){
'Downloading image optimizer to the .tools folder' | Write-Verbose
# nuget install AzureImageOptimizer -Prerelease -OutputDirectory C:\temp\nuget\out\
$cmdArgs = @('install','AzureImageOptimizer','-Prerelease','-OutputDirectory',(Resolve-Path $toolsDir).ToString())
'Calling nuget to install image optimzer with the following args. [{0}]' -f ($cmdArgs -join ' ') | Write-Verbose
&(Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null
}
$imgOptimizer = Get-ChildItem -Path $toolsDir -Include 'ImageCompressor.Job.exe' -Recurse | select -first 1
if(!$imgOptimizer){ throw 'Image optimizer not found' }
$imgOptimizer
}
}
function OptimizeImages(){
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
$dir,
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
process{
[string]$imgOptExe = (GetImageOptimizer -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl)
[string]$folderToOptimize = (Resolve-path $dir)
'Starting image optimizer on folder [{0}]' -f $dir | Write-Output
# .\.tools\AzureImageOptimizer.0.0.10-beta\tools\ImageCompressor.Job.exe --dir M:\temp\images\opt\to-optimize
$cmdArgs = @('--dir', ('{0}\' -f (get-item $folderToOptimize).FullName))
if($force){
$cmdArgs += '--force'
}
'Calling img optimizer with the following args [{0} {1}]' -f $imgOptExe, ($cmdArgs -join ' ') | Write-Output
&$imgOptExe $cmdArgs
'Images optimized' | Write-Output
}
}
OptimizeImages -dir $folderToOptimize -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl