#requires -version 2 # NB lein -? will give help for the powershell script # lein help will give command help for Leiningen Param( [Parameter(Mandatory=$true, Position = 0, HelpMessage = "The command to pass to Leiningen")] [string]$command, [Parameter(HelpMessage = "The version of Clojure to run. Needs to be compatible with the version of Leiningen or strange things can happen.")] [string]$clojureVersion = "1.1.0", # e.g. 1.2.0-master-20100430.160229-59 [Parameter(HelpMessage = "The location of the Clojure jar. Don't use this unless you're compiling Clojure from trunk.")] [string]$clojureJar, # e.g. "D:\julian\Documents\Projects\oss\circumspec\lib\clojure\1.2.0-master-20100430.160229-59.jar" [Parameter(HelpMessage = "The version of Leiningen. 1.1.0 is the currently stable version.")] [string]$leinVersion = "1.1.0", # e.g. 1.2.0 [Parameter(HelpMessage = "The location of Leiningen standalone jar. Don't use this unless you're compiling Leiningen from trunk. Also, make sure it points at an uberjar, not a regular jar.")] [string]$leinJar, # e.g. D:\Julian\Documents\Projects\oss\leiningen\leiningen-standalone.jar [Parameter(HelpMessage = "Where to download Leiningen from if you're doing a self-install. Make sure this value is compatible with leinJar and leinVersion or you'll never unpick the mess.")] [string]$leinUrl, # e.g. http://github.com/downloads/technomancy/leiningen/leiningen-1.1.0-standalone.jar [Parameter(HelpMessage = "Completely overrides Leiningen and runs an arbitrary Clojure command. Don't use it unless you know what you're doing.")] [string]$clojureCommand, [Parameter(ValueFromRemainingArguments = $true)]$args ) (Get-Host).PrivateData.VerboseForegroundColor = "Yellow" # This is pretty badly behaved. Basic users will like it, but advanced users may want to comment this out. # Functions function coalesce { (@($args | ?{$_}) + $null)[0] } function join($separator, $items) { if($items){ [String]::Join($separator, $items) } } function get-libs(){ if(test-path "lib"){ $jars = join ";" ((ls lib -include *.jar -recur) |% { $_.FullName }) } } function maven-location($product, $version) { $format = "{0}`\.m2`\repository`\{1}`\{1}`\{2}`\{1}-{2}.jar"; return [String]::Format($format, $env:UserProfile, $product, $VERSION) } # Argument Defaults $clojureJar = coalesce $clojureJar $env:clojureJar (maven-location "clojure" $clojureVersion) $leinJar = coalesce $leinJar $env:leinJar (maven-location "leiningen" $leinVersion) $leinUrl = coalesce $leinUrl "http://github.com/downloads/technomancy/leiningen/leiningen-$leinVersion-standalone.jar" # Compute ClassPath $libs = (get-libs) $classPath = "src;classes$libs" # Classes directory needs to exist before the JVM is launched apparently if(-not (test-path "classes")){ [void](mkdir classes) } # If we are not running from a checkout if(-not (test-path "classes\leiningen\core.class")){ if((-not (test-path $leinJar)) -and ($command -ne "self-install")){ write-error "Leiningen is not installed. Please run `"lein self-install`"." exit 1 } $classPath= "$classPath;$leinJar" } if(("test", "repl") -contains $command) { # tests should be runnable from repl $testDir = (get-item -erroraction SilentlyContinue "test").FullName $classPath= $testDir + ";" + $classPath } elseif (("compile", "jar", "uberjar") -contains $command){ # Deps need to run before the JVM launches for tasks that need them if(-not $libs){ & $myInvocation.MyCommand deps skip-dev } } write-verbose "Taking Clojure $clojureVersion from $clojureJar" write-verbose "Taking Leiningen $leinVersion from $leinJar" $usingClassPath = (join "`n" ($classPath.Split(";"))) write-verbose "Using class path: `n$usingClassPath`n--End Class Path--" if($command -eq "self-install"){ write-verbose "Downloading Leiningen from $leinUrl" $jarPath = split-path $leinJar if(-not (test-path $jarPath)){ mkdir $jarPath } (new-object System.Net.WebClient).DownloadFile($leinUrl, $leinJar) } else { if ($clojureCommand -eq "") { if ($command -ne "repl") { if ($args) { $newArgs = new-object System.Collections.ArrayList [void]$newArgs.Add($command) $newArgs.AddRange($args) $args = $newArgs } else { $args = @($command) } } $escapedArgs = $args |% { $allArgs = "" } { $allArgs += "\`"$_\`" " } { $allArgs } $clojureCommand = "(use 'leiningen.core) (-main $escapedArgs)" } write-verbose "Clojure command: $clojureCommand" java "-Xbootclasspath/a:$clojureJar" -cp "$classPath" clojure.main -e """$clojureCommand""" } # Credits and Notes: # Coalesce by Keith Dalby http://solutionizing.net/2008/12/20/powershell-coalesce-and-powershellasp-query-string-parameters/ # Help with parameters by Keith Hill http://stackoverflow.com/questions/2795582/any-way-to-specify-a-non-positional-parameter-in-a-powershell-script # Heavily based on Roland Sadowski's original lein.ps1, but merged with concepts from Phil Hagelberg's lein.bat # Anything left might be original :)