all repos — pls @ 66e16b031122b996dfcb569b404197fcca8dd3f2

A polite but determined task runner.

src/pls.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
import 
  json,
  os,
  parseopt,
  logging,
  algorithm,
  strutils,
  sequtils

import
  plspkg/plslogger

newPlsLogger().addHandler()
setLogFilter(lvlInfo)

import
  plspkg/config,
  plspkg/project,
  plspkg/messaging

let usage* = """  $1 v$2 - $3
  (c) 2021 $4

  Usage:
    pls <task> [<target>]           Executes <task> (on <target>).

    => For more information on available tasks, run: pls help

  Options:
    --help,    -h           Displays this message.
    --force,   -f           Do not ask for confirmation when executing the specified task.
    --global,  -g           Execute task within the global project instead of the local one.
    --log,     -l           Specifies the log level (debug|info|notice|warn|error|fatal).
                            Default: info
    --version, -h           Displays the version of the application.
""" % [pkgTitle, pkgVersion, pkgDescription, pkgAuthor]

var force = false
var global = false

# Helper Methods

proc addProperty(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] =
  var done = false
  while (not done):
    if name == "":
      result.key = editValue("Name")
    elif name == "name":
      warn "Property identifier 'name' cannot be modified."
    else:
      printValue(" Name", name)
      result.key = name
    var ok = false
    while (not ok):
      var value = ""
      if parentObj.hasKey(result.key):
        value = $parentObj[result.key]
      try:
        result.value = editValue("Value", value).parseJson
        if (result.value == newJNull()):
          ok = confirm("Remove property '$1'?" % result.key)
          done = true
        else:
          ok = true
      except:
        warn("Please enter a valid JSON value.")
    done = done or confirm("OK?")
    
proc addProperties(obj: var JsonNode) =
  var done = false
  while (not done):
    let prop = addProperty(obj)
    obj[prop.key] = prop.value
    done = not confirm("Do you want to add/remove more properties?")

proc addTaskDefinition(parentObj: JsonNode, name = ""): tuple[key: string, value: JsonNode] =
  # TODO: validate name of task definition! (not _syntax or _description, etc.)
  if name == "":
    result.key = editValue("Task Definition Matcher")
  else:
    printValue(" Task Definition Matcher", name)
    result.key = name
  result.value = newJObject()
  result.value["cmd"] = addProperty(parentObj[name], "cmd").value
  result.value["pwd"] = addProperty(parentObj[name], "pwd").value
  # TODO: delete task definition matcher if all properties are null.

proc addTaskDefinitions(obj: var JsonNode) =
  var done = false
  while (not done):
    let prop = addTaskDefinition(obj)
    obj[prop.key] = prop.value
    done = not confirm("Do you want to add/remove more task definitions?") 

proc changeValue(oldv: tuple[label: string, value: JsonNode], newv: tuple[label: string, value: JsonNode]): bool =
  if oldv.value != newJNull():
    printDeleted(oldv.label, $oldv.value)
  if newv.value != newJNull():
    printAdded(newv.label, $newv.value)
  return confirm("Confirm change?")

proc updateDefinitions(prj: var PlsProject): bool =
  result = false
  let sysTasks = plsTpl.parseJson["tasks"]
  for k, v in sysTasks.pairs:
    if prj.tasks.hasKey(k):
      let sysTask = sysTasks[k]
      var prjTask = prj.tasks[k]
      for prop, val in sysTask.pairs:
        let sysProp = sysTask[prop]
        var prjProp = newJNull()
        if prjTask.hasKey(prop):
          prjProp = prjTask[prop]
        if prjProp != newJNull():
          if prjProp != sysProp:
            let sysVal = (label: k & "." & prop, value: sysProp)
            let prjVal = (label: k & "." & prop, value: prjProp)
            if changeValue(prjVal, sysVal):
              prjTask[prop] = sysProp
              result = true
        else:
          result = true
          # Adding new property
          printAdded("$1.$2" % [k, prop], $sysProp)
          prjTask[prop] = sysProp
    else:
      result = true
      # Adding new task
      printAdded(k, $sysTasks[k])
      prj.tasks[k] = sysTasks[k]

### MAIN ###

var args = newSeq[string](0)

for kind, key, val in getopt():
  case kind:
    of cmdArgument:
      args.add key 
    of cmdLongOption, cmdShortOption:
      case key:
        of "force", "f":
          force = true
        of "global", "g":
          global = true
        of "log", "l":
          var val = val
          setLogLevel(val)
        of "help", "h":
          echo usage
          quit(0)
        of "version", "v":
          echo pkgVersion
          quit(0)
        else:
          discard
    else:
      discard

var localPrj = newPlsProject(getCurrentDir())

