Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(config): Move config handling to core.ps1 #3242

Merged
merged 19 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/checkhashes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ param(

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\config.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\autoupdate.ps1"
. "$PSScriptRoot\..\lib\json.ps1"
Expand Down
1 change: 0 additions & 1 deletion bin/checkurls.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ param(

. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\config.ps1"
. "$PSScriptRoot\..\lib\install.ps1"

$Dir = Resolve-Path $Dir
Expand Down
1 change: 0 additions & 1 deletion bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ param(

. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\manifest.ps1"
. "$psscriptroot\..\lib\config.ps1"
. "$psscriptroot\..\lib\buckets.ps1"
. "$psscriptroot\..\lib\autoupdate.ps1"
. "$psscriptroot\..\lib\json.ps1"
Expand Down
1 change: 0 additions & 1 deletion bin/scoop.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ param($cmd)

set-strictmode -off

. "$psscriptroot\..\lib\config.ps1"
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\git.ps1"
. "$psscriptroot\..\lib\buckets.ps1"
Expand Down
95 changes: 0 additions & 95 deletions lib/config.ps1

This file was deleted.

86 changes: 86 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ $globaldir = $env:SCOOP_GLOBAL, "$env:ProgramData\scoop" | Select-Object -first
# Use at your own risk.
$cachedir = $env:SCOOP_CACHE, "$scoopdir\cache" | Select-Object -first 1

$configHome = $env:XDG_CONFIG_HOME, "$env:USERPROFILE\.config" | Select-Object -First 1
$configFile = "$configHome\scoop\config.json"
if ((Test-Path "$env:USERPROFILE\.scoop") -and !(Test-Path $configFile)) {
New-Item -ItemType Directory (Split-Path -Path $configFile) -ErrorAction Ignore | Out-Null
Move-Item "$env:USERPROFILE\.scoop" $configFile
write-host "WARN Scoop configuration has been migrated from '~/.scoop'" -f darkyellow
write-host "WARN to '$configFile'" -f darkyellow
}

# Note: Github disabled TLS 1.0 support on 2018-02-23. Need to enable TLS 1.2
# for all communication with api.github.com
function Optimize-SecurityProtocol {
Expand Down Expand Up @@ -58,6 +67,83 @@ function Show-DeprecatedWarning {
Write-Host " -> $($Invocation.PSCommandPath):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)" -ForegroundColor DarkGray
}

function load_cfg($file) {
if(!(Test-Path $file)) {
return $null
}

try {
return (Get-Content $file -Raw | ConvertFrom-Json -ErrorAction Stop)
} catch {
Write-Host "ERROR loading $file`: $($_.exception.message)"
}
}

$scoopConfig = load_cfg $configFile

function get_config($name, $default) {
if($null -eq $scoopConfig.$name -and $null -ne $default) {
return $default
}
return $scoopConfig.$name
}

function set_config($name, $value) {
if($null -eq $scoopConfig -or $scoopConfig.Count -eq 0) {
ensure (Split-Path -Path $configFile) | Out-Null
$scoopConfig = New-Object PSObject
$scoopConfig | Add-Member -MemberType NoteProperty -Name $name -Value $value
} else {
if($value -eq [bool]::TrueString -or $value -eq [bool]::FalseString) {
$value = [System.Convert]::ToBoolean($value)
}
if($null -eq $scoopConfig.$name) {
$scoopConfig | Add-Member -MemberType NoteProperty -Name $name -Value $value
} else {
$scoopConfig.$name = $value
}
}

if($null -eq $value) {
$scoopConfig.PSObject.Properties.Remove($name)
}

ConvertTo-Json $scoopConfig | Set-Content $configFile -Encoding ASCII
return $scoopConfig
}

function setup_proxy() {
# note: '@' and ':' in password must be escaped, e.g. 'p@ssword' -> p\@ssword'
$proxy = get_config 'proxy'
if(!$proxy) {
return
}
try {
$credentials, $address = $proxy -split '(?<!\\)@'
if(!$address) {
$address, $credentials = $credentials, $null # no credentials supplied
}

if($address -eq 'none') {
[net.webrequest]::defaultwebproxy = $null
} elseif($address -ne 'default') {
[net.webrequest]::defaultwebproxy = new-object net.webproxy "http://$address"
}

if($credentials -eq 'currentuser') {
[net.webrequest]::defaultwebproxy.credentials = [net.credentialcache]::defaultcredentials
} elseif($credentials) {
$username, $password = $credentials -split '(?<!\\):' | ForEach-Object { $_ -replace '\\([@:])','$1' }
[net.webrequest]::defaultwebproxy.credentials = new-object net.networkcredential($user, $pass)
}
} catch {
warn "Failed to use proxy '$proxy': $($_.exception.message)"
}
}

setup_proxy


# helper functions
function coalesce($a, $b) { if($a) { return $a } $b }

Expand Down
113 changes: 57 additions & 56 deletions libexec/scoop-alias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,91 +25,92 @@ param(

. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\help.ps1"
. "$psscriptroot\..\lib\config.ps1"
. "$psscriptroot\..\lib\install.ps1"

$script:config_alias = "alias"

function init_alias_config {
$aliases = get_config $script:config_alias
if(!$aliases) {
$aliases = @{}
}
$aliases = get_config $script:config_alias
if(!$aliases) {
$aliases = @{}
}

$aliases
return $aliases
}

function add_alias($name, $command) {
if(!$command) {
abort "Can't create an empty alias."
}
if(!$command) {
abort "Can't create an empty alias."
}

# get current aliases from config
$aliases = init_alias_config
if($aliases.containskey($name)) {
abort "Alias $name already exists."
}
# get current aliases from config
$aliases = init_alias_config
if($aliases.$name) {
abort "Alias $name already exists."
}

$alias_file = "scoop-$name"
$alias_file = "scoop-$name"

# generate script
$shimdir = shimdir $false
$script =
# generate script
$shimdir = shimdir $false
$script =
@"
# Summary: $description
$command
"@
$script | out-file "$shimdir\$alias_file.ps1" -encoding utf8
$script | out-file "$shimdir\$alias_file.ps1" -encoding utf8

# add alias to config
$aliases += @{ $name = $alias_file }
set_config $script:config_alias $aliases
# add alias to config
$aliases | Add-Member -MemberType NoteProperty -Name $name -Value $alias_file

set_config $script:config_alias $aliases | Out-Null
}

function rm_alias($name) {
$aliases = init_alias_config
if(!$name) {
abort "Which alias should be removed?"
}
$aliases = init_alias_config
if(!$name) {
abort "Which alias should be removed?"
}

if($aliases.containskey($name)) {
"Removing alias $name..."
if($aliases.$name) {
"Removing alias $name..."

rm_shim $aliases.get_item($name) (shimdir $false)
rm_shim $aliases.$name (shimdir $false)

$aliases.remove($name)
set_config $script:config_alias $aliases
}
else { abort "Alias $name doesn't exist." }
$aliases.PSObject.Properties.Remove($name)
set_config $script:config_alias $aliases | Out-Null
} else {
abort "Alias $name doesn't exist."
}
}

function list_aliases {
$aliases = @()

(init_alias_config).GetEnumerator() | ForEach-Object {
$content = Get-Content (command_path $_.name)
$command = ($content | Select-Object -Skip 1).Trim()
$summary = (summary $content).Trim()

$aliases += New-Object psobject -Property @{Name=$_.name; Summary=$summary; Command=$command}
}

if(!$aliases.count) {
warn "No aliases founds."
}
$aliases = $aliases.GetEnumerator() | Sort-Object Name
if($verbose) {
return $aliases | Select-Object Name, Command, Summary | Format-Table -autosize -wrap
} else {
return $aliases | Select-Object Name, Command | Format-Table -autosize -hidetablehead -wrap
}
$aliases = @()

(init_alias_config).PSObject.Properties.GetEnumerator() | ForEach-Object {
$content = Get-Content (command_path $_.Name)
$command = ($content | Select-Object -Skip 1).Trim()
$summary = (summary $content).Trim()

$aliases += New-Object psobject -Property @{Name=$_.name; Summary=$summary; Command=$command}
}

if(!$aliases.count) {
warn "No aliases founds."
}
$aliases = $aliases.GetEnumerator() | Sort-Object Name
if($verbose) {
return $aliases | Select-Object Name, Command, Summary | Format-Table -autosize -wrap
} else {
return $aliases | Select-Object Name, Command | Format-Table -autosize -hidetablehead -wrap
}
}

switch($opt) {
"add" { add_alias $name $command }
"rm" { rm_alias $name }
"list" { list_aliases }
default { my_usage; exit 1 }
"add" { add_alias $name $command }
"rm" { rm_alias $name }
"list" { list_aliases }
default { my_usage; exit 1 }
}

exit 0
Loading