-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.ps1
96 lines (85 loc) · 2.34 KB
/
publish.ps1
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
$foo = (Get-Content "publish-config.json" | Out-String | ConvertFrom-Json)
$user = $foo.user
$domain = $foo.domain
$password = $foo.password
$ftpUrl = $foo.ftpUrl
$credentials = "$user"+":"+"$password"
$cacheFile = "publish-cache.json"
$ignoreFile = ".publishignore"
$extensions = ".html"
$date = Get-Date -Format "yyyy-MM-ddThh:mm:ssK"
function Relative {
process {
$name = $_ | Resolve-Path -Relative
if ($name[0] -eq '.' -And $name[1] -eq '\') {
$name=$name.ToString().Substring(2);
}
return $name;
}
}
function CreateSiteMap {
begin {
'<?xml version="1.0" encoding="UTF-8"?>'
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
}
process {
$name = $_ | Relative
if ($extensions -contains $_.extension)
{
" <url>"
" <loc>$domain/$name</loc>"
" <lastmod>{0:s}</lastmod>" -f $date
" </url>"
}
}
end {
"</urlset>"
}
}
function LoadCache {
process {
if (! (Test-Path $cacheFile -PathType leaf))
{
@{} | ConvertTo-Json | Set-Content $cacheFile
}
$json = (Get-Content $cacheFile | Out-String | ConvertFrom-Json)
$result = @{}
$json.psobject.properties | Foreach { $result[$_.Name] = $_.Value }
return $result
}
}
function SaveCache {
process {
$_ | ConvertTo-Json | Set-Content $cacheFile
}
}
$filePath = Get-ChildItem . -exclude node_modules,fetch,publish-config.json | Get-ChildItem -Recurse | Where { ! $_.PSIsContainer }
$filePath | CreateSiteMap | out-file -encoding utf8 sitemap.xml
$md5values = LoadCache
ForEach($i in $filePath) {
$name = $i | Relative
$dir = $i.Directory | Resolve-Path -Relative | Relative
if ($dir[0] -eq '.' -And $dir[1] -eq '.') {
$dir=""
} else {
$dir = $dir + "/"
}
$dir = $dir.replace("\","/")
$name = $name.replace("\","/")
$add = $FALSE
$md5 = Get-FileHash $name -Algorithm MD5
if (! ($md5values.ContainsKey($name))) {
$md5values += @{$name = $md5.Hash}
$add = $TRUE;
} elseif (! ($md5values[$name] -eq $md5.Hash)) {
$md5values[$name] = $md5.Hash
$add = $TRUE;
} elseif ($extensions -contains $i.extension) {
$add = $TRUE;
}
if ($add) {
Write-Host $name
curl -s -u $credentials --create-dirs --ftp-create-dirs -T "$name" "$ftpUrl/$dir"
}
}
$md5values | SaveCache