Implement script to prepare packages with Node.js

This commit is contained in:
Maxim Lobanov
2020-04-23 12:58:18 +03:00
commit 4e7c1d2a13
26 changed files with 1486 additions and 0 deletions

71
builders/build-node.ps1 Normal file
View File

@@ -0,0 +1,71 @@
using module "./builders/win-node-builder.psm1"
using module "./builders/nix-node-builder.psm1"
<#
.SYNOPSIS
Generate Node.js artifact.
.DESCRIPTION
Main script that creates instance of NodeBuilder and builds of Node.js using specified parameters.
.PARAMETER Version
Required parameter. The version with which Node.js will be built.
.PARAMETER Architecture
Optional parameter. The architecture with which Node.js will be built. Using x64 by default.
.PARAMETER Platform
Required parameter. The platform for which Node.js will be built.
#>
param(
[Parameter (Mandatory=$true)][Version] $Version,
[Parameter (Mandatory=$true)][string] $Platform,
[string] $Architecture = "x64"
)
Import-Module (Join-Path $PSScriptRoot "../helpers" | Join-Path -ChildPath "nix-helpers.psm1") -DisableNameChecking
Import-Module (Join-Path $PSScriptRoot "../helpers" | Join-Path -ChildPath "win-helpers.psm1") -DisableNameChecking
function Get-NodeBuilder {
<#
.SYNOPSIS
Wrapper for class constructor to simplify importing NodeBuilder.
.DESCRIPTION
Create instance of NodeBuilder with specified parameters.
.PARAMETER Version
The version with which Node.js will be built.
.PARAMETER Platform
The platform for which Node.js will be built.
.PARAMETER Architecture
The architecture with which Node.js will be built.
#>
param (
[version] $Version,
[string] $Architecture,
[string] $Platform
)
$Platform = $Platform.ToLower()
if ($Platform -match 'win32') {
$builder = [WinNodeBuilder]::New($Version, $Platform, $Architecture)
} elseif (($Platform -match 'linux') -or ($Platform -match 'darwin')) {
$builder = [NixNodeBuilder]::New($Version, $Platform, $Architecture)
} else {
Write-Host "##vso[task.logissue type=error;] Invalid platform: $Platform"
exit 1
}
return $builder
}
### Create Node.js builder instance, and build artifact
$Builder = Get-NodeBuilder -Version $Version -Platform $Platform -Architecture $Architecture
$Builder.Build()

View File

@@ -0,0 +1,62 @@
using module "./builders/node-builder.psm1"
class NixNodeBuilder : NodeBuilder {
<#
.SYNOPSIS
Ubuntu Node.js builder class.
.DESCRIPTION
Contains methods that required to build Ubuntu Node.js artifact from sources. Inherited from base NixNodeBuilder.
.PARAMETER platform
The full name of platform for which Node.js should be built.
.PARAMETER version
The version of Node.js that should be built.
#>
[string] $InstallationTemplateName
[string] $InstallationScriptName
[string] $OutputArtifactName
NixNodeBuilder(
[version] $version,
[string] $platform,
[string] $architecture
) : Base($version, $platform, $architecture) {
$this.InstallationTemplateName = "nix-setup-template.sh"
$this.InstallationScriptName = "setup.sh"
$this.OutputArtifactName = "tool.tar.gz"
}
[uri] GetBinariesUri() {
<#
.SYNOPSIS
Get base Node.js URI and return complete URI for Node.js installation executable.
#>
$base = $this.GetBaseUri()
return "${base}/v$($this.Version)/node-v$($this.Version)-$($this.Platform)-$($this.Architecture).tar.gz"
}
[void] ExtractBinaries($archivePath) {
Extract-TarArchive -ArchivePath $archivePath -OutputDirectory $this.ArtifactLocation
}
[void] CreateInstallationScript() {
<#
.SYNOPSIS
Create Node.js artifact installation script based on template specified in InstallationTemplateName property.
#>
$installationScriptLocation = New-Item -Path $this.ArtifactLocation -Name $this.InstallationScriptName -ItemType File
$installationTemplateLocation = Join-Path -Path $this.InstallationTemplatesLocation -ChildPath $this.InstallationTemplateName
$installationTemplateContent = Get-Content -Path $installationTemplateLocation -Raw
$installationTemplateContent = $installationTemplateContent -f $this.Version.ToString(3)
$installationTemplateContent | Out-File -FilePath $installationScriptLocation
Write-Debug "Done; Installation script location: $installationScriptLocation)"
}
}

View File

