-
Notifications
You must be signed in to change notification settings - Fork 38
feat: add support for the Mill build tool #477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0fa032
feat: add support for the Mill build tool
ckipp01 fe2c71c
Use millFile instead of ./mill
olafurpg 870685d
Fix import ordering
olafurpg 60daded
Fix MissingBuildToolSuite
olafurpg bee918d
Revert "Use millFile instead of ./mill"
olafurpg 4117135
refactor(test): add millw into test resources
ckipp01 2d1ca93
docs: change minimal mill to 0.10.0
ckipp01 156a6dc
refactor(test): add in customizable targetRoot
ckipp01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
scip-java/src/main/scala/com/sourcegraph/scip_java/buildtools/MillBuildTool.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.sourcegraph.scip_java.buildtools | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.StandardCopyOption | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
import com.sourcegraph.scip_java.BuildInfo | ||
import com.sourcegraph.scip_java.commands.IndexCommand | ||
|
||
class MillBuildTool(index: IndexCommand) extends BuildTool("mill", index) { | ||
|
||
override def usedInCurrentDirectory(): Boolean = | ||
Files.isRegularFile(index.workingDirectory.resolve("build.sc")) | ||
|
||
override def generateScip(): Int = | ||
millVersion() match { | ||
case Some(version) if isSupportedMillVersion(version) => | ||
unconditionallyGenerateScip() | ||
case Some(version) => | ||
failFast( | ||
s"Unsupported Mill version '${version}'. " + | ||
s"To fix this problem, upgrade Mill to at least ${minimalMillVersion} and try again." | ||
) | ||
case None => | ||
failFast( | ||
s"No Mill version detected. " + | ||
s"To fix this problem, run the following command and try again: " + | ||
s"echo '${minimalMillVersion}' >> .mill-version" | ||
) | ||
} | ||
|
||
private def failFast(message: String): Int = { | ||
index.app.error(message) | ||
1 | ||
} | ||
|
||
private def unconditionallyGenerateScip(): Int = { | ||
val localMill = Files.isRegularFile(millFile) | ||
val command = | ||
if (localMill) { | ||
"./mill" | ||
} else { | ||
"mill" | ||
} | ||
val millProcess = index.process( | ||
List( | ||
command, | ||
"--import", | ||
s"ivy:io.chris-kipp::mill-scip::${BuildInfo.millScipVersion}", | ||
"io.kipp.mill.scip.Scip/generate" | ||
) | ||
) | ||
val scipFile = index | ||
.workingDirectory | ||
.resolve("out") | ||
.resolve("io") | ||
.resolve("kipp") | ||
.resolve("mill") | ||
.resolve("scip") | ||
.resolve("Scip") | ||
.resolve("generate.dest") | ||
.resolve("index.scip") | ||
|
||
if (millProcess.exitCode == 0 && Files.isRegularFile(scipFile)) { | ||
val output = index.workingDirectory.resolve("index.scip") | ||
Files.copy(scipFile, output, StandardCopyOption.REPLACE_EXISTING) | ||
index.app.info(output.toString) | ||
} | ||
millProcess.exitCode | ||
} | ||
|
||
private lazy val minimalMillVersion = BuildInfo.minimalMillVersion | ||
|
||
private lazy val millFile = index.workingDirectory.resolve("mill") | ||
|
||
private def isSupportedMillVersion(version: String): Boolean = | ||
if (version.startsWith("0.1")) | ||
true | ||
else | ||
false | ||
|
||
/** | ||
* Try to grab the Mill version from the .mill-version file. If not found we | ||
* fall back to the mill file which could be the official mill launcher or the | ||
* millw launcher renamed as mill, both which will have a DEFAULT_MILL_VERSION | ||
* line. | ||
*/ | ||
private def millVersion(): Option[String] = { | ||
val millVersionFile = index.workingDirectory.resolve(".mill-version") | ||
if (Files.isRegularFile(millVersionFile)) { | ||
Files.readAllLines(millVersionFile).asScala.headOption | ||
} else if (Files.isRegularFile(millFile)) { | ||
Files | ||
.readAllLines(millFile) | ||
.asScala | ||
.find(_.startsWith("DEFAULT_MILL_VERSION")) | ||
.map(line => line.dropWhile(!_.isDigit)) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#!/usr/bin/env sh | ||
|
||
# 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.2 | ||
# | ||
# If you want to improve this script, please also contribute your changes back! | ||
# | ||
# Licensed under the Apache License, Version 2.0 | ||
|
||
|
||
DEFAULT_MILL_VERSION=0.10.0 | ||
|
||
set -e | ||
|
||
MILL_REPO_URL="https://github.com/com-lihaoyi/mill" | ||
|
||
if [ -z "${CURL_CMD}" ] ; then | ||
CURL_CMD=curl | ||
fi | ||
|
||
# Explicit commandline argument takes precedence over all other methods | ||
if [ "$1" = "--mill-version" ] ; then | ||
shift | ||
if [ "x$1" != "x" ] ; then | ||
MILL_VERSION="$1" | ||
shift | ||
else | ||
echo "You specified --mill-version without a version." 1>&2 | ||
echo "Please provide a version that matches one provided on" 1>&2 | ||
echo "${MILL_REPO_URL}/releases" 1>&2 | ||
false | ||
fi | ||
fi | ||
|
||
# Please note, that if a MILL_VERSION is already set in the environment, | ||
# We reuse it's value and skip searching for a value. | ||
|
||
# If not already set, read .mill-version file | ||
if [ -z "${MILL_VERSION}" ] ; then | ||
if [ -f ".mill-version" ] ; then | ||
MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)" | ||
fi | ||
fi | ||
|
||
if [ -n "${XDG_CACHE_HOME}" ] ; then | ||
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download" | ||
else | ||
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download" | ||
fi | ||
|
||
# If not already set, try to fetch newest from Github | ||
if [ -z "${MILL_VERSION}" ] ; then | ||
# TODO: try to load latest version from release page | ||
echo "No mill version specified." 1>&2 | ||
echo "You should provide a version via '.mill-version' file or --mill-version option." 1>&2 | ||
|
||
mkdir -p "${MILL_DOWNLOAD_PATH}" | ||
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || ( | ||
# we might be on OSX or BSD which don't have -d option for touch | ||
# but probably a -A [-][[hh]mm]SS | ||
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest" | ||
) || ( | ||
# in case we still failed, we retry the first touch command with the intention | ||
# to show the (previously suppressed) error message | ||
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" | ||
) | ||
|
||
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993 | ||
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then | ||
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then | ||
# we know a current latest version | ||
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null) | ||
fi | ||
|
||
if [ -z "${MILL_VERSION}" ] ; then | ||
# we don't know a current latest version | ||
echo "Retrieving latest mill version ..." 1>&2 | ||
LANG=C ${CURL_CMD} -s -i -f -I ${MILL_REPO_URL}/releases/latest 2> /dev/null | grep --ignore-case Location: | sed s'/^.*tag\///' | tr -d '\r\n' > "${MILL_DOWNLOAD_PATH}/.latest" | ||
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null) | ||
fi | ||
|
||
if [ -z "${MILL_VERSION}" ] ; then | ||
# Last resort | ||
MILL_VERSION="${DEFAULT_MILL_VERSION}" | ||
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2 | ||
else | ||
echo "Using mill version ${MILL_VERSION}" 1>&2 | ||
fi | ||
fi | ||
|
||
MILL="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}" | ||
|
||
try_to_use_system_mill() { | ||
MILL_IN_PATH="$(command -v mill || true)" | ||
|
||
if [ -z "${MILL_IN_PATH}" ]; then | ||
return | ||
fi | ||
|
||
UNIVERSAL_SCRIPT_MAGIC="@ 2>/dev/null # 2>nul & echo off & goto BOF" | ||
|
||
if ! head -c 128 "${MILL_IN_PATH}" | grep -qF "${UNIVERSAL_SCRIPT_MAGIC}"; then | ||
if [ -n "${MILLW_VERBOSE}" ]; then | ||
echo "Could not determine mill version of ${MILL_IN_PATH}, as it does not start with the universal script magic2" 1>&2 | ||
fi | ||
return | ||
fi | ||
|
||
# Roughly the size of the universal script. | ||
MILL_VERSION_SEARCH_RANGE="2403" | ||
MILL_IN_PATH_VERSION=$(head -c "${MILL_VERSION_SEARCH_RANGE}" "${MILL_IN_PATH}" |\ | ||
sed -n 's/^.*-DMILL_VERSION=\([^\s]*\) .*$/\1/p' |\ | ||
head -n 1) | ||
|
||
if [ -z "${MILL_IN_PATH_VERSION}" ]; then | ||
echo "Could not determine mill version, even though ${MILL_IN_PATH} has the universal script magic" 1>&2 | ||
return | ||
fi | ||
|
||
if [ "${MILL_IN_PATH_VERSION}" = "${MILL_VERSION}" ]; then | ||
MILL="${MILL_IN_PATH}" | ||
fi | ||
} | ||
try_to_use_system_mill | ||
|
||
# If not already downloaded, download it | ||
if [ ! -s "${MILL}" ] ; then | ||
|
||
# support old non-XDG download dir | ||
MILL_OLD_DOWNLOAD_PATH="${HOME}/.mill/download" | ||
OLD_MILL="${MILL_OLD_DOWNLOAD_PATH}/${MILL_VERSION}" | ||
if [ -x "${OLD_MILL}" ] ; then | ||
MILL="${OLD_MILL}" | ||
else | ||
VERSION_PREFIX="$(echo $MILL_VERSION | cut -b -4)" | ||
case $VERSION_PREFIX in | ||
0.0. | 0.1. | 0.2. | 0.3. | 0.4. ) | ||
DOWNLOAD_SUFFIX="" | ||
;; | ||
*) | ||
DOWNLOAD_SUFFIX="-assembly" | ||
;; | ||
esac | ||
unset VERSION_PREFIX | ||
|
||
DOWNLOAD_FILE=$(mktemp mill.XXXXXX) | ||
# TODO: handle command not found | ||
echo "Downloading mill ${MILL_VERSION} from ${MILL_REPO_URL}/releases ..." 1>&2 | ||
MILL_VERSION_TAG=$(echo $MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/') | ||
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}" | ||
chmod +x "${DOWNLOAD_FILE}" | ||
mkdir -p "${MILL_DOWNLOAD_PATH}" | ||
mv "${DOWNLOAD_FILE}" "${MILL}" | ||
|
||
unset DOWNLOAD_FILE | ||
unset DOWNLOAD_SUFFIX | ||
fi | ||
fi | ||
|
||
unset MILL_DOWNLOAD_PATH | ||
unset MILL_OLD_DOWNLOAD_PATH | ||
unset OLD_MILL | ||
unset MILL_VERSION | ||
unset MILL_VERSION_TAG | ||
unset MILL_REPO_URL | ||
|
||
exec "${MILL}" "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.