Skip to content

Commit

Permalink
major improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
heinthanth committed May 15, 2022
1 parent f4f2c6a commit bd7bd02
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
25 changes: 19 additions & 6 deletions src/core.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as eta from "https://deno.land/x/[email protected]/mod.ts"
import { resolve } from "https://deno.land/[email protected]/path/mod.ts";

eta.configure
tags: ["{{", "}}"]
useWith: true
varName: "opt"
parse: { exec: "#", raw: "%", interpolate: "" }

# magic happens here
export runRecipe = (rc, cwd, recipe, options, recon, asIngredient) ->
export runRecipe = (rc, recipe, options, recon, asIngredient) ->
previousCwd = Deno.cwd()
if not (rc.hasOwnProperty(recipe))
console.error("\nxuerun: oops, recipe '#{recipe}' is not in .xuerun tasks!\n")
Deno.exit(1)
Expand All @@ -33,10 +35,11 @@ export runRecipe = (rc, cwd, recipe, options, recon, asIngredient) ->
dependencies = if typeof currentRecipe.dependencies ==
"string" then currentRecipe.dependencies.split(" ") else currentRecipe.dependencies

usedCwd = currentRecipe.cwd or cwd
usedCwd = currentRecipe.cwd and resolve(Deno.cwd(), currentRecipe.cwd) or Deno.cwd()

for dep in dependencies
# won't pass options
if typeof dep == "string" then await runRecipe(rc, usedCwd, dep, {}); break
if typeof dep == "string" then await runRecipe(rc, dep, {}, recon, true); break

depOption = { ...dep.options }
if typeof dep.passParentOptions == "boolean" and dep.passParentOptions
Expand All @@ -54,8 +57,10 @@ export runRecipe = (rc, cwd, recipe, options, recon, asIngredient) ->
console.error("\nxuerun: oops, something went wrong while reading options.\nError:",
err.message, "\n")
Deno.exit(1)
await runRecipe(rc, usedCwd, dep.name, depOptionToBePassed, recon, true)
await runRecipe(rc, dep.name, depOptionToBePassed, recon, true)

# change to given cwd for Deno process
Deno.chdir(usedCwd)
# make main recipe
_commands = currentRecipe.command
commands = (if typeof _commands == "string" then [_commands] else _commands)
Expand All @@ -68,14 +73,20 @@ export runRecipe = (rc, cwd, recipe, options, recon, asIngredient) ->
"\nxuerun: oops, something went wrong while reading command.\nError:",
err.message, "\n")
Deno.exit(1)

for cmdOption in commands
# used by eval
opt = currentOption
# don't run if eval when is false
if typeof cmdOption == "object" and not Boolean(eval(cmdOption.when)) then continue

commandToRun = [
(if typeof cmdOption == "object" and
cmdOption.shell then cmdOption.shell else currentRecipe.shell), "-c",
if typeof cmdOption == "string" then cmdOption else cmdOption.cmd ]

# if recon, just show command
if recon then return console.info(commandToRun)
if recon then console.info(commandToRun); continue

# run command
preparedEnv = {}
Expand Down Expand Up @@ -104,3 +115,5 @@ export runRecipe = (rc, cwd, recipe, options, recon, asIngredient) ->
if status.code != 0
console.error("\nxuerun: command exit with exit code:", status.code, "\n")
Deno.exit(status.code)
# back to previous cwd ( root of project )
Deno.chdir(previousCwd)
6 changes: 4 additions & 2 deletions src/schema.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { create, defaulted, optional, union, object, record,
array, string, number, boolean, validate } from "https://esm.sh/superstruct";

XueRunUserCmd = union([string(), object({ shell: optional(string()), cmd: string() })]);
# use eval?
XueRunUserCmd = union([string(), object({ shell: optional(string()), cmd: string(),
'when': defaulted(union([string(), number(), boolean()]), () => 'true') })]);

XueRunIngredient$option = union([string(), number(), boolean()])
export XueRunIngredient = object
Expand All @@ -14,7 +16,7 @@ getCurrentSH = -> if Deno.build.os == "windows" then "cmd" else "sh"
XueRunRecipe$dependencies = union([string(), array(union([string(), XueRunIngredient]))])
export XueRunRecipe = object
description: defaulted(optional(string()), () => ""),
cwd: defaulted(optional(string()), () => Deno.cwd()),
cwd: optional(string()),
shell: defaulted(optional(string()), () => Deno.env.get("SHELL") || getCurrentSH()),
command: defaulted(optional(union([string(), array(XueRunUserCmd)])), () => ""),
passEnv: defaulted(optional(union([boolean(), array(string())])), () => !1),
Expand Down
14 changes: 9 additions & 5 deletions src/xuerun.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { parse as parseYAML } from "https://deno.land/[email protected]/encoding/yaml.ts"
import { YAMLError } from "https://deno.land/[email protected]/encoding/_yaml/error.ts"
import { printf } from "https://deno.land/[email protected]/fmt/printf.ts"
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { parse as parseYAML } from "https://deno.land/[email protected]/encoding/yaml.ts"
import { YAMLError } from "https://deno.land/[email protected]/encoding/_yaml/error.ts"
import { dirname, resolve } from "https://deno.land/[email protected]/path/mod.ts";
import { printf } from "https://deno.land/[email protected]/fmt/printf.ts"
import { StructError } from "https://esm.sh/superstruct"
import { runRecipe } from "./core.coffee"
import createConfiguration from "./schema.coffee"
Expand Down Expand Up @@ -86,11 +87,14 @@ programMain = () ->

# load and run
xueRunRc = loadXueRunTasks(tasksPath)
# change dir to task path dir.
Deno.chdir(dirname(resolve(tasksPath)))

if recipes.length == 0
if xueRunRc.hasOwnProperty("all")
return runRecipe(xueRunRc, null, recipes, userOption)
else console.error("\nxuerun: oops, no recipe given, nothing to do!\n"); Deno.exit(1)
recipes.forEach (recipe) -> await runRecipe(xueRunRc, null, recipe, options, recon, !1)
recipes.forEach (recipe) -> await runRecipe(xueRunRc, recipe, options, recon, !1)

# call main function
if import.meta.main then programMain()
2 changes: 1 addition & 1 deletion tools/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run --allow-env

import { compile } from "https://esm.sh/coffeescript";
import { join, parse, format } from "https://deno.land/std@0.132.0/path/mod.ts";
import { join, parse, format } from "https://deno.land/std@0.139.0/path/mod.ts";

const __dirname = new URL(".", import.meta.url).pathname;
const compiler = join(__dirname, "build.coffee");
Expand Down
4 changes: 2 additions & 2 deletions tools/build.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { compile } from "https://esm.sh/coffeescript"
import { walkSync, ensureDirSync, emptyDir } from "https://deno.land/std@0.132.0/fs/mod.ts"
import { join, parse, format } from "https://deno.land/std@0.132.0/path/mod.ts"
import { walkSync, ensureDirSync, emptyDir } from "https://deno.land/std@0.139.0/fs/mod.ts"
import { join, parse, format } from "https://deno.land/std@0.139.0/path/mod.ts"

__dirname = new URL(".", import.meta.url).pathname
srcPath = join(__dirname, "..", "src")
Expand Down

0 comments on commit bd7bd02

Please sign in to comment.