var globalPrj: PlsProject

if defined(windows):
  globalPrj = newPlsProject(getenv("USERPROFILE"))
if not defined(windows):
  globalPrj = newPlsProject(getenv("HOME"))

if not globalPrj.configured:
  globalPrj.init()

globalPrj.load()

var prj = localPrj

if global:
  prj = globalPrj 

if args.len == 0:
  echo usage
  quit(0)
case args[0]:
  of "init":
    if prj.configured:
      fatal "Project already configured."
      quit(2)
    prj.init()
    notice "Project initialized."
  of "def":
    if args.len < 3:
      fatal "No alias specified."
      quit(3)
    let kind = args[1]
    let alias = args[2]
    var props = newJObject()
    if not ["target", "task"].contains(kind):
      fatal "Unknown definition type $1" % kind
      quit(6)
    prj.load
    if kind == "target":
      if prj.targets.hasKey(alias):
        notice "Redefining existing target: " & alias
        warn "Specify properties for target '$1':" % alias
        props = prj.targets[alias]
        for k, v in props.mpairs:
          if k == "name":
            continue
          let prop = addProperty(props, k)
          props[prop.key] = prop.value
        if confirm "Do you want to add/remove more properties?":
          addProperties(props)
      else:
        notice "Definining new target: " & alias
        warn "Specify properties for target '$1':" % alias
        addProperties(props)
      prj.defTarget(alias, props) 
    else: # task
      if prj.tasks.hasKey(alias):
        notice "Redefining existing task: " & alias
        warn "Specify properties for task '$1':" % alias
        props = prj.tasks[alias]
        for k, v in props.mpairs:
          if ["_syntax", "_description"].contains(k):
            let prop = addProperty(props, k)
            props[prop.key] = prop.value
          else:
            let prop = addTaskDefinition(props, k)
            props[prop.key] = prop.value
        if confirm "Do you want to add/remove more task definitions?":
          addTaskDefinitions(props)
      else:
        props["_syntax"] = addProperty(props, "_syntax").value
        props["_description"] = addProperty(props, "_description").value
        addTaskDefinitions(props)
      prj.defTask(alias, props)
  of "undef":
    if args.len < 3:
      fatal "No alias specified."
      quit(3)
    let kind = args[1]
    let alias = args[2]
    if not ["target", "task"].contains(kind):
      fatal "Unknown definition type $1" % kind
      quit(6)
    prj.load
    if kind == "target":
      if not prj.targets.hasKey(alias):
        fatal "Target '$1' not defined." % [alias]
        quit(4)
      if force or confirm("Remove definition for target '$1'?" % alias):
        prj.undefTarget(alias) 
    else: # task
      if not prj.tasks.hasKey(alias):
        fatal "Task '$1' not defined." % [alias]
        quit(4)
      if force or confirm("Remove definition for task '$1'?" % alias):
        prj.undefTask(alias) 
  of "info":
    prj.load
    if args.len < 2:
      for t, props in prj.targets.pairs:
        echo "$1:" % [t]
        for k, v in props.pairs:
          echo " - $1:\t$2" % [k, $v]
    else:
      let alias = args[1]
      if not prj.targets.hasKey(alias):
        fatal "Target '$1' not defined." % [alias]
        quit(4)
      let data = prj.targets[alias]
      for k, v in data.pairs:
        echo "$1:\t$2" % [k, $v]
  of "update":
    prj.load
    if updateDefinitions(prj):
      prj.save
  of "help":
    echo ""
    if args.len < 2:
      var sortedKeys = toSeq(prj.help.keys)
      sortedKeys.sort(cmp[string])
      for k in sortedKeys:
        printGreen "   pls $1" % prj.help[k]["_syntax"].getStr
        echo "\n      $1\n" % prj.help[k]["_description"].getStr
    else:
      let cmd = args[1]
      let help = prj.help[cmd]
      if not prj.help.hasKey(cmd):
        fatal "Task '$1' is not defined." % cmd
        quit(5)
      printGreen "   pls " & help["_syntax"].getStr
      echo "\n      $1\n" % help["_description"].getStr
  else:
    if args.len < 1:
      echo usage
      quit(1)
    if args.len < 2:
      prj.load
      var targets = toSeq(prj.targets.pairs)
      if targets.len == 0:
        warn "No targets defined - nothing to do."
        quit(0)
      for key, val in prj.targets.pairs:
        prj.execute(args[0], key) 
    else:
      try:
        prj.execute(args[0], args[1]) 
      except:
        # Fallback to global project
        if prj == localPrj:
          try:
            globalPrj.execute(args[0], args[1])
          except:
            warn getCurrentExceptionMsg()
        else:
          warn getCurrentExceptionMsg()