Skip to content

Commit

Permalink
Reviews Fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshumahajan138 committed Nov 6, 2024
1 parent 8884cdb commit 4b76b11
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 198 deletions.
76 changes: 18 additions & 58 deletions example/extending/newlang/5-hello-python/build.mill
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// == Basic Python Pipeline
// == Python Integration in Mill

// This Example demonstrates integration of https://www.python.org[Python]
// compilation into a Mill build to compile https://www.python.org[Python] Scripts. Mill
// does not come bundled with https://www.python.org[Python] integration, so here we begin setting
// one up from first principles using the Basic `Python3`(Already Present in OS) command Line.
// This example demonstrates the integration of https://www.python.org[Python]
// compilation into a Mill build to compile Python scripts. Mill does not come
// bundled with Python integration, so here we will set one up from scratch
// using the basic `python3` command line, which is already present in the OS.

// === Python Setup
// === Python installation Setup
//
// First, we need to use the `Python3` CLI tool to create virtual environment and
// install `mypy` for Type Checking using the official Python Virtual Environment
Expand All @@ -16,39 +16,20 @@
package build
import mill._

def createVenv: T[PathRef] = Task {
val venvDir = T.dest / "venv"

os.call(
Seq(
"python3",
"-m",
"venv",
venvDir.toString
)
)

PathRef(venvDir)
}

def setup: T[PathRef] = Task {
// creating virtual environment
val pythonVenv = createVenv().path / "bin" / "python3"
os.call(("python3","-m","venv",Task.dest / "venv"))

val pythonVenv = Task.dest / "venv" / "bin" / "python3"

// installing mypy for Type Checking
os.call(
Seq(
pythonVenv.toString,
"-m",
"pip",
"install",
"mypy"
)
)
os.call((pythonVenv,"-m","pip","install","mypy"))

PathRef(pythonVenv)
}

// The `createVenv` task ensures the Python virtual environment creation process using the official documentaion.
// The `setup` task ensures the Python virtual environment creation process using the official
// https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments[Documentation].
// After this, we need to install python `mypy` library for Type Checking and Error Checking before actual run of the code,
// using the offical https://mypy.readthedocs.io/en/stable[Documentation] for `mypy`

Expand All @@ -72,31 +53,13 @@ def typeCheck: T[PathRef] = Task {
val pythonVenv = setup().path

os.call(
Seq(
pythonVenv.toString,
"-m",
"mypy",
"--strict",
s"${sources().path / mainFileName()}"
),
(pythonVenv,"-m","mypy","--strict",sources().path / mainFileName()),
stdout = os.Inherit
)

PathRef(pythonVenv)
}

// At this point, we have a minimal working build, with a build graph that looks like this:
//
// ```graphviz
// digraph G {
// rankdir=LR
// node [shape=box width=0 height=0 style=filled fillcolor=white]
// createVenv -> setup -> typeCheck
// sources -> typeCheck
// mainFileName -> typeCheck
// }
// ```

// When `typeCheck` task sets up the Setup for Virtual Environment and checks the main file for error free code
// then we have `main.py` and running this file will result in either a generic greeting
// or a personalized one based on the command-line arguments passed, allowing for flexible interaction with the user.
Expand All @@ -114,10 +77,7 @@ def run(args: mill.define.Args) = Task.Command {
val pythonVenv = typeCheck().path

os.call(
Seq(
pythonVenv.toString,
s"${sources().path / mainFileName()}"
) ++ args.value,
(pythonVenv,sources().path / mainFileName(),args.value),
stdout = os.Inherit
)
}
Expand All @@ -129,7 +89,7 @@ def run(args: mill.define.Args) = Task.Command {
// digraph G {
// rankdir=LR
// node [shape=box width=0 height=0 style=filled fillcolor=white]
// createVenv -> setup -> typeCheck -> run
// setup -> typeCheck -> run
// sources -> typeCheck
// mainFileName -> typeCheck
// sources -> run
Expand All @@ -153,6 +113,6 @@ Hello, Mill Python!

// Uncomment the Result variable line(Line Number:13) in the `src/main.py` code to see `typeCheck` error

// So that's a minimal example of implementing a `Basic Python
// pipeline` locally. Next, we will look at turning it into a `PythonModule` that
// So that's a minimal example of implementing a Basic `Python
// Integration` in Mill Build Tool. Next, we will look at turning it into a `PythonModule` that
// can be re-used
52 changes: 11 additions & 41 deletions example/extending/newlang/6-python-modules/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,16 @@ trait PythonModule extends Module {
/** Returns the name of the main Python file. */
def mainFileName: T[String] = Task { "main.py" }

/** Creates a virtual environment using the installed Python version. */
def createVenv: T[PathRef] = Task {
val venvDir = T.dest / "venv"

os.call(
Seq(
"python3",
"-m",
"venv",
venvDir.toString
)
)

PathRef(venvDir)
}

/** This minimal setup provide basic Virtual Environment and Type Checking. */
def setup: T[PathRef] = Task {
// creating virtual environment
val pythonVenv = createVenv().path / "bin" / "python3"
os.call(("python3","-m","venv",Task.dest / "venv"))

val pythonVenv = Task.dest / "venv" / "bin" / "python3"

// installing mypy for Type Checking
os.call(
Seq(
pythonVenv.toString,
"-m",
"pip",
"install",
"mypy"
)
)
os.call((pythonVenv,"-m","pip","install","mypy"))

PathRef(pythonVenv)
}

Expand All @@ -60,13 +39,7 @@ trait PythonModule extends Module {
val pythonVenv = setup().path

os.call(
Seq(
pythonVenv.toString,
"-m",
"mypy",
"--strict",
s"${sources().path / mainFileName()}"
),
(pythonVenv,"-m","mypy","--strict",sources().path / mainFileName()),
stdout = os.Inherit
)

Expand All @@ -78,13 +51,10 @@ trait PythonModule extends Module {
val pythonVenv = typeCheck().path

os.call(
Seq(
pythonVenv.toString,
s"${sources().path / mainFileName()}"
) ++ args.value,
(pythonVenv,sources().path / mainFileName(),args.value),
stdout = os.Inherit
)
}
}

}

Expand Down Expand Up @@ -138,7 +108,7 @@ Hello, Mill Qux!
// subgraph cluster_3 {
// style=dashed
// label=qux
// "qux.createVenv" -> "qux.setup" -> "qux.typeCheck" -> "qux.run"
// "qux.setup" -> "qux.typeCheck" -> "qux.run"
// "qux.sources" -> "qux.typeCheck"
// "qux.mainFileName" -> "qux.typeCheck"
// "qux.sources" -> "qux.run"
Expand All @@ -148,15 +118,15 @@ Hello, Mill Qux!
// subgraph cluster_2 {
// style=dashed
// label=bar
// "bar.createVenv" -> "bar.setup" -> "bar.typeCheck" -> "bar.run"
// "bar.setup" -> "bar.typeCheck" -> "bar.run"
// "bar.sources" -> "bar.typeCheck"
// "bar.mainFileName" -> "bar.typeCheck"
// "bar.sources" -> "bar.run"
// "bar.mainFileName" -> "bar.run"
// }
// style=dashed
// label=foo
// "foo.createVenv" -> "foo.setup" -> "foo.typeCheck" -> "foo.run"
// "foo.setup" -> "foo.typeCheck" -> "foo.run"
// "foo.sources" -> "foo.typeCheck"
// "foo.mainFileName" -> "foo.typeCheck"
// "foo.sources" -> "foo.run"
Expand Down
50 changes: 10 additions & 40 deletions example/extending/newlang/7-python-module-deps/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,16 @@ trait PythonModule extends Module {
/** Returns the name of the main Python file. */
def mainFileName: T[String] = Task { "main.py" }

/** Creates a virtual environment using the installed Python version. */
def createVenv: T[PathRef] = Task {
val venvDir = T.dest / "venv"

os.call(
Seq(
"python3",
"-m",
"venv",
venvDir.toString
)
)

PathRef(venvDir)
}

/** This minimal setup provide basic Virtual Environment and Type Checking. */
def setup: T[PathRef] = Task {
// creating virtual environment
val pythonVenv = createVenv().path / "bin" / "python3"
os.call(("python3","-m","venv",Task.dest / "venv"))

val pythonVenv = Task.dest / "venv" / "bin" / "python3"

// installing mypy for Type Checking
os.call(
Seq(
pythonVenv.toString,
"-m",
"pip",
"install",
"mypy"
)
)
os.call((pythonVenv,"-m","pip","install","mypy"))

PathRef(pythonVenv)
}

Expand All @@ -72,13 +51,7 @@ trait PythonModule extends Module {
val pythonVenv = setup().path

os.call(
Seq(
pythonVenv.toString,
"-m",
"mypy",
"--strict",
s"${sources().path / mainFileName()}"
),
(pythonVenv,"-m","mypy","--strict",sources().path / mainFileName()),
stdout = os.Inherit
)

Expand All @@ -98,10 +71,7 @@ trait PythonModule extends Module {
val pythonVenv = typeCheck().path

os.call(
Seq(
pythonVenv.toString,
s"${sources().path / mainFileName()}"
) ++ args.value,
(pythonVenv,sources().path / mainFileName(),args.value),
env = Map("PYTHONPATH" -> Task.dest.toString),
stdout = os.Inherit
)
Expand Down Expand Up @@ -159,7 +129,7 @@ Add: 10 + 20 = 30 | Multiply: 10 * 20 = 200 | Divide: 10 / 20 = 0.5
// subgraph cluster_3 {
// style=dashed
// label=qux
// "qux.createVenv" -> "qux.setup" -> "qux.typeCheck" -> "qux.run"
// "qux.setup" -> "qux.typeCheck" -> "qux.run"
// "qux.sources" -> "qux.typeCheck"
// "qux.mainFileName" -> "qux.typeCheck"
// "qux.sources" -> "qux.run"
Expand All @@ -169,15 +139,15 @@ Add: 10 + 20 = 30 | Multiply: 10 * 20 = 200 | Divide: 10 / 20 = 0.5
// subgraph cluster_2 {
// style=dashed
// label=bar
// "bar.createVenv" -> "bar.setup" -> "bar.typeCheck" -> "bar.run"
// "bar.setup" -> "bar.typeCheck" -> "bar.run"
// "bar.sources" -> "bar.typeCheck"
// "bar.mainFileName" -> "bar.typeCheck"
// "bar.sources" -> "bar.run"
// "bar.mainFileName" -> "bar.run"
// }
// style=dashed
// label=foo
// "foo.createVenv" -> "foo.setup" -> "foo.typeCheck" -> "foo.run"
// "foo.setup" -> "foo.typeCheck" -> "foo.run"
// "foo.sources" -> "foo.typeCheck"
// "foo.mainFileName" -> "foo.typeCheck"
// "foo.sources" -> "foo.run"
Expand Down
Loading

0 comments on commit 4b76b11

Please sign in to comment.