Skip to content

Commit

Permalink
Added task to build releases
Browse files Browse the repository at this point in the history
  • Loading branch information
Shtsh committed Apr 11, 2024
1 parent 21e2ca4 commit 2ded3df
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 51 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
on:
release:
types: [created]
push:
tags:
- v[0-9]+.*

jobs:
release:
name: release ${{ matrix.target }}
runs-on: ${{ matrix.os }}
if: github.event_name == 'release'
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-unknown-linux-gnu
archive: tar.gz
os: ubuntu-latest
bin: rsvenv
name: rsvenv-x86_64-unknown-linux-gnu.tar.gz
- target: aarch64-apple-darwin
archive: zip
os: macos-latest
bin: rsvenv
name: rsvenv-aarch64-apple-darwin.tar.gz
- target: x86_64-apple-darwin
archive: zip
os: macos-latest
bin: rsvenv
name: rsvenv-x86_64-apple-darwin.tar.gz
steps:
- name: Checkout
uses: actions/checkout@4
- name: Install musl-tools on Linux
run: sudo apt-get update --yes && sudo apt-get install --yes musl-tools
if: contains(matrix.os, 'ubuntu')
- name: Build binary
shell: bash
run: |
rustup target add ${{ matrix.target }}
cargo build --release --target ${{ matrix.target }}
if: ${{ !contains(matrix.os, 'windows') }}
- name: Strip binary
shell: bash
run: |
strip target/${{ matrix.target }}/release/${{ matrix.bin }}
- name: Package as archive
shell: bash
run: |
cd target/${{ matrix.target }}/release
tar czvf ../../../${{ matrix.name }} ${{ matrix.bin }}
cd -
- name: Publish release artifacts
uses: actions/upload-artifact@v4
with:
name: rsvenv-${{ matrix.target }}
path: "rsvenv*"
if: github.ref == 'refs/tags/test-release'
- name: Publish GitHub release
uses: softprops/action-gh-release@v2
with:
draft: true
files: "rsvenv*"
body_path: Changes.md
if: startsWith( github.ref, 'refs/tags/v' )

10 changes: 2 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: Rust
name: Rust tests

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
on: push

env:
CARGO_TERM_COLOR: always
Expand All @@ -16,7 +12,5 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ shellexpand = "3.1.0"
simplelog = { version = "0.12.2", features = ["paris"] }
sysinfo = "0.30.7"

[dev-dependencies]
mockall = "0.12.1"
tempfile = "3.10.1"

26 changes: 2 additions & 24 deletions src/virtualenv/pyenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,12 @@ use crate::errors::VirtualEnvError;

use super::{
traits::VirtualEnvCompatible,
utils::{get_current_dir, is_virtualenv},
utils::{get_current_dir, get_venvs_by_glob},
};

#[derive(Debug)]
pub struct Pyenv;

impl Pyenv {
fn try_list_root_dir<'a>(dir: PathBuf) -> Result<HashSet<String>, VirtualEnvError> {
let mut result = HashSet::new();
for path in glob::glob(format!("{}/*/envs/*/", &dir.as_path().display()).as_str())
.attach_printable("Unable to parse glob /*/envs/*/")
.change_context(VirtualEnvError::VenvBuildError)?
.flatten()
{
let value = path
.strip_prefix(&dir)
.attach_printable("Unable to strip prefix")
.change_context(VirtualEnvError::VenvBuildError)?
.to_str();
if is_virtualenv(&path).is_ok() && !value.is_none() {
result.insert(String::from(value.unwrap()));
}
}

Ok(result)
}
}

