Skip to content
Permalink
Browse files

Paths (#6222)

* $Env:LocalAppData -> [System.Environment]::GetFolderPath("LocalApplicationData")

* more placements for linux just in case

* fix formatting plus add () around new config var

* hug var

* hug that one too

* avoid [System.Environment] in some Environments? this is wierd

* more wraps
  • Loading branch information
potatoqualitee committed Nov 30, 2019
1 parent 1f4f89f commit 5e58ae99335728618b68a09157b5d5023c304cd3
@@ -31,7 +31,7 @@ function Get-DotNetPath {
param ()
process {
if (Get-Command dotnet.exe -CommandType Application -ErrorAction SilentlyContinue) { return (Get-Command dotnet.exe -CommandType Application | Select-Object -First 1).Source }
"$($env:ProgramFiles)\dotnet\dotnet.exe" | Select-Object -First 1
"$([System.Environment]::GetFolderPath("ProgramFiles"))\dotnet\dotnet.exe" | Select-Object -First 1
}
}

@@ -10,7 +10,7 @@ function Get-DbaFeature {
https://itsalljustelectrons.blogspot.be/2018/04/SQL-Server-Discovery-Report.html
Assumptions:
1. The sub-folder "Microsoft SQL Server" exists in $env:ProgramFiles,
1. The sub-folder "Microsoft SQL Server" exists in [System.Environment]::GetFolderPath("ProgramFiles"),
even if SQL was installed to a non-default path. This has been
verified on SQL 2008R2 and SQL 2012. Further verification may be needed.
2. The discovery report displays installed components for the version of SQL
@@ -69,7 +69,7 @@ function Get-DbaFeature {

begin {
$scriptblock = {
$setup = Get-ChildItem -Recurse -Include setup.exe -Path "$env:ProgramFiles\Microsoft SQL Server" -ErrorAction SilentlyContinue |
$setup = Get-ChildItem -Recurse -Include setup.exe -Path "$([System.Environment]::GetFolderPath("ProgramFiles"))\Microsoft SQL Server" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match 'Setup Bootstrap\\SQL' -or $_.FullName -match 'Bootstrap\\Release\\Setup.exe' -or $_.FullName -match 'Bootstrap\\Setup.exe' } |
Sort-Object FullName -Descending | Select-Object -First 1
if ($setup) {
@@ -110,7 +110,7 @@ function Invoke-DbaAdvancedInstall {
[version]$Version
)
$versionNumber = "$($Version.Major)$($Version.Minor)".Substring(0, 3)
$rootPath = "$env:ProgramFiles\Microsoft SQL Server\$versionNumber\Setup Bootstrap\Log"
$rootPath = "$([System.Environment]::GetFolderPath("ProgramFiles"))\Microsoft SQL Server\$versionNumber\Setup Bootstrap\Log"
$summaryPath = "$rootPath\Summary.txt"
$output = [PSCustomObject]@{
Path = $null
@@ -30,7 +30,7 @@ Examples are better than a thousand words:
a) Setting the configuration value
# Put this in a configuration file in this folder
Set-DbatoolsConfig -Name 'Path.DbatoolsLog' -Value "$($env:AppData)\PowerShell\dbatools" -Initialize -Description "Sopmething meaningful here"
Set-DbatoolsConfig -Name 'Path.DbatoolsLog' -Value "$([System.Environment]::GetFolderPath("ApplicationData"))\PowerShell\dbatools" -Initialize -Description "Sopmething meaningful here"
b) Retrieving the configuration value in your function
# Put this in the function that uses this setting
@@ -39,7 +39,7 @@ $path = Get-DbatoolsConfigValue -Name 'Path.DbatoolsLog' -FallBack $env:temp
# Explanation #
#-------------#
In step a), which is run during module import, we assign the configuration of the name 'Path.DbatoolsLog' the value "$($env:AppData)\PowerShell\dbatools"
In step a), which is run during module import, we assign the configuration of the name 'Path.DbatoolsLog' the value "$([System.Environment]::GetFolderPath("ApplicationData"))\PowerShell\dbatools"
Unless there already IS a value set to this name (that's what the '-Default' switch is doing).
That means, that if a user had a different configuration value in his profile, that value will win. Userchoice over preset.
ALL configurations defined by the module should be 'default' values.
@@ -58,56 +58,51 @@ $psVersionName = "WindowsPowerShell"
if ($PSVersionTable.PSVersion.Major -ge 6) { $psVersionName = "PowerShell" }

#region User Local
if ($IsLinux -or $IsMacOs)
{
if ($IsLinux -or $IsMacOs) {
# Defaults to $Env:XDG_CONFIG_HOME on Linux or MacOS ($HOME/.config/)
$fileUserLocal = $Env:XDG_CONFIG_HOME
if (-not $fileUserLocal) { $fileUserLocal = Join-Path $HOME .config/ }

$script:path_FileUserLocal = Join-DbaPath $fileUserLocal $psVersionName "dbatools/"
}
else
{
# Defaults to $Env:LocalAppData on Windows
$script:path_FileUserLocal = Join-Path $Env:LocalAppData "$psVersionName\dbatools\Config"
} else {
# Defaults to $localappdatapath on Windows
if ($env:LOCALAPPDATA) {
$localappdatapath = $env:LOCALAPPDATA
} else {
$localappdatapath = [System.Environment]::GetFolderPath("LocalApplicationData")
}
$script:path_FileUserLocal = Join-Path $localappdatapath "$psVersionName\dbatools\Config"
if (-not $script:path_FileUserLocal) { $script:path_FileUserLocal = Join-Path ([Environment]::GetFolderPath("LocalApplicationData")) "$psVersionName\dbatools\Config" }
}
#endregion User Local

#region User Shared
if ($IsLinux -or $IsMacOs)
{
if ($IsLinux -or $IsMacOs) {
# Defaults to the first value in $Env:XDG_CONFIG_DIRS on Linux or MacOS (or $HOME/.local/share/)
$fileUserShared = @($Env:XDG_CONFIG_DIRS -split ([IO.Path]::PathSeparator))[0]
if (-not $fileUserShared) { $fileUserShared = Join-Path $HOME .local/share/ }

$script:path_FileUserShared = Join-DbaPath $fileUserShared $psVersionName "dbatools/"
$script:AppData = $fileUserShared
}
else
{
# Defaults to $Env:AppData on Windows
$script:path_FileUserShared = Join-DbaPath $Env:AppData $psVersionName "dbatools" "Config"
$script:AppData = $Env:APPDATA
if (-not $Env:AppData)
{
$script:path_FileUserShared = Join-DbaPath ([Environment]::GetFolderPath("ApplicationData")) $psVersionName "dbatools" "Config"
$script:AppData = [Environment]::GetFolderPath("ApplicationData")
} else {
# Defaults to [System.Environment]::GetFolderPath("ApplicationData") on Windows
$script:path_FileUserShared = Join-DbaPath $([System.Environment]::GetFolderPath("ApplicationData")) $psVersionName "dbatools" "Config"
$script:AppData = [System.Environment]::GetFolderPath("ApplicationData")
if (-not $([System.Environment]::GetFolderPath("ApplicationData"))) {
$script:path_FileUserShared = Join-DbaPath $([Environment]::GetFolderPath("ApplicationData")) $psVersionName "dbatools" "Config"
$script:AppData = [System.Environment]::GetFolderPath("ApplicationData")
}
}
#endregion User Shared

#region System
if ($IsLinux -or $IsMacOs)
{
if ($IsLinux -or $IsMacOs) {
# Defaults to /etc/xdg elsewhere
$XdgConfigDirs = $Env:XDG_CONFIG_DIRS -split ([IO.Path]::PathSeparator) | Where-Object { $_ -and (Test-Path $_) }
if ($XdgConfigDirs.Count -gt 1) { $basePath = $XdgConfigDirs[1] }
else { $basePath = "/etc/xdg/" }
$script:path_FileSystem = Join-DbaPath $basePath $psVersionName "dbatools/"
}
else
{
} else {
# Defaults to $Env:ProgramData on Windows
$script:path_FileSystem = Join-DbaPath $Env:ProgramData $psVersionName "dbatools" "Config"
if (-not $script:path_FileSystem) { $script:path_FileSystem = Join-DbaPath ([Environment]::GetFolderPath("CommonApplicationData")) $psVersionName "dbatools" "Config" }
@@ -123,51 +118,42 @@ $script:path_typedata = Join-DbaPath $script:AppData $psVersionName "dbatools" "

# Determine Registry Availability
$script:NoRegistry = $false
if (($PSVersionTable.PSVersion.Major -ge 6) -and ($PSVersionTable.OS -notlike "*Windows*"))
{
if (($PSVersionTable.PSVersion.Major -ge 6) -and ($PSVersionTable.OS -notlike "*Windows*")) {
$script:NoRegistry = $true
}

$configpath = Resolve-Path "$script:PSModuleRoot\internal\configurations"

# Import configuration validation
foreach ($file in (Get-ChildItem -Path (Resolve-Path "$configpath\validation")))
{
foreach ($file in (Get-ChildItem -Path (Resolve-Path "$configpath\validation"))) {
if ($script:doDotSource) { . $file.FullName }
else { . ([scriptblock]::Create([io.file]::ReadAllText($file.FullName))) }
}

# Import other configuration files
foreach ($file in (Get-ChildItem -Path (Resolve-Path "$configpath\settings")))
{
foreach ($file in (Get-ChildItem -Path (Resolve-Path "$configpath\settings"))) {
if ($script:doDotSource) { . $file.FullName }
else { . ([scriptblock]::Create([io.file]::ReadAllText($file.FullName))) }
}

if (-not [Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::ImportFromRegistryDone)
{
if (-not [Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::ImportFromRegistryDone) {
# Read config from all settings
$config_hash = Read-DbatoolsConfigPersisted -Scope 127

foreach ($value in $config_hash.Values)
{
try
{

foreach ($value in $config_hash.Values) {
try {
if (-not $value.KeepPersisted) { Set-DbatoolsConfig -FullName $value.FullName -Value $value.Value -EnableException }
else { Set-DbatoolsConfig -FullName $value.FullName -PersistedValue $value.Value -PersistedType $value.Type -EnableException }
[Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::Configurations[$value.FullName.ToLowerInvariant()].PolicySet = $value.Policy
[Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::Configurations[$value.FullName.ToLowerInvariant()].PolicyEnforced = $value.Enforced
}
catch { }
} catch { }
}

if ($null -ne $global:dbatools_config)
{
if ($global:dbatools_config.GetType().FullName -eq "System.Management.Automation.ScriptBlock")
{

if ($null -ne $global:dbatools_config) {
if ($global:dbatools_config.GetType().FullName -eq "System.Management.Automation.ScriptBlock") {
[System.Management.Automation.ScriptBlock]::Create($global:dbatools_config.ToString()).Invoke()
}
}

[Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::ImportFromRegistryDone = $true
}
}
@@ -80,8 +80,8 @@ if ($IsLinux -or $IsMacOs) {
$path_LocalAppData = $Env:XDG_CONFIG_HOME
if (-not $path_LocalAppData) { $path_LocalAppData = Join-Path $HOME .config/ }
} else {
# Defaults to $Env:LocalAppData on Windows
$path_LocalAppData = $Env:LocalAppData
# Defaults to [System.Environment]::GetFolderPath("LocalApplicationData") on Windows
$path_LocalAppData = [System.Environment]::GetFolderPath("LocalApplicationData")
if (-not $path_LocalAppData) { $path_LocalAppData = [Environment]::GetFolderPath("LocalApplicationData") }
}
#endregion $path_localAppData
@@ -92,8 +92,8 @@ if ($IsLinux -or $IsMacOs) {
$path_AppData = @($Env:XDG_CONFIG_DIRS -split ([IO.Path]::PathSeparator))[0]
if (-not $path_AppData) { $path_AppData = Join-Path $HOME .local/share/ }
} else {
# Defaults to $Env:AppData on Windows
$path_AppData = $env:APPDATA
# Defaults to [System.Environment]::GetFolderPath("ApplicationData") on Windows
$path_AppData = [System.Environment]::GetFolderPath("ApplicationData")
if (-not $path_AppData) { $path_AppData = [Environment]::GetFolderPath("ApplicationData") }
}
#endregion $path_AppData

0 comments on commit 5e58ae9

Please sign in to comment.
You can’t perform that action at this time.