File: lein.ps1

package info (click to toggle)
leiningen-clojure 2.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,124 kB
  • sloc: xml: 925; sh: 776; lisp: 46; makefile: 45; java: 16
file content (164 lines) | stat: -rw-r--r-- 6,810 bytes parent folder | download
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<#
.Synopsis
    Leiningen bootstrap.

.Parameter Command
    The command to pass to leiningen.

.Notes
    This is a very literal port of lein.bat to PowerShell.

    TODO:
    - Determine which (if any) environment variables are used from within Leiningen/Clojure
      and convert the rest to local- or script-scoped variables.
    - Probably should parse the version from the newest .jar filename, rather than hard-code
      it and search for the .jar file from it.
    - Further reduce/simplify/refactor away from batch-file idioms toward idiomatic PowerShell.

.Link
    https://leiningen.org/
#>

#require -version 3
[CmdletBinding(SupportsShouldProcess=$true)] Param(
[Parameter(Position=0,ValueFromRemainingArguments=$true)][string[]]$Command = 'help'
)

function Set-ParentLocation([string]$file)
{
    for($dir = [IO.DirectoryInfo]"$PWD"; $dir.Parent; $dir = $dir.Parent)
    {
        if(Test-Path (Join-Path $dir.FullName $file) -PathType Leaf) { cd $dir.FullName; break }
    }
}

function Initialize-Environment
{
    $env:LEIN_VERSION = '2.11.2'
    $env:SNAPSHOT = if($env:LEIN_VERSION -like '*-SNAPSHOT'){'YES'}else{'NO'} #TODO: Still needed?
    $env:ORIGINAL_PWD = $PWD -replace '\\$','\\'
    Set-ParentLocation project.clj
    if(!$env:LEIN_HOME) {$env:LEIN_HOME = "$env:USERPROFILE\.lein"}
    if(!$env:LEIN_JAR) {$env:LEIN_JAR = "$env:LEIN_HOME\self-installs\leiningen-$env:LEIN_VERSION-standalone.jar"}
    if($PSVersionTable.PSVersion.Major -gt 5) {
        if(!($([System.Net.WebProxy]::new()).IsBypassed('https://github.com/')))
        {
            $proxy = $([System.Net.WebProxy]::new()).GetProxy('https://github.com/')
            Write-Verbose "Using proxy: $proxy"
            $Script:PSBoundParameters = @{
                'Invoke-WebRequest:Proxy' = $proxy
                'Invoke-WebRequest:ProxyUseDefaultCredentials' = $true
            }
        }
    } else {
        if(!([Net.WebRequest]::DefaultWebProxy.IsBypassed('https://github.com/')))
        {
            $proxy = [Net.WebRequest]::DefaultWebProxy.GetProxy('https://github.com/')
            Write-Verbose "Using proxy: $proxy"
            $Script:PSBoundParameters = @{
                'Invoke-WebRequest:Proxy' = $proxy
                'Invoke-WebRequest:ProxyUseDefaultCredentials' = $true
            }
        }
    }
}

function Use-ClassPath([string]$Value)
{
    $env:CLASSPATH =
        if(!(Test-Path .lein-classpath -PathType Leaf)) {$Value}
        else {"$(gc .lein-classpath |? {$_} |select -Last 1);$Value"}
}

function Initialize-Binary
{
    if(!(Test-Path $env:LEIN_JAR -PathType Leaf))
    {throw "$env:LEIN_JAR cannot be found. Try running 'lein self-install' or change the LEIN_JAR environment variable."}
    Use-ClassPath $env:LEIN_JAR
}