impl VirtualEnvCompatible for Pyenv {
fn root_dir(&self) -> Result<PathBuf, VirtualEnvError> {
let root = std::env::var("PYENV_ROOT").unwrap_or("~/.pyenv".to_string());
Expand All @@ -53,7 +31,7 @@ impl VirtualEnvCompatible for Pyenv {

fn list(&self) -> HashSet<String> {
if let Ok(root) = self.root_dir() {
return Pyenv::try_list_root_dir(root).unwrap_or_default();
return get_venvs_by_glob("*/envs/*".into(), &root).unwrap_or_default();
}
HashSet::new()
}
Expand Down
22 changes: 4 additions & 18 deletions src/virtualenv/rsenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,15 @@ use error_stack::{Report, Result, ResultExt};
use simplelog::{error, info};
use std::io::Write;

use itertools::Itertools;

use super::{
traits::VirtualEnvCompatible,
utils::{get_current_dir, is_virtualenv},
utils::{get_current_dir, get_venvs_by_glob},
};

#[derive(Debug)]
pub struct Rsenv;

impl Rsenv {
fn try_list_root_dir<'a>(dir: PathBuf) -> Result<HashSet<String>, VirtualEnvError> {
let result = dir
.read_dir()
.change_context(VirtualEnvError::IOError)
.attach_printable("Unable to create .python-version")?
.into_iter()
.filter_ok(|x| is_virtualenv(&x.path()).is_ok())
.map(|x| x.unwrap().file_name().into_string())
.filter(|x| x.is_ok())
.map(|x| x.unwrap());

Ok(HashSet::from_iter(result))
}

pub fn create(&self, name: &String, python: &String) -> Result<(), VirtualEnvError> {
if self.list().contains(name) {
return Err(Report::new(VirtualEnvError::AlreadyExists(
Expand Down Expand Up @@ -112,7 +96,9 @@ impl VirtualEnvCompatible for Rsenv {

fn list(&self) -> HashSet<String> {
if let Ok(root) = self.root_dir() {
return Rsenv::try_list_root_dir(root).unwrap_or_default();
let mut venvs = get_venvs_by_glob("*/*".into(), &root).unwrap_or_default();
venvs.extend(get_venvs_by_glob("*".into(), &root).unwrap_or_default());
return venvs;
}
HashSet::new()
}
Expand Down
76 changes: 75 additions & 1 deletion src/virtualenv/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, path::PathBuf};
use std::{collections::HashSet, fs, path::PathBuf};

use error_stack::{Report, Result, ResultExt};

Expand All @@ -19,3 +19,77 @@ pub fn get_current_dir() -> Result<PathBuf, VirtualEnvError> {
.attach_printable("Unable to get current dir")
.change_context(VirtualEnvError::VenvBuildError)
}

pub fn get_venvs_by_glob(glob: String, dir: &PathBuf) -> Result<HashSet<String>, VirtualEnvError> {
let mut result = HashSet::new();
for path in glob::glob(format!("{}/{glob}", &dir.as_path().display()).as_str())
.attach_printable(format!("Unable to parse glob {glob}"))
.change_context(VirtualEnvError::VenvBuildError)?
.flatten()
{
let value = path
.strip_prefix(&dir)
.attach_printable("Unable to strip prefix")
.change_context(VirtualEnvError::VenvBuildError)?
.to_str();
if is_virtualenv(&path).is_ok() && !value.is_none() {
result.insert(String::from(value.unwrap()));
}
}

Ok(result)
}

#[cfg(test)]
mod tests {
use std::fs::create_dir_all;

use super::*;
use tempfile;

#[test]
fn test_is_virtualenv_ok() {
let dir = tempfile::TempDir::new().unwrap();
let bin_dir = dir.as_ref().join("bin");
fs::create_dir(&bin_dir).unwrap();
fs::File::create(bin_dir.join("activate")).unwrap();
let result = is_virtualenv(&dir.as_ref().to_path_buf());
assert!(result.is_ok());
}

#[test]
fn test_is_virtualenv_error() {
let dir = tempfile::TempDir::new().unwrap();
let result = is_virtualenv(&dir.as_ref().to_path_buf());
assert!(result.is_err());
}

#[test]
fn test_get_venvs_by_glob() {
let dir = tempfile::TempDir::new().unwrap();
fs::create_dir(dir.as_ref().join("version")).unwrap();
create_dir_all(dir.as_ref().join("version").join("v1").join("bin")).unwrap();
create_dir_all(dir.as_ref().join("version").join("v2").join("bin")).unwrap();
create_dir_all(dir.as_ref().join("version").join("v3").join("bin")).unwrap();
fs::File::create(
dir.as_ref()
.join("version")
.join("v1")
.join("bin")
.join("activate"),
)
.unwrap();
fs::File::create(
dir.as_ref()
.join("version")
.join("v3")
.join("bin")
.join("activate"),
)
.unwrap();
let result = get_venvs_by_glob("*/*".into(), &dir.into_path()).unwrap();
// v2 is not a valid vitual environment
let expected = HashSet::from([String::from("version/v1"), String::from("version/v3")]);
assert_eq!(result, expected)
}
}

0 comments on commit 2ded3df

Please sign in to comment.