From 9bc19e6131154c8adb12489aa38f5f0b25a64897 Mon Sep 17 00:00:00 2001 From: David Lopez Saez Date: Mon, 10 Jul 2023 01:33:48 +0200 Subject: [PATCH] Add github actions (#1) * Add github actions * Add missing actions references * Remove use of unmaintained action * Fix matrix strategy and added dependencies for `aarch64-unknown-linux-gnu` compilation * Add caching, calls to rustfmt, and improved descriptions * Fix lint errors * Add release action * Change releasae action triggers for testing * Modify path * Fix file path * Fix file path * Fix file path * Add ceation of release * Add missing checkout action * Fix permissions * Remove upload of binaries * Fix typo * Add upload of binaries to release * Fix upload binary path * Fix download of artifacts * Add debug steps * Fix binary path * Rename artifact before uploading asset to release * Add retention rules to upload and remove windows upload for testing * Fix download path * Add prerelease to releases and change trigger * Implement reusable workflows to reduce code duplication --- .cargo/config.toml | 2 + .github/release_notes.template | 0 .github/workflows/build_lint.yml | 69 +++++++++++++++++++++++++++ .github/workflows/release.yml | 80 ++++++++++++++++++++++++++++++++ src/wrapper.rs | 10 ++-- src/wrapper/python.rs | 12 ++--- 6 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 .github/release_notes.template create mode 100644 .github/workflows/build_lint.yml create mode 100644 .github/workflows/release.yml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..3c32d25 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.aarch64-unknown-linux-gnu] +linker = "aarch64-linux-gnu-gcc" diff --git a/.github/release_notes.template b/.github/release_notes.template new file mode 100644 index 0000000..e69de29 diff --git a/.github/workflows/build_lint.yml b/.github/workflows/build_lint.yml new file mode 100644 index 0000000..a9dd36e --- /dev/null +++ b/.github/workflows/build_lint.yml @@ -0,0 +1,69 @@ +name: Build and lint +on: + push: + branches: + - main + pull_request: + branches: + - main + workflow_call: + inputs: + for_release: + type: boolean + description: | + Mark if additional steps required for a release, like uploading artifacts, should be executed + required: true + default: false + version: + type: string + description: Project version used for naming artifacts + required: true +jobs: + build: + strategy: + matrix: + os-target: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + - os: ubuntu-latest + target: aarch64-unknown-linux-gnu + - os: windows-latest + target: x86_64-pc-windows-msvc + - os: macos-latest + target: x86_64-apple-darwin + runs-on: ${{ matrix.os-target.os }} + steps: + - uses: actions/checkout@v3 + - name: Setup rust toolchain + uses: dtolnay/rust-toolchain@stable + with: + toolchain: 1.70.0 + targets: ${{ matrix.os-target.target }} + - uses: Swatinem/rust-cache@v2 + - name: Install aarch64-unknown-linux-gnu requisites + run: sudo apt-get install g++-aarch64-linux-gnu -y + if: matrix.os-target.target == 'aarch64-unknown-linux-gnu' + - name: Run cargo check + run: cargo build --release --target ${{ matrix.os-target.target }} + - name: Upload artifacts for reuse in release job + uses: actions/upload-artifact@v3 + if: ${{ inputs.for_release }} + with: + name: arcanist_${{ inputs.version }}_${{ matrix.os-target.target }} + path: ${{ github.workspace }}/target/${{ matrix.os-target.target }}/release/arcanist${{ matrix.os-target.target == 'x86_64-pc-windows-msvc' && '.exe' || '' }} + if-no-files-found: error + retention-days: 5 + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup rust toolchain + uses: dtolnay/rust-toolchain@stable + with: + toolchain: 1.70.0 + components: clippy,rustfmt + - uses: Swatinem/rust-cache@v2 + - name: Run clippy + run: cargo clippy -- -D warnings + - name: Run rustfmt + run: cargo fmt --all -- --check diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..d6413fe --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,80 @@ +name: Create release +on: + push: + tags: + - "v*.*.*" +jobs: + setup-validation: + outputs: + version: ${{ steps.gather-version.outputs.version }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Extract version from Cargo.toml + id: gather-version + run: echo version=$(cargo metadata --format-version 1 | jq -r '.packages[] | select(.name == "arcanist").version') >> $GITHUB_OUTPUT + - name: Ensure release does not already exist + run: | + git fetch origin &> /dev/null + version=${{ steps.gather-version.outputs.version }} + if [[ -n "$(git tag -l v${version})" ]]; then + echo "A release '${version}' already exists." >&2 + exit 1 + else + echo "Tag 'v${version}' will be created" + fi + build-lint: + uses: ./.github/workflows/build_lint.yml + needs: setup-validation + with: + for_release: true + version: ${{ needs.setup-validation.outputs.version }} + release: + needs: [setup-validation, build-lint] + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v3 + - name: Generate release notes + run: | + sed 's/{version}/${{ needs.setup-validation.outputs.version }}/g' ${{ github.workspace }}/.github/release_notes.template \ + > ${{ github.workspace }}/.github/release_notes.txt + - name: Create release + uses: softprops/action-gh-release@v1 + id: release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + generate_release_notes: true + name: Arcanist - ${{ needs.setup-validation.outputs.version }} + tag_name: v${{ needs.setup-validation.outputs.version }} + body_path: ${{ github.workspace }}/.github/release_notes.txt + target_commitish: ${{ github.base_ref }} + prerelease: true + upload-to-release: + needs: [setup-validation, build-lint, release] + permissions: + contents: write + strategy: + matrix: + target: + - x86_64-unknown-linux-gnu + - aarch64-unknown-linux-gnu + - x86_64-pc-windows-msvc + - x86_64-apple-darwin + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/download-artifact@v3 + with: + name: arcanist_${{ needs.setup-validation.outputs.version }}_${{ matrix.target }} + path: ${{ github.workspace }}/target/${{ matrix.target }}/release + - name: Upload arcanist binary to release + run: | + mv ${{ github.workspace }}/target/${{ matrix.target }}/release/arcanist${{ matrix.target == 'x86_64-pc-windows-msvc' && '.exe' || '' }} \ + arcanist_${{ needs.setup-validation.outputs.version }}_${{ matrix.target }}${{ matrix.target == 'x86_64-pc-windows-msvc' && '.exe' || '' }} + gh release upload v${{ needs.setup-validation.outputs.version }} \ + arcanist_${{ needs.setup-validation.outputs.version }}_${{ matrix.target }}${{ matrix.target == 'x86_64-pc-windows-msvc' && '.exe' || '' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/wrapper.rs b/src/wrapper.rs index 9be8ca6..a713b1b 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; mod python; mod yaml; @@ -14,19 +14,19 @@ pub enum WrapperKind { } trait WrapperKindActions { - fn does_function_exists(&self, spec_file_path: &PathBuf, function_name: &str) -> bool; - fn call_function(&self, spec_file_path: &PathBuf, function_name: &str); + fn does_function_exists(&self, spec_file_path: &Path, function_name: &str) -> bool; + fn call_function(&self, spec_file_path: &Path, function_name: &str); } impl WrapperKindActions for WrapperKind { - fn does_function_exists(&self, spec_file_path: &PathBuf, function_name: &str) -> bool { + fn does_function_exists(&self, spec_file_path: &Path, function_name: &str) -> bool { match self { WrapperKind::Yaml => todo!(), WrapperKind::Python => python::does_function_exists(spec_file_path, function_name), } } - fn call_function(&self, spec_file_path: &PathBuf, function_name: &str) { + fn call_function(&self, spec_file_path: &Path, function_name: &str) { match self { WrapperKind::Yaml => todo!(), WrapperKind::Python => python::call_function(spec_file_path, function_name), diff --git a/src/wrapper/python.rs b/src/wrapper/python.rs index b79ee15..ce49cb0 100644 --- a/src/wrapper/python.rs +++ b/src/wrapper/python.rs @@ -2,22 +2,22 @@ use duct::cmd; use std::fs; use std::io::BufRead; use std::io::BufReader; -use std::path::PathBuf; +use std::path::Path; use regex::Regex; const REGEX_FUNCTION_PATTERN: &str = r"def[ \t]*{{function_name}}\b.*:"; -pub fn does_function_exists(spec_file_path: &PathBuf, function_name: &str) -> bool { - let file_content = - fs::read_to_string(spec_file_path).expect(&format!("Failed to read {:?}", spec_file_path)); +pub fn does_function_exists(spec_file_path: &Path, function_name: &str) -> bool { + let file_content = fs::read_to_string(spec_file_path) + .unwrap_or_else(|_| panic!("Failed to read {:?}", spec_file_path)); let regex_pattern = ®EX_FUNCTION_PATTERN.replace("{{function_name}}", function_name); - let regex = Regex::new(®ex_pattern).expect("Failed to define regex"); + let regex = Regex::new(regex_pattern).expect("Failed to define regex"); regex.is_match(&file_content) } -pub fn call_function(spec_file_path: &PathBuf, function_name: &str) { +pub fn call_function(spec_file_path: &Path, function_name: &str) { let python_code = "\ import importlib.util; \ spec = importlib.util.spec_from_file_location('main', '{{spec_file_path}}'); \