@@ -0,0 +1,93 @@
class NodeBuilder {
<#
.SYNOPSIS
Base Node.js builder class.
.DESCRIPTION
Base Node.js builder class that contains general builder methods.
.PARAMETER Version
The version of Node.js that should be built.
.PARAMETER Platform
The platform of Node.js that should be built.
.PARAMETER Architecture
The architecture with which Node.js should be built.
.PARAMETER TempFolderLocation
The location of temporary files that will be used during Node.js package generation. Using system BUILD_STAGINGDIRECTORY variable value.
.PARAMETER ArtifactLocation
The location of generated Node.js artifact. Using system environment BUILD_BINARIESDIRECTORY variable value.
.PARAMETER InstallationTemplatesLocation
The location of installation script template. Using "installers" folder from current repository.
#>
[version] $Version
[string] $Platform
[string] $Architecture
[string] $TempFolderLocation
[string] $ArtifactLocation
[string] $InstallationTemplatesLocation
NodeBuilder ([version] $version, [string] $platform, [string] $architecture) {
$this.Version = $version
$this.Platform = $platform
$this.Architecture = $architecture
$this.ArtifactLocation = $env:BUILD_BINARIESDIRECTORY
$this.TempFolderLocation = $env:BUILD_STAGINGDIRECTORY
$this.InstallationTemplatesLocation = Join-Path -Path $PSScriptRoot -ChildPath "../installers"
}
[uri] GetBaseUri() {
<#
.SYNOPSIS
Return base URI for Node.js binaries.
#>
return "https://nodejs.org/download/release"
}
[string] Download() {
<#
.SYNOPSIS
Download Node.js binaries into artifact location.
#>
$binariesUri = $this.GetBinariesUri()
$targetFilename = [IO.Path]::GetFileName($binariesUri)
$targetFilepath = Join-Path -Path $this.TempFolderLocation -ChildPath $targetFilename
Write-Debug "Download binaries from $binariesUri to $targetFilepath"
try {
(New-Object System.Net.WebClient).DownloadFile($binariesUri, $targetFilepath)
} catch {
Write-Host "Error during downloading file from '$binariesUri'"
exit 1
}
Write-Debug "Done; Binaries location: $targetFilepath"
return $targetFilepath
}
[void] Build() {
<#
.SYNOPSIS
Generates Node.js artifact from downloaded binaries.
#>
Write-Host "Download Node.js $($this.Version) [$($this.Architecture)] executable..."
$binariesArchivePath = $this.Download()
Write-Host "Unpack binaries to target directory"
$this.ExtractBinaries($binariesArchivePath)
Write-Host "Create installation script..."
$this.CreateInstallationScript()
}
}

View File

@@ -0,0 +1,69 @@
using module "./builders/node-builder.psm1"
class WinNodeBuilder : NodeBuilder {
<#
.SYNOPSIS
Ubuntu Node.js builder class.
.DESCRIPTION
Contains methods that required to build Ubuntu Node.js artifact from sources. Inherited from base NixNodeBuilder.
.PARAMETER platform
The full name of platform for which Node.js should be built.
.PARAMETER version
The version of Node.js that should be built.
#>
[string] $InstallationTemplateName
[string] $InstallationScriptName
[string] $OutputArtifactName
WinNodeBuilder(
[version] $version,
[string] $platform,
[string] $architecture
) : Base($version, $platform, $architecture) {
$this.InstallationTemplateName = "win-setup-template.ps1"
$this.InstallationScriptName = "setup.ps1"
$this.OutputArtifactName = "tool.7z"
}
[uri] GetBinariesUri() {
<#
.SYNOPSIS
Get base Node.js URI and return complete URI for Node.js installation executable.
#>
$base = $this.GetBaseUri()
return "${base}/v$($this.Version)/node-v$($this.Version)-win-$($this.Architecture).7z"
}
[void] ExtractBinaries($archivePath) {
$extractTargetDirectory = Join-Path $this.TempFolderLocation "tempExtract"
Extract-7ZipArchive -ArchivePath $archivePath -OutputDirectory $extractTargetDirectory
$nodeOutputPath = Get-Item $extractTargetDirectory\* | Select-Object -First 1 -ExpandProperty Fullname
Move-Item -Path $nodeOutputPath\* -Destination $this.ArtifactLocation
}
[void] CreateInstallationScript() {
<#
.SYNOPSIS
Create Node.js artifact installation script based on specified template.
#>
$installationScriptLocation = New-Item -Path $this.ArtifactLocation -Name $this.InstallationScriptName -ItemType File
$installationTemplateLocation = Join-Path -Path $this.InstallationTemplatesLocation -ChildPath $this.InstallationTemplateName
$installationTemplateContent = Get-Content -Path $installationTemplateLocation -Raw
$variablesToReplace = @{
"{{__VERSION__}}" = $this.Version;
"{{__ARCHITECTURE__}}" = $this.Architecture;
}
$variablesToReplace.keys | ForEach-Object { $installationTemplateContent = $installationTemplateContent.Replace($_, $variablesToReplace[$_]) }
$installationTemplateContent | Out-File -FilePath $installationScriptLocation
Write-Debug "Done; Installation script location: $installationScriptLocation)"
}
}