From 3cc8278f368e8319ccbd9ac3d924bc552c013fcc Mon Sep 17 00:00:00 2001 From: messense Date: Thu, 9 Feb 2023 17:27:50 +0800 Subject: [PATCH] Replace `CI.yml` template with `generate-ci` --- src/ci.rs | 6 +++- src/new_project.rs | 23 ++++++++++++-- src/templates/CI.yml.j2 | 70 ----------------------------------------- 3 files changed, 25 insertions(+), 74 deletions(-) delete mode 100644 src/templates/CI.yml.j2 diff --git a/src/ci.rs b/src/ci.rs index 8b96c9711..963103c06 100644 --- a/src/ci.rs +++ b/src/ci.rs @@ -111,7 +111,11 @@ impl GenerateCI { } } - fn generate_github(&self, project_name: &str, bridge_model: &BridgeModel) -> Result { + pub(crate) fn generate_github( + &self, + project_name: &str, + bridge_model: &BridgeModel, + ) -> Result { let is_abi3 = matches!(bridge_model, BridgeModel::BindingsAbi3(..)); let is_bin = bridge_model.is_bin(); let setup_python = self.pytest diff --git a/src/new_project.rs b/src/new_project.rs index c13ada339..0c235cd3a 100644 --- a/src/new_project.rs +++ b/src/new_project.rs @@ -1,3 +1,5 @@ +use crate::ci::GenerateCI; +use crate::BridgeModel; use anyhow::{bail, Context, Result}; use console::style; use dialoguer::{theme::ColorfulTheme, Select}; @@ -18,6 +20,7 @@ struct ProjectGenerator<'a> { crate_name: String, bindings: String, layout: ProjectLayout, + ci_config: String, overwrite: bool, } @@ -40,14 +43,23 @@ impl<'a> ProjectGenerator<'a> { env.add_template("main.rs", include_str!("templates/main.rs.j2"))?; env.add_template("build.rs", include_str!("templates/build.rs.j2"))?; env.add_template("__init__.py", include_str!("templates/__init__.py.j2"))?; - env.add_template("CI.yml", include_str!("templates/CI.yml.j2"))?; env.add_template("example.udl", include_str!("templates/example.udl.j2"))?; + + let bridge_model = match bindings.as_str() { + "bin" => BridgeModel::Bin(None), + "cffi" => BridgeModel::Cffi, + "uniffi" => BridgeModel::UniFfi, + _ => BridgeModel::Bindings(bindings.clone(), 7), + }; + let ci_config = GenerateCI::default().generate_github(&project_name, &bridge_model)?; + Ok(Self { env, project_name, crate_name, bindings, layout, + ci_config, overwrite, }) } @@ -60,7 +72,7 @@ impl<'a> ProjectGenerator<'a> { // CI configuration let gh_action_path = project_path.join(".github").join("workflows"); fs::create_dir_all(&gh_action_path)?; - self.write_project_file(&gh_action_path, "CI.yml")?; + self.write_content(&gh_action_path, "CI.yml", self.ci_config.as_bytes())?; let rust_project = match self.layout { ProjectLayout::Mixed { src } => { @@ -114,9 +126,14 @@ impl<'a> ProjectGenerator<'a> { } fn write_project_file(&self, directory: &Path, file: &str) -> Result<()> { + let content = self.render_template(file)?; + self.write_content(directory, file, content.as_bytes()) + } + + fn write_content(&self, directory: &Path, file: &str, content: &[u8]) -> Result<()> { let path = directory.join(file); if self.overwrite || !path.exists() { - fs::write(path, self.render_template(file)?)?; + fs::write(path, content)?; } Ok(()) } diff --git a/src/templates/CI.yml.j2 b/src/templates/CI.yml.j2 deleted file mode 100644 index d90595712..000000000 --- a/src/templates/CI.yml.j2 +++ /dev/null @@ -1,70 +0,0 @@ -name: CI - -on: - push: - branches: - - main - - master - pull_request: - workflow_dispatch: - -jobs: - linux: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: PyO3/maturin-action@v1 - with: - manylinux: auto - command: build - args: --release --sdist -o dist --find-interpreter - - name: Upload wheels - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist - - windows: - runs-on: windows-latest - steps: - - uses: actions/checkout@v3 - - uses: PyO3/maturin-action@v1 - with: - command: build - args: --release -o dist --find-interpreter - - name: Upload wheels - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist - - macos: - runs-on: macos-latest - steps: - - uses: actions/checkout@v3 - - uses: PyO3/maturin-action@v1 - with: - command: build - args: --release -o dist --universal2 --find-interpreter - - name: Upload wheels - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist - - release: - name: Release - runs-on: ubuntu-latest - if: "startsWith(github.ref, 'refs/tags/')" - needs: [ macos, windows, linux ] - steps: - - uses: actions/download-artifact@v3 - with: - name: wheels - - name: Publish to PyPI - uses: PyO3/maturin-action@v1 - env: - MATURIN_PYPI_TOKEN: {{ '${{ secrets.PYPI_API_TOKEN }}' }} - with: - command: upload - args: --skip-existing *