Skip to content

Instantly share code, notes, and snippets.

@kamranayub
Last active September 21, 2016 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamranayub/cc2c4d371a83aec8279e to your computer and use it in GitHub Desktop.
Save kamranayub/cc2c4d371a83aec8279e to your computer and use it in GitHub Desktop.
PowerShell script to build an HTML5 application manifest file
<#
.SYNOPSIS
Generates an HTML5 offline app cache manifest file
.DESCRIPTION
Generates an offline app cache manifest file according to file paths specified
and outputs with MD5 checksums so manifest only changes when dependent files change.
Meant to run as part of build.
.NOTES
Could easily be made into a function that accepts parameters.
Does not do anything to support wildcards or exclusions.
#>
# Project folder to scan (relative to script)
$ProjectFolder = 'www'
# Output file
$OutputFile = 'offline.appcache'
# Extensions to exclude
$Exclude = '*.txt', '*.ts'
# Folders to recursively add assets from (relative to project folder)
$Folders = @(
'css',
'fonts',
'images',
'scripts',
'sounds'
)
# Individual file assets (relative to project folder)
$Files = @(
'app.min.css',
'game.js',
'Excalibur.js',
'index.html',
'apple-touch-icon-57x57.png',
'icon-128.png',
'icon-192.png',
'icon.png',
'touch-icon-ipad-retina.png',
'touch-icon-ipad.png',
'touch-icon-iphone-retina.png',
'touch-icon-iphone.png'
)
# Customize query string or path for a file
# e.g. 'app.min.css?abc=foo'
$CustomQueryStrings = @{
'app.min.css' = '?COMMIT_NUMBER';
'game.js' = '?COMMIT_NUMBER';
'Excalibur.js' = '?COMMIT_NUMBER'
}
######################################
## No need to edit beyond this point
## unless you want to customize file
## generation
######################################
# Prepend script path
$ProjectFolder = ((Get-Variable MyInvocation).Value.MyCommand.Path | Split-Path) + '\' + $ProjectFolder + '\'
# Get asset files in project folders
$ExpandedFiles = $Folders `
| % { $ProjectFolder + $_ } `
| gci -Recurse -Exclude $Exclude -File `
| % { $_.FullName.Replace($ProjectFolder, '') }
# Union individual files
$ExpandedFiles += $Files
# Output
$OfflineManifestOutput = @()
# Header
$OfflineManifestOutput += 'CACHE MANIFEST'
$OfflineManifestOutput += ''
# Write cache entries
$ExpandedFiles | ForEach-Object {
$File = $_
$OfflineManifestOutput += $File + ($CustomQueryStrings.GetEnumerator() | ? { $_.Name -eq $File }).Value + ' # ' + (Get-FileHash ($ProjectFolder + $File) -Algorithm MD5).Hash
}
# Whitelist all other resources
$OfflineManifestOutput += ''
$OfflineManifestOutput += 'NETWORK:'
$OfflineManifestOutput += '*'
# Write output
$OfflineManifestOutput | Out-File ($ProjectFolder + $OutputFile) -Encoding utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment