nimbus.nim: implement running tasks

This commit is contained in:
Anna “CyberTailor” 2022-06-29 10:02:15 +05:00
parent 0472d62487
commit e5eeb9771b
Signed by: CyberTaIlor
GPG Key ID: E7B76EDC50864BB1
3 changed files with 42 additions and 10 deletions

View File

@ -1,7 +1,6 @@
# SPDX-FileCopyrightText: 2022 Anna <cyber@sysrq.in>
# SPDX-License-Identifier: BSD-3-Clause
import std/tempfiles
import json, os, osproc, sequtils, strformat, strutils
import options
@ -28,10 +27,7 @@ proc queryString*(nimbleFile, variable: string, options: Options): string =
proc queryArray*(nimbleFile, variable: string, options: Options): seq[string] =
result = nimbleFile.query(variable, options).parseJson().to(seq[string])
proc getTasks*(nimbleFile: string, options: Options): seq[string] =
let nimsFile = genTempPath("nimscript_", ".nims")
copyFile(nimbleFile, nimsFile)
proc getTasks*(nimsFile: string, options: Options): seq[string] =
let cmd = (
"$# $# help $#" % [
getNimBin(options).quoteShell,
@ -41,9 +37,8 @@ proc getTasks*(nimbleFile: string, options: Options): seq[string] =
)
let (output, exitCode) = execCmdEx(cmd)
removeFile(nimsFile)
if exitCode != QuitSuccess:
quit(fmt"Failed to parse tasks from {nimbleFile}")
quit(fmt"Failed to parse tasks from {nimsFile}")
for line in output.splitLines().filterIt(it.len != 0):
result.add(line.splitWhitespace()[0])

View File

@ -4,7 +4,8 @@
import os, sequtils, strtabs, strutils
import nimbs/common, nimbs/dependencyresolver, nimbs/installerscript,
nimbs/ninjasyntax, nimbs/options, nimbs/packageinfo, nimbs/version
nimbs/ninjasyntax, nimbs/nimbleexecutor, nimbs/options,
nimbs/packageinfo, nimbs/version
proc processDependencies(requires: seq[string], options: Options): seq[string] =
for req in requires:
@ -29,6 +30,17 @@ proc application(ninja: File, input, output: string, paths: seq[string]) =
variables = vars
)
proc task(ninja: File, nimsFile, taskName: string) =
var vars = newStringTable()
vars["taskname"] = taskName
ninja.build(@[taskName],
rule = "nimbletask",
inputs = @[nimsFile],
implicit = @["PHONY"],
variables = vars
)
proc setup(options: Options) =
echo "The nimbus build system"
echo "Version: " & nimbusVersion
@ -41,8 +53,16 @@ proc setup(options: Options) =
echo "Nim compiler: " & options.getNimBin()
echo ""
let nimbusPriv = options.getBuildDir() / "nimbus-private"
createDir(nimbusPriv)
let depPaths = processDependencies(pkgInfo.requires, options)
let nimsFile =
nimbusPriv / splitFile(pkgInfo.nimbleFile).name.changeFileExt("nims")
copyFile(pkgInfo.nimbleFile, nimsFile)
let tasks = nimsFile.getTasks(options)
echo "-- Generating installer script"
let installer = open(options.getBuildDir() / installerFileName, fmWrite)
installer.writeInstallerScript(pkgInfo, options)
@ -78,6 +98,14 @@ proc setup(options: Options) =
pool = "console")
ninja.newline()
if tasks.len != 0:
# most tasks are supposed to be run from the project root
ninja.rule("nimbletask",
command = "cd $sourcedir && $nim --hints:off $taskname $in",
description = "Executing task $taskname",
pool = "console")
ninja.newline()
if pkgInfo.bin.len != 0:
ninja.rule("nimc",
command = "$nim --hints:off $nimflags c -o:$out $paths $in",
@ -88,6 +116,10 @@ proc setup(options: Options) =
ninja.build(@["PHONY"], rule = "phony")
ninja.newline()
for taskName in tasks:
ninja.task(nimsFile, taskName)
ninja.newline()
ninja.build(@["build.ninja"],
rule = "REGENERATE_BUILD",
inputs = @[pkgInfo.nimbleFile])

View File

@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2022 Anna <cyber@sysrq.in>
# SPDX-License-Identifier: BSD-3-Clause
import os
import os, std/tempfiles
import nimbs/nimbleexecutor, nimbs/options
var opts = Options()
@ -9,4 +9,9 @@ opts.setNimBin
const nimbleFile = "tests" / "nimbleexecutor" / "nimble.nimble"
assert nimbleFile.getTasks(opts) == @["test"]
let nimsFile = genTempPath("nimscript_", ".nims")
copyFile(nimbleFile, nimsFile)
assert nimsFile.getTasks(opts) == @["test"]
removeFile(nimsFile)