-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcraftenv.ps1
140 lines (119 loc) · 3.46 KB
/
craftenv.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
# this file sets some environment variables that are needed
# for finding programs and libraries etc.
# by Hannah von Reth <[email protected]>
# you should copy CraftSettings.ini.template to ..\etc\CraftSettings.ini
# and adapt it to your needs (see that file for more info)
cls
$env:CraftRoot=[System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
&{
[version]$minPythonVersion = '3.9'
function findPython([string] $name)
{
if ($env:CRAFT_PYTHON -and (Test-Path -path $env:CRAFT_PYTHON)) {
return
}
$py = (Get-Command $name -ErrorAction SilentlyContinue)
if ($py) {
if (($py | Get-Member Version) -and $py.Version -ge $minPythonVersion) {
$env:CRAFT_PYTHON=$py.Source
} elseif ((& $py --version) -match 'Python (?<version>[\d.]+)' -and [version]$Matches['version'] -ge $minPythonVersion) {
$env:CRAFT_PYTHON=$py.Source
}
}
}
function readINI([string] $fileName)
{
$ini = @{}
switch -regex -file $fileName {
"^\[(.+)\]$" {
$section = $matches[1].Trim()
$ini[$section] = @{}
}
"^\s*([^#].+?)\s*=\s*(.*)" {
$name,$value = $matches[1..2]
$ini[$section][$name] = $value.Trim()
}
}
$ini
}
if(test-path -path $env:CraftRoot\..\etc\kdesettings.ini)
{
mv $env:CraftRoot\..\etc\kdesettings.ini $env:CraftRoot\..\etc\CraftSettings.ini
}
if(test-path -path $env:CraftRoot\..\etc\CraftSettings.ini)
{
$settings = readINI $env:CraftRoot\..\etc\CraftSettings.ini
}
else
{
Write-Error("$env:CraftRoot\..\etc\CraftSettings.ini Does not exist")
break
}
if($settings["Paths"].Contains("Python") -and (Test-Path -path $settings["Paths"]["Python"]))
{
# prefer the python from the settings
findPython("{0}/python" -f $settings["Paths"]["Python"])
}
findPython("python3.12")
findPython("python3.11")
findPython("python3.10")
findPython("python3.9")
findPython("python3")
findPython("python")
findPython("py")
}
function Global:craft()
{
$python = (Get-Command "$env:KDEROOT/dev-utils/bin/python3" -ErrorAction SilentlyContinue).Source
if (-not $python) {
$python = $env:CRAFT_PYTHON
}
return & $python ([IO.PATH]::COMBINE("$env:CraftRoot", "bin", "craft.py")) @args
}
function Global:craftCd([string] $package, [string]$property, [string] $target="")
{
$command = @()
if ($target) {
$command += @("--target", $target)
}
$command += @("-q", "--ci-mode", "--get", $property, $package)
$dir = craft @command | Out-String
if($LASTEXITCODE) {
Write-Host $dir
} else {
cd "$dir".Trim()
}
}
function Global:cb([string] $package, [string] $target="")
{
craftCd $package "buildDir()" $target
}
function Global:cs([string] $package, [string] $target="")
{
craftCd $package "sourceDir()" $target
}
function Global:ci([string] $package, [string] $target="")
{
craftCd $package "imageDir()" $target
}
function Global:cr()
{
cd $env:CraftRoot/..
}
if (-not $env:CRAFT_PYTHON) {
Write-Error "Failed to detect python"
exit(1)
}
if($args.Length -ne 0)
{
craft --run @args
} else {
$env2 = ConvertFrom-Json (& $env:CRAFT_PYTHON ([IO.PATH]::COMBINE("$env:CraftRoot", "bin", "CraftSetupHelper.py")) @("--setup", "--format=json"))
Get-ChildItem env: | ForEach-Object {
Remove-Item ("ENV:{0}" -f ${_}.Name)
}
foreach ($property in $env2.PSObject.Properties) {
Set-Item -force -path "ENV:\$($property.Name)" -value "$($property.Value)"
}
cr
}