Skip to content

Commit

Permalink
wip: refactor language to a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Sep 21, 2024
1 parent 0f8a477 commit 2c30eeb
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 106 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/linux_shelltests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Shelltests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-shelltest

jobs:
run:
strategy:
matrix:
os: ["ubuntu-latest"]
runs-on: ${{ matrix.os }}
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.run_id }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run build and shelltests in docker
run: docker run -v $(pwd):/app ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.run_id }} in-docker-shelltests
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ on:
pull_request:
branches: [ main ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-shelltest

jobs:
run:
strategy:
matrix:
os: ["ubuntu-latest", "macos-latest"]
os: ["macos-latest"]
runs-on: ${{ matrix.os }}
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -31,6 +39,7 @@ jobs:
- name: Build beamup
run: cargo build

- name: Run ShellTests
- name: Run MacOS ShellTests
if: ${{ matrix.os == 'macos-latest' }}
run: |
just shelltests
just shelltests-macos
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
run:
strategy:
matrix:
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
os: ["windows-latest"]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand Down
3 changes: 3 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ in-docker-shelltests: build

shelltests:
shelltest -c --diff --debug --all shelltests/*.test

shelltests-macos:
shelltest -c --diff --debug --all shelltests/macos/*.test
11 changes: 10 additions & 1 deletion shelltests/installs.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ v1.4.1
ls ~/.local/bin/gleam || ls ~/.beamup/bin/gleam
>>>=0

ls /root/.local/share/beamup/elixir/v1.4.1/bin/gleam
>>>=0

find /root/.* -name gleam
>>>=0

ls -l /root/.local/share/beamup/gleam/v1.4.1/gleam
>>>=0

# test the version is right
~/.local/bin/gleam --version || ~/.beamup/bin/gleam --version
DEBUG=1 ~/.local/bin/gleam --version
>>>
gleam 1.4.1
>>>=0
Expand Down
3 changes: 3 additions & 0 deletions shelltests/macos/install_erlang.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test an install of erlang on macos
target/debug/beamup install erlang latest
>>>=0
17 changes: 10 additions & 7 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config;
use crate::git::GitRef;
use crate::github::download_release_tarball;
use crate::languages::{get_github_repo, Language};
use crate::languages::LanguageStruct;
use color_eyre::{eyre::Result, eyre::WrapErr};
use console::Emoji;
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -44,29 +44,32 @@ enum BuildStep<'a> {
}

pub fn run(
language: &Language,
language: &LanguageStruct,
git_ref: &GitRef,
id: &String,
_repo: &Option<String>,
force: bool,
config: &config::Config,
) -> Result<String> {
debug!("Building {language} from source from git ref={git_ref} with id={id}");
debug!(
"Building {:?} from source from git ref={git_ref} with id={id}",
language.language
);

let release_dir = config::language_release_dir(language, id, force)?;
let release_dir = config::language_release_dir(&language.language, id, force)?;

//maybe grab configure options from environment
let key = "BEAMUP_BUILD_OPTIONS";
let user_build_options = match env::var(key) {
Ok(options) => options,
_ => config::lookup_default_build_options(language, config),
_ => config::lookup_default_build_options(&language.language, config),
};

let github_repo = get_github_repo(language);
let github_repo = &language.source_repo;
let release = git_ref.to_string();

let out_dir = TempDir::new(github_repo.repo.as_str())?;
let file = download_release_tarball(language, out_dir.path(), &github_repo, &release)?;
let file = download_release_tarball(&language.language, out_dir.path(), github_repo, &release)?;

let tar_gz = File::open(&file).wrap_err_with(|| {
format!(
Expand Down
29 changes: 5 additions & 24 deletions src/cmd/component_install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::components;
use crate::eyre;
use crate::github;
use crate::utils;
use color_eyre::{eyre::Report, eyre::Result, eyre::WrapErr};
use flate2::read::GzDecoder;
use std::fs;
Expand All @@ -12,10 +12,10 @@ use zip;
pub fn run(
c: &components::Component,
release: &String,
id: &String,
_id: &str,
force: bool,
) -> Result<String, Report> {
maybe_create_release_dir(c, id, force)?;
utils::check_release_dir(&c.release_dir, force)?;
let release_dir_string = c
.release_dir
.clone()
Expand All @@ -34,6 +34,8 @@ pub fn run(
)
})?;

utils::maybe_create_release_dir(&c.release_dir, force)?;

// TODO: better ways to check the type than the extension
let ext = file.extension().map_or("", |e| e.to_str().unwrap_or(""));
match ext {
Expand Down Expand Up @@ -87,24 +89,3 @@ fn set_permissions(to: &PathBuf) -> Result<()> {
fn set_permissions(_to: &PathBuf) -> Result<()> {
Ok(())
}

fn maybe_create_release_dir(c: &components::Component, id: &String, force: bool) -> Result<()> {
match c.release_dir.try_exists() {
Ok(true) =>
match force {
true => {
info!("Force enabled. Deleting existing release directory {:?}", c.release_dir);
fs::remove_dir_all(&c.release_dir)?
},
_ => return Err(eyre!("Release directory for id {id:} already exists. Use `-f true` to delete {:?} and recreate instead of giving this error.", c.release_dir)),
}
Ok(false) => {},
Err(e) => return Err(eyre!(
"Unable to check for existence of release directory for id {id}: {e:?}"
)),
};

let _ = std::fs::create_dir_all(&c.release_dir);

Ok(())
}
31 changes: 17 additions & 14 deletions src/cmd/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config;
//use crate::config;
use crate::github;
use crate::languages;
use crate::utils;
use color_eyre::{eyre::eyre, eyre::Report, eyre::Result, eyre::WrapErr};
use flate2::read::GzDecoder;
use std::fs::File;
Expand All @@ -13,16 +14,18 @@ use zip;
use std::process::ExitStatus;

pub fn run(
language: &languages::Language,
github_repo: &github::GithubRepo,
language: &languages::LanguageStruct,
release: &str,
id: &String,
_id: &str,
_repo: &Option<String>,
force: bool,
) -> Result<String, Report> {
let release_dir = &language.release_dir;
utils::check_release_dir(release_dir, force)?;
let github_repo = &language.binary_repo;
let out_dir = TempDir::new(github_repo.repo.as_str())?;
let asset_name = github::language_asset_name(language, release)?;
let file = github::download_asset(&asset_name, out_dir.path(), github_repo, release)?;
let asset_name = &language.asset_prefix;
let file = github::download_asset(asset_name, out_dir.path(), github_repo, release)?;
debug!("file {:?} downloaded", file);
let open_file = File::open(&file).wrap_err_with(|| {
format!(
Expand All @@ -31,34 +34,34 @@ pub fn run(
)
})?;

let release_dir = config::language_release_dir(language, id, force)?;
utils::maybe_create_release_dir(release_dir, force)?;

// TODO: better ways to check the type than the extension
let ext = file.extension().map_or("", |e| e.to_str().unwrap_or(""));
match ext {
"exe" => {
let release_dir = release_dir.into_os_string().into_string().unwrap();
let release_dir = release_dir.clone().into_os_string().into_string().unwrap();
exe_run(file, release_dir.clone())?;
Ok(release_dir)
}
"zip" => {
let mut archive = zip::ZipArchive::new(open_file)?;
let extract_dir = match language {
let extract_dir = match language.language {
languages::Language::Gleam => &release_dir.join("bin"),
_ => &release_dir,
_ => release_dir,
};
archive.extract(extract_dir)?;
Ok(release_dir.into_os_string().into_string().unwrap())
Ok(release_dir.clone().into_os_string().into_string().unwrap())
}
_ => {
let tar = GzDecoder::new(open_file);
let mut archive = Archive::new(tar);
let extract_dir = match language {
let extract_dir = match language.language {
languages::Language::Gleam => &release_dir.join("bin"),
_ => &release_dir,
_ => release_dir,
};
archive.unpack(extract_dir)?;
Ok(release_dir.into_os_string().into_string().unwrap())
Ok(release_dir.clone().into_os_string().into_string().unwrap())
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/releases.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::github::print_releases;
use crate::languages::{get_github_repo, Language};
use crate::languages::LanguageStruct;

pub fn run(language: &Language) {
let github_repo = get_github_repo(language);
print_releases(&github_repo);
pub fn run(language: &LanguageStruct) {
print_releases(&language.source_repo);
}
41 changes: 0 additions & 41 deletions src/github.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::config;
use crate::languages::Language;
use color_eyre::{eyre::eyre, eyre::Report, eyre::Result, eyre::WrapErr};
use console::{style, Emoji};
Expand All @@ -21,46 +20,6 @@ pub struct GithubRepo {
pub repo: String,
}

pub fn language_asset_name(language: &Language, tag: &str) -> Result<String> {
let asset_name = match language {
Language::Gleam => {
let suffix = match (std::env::consts::ARCH, std::env::consts::OS) {
("x86_64", "linux") => "x86_64-unknown-linux-musl.tar.gz",
("aarch64", "linux") => "aarch64-unknown-linux-musl.tar.gz",
("x86_64", "macos") => "x86_64-apple-darwin.tar.gz",
("aarch64", "macos") => "aarch64-apple-darwin.tar.gz",
("x86_64", "windows") => "x86_64-pc-windows-msvc.zip",
(arch, os) => {
let e: Report =
eyre!("no {language} asset found to support arch:{arch} os:{os}");
return Err(e);
}
};

format!("{language}-{tag}-{suffix}")
}
Language::Erlang => {
let vsn = tag.strip_prefix("OTP-").unwrap_or(tag);
match (std::env::consts::ARCH, std::env::consts::OS) {
("x86", "windows") => format!("otp_win32_{vsn}.exe"),
("x86_64", "windows") => format!("otp_win64_{vsn}.exe"),
(arch, os) => {
let e: Report =
eyre!("no {language} asset found to support arch:{arch} os:{os}");
return Err(e);
}
}
}
Language::Elixir => {
// find dir of active Erlang
let otp_major_vsn = config::get_otp_major_vsn()?;
format!("elixir-otp-{otp_major_vsn:}.zip")
}
};

Ok(asset_name)
}

pub fn print_releases(GithubRepo { org, repo }: &GithubRepo) {
let rt = setup_tokio();

Expand Down
Loading

0 comments on commit 2c30eeb

Please sign in to comment.