Skip to content

Commit ae8e14f

Browse files
committed
Create PowerShell script for creating library without bower
Refactor PS scripts into one module that is imported by three specific scripts
1 parent d12c26c commit ae8e14f

File tree

6 files changed

+121
-109
lines changed

6 files changed

+121
-109
lines changed

JsLibraryPackaging.psm1

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
Set-StrictMode -Version:Latest
2+
#Import-Module PSCX -RequiredVersion 3.2.0.0
3+
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
4+
5+
function New-Package(
6+
$directory = '.',
7+
[switch]$recurse,
8+
$outputPath = '_InstallPackages')
9+
{
10+
if ($directory -is [string]) {
11+
$directory = Resolve-Path $directory
12+
} elseif ($directory -is [System.IO.DirectoryInfo]){
13+
$directory = Resolve-Path $directory.FullName
14+
}
15+
#Write-Host ('directory is ' + $directory.Path)
16+
17+
if ($outputPath -is [string]) {
18+
$outputPath = Resolve-Path $outputPath
19+
} elseif ($outputPath -is [System.IO.DirectoryInfo]) {
20+
$outputPath = Resolve-Path $outputPath.FullName
21+
}
22+
#Write-Host ('output path is ' + $outputPath.Path)
23+
24+
$zipThisDirectory = -not $recurse
25+
if ($recurse) {
26+
$subDirectories = ls $directory -Directory | ? { $_.FullName -ne $outputPath.Path -and $_.Name -notmatch '^_' }
27+
if (@($subDirectories).Length -gt 0) {
28+
foreach ($subDirectory in $subDirectories) {
29+
New-Package $subDirectory -recurse -outputPath $outputPath
30+
}
31+
} else {
32+
$zipThisDirectory = $true
33+
}
34+
}
35+
36+
if ($zipThisDirectory) {
37+
#Write-Host ('zipping ' + $directory.Path)
38+
$zipName = (Split-Path $directory.Path -Leaf) + '.zip'
39+
[System.IO.Compression.ZipFile]::CreateFromDirectory($directory, (Join-Path $outputPath $zipName), [System.IO.Compression.CompressionLevel]::Optimal, $false)
40+
}
41+
}
42+
43+
function New-BowerLibrary ($name) {
44+
bower install $name
45+
if ($name -match '#') {
46+
$split = $name.Split('#')
47+
$name = $split[0]
48+
$version = $split[1]
49+
}
50+
51+
$packageInfo = (Get-Content .\_bower_components\$name\.bower.json) -join "`n" | ConvertFrom-Json
52+
if (@(Get-Member -InputObject $packageInfo | ? { $_.Name -eq 'version' }).Count -gt 0) {
53+
$version = $packageInfo.version
54+
}
55+
56+
$allPaths = (bower list --paths --json) -join "`n" | ConvertFrom-Json
57+
$paths = @($allPaths.$name)
58+
$jsPaths = @($paths | ? { $_ -match '\.js$' })
59+
60+
if ($jsPaths.Count -eq 0) {
61+
$jsPaths = @(ls $paths *.js -Recurse)
62+
}
63+
if ($jsPaths.Count -gt 1) {
64+
Write-Warning 'Package contains multiple JS files, only the first will be listed in the package'
65+
}
66+
67+
$jsFile = $jsPaths[0]
68+
$fileName = Split-Path $jsFile -Leaf
69+
70+
$versionedFolder = New-Library $name $version $fileName
71+
72+
$filePaths = @($paths | ? { -not (Test-Path $_ -PathType Container) })
73+
if ($filePaths.Count -eq 0) {
74+
$jsPaths | % { cp $_.FullName $versionedFolder }
75+
} else {
76+
$filePaths | % { cp $_ $versionedFolder }
77+
}
78+
79+
if (@(Get-Member -InputObject $packageInfo | ? { $_.Name -eq 'dependencies' }).Count -gt 0) {
80+
Write-Warning 'Package has dependencies'; Write-Warning $packageInfo.dependencies
81+
}
82+
}
83+
84+
function New-JavaScriptLibrary ($name, $version, $jsFileName) {
85+
$versionedFolder = "$($name)_$version"
86+
mkdir $versionedFolder
87+
88+
$manifestFile = "$versionedFolder\$name.dnn"
89+
cp _template\library.dnn $manifestFile
90+
$changesFile = "$versionedFolder\CHANGES.htm"
91+
cp _template\CHANGES.htm $changesFile
92+
$licenseFile = "$versionedFolder\LICENSE.htm"
93+
cp _template\LICENSE.htm $licenseFile
94+
95+
ReplaceTokens $manifestFile $name $version $jsFileName
96+
ReplaceTokens $changesFile $name $version $jsFileName
97+
ReplaceTokens $licenseFile $name $version $jsFileName
98+
99+
notepad $manifestFile
100+
notepad $changesFile
101+
notepad $licenseFile
102+
103+
return $versionedFolder
104+
}
105+
106+
function ReplaceTokens($file, $name, $version, $fileName) {
107+
(Get-Content $file) |
108+
% { $_ -replace '\[name\]', $name -replace '\[version\]', $version -replace '\[file\]', $fileName } |
109+
Set-Content $file
110+
}
111+
112+
Export-ModuleMember New-JavaScriptLibrary
113+
Export-ModuleMember New-BowerLibrary
114+
Export-ModuleMember New-Package

New-Package.psm1

Lines changed: 0 additions & 43 deletions
This file was deleted.

New-PackageFromBower.psm1

Lines changed: 0 additions & 65 deletions
This file was deleted.

NewBowerLibrary.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
param ($name)
2+
Import-Module .\JsLibraryPackaging.psm1
3+
New-BowerLibrary $name

NewLibrary.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
param ($name, $version, $jsFileName)
2+
Import-Module .\JsLibraryPackaging.psm1
3+
New-JavaScriptLibrary $name $version $jsFileName

ZipAllLibraries.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module .\New-Package.psm1
1+
Import-Module .\JsLibraryPackaging.psm1
22
rmdir .\_InstallPackages -Recurse -Force
33
mkdir .\_InstallPackages | Out-Null
44
New-Package -Recurse

0 commit comments

Comments
 (0)