function Initialize-Source
{
    $env:LEIN_ROOT = $PSScriptRoot
    $env:bootstrapfile = "$env:LEIN_ROOT\leiningen-core\.lein-bootstrap"
    Write-Verbose "LEIN_ROOT=$env:LEIN_ROOT"
    if($env:bootstrapfile -like '*;*') #TODO: Still important?
    {throw "bootstrap file ($env:bootstrapfile) should not contain semicolons!"}
    if(!(Test-Path $env:bootstrapfile -PathType Leaf)) 
    {throw @'
Leiningen is missing its dependencies. Run 'lein bootstrap' in the leiningen-core/ directory with a stable release.
See CONTRIBUTING.md for details.
'@}
    if((Get-Content $env:bootstrapfile -raw) -like '*"*')
    {throw "Double quotes detected inside bootstrap file $env:bootstrapfile!?"}
    $env:LEIN_LIBS = (Get-Content $env:bootstrapfile |% {$_ -split ';'} |% {"$_"}) -join ';'
    Write-Verbose "LEIN_LIBS=$env:LEIN_LIBS"
    Use-ClassPath "$env:LEIN_LIBS;$env:LEIN_ROOT\src;$env:LEIN_ROOT\resources"
}

function Install-Self
{
    if(Test-Path $env:LEIN_JAR -PathType Leaf) {throw "$env:LEIN_JAR already exists. Delete and retry."}
    $jardir = ([IO.FileInfo]$env:LEIN_JAR).Directory.FullName
    if(!(Test-Path $jardir -PathType Container)) {mkdir $jardir |Out-Null}
    @{ # splatting Invoke-WebRequest due to long URI
        Uri = "https://github.com/technomancy/leiningen/releases/download/$env:LEIN_VERSION/leiningen-$env:LEIN_VERSION-standalone.jar"
        OutFile = $env:LEIN_JAR
    } |% {Write-Progress 'Install-Self' $_.Uri -CurrentOperation "Downloading to $env:LEIN_JAR" ; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest @_}
    Write-Progress 'Install-Self' -Completed
}

function Update-Self
{
    $targetVersion = if($Command.Length -gt 1) {$Command[1]} else {'stable'}
    if(!$PSCmdlet.ShouldProcess($PSCommandPath,"upgrade to $targetVersion")) {throw 'Cancelled'}
    @{ # splatting Invoke-WebRequest due to long URI
        Uri = "https://github.com/technomancy/leiningen/raw/$targetVersion/bin/lein.cmd"
        OutFile = "$PSScriptRoot\lein.cmd.pending"
    } |% {Write-Progress 'Update-Self' $_.Uri -CurrentOperation "Downloading to $PSScriptRoot\lein.cmd.pending" ; Invoke-WebRequest @_}
    @{ # splatting Invoke-WebRequest due to long URI
        Uri = "https://github.com/technomancy/leiningen/raw/$targetVersion/bin/lein.ps1"
        OutFile = "$PSCommandPath.pending"
    } |% {Write-Progress 'Update-Self' $_.Uri -CurrentOperation "Downloading to $PSCommandPath.pending" -PercentComplete 50 ; Invoke-WebRequest @_}
    Write-Progress 'Update-Self' -Completed
    Move-Item "$PSScriptRoot\lein.cmd.pending" "$PSScriptRoot\lein.cmd" -force
    Move-Item "$PSCommandPath.pending" "$PSCommandPath" -force
    . "$PSCommandPath" self-install
}

function Invoke-Java
{
    Write-Verbose "CLASSPATH=$env:CLASSPATH"
    $env:JAVA_CMD = if($env:JAVA_CMD){$env:JAVA_CMD -replace '\A"|"\Z',''}else{'java'}
    $env:LEIN_JAVA_CMD = if($env:LEIN_JAVA_CMD){$env:LEIN_JAVA_CMD -replace '\A"|"\Z',''}else{$env:JAVA_CMD}
    if(!$env:JVM_OPTS){$env:JVM_OPTS = $env:JAVA_OPTS}
    $JavaArgs = @(
        '-client',$env:LEIN_JVM_OPTS,
        '-Dfile.encoding=UTF-8',
        "`"-Dclojure.compile.path=$PWD/target/classes`"", #TODO: Add this line only when we're initializing from source
        "`"-Dleiningen.original.pwd=$env:ORIGINAL_PWD`"",
        '-cp',$env:CLASSPATH,
        'clojure.main',
        '-m','leiningen.core.main'
    )
    &$env:LEIN_JAVA_CMD @JavaArgs @Command
}

function Invoke-Leiningen
{
    Initialize-Environment
    switch($Command[0])
    {
        self-install {Install-Self}
        upgrade      {Update-Self }
        downgrade    {Update-Self }
        default
        {
            if(Test-Path "$PSCommandPath\..\src\leiningen\version.clj" -PathType Leaf) {Initialize-Source}
            else {Initialize-Binary}
            Invoke-Java
        }
    }
}

Invoke-Leiningen