Skip to content

Commit

Permalink
feat: Implement "extends" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
aklaswad committed Jan 22, 2023
1 parent c58093d commit da1b134
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
33 changes: 30 additions & 3 deletions lib/aclii.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,35 @@ class AcliiOption {
}

class AcliiNode {
constructor (obj, owner, aclii) {
constructor (_obj, owner, aclii) {
let obj
const extendSource = _obj["extends"]
if ( extendSource ) {
const extendPath = path.resolve(extendSource)
let branch
try {
branch = Aclii._loadYaml(extendPath)
}
catch (e) {
throw "aclii error: Failed to load extend file: " + extendSource + " : " + e
}
obj = Object.assign( branch, _obj )
obj.__source = extendPath
}
else {
obj = _obj
}

this.id = aclii.items++
this.__source = obj.__source || owner.__source
if (!obj.name) throw "Name is required"
this.name = obj.name
this.path = owner ? owner.path + '.' + this.name : this.name
this.bin = obj.bin || (owner ? owner.bin : '')
this.argstyle = obj.argstyle
this.binPath = (obj.script || obj.bin ) ? this.path
: owner ? owner.binPath : this.path
: owner ? owner.binPath
: this.path
this.ownOptions
= (obj.options || []).map( option => new AcliiOption(option, this, aclii ) )
this.inheritedOptions = owner ? owner.options.filter(o => o.inherit ) : []
Expand Down Expand Up @@ -204,7 +224,7 @@ class Aclii {
input.inputId = this.inputs.length - 1
}

static fromFile (filename) {
static _loadYaml (filename) {
if ( process.env.ACLII_DEBUG ) {
console.error("Loading aclii config file from '" + filename + "'...")
}
Expand All @@ -222,6 +242,13 @@ class Aclii {
catch (e) {
throw "aclii error: Failed to load YAML file. Please check the syntax of your config file.\n" + e
}
return definition
}

static fromFile (filename) {
const filepath = path.resolve(filename)
const definition = Aclii._loadYaml(filepath)
definition.__source = filepath
return new Aclii(definition)
}

Expand Down
15 changes: 3 additions & 12 deletions tmpl/launcher.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@ _aclii_debug () {
}

_aclii_exec_json () {
if [ "${1:0:1}" = "/" ] || [ "${1:0:1}" = "~" ]; then
binname=$1
else
binname="$(dirname "$0")"/"$1"
fi
binname="$1"
exec "$binname" "$2"
}

_aclii_exec () {
if [ "${1:0:1}" = "/" ] || [ "${1:0:1}" = "~" ]; then
binname=$1
else
binname="$(dirname "$0")/$1"
fi
binname="$1"
exec "$binname" "${@:2}"

}

_aclii_debug "Launch aclii..."
Expand Down Expand Up @@ -246,7 +237,7 @@ __parse_args () {
case "$binPath" in
##// aclii.allCommands.forEach( c => {
"<%= c.path %>" )

cd $(dirname "<%= c.__source %>")
##// // TODO: Prepare input for each env. Maybe it could be plaggable?
##// if ( c.script ) {
tmp=$(mktemp)
Expand Down

0 comments on commit da1b134

Please sign in to comment.