From 54eeaef4e9964a48be6a5f81f714928191453df7 Mon Sep 17 00:00:00 2001 From: hustcer Date: Mon, 11 Nov 2024 10:01:35 +0800 Subject: [PATCH] refactor: Remove common.nu --- nu/common.nu | 115 -------------------------------------------------- nu/moonbit.nu | 31 +++++++++++++- 2 files changed, 29 insertions(+), 117 deletions(-) delete mode 100644 nu/common.nu diff --git a/nu/common.nu b/nu/common.nu deleted file mode 100644 index ed62cc0..00000000 --- a/nu/common.nu +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/env nu -# Author: hustcer -# Created: 2022/05/01 18:36:56 -# Usage: -# use `source` command to load it - -# If current host is Windows -export def windows? [] { - # Windows / Darwin / Linux - (sys host | get name) == 'Windows' -} - -# Check if some command available in current shell -export def is-installed [ app: string ] { - (which $app | length) > 0 -} - -# Get the specified env key's value or '' -export def 'get-env' [ - key: string # The key to get it's env value - default?: string # The default value for an empty env -] { - $env | get -i $key | default $default -} - -# Check if a git repo has the specified ref: could be a branch or tag, etc. -export def has-ref [ - ref: string # The git ref to check -] { - let checkRepo = (do -i { git rev-parse --is-inside-work-tree } | complete) - if not ($checkRepo.stdout =~ 'true') { return false } - # Brackets were required here, or error will occur - let parse = (do -i { git rev-parse --verify -q $ref } | complete) - if ($parse.stdout | is-empty) { false } else { true } -} - -# Compare two version number, return `1` if first one is higher than second one, -# Return `0` if they are equal, otherwise return `-1` -export def compare-ver [ - from: string, - to: string, -] { - let dest = ($to | str downcase | str trim -c 'v' | str trim) - let source = ($from | str downcase | str trim -c 'v' | str trim) - # Ignore '-beta' or '-rc' suffix - let v1 = ($source | split row '.' | each {|it| ($it | parse -r '(?P\d+)' | get v | get 0 )}) - let v2 = ($dest | split row '.' | each {|it| ($it | parse -r '(?P\d+)' | get v | get 0 )}) - for $v in ($v1 | enumerate) { - let c1 = ($v1 | get -i $v.index | default 0 | into int) - let c2 = ($v2 | get -i $v.index | default 0 | into int) - if $c1 > $c2 { - return 1 - } else if ($c1 < $c2) { - return (-1) - } - } - return 0 -} - -# Compare two version number, return true if first one is lower then second one -export def is-lower-ver [ - from: string, - to: string, -] { - (compare-ver $from $to) < 0 -} - -# Check if git was installed and if current directory is a git repo -export def git-check [ - dest: string, # The dest dir to check - --check-repo: int, # Check if current directory is a git repo -] { - cd $dest - let isGitInstalled = (which git | length) > 0 - if (not $isGitInstalled) { - print $'You should (ansi r)INSTALL git(ansi reset) first to run this command, bye...' - exit 2 - } - # If we don't need repo check just quit now - if ($check_repo != 0) { - let checkRepo = (do -i { git rev-parse --is-inside-work-tree } | complete) - if not ($checkRepo.stdout =~ 'true') { - print $'Current directory is (ansi r)NOT(ansi reset) a git repo, bye...(char nl)' - exit 5 - } - } -} - -# Create a line by repeating the unit with specified times -def build-line [ - times: int, - unit: string = '-', -] { - 0..<$times | reduce -f '' { |i, acc| $unit + $acc } -} - -# Log some variables -export def log [ - name: string, - var: any, -] { - print $'(ansi g)(build-line 18)> Debug Begin: ($name) <(build-line 18)(ansi reset)' - print $var - print $'(ansi g)(build-line 20)> Debug End <(build-line 20)(char nl)(ansi reset)' -} - -export def hr-line [ - width?: int = 90, - --color(-c): string = 'g', - --blank-line(-b), - --with-arrow(-a), -] { - print $'(ansi $color)(build-line $width)(if $with_arrow {'>'})(ansi reset)' - if $blank_line { char nl } -} diff --git a/nu/moonbit.nu b/nu/moonbit.nu index 016c96e..beeeb3a 100644 --- a/nu/moonbit.nu +++ b/nu/moonbit.nu @@ -13,8 +13,6 @@ # setup moonbit # setup moonbit 0.1.20240910+3af041b9a -use common.nu [hr-line windows? is-installed] - const CLI_HOST = 'https://cli.moonbitlang.com' const ARCH_TARGET_MAP = { @@ -133,4 +131,33 @@ def bundle-core [coreDir: string] { } } +# If current host is Windows +export def windows? [] { + # Windows / Darwin / Linux + (sys host | get name) == 'Windows' +} + +# Check if some command available in current shell +export def is-installed [ app: string ] { + (which $app | length) > 0 +} + +export def hr-line [ + width?: int = 90, + --color(-c): string = 'g', + --blank-line(-b), + --with-arrow(-a), +] { + # Create a line by repeating the unit with specified times + def build-line [ + times: int, + unit: string = '-', + ] { + 0..<$times | reduce -f '' { |i, acc| $unit + $acc } + } + + print $'(ansi $color)(build-line $width)(if $with_arrow {'>'})(ansi reset)' + if $blank_line { char nl } +} + alias main = setup moonbit