Skip to content

Commit

Permalink
feat: Add support for specifying Moonbit Core version in setup process (
Browse files Browse the repository at this point in the history
#55)

* feat: Add support for specifying Moonbit Core version in setup process

* feat: Add support for specifying Moonbit Core version in setup process

* chore: Add version check

* chore: Add version check
  • Loading branch information
hustcer authored Feb 17, 2025
1 parent 322d1ac commit 6dbe932
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ In rare circumstances you might get rate limiting errors, if this happens you ca
| ------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `version` | `string` | Optional, A valid moonbit tool chain version, such as `0.1.20250210+7be093d1f`, `nightly`, `latest`, etc. or even `bleeding`, default to `latest` |
| `setup-core` | `bool` | Optional, Set to `true` to download and bundle Moonbit Core, `false` to ignore it, default to `true` |
| `core-version` | `string` | Optional, A valid moonbit core version, such as `0.1.20250210+7be093d1f`, `nightly`, `latest`, `bleeding` etc. Default to `latest` |

## License

Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ steps:
| ------------ | -------- | --------------------------------------------------------------------------------------------------------------------- |
| `version` | `string` | 可选,合法的 Moonbit 工具链版本,比如: `0.1.20250210+7be093d1f`, `nightly`, `latest` 或者 `bleeding`,默认为 `latest` |
| `setup-core` | `bool` | 设置为 `true` 则下载并打包 Moonbit Core, `false` 则忽略,默认为 `true` |
| `core-version` | `string` | 可选,合法的 Moonbit Core 版本,比如: `0.1.20250210+7be093d1f`, `nightly`, `latest` 或者 `bleeding`,默认为 `latest` |

## 许可

Expand Down
9 changes: 7 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
required: false
default: true
description: 'Set to `true` if you want to setup moonbit core.'
core-version:
required: false
default: 'latest'
description: 'The moonbit core version to setup.'

runs:
using: 'composite'
Expand All @@ -37,6 +41,7 @@ runs:
run: |
use ${{ github.action_path }}/nu/moonbit.nu *
let version = '${{inputs.version}}'
let setupCore = '${{inputs.setup-core}}' | into bool
setup moonbit $version --setup-core=$setupCore
let setup_core = '${{inputs.setup-core}}' | into bool
let core_version = '${{inputs.core-version}}'
setup moonbit $version --setup-core=$setup_core --core-version=$core_version
35 changes: 28 additions & 7 deletions nu/moonbit.nu
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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

const VALID_VERSION_TAG = [latest, bleeding, nightly]
const ARCH_TARGET_MAP = {
linux_x86_64: 'linux-x86_64',
macos_x86_64: 'darwin-x86_64',
Expand All @@ -39,6 +40,9 @@ def fetch-release [ version: string, archive: string ] {

# Download moonbit core from CLI_HOST with aria2c or `http get`
def fetch-core [ version: string ] {
if ($version not-in $VALID_VERSION_TAG) and not (is-semver $version) {
print $'(ansi r)Invalid version: ($version)(ansi reset)'; exit 2
}
let version = $version | str replace + %2B
let suffix = if (windows?) { $'($version).zip' } else { $'($version).tar.gz' }
if (is-installed aria2c) {
Expand All @@ -50,9 +54,13 @@ def fetch-core [ version: string ] {

# Download moonbit binary files to local
export def 'setup moonbit' [
version: string = 'latest', # The version of moonbit toolchain to setup, and `latest` by default
--setup-core(-c), # Setup moonbit core
version: string = 'latest', # The version of moonbit toolchain to setup, and `latest` by default
--setup-core(-c), # Setup moonbit core
--core-version(-V): string = 'latest', # The version of moonbit core to setup, `latest` by default
] {
if ($version not-in $VALID_VERSION_TAG) and not (is-semver $version) {
print $'(ansi r)Invalid version: ($version)(ansi reset)'; exit 2
}
let MOONBIT_HOME = $env.MOONBIT_HOME? | default ([$nu.home-path .moon] | path join)
let MOONBIT_BIN_DIR = [$MOONBIT_HOME bin] | path join
let MOONBIT_LIB_DIR = [$MOONBIT_HOME lib] | path join
Expand Down Expand Up @@ -98,21 +106,21 @@ export def 'setup moonbit' [
}

if $setup_core {
print $'(char nl)Setup moonbit core of version: (ansi g)($version)(ansi reset)'; hr-line
print $'(char nl)Setup moonbit core of version: (ansi g)($core_version)(ansi reset)'; hr-line
cd $MOONBIT_LIB_DIR; rm -rf ./core/*
if $version == 'bleeding' {
if $core_version == 'bleeding' {
if ($coreDir | path exists) { rm -rf $coreDir }
git clone --depth 1 https://github.com/moonbitlang/core.git $coreDir
bundle-core $coreDir
return
}

fetch-core $version
fetch-core $core_version

if (windows?) {
unzip -qo core*.zip -d $MOONBIT_LIB_DIR; rm core*.zip
unzip -qo $'core-($core_version).zip' -d $MOONBIT_LIB_DIR; rm $'core-($core_version).zip'
} else {
tar xf core*.tar.gz --directory $MOONBIT_LIB_DIR; rm core*.tar.gz
tar xf $'core-($core_version).tar.gz' --directory $MOONBIT_LIB_DIR; rm $'core-($core_version).tar.gz'
}
bundle-core $coreDir
}
Expand Down Expand Up @@ -140,6 +148,19 @@ export def windows? [] {
(sys host | get name) == 'Windows'
}

# A custom command to check if a string is a valid SemVer version
def is-semver [version?: string] {
let version = if ($version | is-empty) { $in } else { $version }
if ($version | is-empty) { return false }
# Use regex pattern to match the SemVer version string
# The `v` prefix is not supported, add `v?` at the beginning of the regex if needed
# ^v?(0|[1-9]\d*)\.(0|[1-9]\d*)... Keep the reset of the pattern the same
let semver_pattern = '^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$'
# Check if the version string matches the SemVer pattern
if $version =~ $semver_pattern { true } else { false }
# $version | str replace --regex $semver_pattern 'match' | $in == 'match'
}

# Check if some command available in current shell
export def is-installed [ app: string ] {
(which $app | length) > 0
Expand Down

0 comments on commit 6dbe932

Please sign in to comment.