forked from DefinitelyTyped/NugetAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreatePackages.ps1
378 lines (295 loc) · 12.2 KB
/
CreatePackages.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
param(
$nugetApiKey,
[switch]$CommitLocalGit,
[switch]$PushGit,
[switch]$PublishNuget,
[switch]$IsTeamCity,
$specificPackages
)
# https://github.com/borisyankov/DefinitelyTyped.git
$nuget = (get-item ".\tools\NuGet.CommandLine.2.2.1\tools\NuGet.exe")
$packageIdFormat = "{0}.TypeScript.DefinitelyTyped"
$nuspecTemplate = get-item ".\PackageTemplate.nuspec"
$packageToIgnoreBecauseSomeoneStoleTheNugetIdBOOOO = @(
"Sjcl", # https://www.nuget.org/packages/Sjcl.TypeScript.DefinitelyTyped/
"Jsbn", # https://www.nuget.org/packages/Jsbn.TypeScript.DefinitelyTyped/
"bigint", # https://www.nuget.org/packages/BigInt.TypeScript.DefinitelyTyped/
"TGrid" # https://www.nuget.org/packages/TGrid.TypeScript.DefinitelyTyped/
"react" # https://www.nuget.org/packages/React.TypeScript.DefinitelyTyped/
)
# Store git credentials so we can push from AppVeyor
git config --global credential.helper store
Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:access_token):[email protected]`n"
git checkout -q master
function Get-MostRecentNugetSpec($nugetPackageId) {
$feeedUrl= "http://packages.nuget.org/v1/FeedService.svc/Packages()?`$filter=Id%20eq%20'$nugetPackageId'&`$orderby=Version%20desc&`$top=1"
$webClient = new-object System.Net.WebClient
$feedResults = [xml]($webClient.DownloadString($feeedUrl))
return $feedResults.feed.entry
}
function Get-Last-NuGet-Version($spec) {
$v = $spec.properties.version."#text"
if(!$v) {
$v = $spec.properties.version
}
$v
}
function Create-Directory($name){
if(!(test-path $name)){
mkdir $name | out-null
write-host "Created Dir: $name"
}
}
function Increment-Version($version){
if(!$version) {
return "0.0.1";
}
$parts = $version.split('.')
for($i = $parts.length-1; $i -ge 0; $i--){
$x = ([int]$parts[$i]) + 1
if($i -ne 0) {
# Don't roll the previous minor or ref past 10
if($x -eq 10) {
$parts[$i] = "0"
continue
}
}
$parts[$i] = $x.ToString()
break;
}
$newVersion = [System.String]::Join(".", $parts)
if($newVersion) {
$newVersion
} else {
"0.0.1"
}
}
function Configure-NuSpec($spec, $packageId, $newVersion, $pakageName, $dependentPackages, $newCommitHash) {
$metadata = $spec.package.metadata
$metadata.id = $packageId
$metadata.version = [string]"$newVersion"
$metadata.tags = "TypeScript JavaScript $pakageName"
$metadata.description = "TypeScript Definitions (d.ts) for {0}. Generated based off the DefinitelyTyped repository [git commit: {1}]. http://github.com/DefinitelyTyped" -f $packageName, $newCommitHash
if($dependentPackages) {
#TODO: there may be a more concise way to work with this xml than doing string manipulation.
$dependenciesXml = ""
foreach($key in $dependentPackages.Keys) {
$dependentPackageName = $packageIdFormat -f $key
$dependenciesXml = $dependenciesXml + "<dependency id=`"$dependentPackageName`" />"
}
$metadata["dependencies"].InnerXml = $dependenciesXml
}
}
function Resolve-Dependencies($packageFolder, $dependentPackages, $packageName) {
$packageFolder = get-item $packageFolder
function Resolve-SubDependencies($dependencyName){
if($dependentPackages.ContainsKey($dependencyName)){
# Try to guard against recursive dependencies
return
}
if($dependencyName -eq $packageName) {
# Don't include itself as a dependency
return;
}
echo "dependencyName: $dependencyName packageName: $packageName"
$dependentPackages.Add($dependencyName, $dependencyName);
$dependentFolder = get-item "$($packageFolder.Parent.FullName)\$dependencyName"
if(!$dependentFolder -or !(test-path $dependentFolder)){
throw "no dependency [$dependencyName] found in [$dependentFolder]"
} else {
Resolve-Dependencies $dependentFolder $dependentPackages $packageName
}
}
(ls $packageFolder -Recurse -Include *.d.ts) | Where-Object {$_.FullName -notMatch "legacy"} | `
cat | `
where { $_ -match "//.*(reference\spath=('|`")../(?<package>.*)(/|\\)(.*)\.ts('|`"))" } | `
%{ $matches.package } | `
?{ $_ } | `
?{ $_ -ne $packageFolder } | `
%{ $_.TrimStart("../") } | ` # Not sure why, but the dx.devexpress package started creating an error that would return a package with '../jquery' from the above. (maybe a bad regex?). This is an ugly stop-gap for now.
%{ Resolve-SubDependencies $_ }
}
function Create-Package($packagesAdded, $newCommitHash) {
BEGIN {
}
PROCESS {
$dir = $_
$packageName = $dir.Name
$packageId = $packageIdFormat -f $packageName
$tsFiles = ls $dir -recurse -include *.d.ts | Where-Object {$_.FullName -notMatch "legacy"}
if(!($tsFiles)) {
return;
} else {
if($IsTeamCity) {
"##teamcity[testStarted name='$packageId']"
}
try {
$mostRecentNuspec = (Get-MostRecentNugetSpec $packageId)
$currentVersion = Get-Last-NuGet-Version $mostRecentNuspec
$newVersion = Increment-Version $currentVersion
$packageFolder = "$packageId.$newVersion"
# Create the directory structure
$deployDir = "$packageFolder\Content\Scripts\typings\$packageName"
Create-Directory $deployDir
foreach($file in $tsFiles) {
$destFile = $deployDir + $file.FullName.Replace($dir, "")
mkdir (Split-Path $destFile) -Force | Out-Null
cp $file $destFile
}
$dependentPackages = @{}
Resolve-Dependencies $dir $dependentPackages $packageName
# setup the nuspec file
$currSpecFile = "$packageFolder\$packageId.nuspec"
cp $nuspecTemplate $currSpecFile
$nuspec = [xml](cat $currSpecFile)
"Configuring Nuspec newVersion:$newVersion"
Configure-NuSpec $nuspec $packageId $newVersion $pakageName $dependentPackages $newCommitHash
$nuspec.Save((get-item $currSpecFile))
& $nuget pack $currSpecFile
if($PublishNuget) {
if($nugetApiKey) {
& $nuget push "$packageFolder.nupkg" -ApiKey $nugetApiKey -NonInteractive
} else {
& $nuget push "$packageFolder.nupkg" -NonInteractive
}
} else {
"***** - NOT publishing to Nuget - *****"
}
$packagesAdded.add($packageId);
} catch {
if($IsTeamCity) {
"##teamcity[message text='Error on package: $packageId' errorDetails='$_' status='ERROR']"
} else {
throw
}
}
}
if($IsTeamCity) {
"##teamcity[testFinished name='$packageId']"
}
}
END {
}
}
function Update-Submodules {
git submodule update --init --recursive -q
# make sure the submodule is here and up to date.
pushd .\Definitions
git pull origin master -q
popd
}
function Get-MostRecentSavedCommit {
$file = cat LAST_PUBLISHED_COMMIT -ErrorAction SilentlyContinue
# first-time run and the file won't exist - clear any errors for now
$Error.Clear()
return $file;
}
function Get-NewestCommitFromDefinetlyTyped($definetlyTypedFolder, $lastPublishedCommitReference, $projectsToUpdate) {
Write-Host (Update-Submodules)
pushd $definetlyTypedFolder
git pull -q origin master | Out-Null
if($lastPublishedCommitReference) {
# Figure out what project (folders) have changed since our last publish
git diff --name-status ($lastPublishedCommitReference).Trim() master | `
Select @{Name="ChangeType";Expression={$_.Substring(0,1)}}, @{Name="File"; Expression={$_.Substring(2)}} | `
%{ [System.IO.Path]::GetDirectoryName($_.File) -replace "(.*)\\(.*)", '$1' } | `
where { ![string]::IsNullOrEmpty($_) } | `
select -Unique | `
where { !([string]$_).StartsWith("_") } | `
%{ $projectsToUpdate.add($_); Write-host "found project to update: $_"; }
}
$newLastCommitPublished = (git rev-parse HEAD);
popd
return $newLastCommitPublished;
}
$lastPublishedCommitReference = Get-MostRecentSavedCommit
$projectsToUpdate = New-Object Collections.Generic.List[string]
# Find updated repositories
$newCommitHash = Get-NewestCommitFromDefinetlyTyped ".\Definitions" $lastPublishedCommitReference $projectsToUpdate
if(($newCommitHash | measure).count -ne 1) {
"*****"
$newCommitHash
"*****"
throw "commit hash not correct"
}
"*** Projects to update ***"
"**************************"
if($specificPackages) {
$specificPackages
$allPackageDirectories = ls .\Definitions\* | ?{ $_.PSIsContainer } | ?{ $specificPackages -contains $_.Name }
}
else {
$projectsToUpdate
$allPackageDirectories = ls .\Definitions\* | ?{ $_.PSIsContainer }
}
# Clean the build directory
if(test-path build) {
rm build -recurse -force -ErrorAction SilentlyContinue
}
Create-Directory build
pushd build
$packagesUpdated = New-Object Collections.Generic.List[string]
# Filter out already published packages if we already have a LAST_PUBLISHED_COMMIT
if($lastPublishedCommitReference -ne $null) {
$packageDirectories = $allPackageDirectories | where { $projectsToUpdate -contains $_.Name }
}
else {
# first-time run. let's run all the packages.
$packageDirectories = $allPackageDirectories
}
if($IsTeamCity) {
"##teamcity[testSuiteStarted name='DefinitlyTyped NugetAutomation']"
}
"*****"
"`$packageDirectories - before filter"
$packageDirectories
"*****"
# Some people for some reason claimed the NuGet id ending in this project's convention - arg
# until we can work with NuGet team or package owner's to remove them we have to exclue them for now...
$packageDirectories = $packageDirectories | where { $packageToIgnoreBecauseSomeoneStoleTheNugetIdBOOOO -notcontains $_.Name }
"*****"
"`$packageDirectories - after filter"
$packageDirectories
"*****"
$packageDirectories | create-package $packagesUpdated $newCommitHash
if($IsTeamCity) {
"##teamcity[testSuiteFinished name='DefinitlyTyped NugetAutomation']"
}
popd
$newCommitHash | out-file LAST_PUBLISHED_COMMIT -Encoding ascii
if($newCommitHash -eq $lastPublishedCommitReference) {
"No new changes detected"
}
elseif($Error.Count -eq 0) {
if($packagesUpdated.Count -gt 0)
{
$commitMessage = "Published NuGet Packages`n`n - $([string]::join([System.Environment]::NewLine + " - ", $packagesUpdated))"
} else {
$commitMessage = "No packages updated but something in the DefinitelyTyped submodule changed - upping the submodule commit"
}
"****"
$commitMessage
"****"
if($CommitLocalGit) {
git config user.name AppVeyor
git config user.email [email protected]
git add Definitions
git add LAST_PUBLISHED_COMMIT
git commit -m $commitMessage
}
if($PushGit) {
git remote add github https://github.com/DefinitelyTyped/NugetAutomation.git
git push -q github master
if ($LastExitCode -ne 0) {
"git push exited with error"
exit 1
}
"Pushed changes..."
}
}
else {
"*****"
"ERROR During Process:"
$Error
exit 1
}