tools/helper.psm1 (172 lines of code) (raw):
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
using namespace System.Runtime.InteropServices
$IsWindowsEnv = [RuntimeInformation]::IsOSPlatform([OSPlatform]::Windows)
$DotnetSDKVersionRequirements = @{
# .NET SDK 3.1 is required by the Microsoft.ManifestTool.dll tool
'3.1' = @{
MinimalPatch = '426'
DefaultPatch = '426'
}
'6.0' = @{
MinimalPatch = '408'
DefaultPatch = '408'
}
}
function Find-Dotnet
{
$listSdksOutput = dotnet --list-sdks
$installedDotnetSdks = $listSdksOutput | ForEach-Object { $_.Split(" ")[0] }
Write-Log "Detected dotnet SDKs: $($installedDotnetSdks -join ', ')"
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
$minimalVersion = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].MinimalPatch)"
$firstAcceptable = $installedDotnetSdks |
Where-Object { $_.StartsWith("$majorMinorVersion.") } |
Where-Object { [System.Management.Automation.SemanticVersion]::new($_) -ge [System.Management.Automation.SemanticVersion]::new($minimalVersion) } |
Select-Object -First 1
if (-not $firstAcceptable) {
throw "Cannot find the dotnet SDK for .NET Core $majorMinorVersion. Version $minimalVersion or higher is required. Please specify '-Bootstrap' to install build dependencies."
}
}
}
function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = 'release'
)
try {
Find-Dotnet
return # Simply return if we find dotnet SDk with the correct version
} catch { }
$obtainUrl = "https://raw.githubusercontent.com/dotnet/install-scripts/main/src"
try {
$installScript = if ($IsWindowsEnv) { "dotnet-install.ps1" } else { "dotnet-install.sh" }
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
foreach ($majorMinorVersion in $DotnetSDKVersionRequirements.Keys) {
$version = "$majorMinorVersion.$($DotnetSDKVersionRequirements[$majorMinorVersion].DefaultPatch)"
Write-Log "Installing dotnet SDK version $version" -Warning
if ($IsWindowsEnv) {
& .\$installScript -Channel $Channel -Version $Version -InstallDir "$env:ProgramFiles/dotnet"
} else {
bash ./$installScript -c $Channel -v $Version --install-dir /usr/share/dotnet
}
}
}
finally {
Remove-Item $installScript -Force -ErrorAction SilentlyContinue
}
}
function Write-Log
{
param(
[string] $Message,
[switch] $Warning,
[switch] $Indent
)
$foregroundColor = if ($Warning) { "Yellow" } else { "Green" }
$indentPrefix = if ($Indent) { " " } else { "" }
Write-Host -ForegroundColor $foregroundColor "${indentPrefix}${Message}"
}
function Get-NugetPackagesPath
{
if ($env:NUGET_PACKAGES)
{
return $env:NUGET_PACKAGES
}
if ($IsWindowsEnv)
{
return "${env:USERPROFILE}\.nuget\packages"
}
else
{
return "${env:HOME}/.nuget/packages"
}
}
#region Start-ResGen
$generated_code_template = @'
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a running 'Start-ResGen' from tools\helper.psm1.
// To add or remove a member, edit your .resx file then rerun 'Start-ResGen'.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
internal class {0} {{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {{
get {{
if (object.ReferenceEquals(resourceMan, null)) {{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AzureFunctions.PowerShell.SDK.resources.{0}", typeof({0}).Assembly);
resourceMan = temp;
}}
return resourceMan;
}}
}}
/// <summary>
/// Overrides the current threads CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {{
get {{ return resourceCulture; }}
set {{ resourceCulture = value; }}
}}
{1}
}}
'@
$individual_resource_string_property = @'
/// <summary>
/// Looks up a localized string similar to
/// {0}
/// </summary>
internal static string {1} {{
get {{
return ResourceManager.GetString("{1}", resourceCulture);
}}
}}
'@
function Start-ResGen
{
param([switch] $Force)
$sourceDir = (Resolve-Path -Path "$PSScriptRoot/../src").Path
$genDir = Join-Path -Path $sourceDir -ChildPath gen
if (Test-Path -Path $genDir -PathType Container) {
if ($Force) {
Remove-Item -Path $genDir -Recurse -Force
} else {
return
}
}
$resourceDir = Join-Path -Path $sourceDir -ChildPath resources
$resxFiles = Get-ChildItem -Path $resourceDir -Filter *.resx
$null = New-Item -Path $genDir -ItemType Directory -Force
foreach ($resx in $resxFiles) {
$typeName = [System.IO.Path]::GetFileNameWithoutExtension($resx.FullName)
$resXml = [xml] (Get-Content $resx.FullName)
$properties = [System.Text.StringBuilder]::new()
foreach ($data in $resXml.root.data) {
$name = $data.name
$value = $data.value -replace "`n","\n"
$property = $individual_resource_string_property -f $value, $name
$null = $properties.Append($property)
}
$typeCode = $generated_code_template -f $typeName, $properties.ToString()
$typeFile = Join-Path -Path $genDir "$typeName.cs"
Set-Content -Path $typeFile -Value $typeCode
}
}
#endregion Start-ResGen