Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ indent_size = 2
ktlint_code_style = intellij_idea
ktlint_standard_no-wildcard-imports = disabled

[example/kotlinlib/basic/5-single-file/**/*]
ktlint = disabled

[example/kotlinlib/module/1-single-file/**/*]
ktlint = disabled

[example/kotlinlib/linting/**/*]
ktlint = disabled

Expand Down
2 changes: 1 addition & 1 deletion build.mill
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//| mill-version: 1.0.4-26-a6e4c1
//| mill-version: 1.0.4-38-cbb9e8
//| mill-jvm-opts: ["-XX:NonProfiledCodeHeapSize=250m", "-XX:ReservedCodeCacheSize=500m"]
//| mill-opts: ["--jobs=0.5C"]

Expand Down
13 changes: 10 additions & 3 deletions core/constants/src/mill/constants/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand Down Expand Up @@ -84,15 +86,20 @@ private static String throwBuildHeaderError(
+ ": " + line + "\n" + msg);
}

public static String readBuildHeader(java.nio.file.Path buildFile, String errorFileName) {
public static String readBuildHeader(Path buildFile, String errorFileName) {
return readBuildHeader(buildFile, errorFileName, false);
}

public static String readBuildHeader(
Path buildFile, String errorFileName, boolean allowNonBuild) {
try {
java.util.List<String> lines = java.nio.file.Files.readAllLines(buildFile);
java.util.List<String> lines = Files.readAllLines(buildFile);
boolean readingBuildHeader = true;
java.util.List<String> output = new ArrayList<>();
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if (!line.startsWith("//|")) readingBuildHeader = false;
else if (!buildFile.getFileName().toString().startsWith("build.")) {
else if (!allowNonBuild && !buildFile.getFileName().toString().startsWith("build.")) {
throwBuildHeaderError(
errorFileName,
i,
Expand Down
5 changes: 1 addition & 4 deletions core/internal/cli/package.mill
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ import millbuild.*
object `package` extends MillPublishScalaModule {
def moduleDeps = Seq(build.libs.util)

def mvnDeps = Seq(
Deps.millModuledefs,
Deps.mainargs
)
def mvnDeps = Seq(Deps.mainargs)
}
5 changes: 5 additions & 0 deletions core/internal/cli/src/mill/internal/MillCliConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ case class MillCliConfig(
doc = """Runs Mill in tab-completion mode"""
)
tabComplete: Flag = Flag(),
@arg(
short = 'f',
doc = """Select the build.mill file or Java/Scala/Kotlin script file to run"""
)
file: Option[os.Path] = None,

// ==================== DEPRECATED CLI FLAGS ====================
@arg(hidden = true, short = 'h', doc = "Unsupported")
Expand Down
35 changes: 35 additions & 0 deletions example/javalib/basic/5-single-file/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//| mvnDeps:
//| - "net.sourceforge.argparse4j:argparse4j:0.9.0"
//| - "org.thymeleaf:thymeleaf:3.1.1.RELEASE"
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.Namespace;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

public class Foo {
public static String generateHtml(String text) {
Context context = new Context();
context.setVariable("text", text);
return new TemplateEngine().process("<h1 th:text=\"${text}\"></h1>", context);
}

public static void main(String[] args) {
ArgumentParser parser = ArgumentParsers.newFor("template")
.build()
.defaultHelp(true)
.description("Inserts text into a HTML template");

parser.addArgument("-t", "--text").required(true).help("text to insert");

Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}

System.out.println(generateHtml(ns.getString("text")));
}
}
30 changes: 30 additions & 0 deletions example/javalib/basic/5-single-file/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// SNIPPET:FILE
/** See Also: Foo.java */
/** Usage
> ./mill Foo.java --text hello
compiling 1 Java source to...
<h1>hello</h1>
*/

/** Usage
> ./mill -f Foo.java run --text hello
<h1>hello</h1>
*/

//// SNIPPET:END
//// SNIPPET:MORE

/** Usage
> ./mill -f Foo.java show assembly # show the output of the assembly task
".../out/Foo.java/assembly.dest/out.jar"

> java -jar ./out/Foo.java/assembly.dest/out.jar --text hello
<h1>hello</h1>

> ./out/Foo.java/assembly.dest/out.jar --text hello # mac/linux
<h1>hello</h1>


*/

//// SNIPPET:END
17 changes: 17 additions & 0 deletions example/javalib/module/1-single-file/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//| jvmId: "graalvm-community:24"
//| nativeImageOptions: ["--no-fallback"]
//| publishVersion: "0.0.1"
//| artifactName: "example"
//| pomSettings:
//| description: "Example"
//| organization: "com.lihaoyi"
//| url: "https://github.com/com.lihaoyi/example"
//| licenses: ["MIT"]
//| versionControl: "https://github.com/com.lihaoyi/example"
//| developers: [{"name": "Li Haoyi", "email": "example@example.com"}]

public class Bar {
public static void main(String[] args) {
System.out.println("Hello Graal Native: " + System.getProperty("java.version"));
}
}
24 changes: 24 additions & 0 deletions example/javalib/module/1-single-file/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// SNIPPET:LAST

/** See Also: Bar.java */
/** Usage

> ./mill -f Bar.java nativeImage

> out/Bar.java/nativeImage.dest/native-executable
Hello Graal Native: 24...

> ./mill -f Bar.java publishLocal
Publishing Artifact(com.lihaoyi,example...,0.0.1) to ivy repo ...

*/

// Apart from publishing locally, you can also publish this single-file project to
// Sonatype Maven Central via:
//
// [source,console]
// ----
// > ./mill -f Bar.java mill.javalib.SonatypeCentralPublishModule/
// ----

//// SNIPPET:END
20 changes: 20 additions & 0 deletions example/kotlinlib/basic/5-single-file/Foo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//| mvnDeps:
//| - "com.github.ajalt.clikt:clikt:4.4.0"
//| - "org.jetbrains.kotlinx:kotlinx-html:0.11.0"
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import kotlinx.html.h1
import kotlinx.html.stream.createHTML

class Foo : CliktCommand() {
val text by option("-t", "--text", help = "text to insert").required()

override fun run() {
echo(generateHtml(text))
}
}

fun generateHtml(text: String): String = createHTML(prettyPrint = false).h1 { text(text) }.toString()

fun main(args: Array<String>) = Foo().main(args)
29 changes: 29 additions & 0 deletions example/kotlinlib/basic/5-single-file/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// SNIPPET:FILE
/** See Also: Foo.kt */
/** Usage
> ./mill Foo.kt --text hello
Compiling 1 Kotlin sources to...
<h1>hello</h1>
*/

//// SNIPPET:END
//// SNIPPET:MORE

/** Usage
> ./mill -f Foo.kt run --text hello
<h1>hello</h1>
*/

/** Usage
> ./mill -f Foo.kt show assembly # show the output of the assembly task
".../out/Foo.kt/assembly.dest/out.jar"

> java -jar ./out/Foo.kt/assembly.dest/out.jar --text hello
<h1>hello</h1>

> ./out/Foo.kt/assembly.dest/out.jar --text hello # mac/linux
<h1>hello</h1>

*/

//// SNIPPET:END
15 changes: 15 additions & 0 deletions example/kotlinlib/module/1-single-file/Bar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//| jvmId: "graalvm-community:24"
//| nativeImageOptions: ["--no-fallback"]
//| publishVersion: "0.0.1"
//| artifactName: "example"
//| pomSettings:
//| description: "Example"
//| organization: "com.lihaoyi"
//| url: "https://github.com/com.lihaoyi/example"
//| licenses: ["MIT"]
//| versionControl: "https://github.com/com.lihaoyi/example"
//| developers: [{"name": "Li Haoyi", "email": "example@example.com"}]

fun main(args: Array<String>) {
println("Hello Graal Native: " + System.getProperty("java.version"))
}
23 changes: 23 additions & 0 deletions example/kotlinlib/module/1-single-file/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// SNIPPET:LAST
/** See Also: Bar.kt */
/** Usage

> ./mill -f Bar.kt nativeImage

> out/Bar.kt/nativeImage.dest/native-executable
Hello Graal Native: 24...

> ./mill -f Bar.kt publishLocal
Publishing Artifact(com.lihaoyi,example...,0.0.1) to ivy repo ...

*/

// Apart from publishing locally, you can also publish this single-file project to
// Sonatype Maven Central via:
//
// [source,console]
// ----
// > ./mill -f Bar.kt mill.javalib.SonatypeCentralPublishModule/
// ----

//// SNIPPET:END
3 changes: 2 additions & 1 deletion example/package.mill
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ object `package` extends Module {
.map {
case (s"see:$path", txt) =>
// avoid .stripMargin, as the embedded content may contain the margin symbol
val lang = os.FilePath(path).ext
s"""
.$path ({mill-example-url}/$examplePath/$path[browse])
[source,scala,subs="attributes,verbatim"]
[source,$lang,subs="attributes,verbatim"]
----
$txt
----"""
Expand Down
18 changes: 18 additions & 0 deletions example/scalalib/basic/5-single-file/Foo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//| mvnDeps:
//| - "com.lihaoyi::scalatags:0.13.1"
//| - "com.lihaoyi::mainargs:0.7.6"
import scalatags.Text.all.*
import mainargs.{main, ParserForMethods}

object Foo {
def generateHtml(text: String) = {
h1(text).toString
}

@main
def main(text: String) = {
println(generateHtml(text))
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
50 changes: 50 additions & 0 deletions example/scalalib/basic/5-single-file/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Mill also allows you to run single-file {language} programs easily from the command-line,
// even those that contain third-party dependencies or other such configuration. These can be
// useful as a replacement for Bash scripts, letting you write small scripts or programs
// in {language} with full access to third-party libraries and other build-tool features.
//
// For example, given the single-file program below, it can be run directly using Mill:

//// SNIPPET:FILE
/** See Also: Foo.scala */

/** Usage
> ./mill Foo.scala --text hello
compiling 1 Scala source to...
<h1>hello</h1>
*/

//// SNIPPET:END

// The `./mill Foo.{language-ext}` syntax is shorthand for `./mill -f Foo.{language-ext} run`
// or `./mill --file Foo.{language-ext} run`, which can also be passed explicitly. The
// explicit `-f`/`--file` also allows you to call other tasks on the single-file project,
// e.g. bundling it into an assembly:

//// SNIPPET:MORE

/** Usage
> ./mill -f Foo.scala run --text hello
<h1>hello</h1>
*/

/** Usage
> ./mill -f Foo.scala show assembly # show the output of the assembly task
".../out/Foo.scala/assembly.dest/out.jar"

> java -jar ./out/Foo.scala/assembly.dest/out.jar --text hello
<h1>hello</h1>

> ./out/Foo.scala/assembly.dest/out.jar --text hello # mac/linux
<h1>hello</h1>

*/

//// SNIPPET:END

// For more details on single-file projects, see
// xref:{language-small}lib/module-config.adoc#_configuring_single_file_projects[Configuring Single-File Projects]
// The rest of the examples below discuss the Mill config for building larger projects
// beyond the single-file project format discussed above, using a dedicated `build.mill`
// file to provide additional flexibility in setting up your project.
//
17 changes: 17 additions & 0 deletions example/scalalib/module/1-single-file/Bar.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//| jvmId: "graalvm-community:24"
//| nativeImageOptions: ["--no-fallback"]
//| publishVersion: "0.0.1"
//| artifactName: "example"
//| pomSettings:
//| description: "Example"
//| organization: "com.lihaoyi"
//| url: "https://github.com/com.lihaoyi/example"
//| licenses: ["MIT"]
//| versionControl: "https://github.com/com.lihaoyi/example"
//| developers: [{"name": "Li Haoyi", "email": "example@example.com"}]

object Bar {
def main(args: Array[String]): Unit = {
println("Hello Graal Native: " + System.getProperty("java.version"))
}
}
Loading
Loading