fxkit 0.1.4

Useful utilities for writting Rust CLI tools
Documentation
#!/usr/bin/env nu

const nubuild_version = 0.1


def todo [msg] {
  error make { msg: $msg }
}

def msg [msg file path] {
  if $file == null and $path == null {
    print $"(ansi green)(ansi attr_bold)($msg)(ansi reset).."
    return
  }


  if $path == null {
    print $"(ansi green)(ansi attr_bold)($msg)(ansi reset): ($file)"
    return
  }

  print $"(ansi green)(ansi attr_bold)($msg)(ansi reset): ($file) to ($path)"
}

def build [$release, $features, $targets, $jobs, $offline] {
  if (which cargo | is-empty) {
    error make { msg: "Cargo is missing. Please install it in order to build this project" }
  }

  if (ls | find Cargo.toml | is-empty) {
    error make { msg: "Cargo.toml is missing, Are you in the root of the project?" }
  }

  msg "Building project" null null

  mut features_list = []

  for feature in $features {
    $features_list = ($features_list | append ["-F", $feature])
  }

  mut target_lists = []

  for target in $targets {
    $target_lists = $target_lists | append ["--target", $target]
  }

  let amount_jobs = if ($jobs | is-empty) {
    "default"
  } else {
    $jobs
  }

  mut build_command = ["cargo", "b", "-j", ($amount_jobs | to text)]


  if $release {
    $build_command = ($build_command | append "--release")
  }

  if $offline {
    $build_command = $build_command | append "--offline"
  }

  # Features
  if ($features_list | is-not-empty) {
    $build_command = $build_command | append $features_list
  }

  if ($target_lists | is-not-empty) {
    $build_command = $build_command | append $target_lists
  }

  nu -c ($build_command | str join " ")
}

export def "main build" [
  --release (-r) # Release build
  ...features: string # Features
  --targets (-t): string # Target
  --jobs (-j): int # Jobs
  --offline (-o) # Offline mode
] {
  build $release $features $targets $jobs $offline
}

export def "main install" [
  --library-path (-l): string
  --header-path (-h): string
  --build-type (-t): string
  --static (-s)
] {
  if $library_path == null or $header_path == null {
    print $"(ansi attr_bold)(ansi yellow)WARN(ansi reset): Pleave provide a path."
    return
  }

  let type = if ($build_type | is-empty) {
    "debug"
  } else {
    $build_type
  }

  if $static {
    msg "Installing" "libfxkit.a" $library_path
    install -Dm644 ./target/($type)/libfxkit.a ($library_path)
    
  } else {
    msg "Installing" "libfxkit.so" $library_path
    install -Dm644 ./target/($type)/libfxkit.so ($library_path)
  }
  
  if (ls bindings | find fxkit.h | is-empty) {
    print $"(ansi yellow)WARN(ansi reset): The header is missing, not installing."
    return
  }

  msg "Installing" "fxkit.h" $header_path
  install -Dm644 ./bindings/fxkit.h ($header_path)

}

export def "main make-header" [
  --force (-f) # Force
] {
  if (which cbindgen | is-empty) {
    error make { msg: "Cbindgen is missing. Please install it in order to create the header" }
  }

  if (ls bindings | find fxkit.h | is-empty) or $force {
    msg "Creating header" "bindings/fxkit.h" null
    cbindgen -q -o bindings/fxkit.h
    return
  }

  print $"(ansi attr_bold)(ansi blue)Info(ansi reset): Header already exists. Not remaking..."
}

export def list_features [] {
  if (ls | find Cargo.toml | is-empty) {
    error make { msg: "Cargo.toml is missing, Are you in the root of the project?" }
  }

  msg "Features" (open Cargo.toml | get features | columns | str join " ") null
}

export def "main list-features" [] {
  list_features
}

export def "main lint" [] {
  if (which cargo | is-empty) {
    error make { msg: "Cargo is missing. Please install it in order to lint this project" }
  }

  msg "Checking" null null


  cargo clippy -- -Dwarnings
}

def main [--version (-v)] {

  if $version {
    print $"(ansi green)(ansi attr_bold)Nubuild(ansi reset): v0.1"
    return
  }
  print $"(ansi green)(ansi attr_bold)Nubuild(ansi reset): No args."
}