Skip to content

Commit

Permalink
More Mill launcher integration work (#4087)
Browse files Browse the repository at this point in the history
Fixes #2161

- Adds some integration tests for older mill versions
- Includes the `mill.ps1` file
  • Loading branch information
lihaoyi authored Dec 8, 2024
1 parent 7f1b618 commit d219462
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 6 deletions.
12 changes: 7 additions & 5 deletions dist/package.mill
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ object `package` extends RootModule with build.MillPublishJavaModule {
def millBootstrap = Task.Source(Task.workspace / "mill")
def millBootstrapBat = Task.Source(Task.workspace / "mill.bat")

def prepareBootstrapLauncher(bootstrap: os.Path, dest: os.Path, buildVersion: String) = {
val outputPath = dest / "mill"
def prepareBootstrapLauncher(bootstrap: os.Path, dest: os.Path, buildVersion: String, fileName: String) = {
val outputPath = dest / fileName
val millBootstrapGrepPrefix = "(\n *DEFAULT_MILL_VERSION=)"

os.write(
Expand All @@ -272,14 +272,16 @@ object `package` extends RootModule with build.MillPublishJavaModule {
"$1" + buildVersion
)
)
os.perms.set(outputPath, "rwxrwxrwx")

if (!scala.util.Properties.isWin) os.perms.set(outputPath, "rwxrwxrwx")

PathRef(outputPath)
}
def bootstrapLauncher = Task {
prepareBootstrapLauncher(millBootstrap().path, Task.dest, build.millVersion())
prepareBootstrapLauncher(millBootstrap().path, Task.dest, build.millVersion(), "mill")
}
def bootstrapLauncherBat = Task {
prepareBootstrapLauncher(millBootstrapBat().path, Task.dest, build.millVersion())
prepareBootstrapLauncher(millBootstrapBat().path, Task.dest, build.millVersion(), "mill.bat")
}

def examplePathsWithArtifactName:Task[Seq[(os.Path,String)]] = Task.Anon{
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mill.integration

import mill.testkit.UtestIntegrationTestSuite

import utest._

class LauncherOldVersionsTests(version: String) extends UtestIntegrationTestSuite {
val tests: Tests = Tests {
test - integrationTest { tester =>
val launcherEnv =
if (mill.main.client.Util.isWindows) "MILL_LAUNCHER_BAT"
else "MILL_LAUNCHER"

val workspacePath = tester.workspacePath

val launcherScript = sys.env(launcherEnv)
os.write.over(workspacePath / ".mill-version", version)

// Run Mill once beforehand just to make sure it gets initialized and
// none of the initialization output gets into `outText`
os.call(cmd = (launcherScript, "version"), cwd = workspacePath, stderr = os.Pipe)

val res = os.call(cmd = (launcherScript, "version"), cwd = workspacePath, stderr = os.Pipe)
val outText = res.out.text().trim
assert(outText == version)
}
}
}

// Split these into separate test groups so they can run in parallel
//
// Older versions of Mill do not work on latest macbook pros
//object LauncherVersionTests_0_1 extends LauncherOldVersionsTests("0.1.7")
//object LauncherVersionTests_0_2 extends LauncherOldVersionsTests("0.2.7")
//object LauncherVersionTests_0_3 extends LauncherOldVersionsTests("0.3.6")
//object LauncherVersionTests_0_4 extends LauncherOldVersionsTests("0.4.2")
//object LauncherVersionTests_0_5 extends LauncherOldVersionsTests("0.5.9")
//object LauncherVersionTests_0_6 extends LauncherOldVersionsTests("0.6.3")
//object LauncherVersionTests_0_7 extends LauncherOldVersionsTests("0.7.4")
//object LauncherVersionTests_0_8 extends LauncherOldVersionsTests("0.8.0")
object LauncherVersionTests_0_9 extends LauncherOldVersionsTests("0.9.12")
object LauncherVersionTests_0_10 extends LauncherOldVersionsTests("0.10.15")
object LauncherVersionTests_0_11 extends LauncherOldVersionsTests("0.11.13")
9 changes: 8 additions & 1 deletion integration/package.mill
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ object `package` extends RootModule {
def forkEnv =
super.forkEnv() ++
IntegrationTestModule.this.forkEnv() ++
Map("MILL_INTEGRATION_SERVER_MODE" -> (mode == "local" || mode == "server").toString) ++
Map(
"MILL_INTEGRATION_SERVER_MODE" -> (mode == "local" || mode == "server").toString,
"MILL_LAUNCHER" -> build.dist.bootstrapLauncher().path.toString,
"MILL_LAUNCHER_BAT" -> build.dist.bootstrapLauncherBat().path.toString,
) ++
testReleaseEnv()


def forkArgs = Task { super.forkArgs() ++ build.dist.forkArgs() }

def testReleaseEnv =
Expand All @@ -60,6 +65,8 @@ object `package` extends RootModule {

def compile: T[mill.scalalib.api.CompilationResult] =
T { mill.scalalib.api.CompilationResult(null, null) } // binary compatibility stub, not used

def testForkGrouping = discoveredTestClasses().grouped(1).toSeq
}

object local extends ModeModule
Expand Down
137 changes: 137 additions & 0 deletions mill.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# This is a wrapper script, that automatically download mill from GitHub release pages
# You can give the required mill version with --mill-version parameter
# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
#
# Project page: https://github.com/lefou/millw
# Script Version: 0.4.12
#
# If you want to improve this script, please also contribute your changes back!
#
# Licensed under the Apache License, Version 2.0

[CmdletBinding(PositionalBinding = $false)]

param(
[Parameter(ValueFromRemainingArguments = $true, Position = 0)]
[string[]] $remainingArgs
)

$DEFAULT_MILL_VERSION = $Env:DEFAULT_MILL_VERSION ?? '0.11.6'

$GITHUB_RELEASE_CDN = $Env:GITHUB_RELEASE_CDN ?? ''

$MILL_REPO_URL = 'https://github.com/com-lihaoyi/mill'

$MILL_VERSION = $null

if ($null -ne $remainingArgs) {
if ($remainingArgs[0] -eq '--mill-version') {
$remainingArgs = Select-Object -InputObject $remainingArgs -Skip 1
if ($null -ne $remainingArgs) {
$MILL_VERSION = $remainingArgs[0]
$remainingArgs = Select-Object -InputObject $remainingArgs -Skip 1
}
else {
Write-Error -Message "Please provide a version that matches one provided on $MILL_REPO_URL/releases"
throw [System.ArgumentNullException] '--mill-version'
}
}
}

if ($null -eq $MILL_VERSION) {
if (Test-Path -Path '.mill-version' -PathType Leaf) {
$MILL_VERSION = Get-Content -Path '.mill-version' -TotalCount 1
}
elseif (Test-Path -Path '.config/mill-version' -PathType Leaf) {
$MILL_VERSION = Get-Content -Path '.config/mill-version' -TotalCount 1
}
}

$MILL_USER_CACHE_DIR = Join-Path -Path $Env:LOCALAPPDATA -ChildPath 'mill'

$MILL_DOWNLOAD_PATH = $Env:MILL_DOWNLOAD_PATH ?? @(Join-Path -Path ${MILL_USER_CACHE_DIR} -ChildPath 'download')

if (-not (Test-Path -Path $MILL_DOWNLOAD_PATH)) {
New-Item -Path $MILL_DOWNLOAD_PATH -ItemType Directory | Out-Null
}

if ($null -eq $MILL_VERSION) {
Write-Warning -Message 'No mill version specified.'
Write-Warning -Message "You should provide a version via '.mill-version' file or --mill-version option."

if (-not (Test-Path -Path "$MILL_DOWNLOAD_PATH" -PathType Container)) {
New-Item "$MILL_DOWNLOAD_PATH" -ItemType Directory | Out-Null
}

$MILL_LATEST_PATH = Join-Path -Path $MILL_DOWNLOAD_PATH -ChildPath '.latest'

if (Test-Path -Path $MILL_LATEST_PATH -PathType Leaf) {
if ($(Get-Item -Path $MILL_LATEST_PATH).LastWriteTime -lt $(Get-Date).AddHours(-1)) {
$MILL_VERSION = Get-Content -Path $MILL_LATEST_PATH -TotalCount 1
}
}

if ($null -eq $MILL_VERSION) {
Write-Output 'Retrieving latest mill version ...'

# https://github.com/PowerShell/PowerShell/issues/20964
$targetUrl = try {
Invoke-WebRequest -Uri "$MILL_REPO_URL/releases/latest" -MaximumRedirection 0
}
catch {
$_.Exception.Response.Headers.Location.AbsoluteUri
}

$targetUrl -match "^$MILL_REPO_URL/releases/tag/(.+)$" | Out-Null

$MILL_VERSION = $Matches.1

if ($null -ne $MILL_VERSION) {
Set-Content -Path $MILL_LATEST_PATH -Value $MILL_VERSION
}
}

if ($null -eq $MILL_VERSION) {
$MILL_VERSION = $DEFAULT_MILL_VERSION
Write-Warning "Falling back to hardcoded mill version $MILL_VERSION"
}
else {
Write-Output "Using mill version $MILL_VERSION"
}
}

$MILL = "$MILL_DOWNLOAD_PATH/$MILL_VERSION.bat"

if (-not (Test-Path -Path $MILL -PathType Leaf)) {
$DOWNLOAD_SUFFIX, $DOWNLOAD_FROM_MAVEN = switch -Regex ($MILL_VERSION) {
'^0\.[0-4]\..*$' { '', $false }
'0\.(?:[5-9]\.|10\.|11\.0-M).*' { '-assembly', $false }
Default { '-assembly', $true }
}

if ($DOWNLOAD_FROM_MAVEN) {
$DOWNLOAD_URL = "https://repo1.maven.org/maven2/com/lihaoyi/mill-dist/$MILL_VERSION/mill-dist-$MILL_VERSION.jar"
}
else {
$MILL_VERSION -match '(\d+\.\d+\.\d+(?:-M\d+)?)' | Out-Null
$MILL_VERSION_TAG = $Matches.1
$DOWNLOAD_URL = "$GITHUB_RELEASE_CDN$MILL_REPO_URL/releases/download/$MILL_VERSION_TAG/$MILL_VERSION$DOWNLOAD_SUFFIX"
}
Write-Output "Downloading mill $MILL_VERSION from $DOWNLOAD_URL ..."

Invoke-WebRequest -Uri $DOWNLOAD_URL -OutFile $MILL
}

$MILL_MAIN_CLI = $Env:MILL_MAIN_CLI ?? $PSCommandPath

$MILL_FIRST_ARG = $null
$REMAINING_ARGUMENTS = $remainingArgs

if ($null -ne $remainingArgs) {
if ($remainingArgs[0] -eq '--bsp' -or $remainingArgs -eq '-i' -or $remainingArgs -eq '--interactive' -or $remainingArgs -eq '--no-server') {
$MILL_FIRST_ARG = $remainingArgs[0]
$REMAINING_ARGUMENTS = Select-Object -InputObject $remainingArgs -Skip 1
}
}

& $MILL $MILL_FIRST_ARG -D "mill.main.cli=$MILL_MAIN_CLI" $REMAINING_ARGUMENTS

0 comments on commit d219462

Please sign in to comment.