Skip to content

Commit

Permalink
feat: Add download specified version support of moonbit toolchains
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcer committed Sep 16, 2024
1 parent 7b32ff4 commit deda733
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions nu/moonbit.nu
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,57 @@
# [x] Support Windows, macOS, Linux
# [x] This script should run both in Github Runners and local machines
# Description: Scripts for setting up MoonBit environment
# Usage:
# setup moonbit
# setup moonbit 0.1.20240910+3af041b9a

use common.nu [hr-line windows? is-installed]

const CLI_HOST = 'https://cli.moonbitlang.com'

const CLI_DOWNLOAD_PATH = {
windows_x86_64: 'windows',
linux_x86_64: 'ubuntu_x86',
macos_aarch64: 'macos_m1',
macos_x86_64: 'macos_intel',
const ARCH_TARGET_MAP = {
linux_x86_64: 'linux-x86_64',
macos_x86_64: 'darwin-x86_64',
macos_aarch64: 'darwin-aarch64',
windows_x86_64: 'windows-x86_64',
}

export-env {
$env.config.color_config.leading_trailing_space_bg = { attr: n }
}

# Download binary file from CLI_HOST with aira2c or `http get`
def fetch-bin [ bin: string, download_path: string ] {
def fetch-release [ version: string, archive: string ] {
let version = $version | str replace + %2B
if (is-installed aria2c) {
aria2c --allow-overwrite $'($CLI_HOST)/($download_path)/($bin)'
aria2c --allow-overwrite $'($CLI_HOST)/binaries/($version)/($archive)'
} else {
http get $'($CLI_HOST)/($download_path)/($bin)' | save --progress --force $bin
http get $'($CLI_HOST)/binaries/($version)/($archive)' | save --progress --force $archive
}
}

# Download moonbit binary files to local
export def 'setup moonbit' [] {
export def 'setup moonbit' [version: string = 'latest'] {
let MOONBIT_BIN_DIR = [$nu.home-path .moon bin] | path join
const DEFAULT_BINS = [moon, moonc, moonfmt, moonrun, mooninfo, moondoc, moon_cove_report]
const WINDOWS_BINS = [moon.exe, moonc.exe, moonfmt.exe, moonrun.exe, moondoc.exe]
mkdir $MOONBIT_BIN_DIR; cd $MOONBIT_BIN_DIR
let OS_INFO = $'($nu.os-info.name)_($nu.os-info.arch)'
let DOWNLOAD_PATH = $CLI_DOWNLOAD_PATH | get -i $OS_INFO
if ($DOWNLOAD_PATH | is-empty) { print $'Unsupported Platform: ($OS_INFO)'; exit 2 }
let archive = $ARCH_TARGET_MAP | get -i $OS_INFO
if ($archive | is-empty) { print $'Unsupported Platform: ($OS_INFO)'; exit 2 }

if (windows?) {
$WINDOWS_BINS | each {|it| fetch-bin $it $DOWNLOAD_PATH }
fetch-release $version $'moonbit-($archive).zip'
unzip $'moonbit-($archive).zip' -d $MOONBIT_BIN_DIR
rm moonbit*.zip
} else {
$DEFAULT_BINS | each {|it| fetch-bin $it $DOWNLOAD_PATH; chmod +x $it }
fetch-release $version $'moonbit-($archive).tar.gz'
tar xf $'moonbit-($archive).tar.gz' --directory $MOONBIT_BIN_DIR
ls $MOONBIT_BIN_DIR | get name | each { chmod u+x $in }
rm moonbit*.tar.gz
}

print 'OS Info:'; print $nu.os-info; hr-line
print $'Contents of ($MOONBIT_BIN_DIR):'; hr-line -b
print (ls -l $MOONBIT_BIN_DIR)
print (ls $MOONBIT_BIN_DIR)
if ('GITHUB_PATH' in $env) {
echo $MOONBIT_BIN_DIR o>> $env.GITHUB_PATH
}
Expand Down

0 comments on commit deda733

Please sign in to comment.