Skip to content

Commit

Permalink
feat: read version from environment variable (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc authored Apr 17, 2024
1 parent 25bd017 commit 685d7d1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 25 deletions.
22 changes: 1 addition & 21 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,12 @@ jobs:
os: ${{ matrix.os }}
host_arch: ${{ matrix.os == 'macos-14' && 'aarch64' || 'x86_64' }}
target_arch: ${{ matrix.arch }}
- name: Patch Version
env:
VERSION: ${{ needs.meta.outputs.version }}
run: |
if [ "$(uname -s)" == "Darwin" ]; then
SED="sed -i '' -E" # BSD sed should use '' as backup extension
else
SED="sed -i -E"
fi
S="[[:space:]]" # BSD sed doesn't support \s
$SED "1,6 s/(version${S}*=${S}*)\"[^\"]+\"/\1\"$VERSION\"/" maa-cli/Cargo.toml
NL=$(grep -n -E "name${S}*=${S}*\"maa-cli\"" Cargo.lock | cut -d: -f1)
NS=$((NL - 1))
NE=$((NL + 3))
$SED "$NS,$NE s/(version${S}*=${S}*)\"[^\"]+\"/\1\"$VERSION\"/" Cargo.lock
{
echo 'Patch Version (${{ matrix.os }}, ${{ matrix.arch }})'
echo '```diff'
git diff
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
- name: Build
env:
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 1
CARGO_PROFILE_RELEASE_LTO: true
CARGO_PROFILE_RELEASE_STRIP: true
MAA_VERSION: ${{ needs.meta.outputs.version }}
run: |
cargo build --release --package maa-cli --locked --features vendored-openssl
- name: Tar Artifact
Expand Down
8 changes: 8 additions & 0 deletions maa-cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
println!("cargo:rerun-if-env-changed=MAA_VERSION");
if let Ok(version) = std::env::var("MAA_VERSION") {
println!("cargo:rustc-env=MAA_VERSION={}", version);
} else {
println!("cargo:rustc-env=MAA_VERSION={}", env!("CARGO_PKG_VERSION"));
}
}
2 changes: 1 addition & 1 deletion maa-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;

#[derive(Parser)]
#[command(name = "maa", author, version, about = "A tool for Arknights.")]
#[command(name = "maa", author, version = env!("MAA_VERSION"), about = "A tool for Arknights.")]
#[allow(clippy::upper_case_acronyms)]
pub(crate) struct CLI {
#[command(subcommand)]
Expand Down
2 changes: 1 addition & 1 deletion maa-cli/src/installer/maa_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn update(args: &CommonArgs) -> Result<()> {
.context("Failed to fetch version info")?
.json()
.context("Failed to parse version info")?;
let current_version: Version = env!("CARGO_PKG_VERSION").parse()?;
let current_version: Version = env!("MAA_VERSION").parse()?;
if !version_json.can_update("maa-cli", &current_version)? {
return Ok(());
}
Expand Down
4 changes: 2 additions & 2 deletions maa-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ fn main() -> Result<()> {
},
Command::Version { component } => match component {
Component::All => {
println!("maa-cli v{}", env!("CARGO_PKG_VERSION"));
println!("maa-cli v{}", env!("MAA_VERSION"));
println!("MaaCore {}", run::core_version()?);
}
Component::MaaCLI => {
println!("maa-cli v{}", env!("CARGO_PKG_VERSION"));
println!("maa-cli v{}", env!("MAA_VERSION"));
}
Component::MaaCore => {
println!("MaaCore {}", run::core_version()?);
Expand Down

0 comments on commit 685d7d1

Please sign in to comment.