Skip to main content

rl_lang/tooling/
workflows.rs

1use std::fs;
2use std::path::Path;
3
4const CHECK_YML: &str = r#"name: RL Check
5
6on:
7  push:
8    branches: [main]
9  pull_request:
10    branches: [main]
11  workflow_dispatch:
12
13jobs:
14  rl-check:
15    name: Check ${{ matrix.file }}
16    runs-on: ubuntu-latest
17    strategy:
18      fail-fast: false
19      matrix:
20        file:
21          - src/main.rl
22    steps:
23      - uses: actions/checkout@v4
24
25      - name: Run RL check
26        uses: rl-lang/rl-check@main
27        with:
28          file: ${{ matrix.file }}
29"#;
30
31const PACKAGE_YML: &str = r#"name: Release
32
33on:
34  workflow_dispatch:
35
36jobs:
37  package:
38    strategy:
39      matrix:
40        os: [ubuntu-latest, windows-latest]
41        file:
42          - { path: src/main.rl, name: program }
43    runs-on: ${{ matrix.os }}
44    steps:
45      - uses: rl-lang/rl-package@main
46        with:
47          file: ${{ matrix.file.path }}
48          output: ${{ matrix.file.name }}
49
50  release:
51    needs: package
52    runs-on: ubuntu-latest
53    permissions:
54      contents: write
55    steps:
56      - name: Download all artifacts
57        uses: actions/download-artifact@v4
58        with:
59          path: artifacts
60
61      - name: Create GitHub Release
62        uses: softprops/action-gh-release@v2
63        with:
64          tag_name: release-${{ github.run_number }}
65          name: Release ${{ github.run_number }}
66          files: artifacts/**/*
67"#;
68
69/// Generates GitHub Actions workflow files based on the provided flags.
70///
71/// - check   -> workflows/check.yml
72/// - package -> workflows/release.yml
73///
74/// Exits with code 1 if a target file already exists or any IO error occurs.
75pub fn generate(check: bool, package: bool) {
76    let dir = Path::new("workflows");
77    if let Err(e) = fs::create_dir_all(dir) {
78        eprintln!("error: could not create workflows: {}", e);
79        std::process::exit(1);
80    }
81
82    if check {
83        write_file(dir.join("check.yml").as_path(), CHECK_YML);
84    }
85    if package {
86        write_file(dir.join("release.yml").as_path(), PACKAGE_YML);
87    }
88}
89
90fn write_file(path: &Path, content: &str) {
91    if path.exists() {
92        eprintln!("error: '{}' already exists", path.display());
93        std::process::exit(1);
94    }
95    if let Err(e) = fs::write(path, content) {
96        eprintln!("error: failed to write '{}': {}", path.display(), e);
97        std::process::exit(1);
98    }
99    println!("created '{}'", path.display());
100}