Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/cargo-gen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Create Cargo Generate Template
# Template is created on a new branch

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
push:
branches:
- main

permissions:
contents: write # for committing to cargo-gen branch.

jobs:
create-cargo-generate-template:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 # repo checkout
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
- name: Replace values with placeholders
run: ./fill_template.rs
- name: Commit and Push
run: |
# 1. Configure Git
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"

# 2. Create/Switch to the target branch
git checkout -b cargo-gen

# 3. Add and commit
git add .
git commit -m "Replace values with template values"

# 4. Push to the remote
git push origin cargo-gen --force
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ You can compile your app natively or for the web, and share it using Github Page

## Getting started

### Using Github "Use this template" and manual edit

Start by clicking "Use this template" at https://github.com/emilk/eframe_template/ or follow [these instructions](https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template).

Change the name of the crate: Choose a good name for your project, and change the name to it in:
Expand All @@ -25,8 +27,27 @@ Change the name of the crate: Choose a good name for your project, and change th
* Change the `'./eframe_template.js'` to `./your_crate.js` (in `filesToCache` array)
* Change the `'./eframe_template_bg.wasm'` to `./your_crate_bg.wasm` (in `filesToCache` array)

Alternatively, you can run `fill_template.sh` which will ask for the needed names and email and perform the above patches for you. This is particularly useful if you clone this repository outside GitHub and hence cannot make use of its
templating function.
### Using cargo-generate

This is particularly useful if you clone this repository outside GitHub and hence cannot make use of its templating function.

You first need to install `cargo generate` this can be done using the command `cargo install cargo-generate` or see the [docs](https://cargo-generate.github.io/cargo-generate/installation.html) for more info.

Then run the following command and fill in the values to generate a new project `cargo generate emilk/eframe_template --branch cargo-gen`.
You may also provide default values via [many methods](https://cargo-generate.github.io/cargo-generate/templates/template_defined_placeholders.html#default-values-for-placeholders).
For the list of placeholder names see (cargo-generate.toml)

You can also add egui to your cargo generate config as a [favorite](https://cargo-generate.github.io/cargo-generate/favorites.html) to make the command you need to type shorter. For example you could add the following:

```
# $CARGO_HOME/cargo-generate.toml eg. ~/.cargo/cargo-generate.toml
[favorites.egui]
git = "git@github.com:emilk/eframe_template.git"
branch = "cargo-gen"
description = "Sets up a new egui project"
```

Then you can use the shorter command `cargo generate egui`.

### Learning about egui

Expand Down
24 changes: 24 additions & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[template]
# These will not be copied to users project
ignore = [
"fill_template.rs",
".github/workflows/cargo-gen.yml",
]
exclude = [
# These cause the substitution code to fail
"assets/apple-touch-icon.png",
"assets/favicon-16x16.png",
"assets/favicon-192x192.png",
"assets/favicon-32x32.png",
"assets/favicon-512x512.png",
"assets/favicon.ico",
# These use similar syntax to Liquid and we don't have any changes we need to make
".github",
]

[placeholders]
crate_display_name = { prompt = "Crate display name", default = "eframe template", type = "string" }
pwa_short_name = { prompt = "PWA Short Name", default = "egui-template-pwa", type = "string" }
pwa_name = { prompt = "PWA Name", default = "egui Template PWA", type = "string" }
app_struct_identifier = { prompt = "App struct identifier", default = "TemplateApp", type = "string" }
github_repository_owner_and_name = { prompt = "Github user/organization and repo (for status badges in readme)", default = "emilk/eframe_template", type = "string" }
13 changes: 0 additions & 13 deletions fill_template.ps1

This file was deleted.

91 changes: 91 additions & 0 deletions fill_template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env -S cargo +nightly -Zscript
---cargo
[dependencies]
anyhow = "1.0.102"
---


use std::{io::Write as _, path::Path};

use anyhow::Context as _;

pub fn main() -> anyhow::Result<()> {
do_switch(
&["Cargo.toml", "src/main.rs", "index.html", "assets/sw.js"],
"eframe_template",
"{{ crate_name }}",
)?;
do_switch(
&["Cargo.toml"],
"Emil Ernerfeldt <emil.ernerfeldt@gmail.com>",
"{{ authors }}",
)?;
do_switch(
&["src/main.rs", "src/app.rs", "index.html"],
"eframe template",
"{{ crate_display_name }}",
)?;
do_switch(
&["assets/sw.js"],
"egui-template-pwa",
"{{ pwa_short_name }}",
)?;
do_switch(
&["assets/manifest.json"],
"egui-template-pwa",
"{{ pwa_short_name }}",
)?;
do_switch(
&["assets/manifest.json"],
"egui Template PWA",
"{{ pwa_name }}",
)?;
do_switch(
&["src/app.rs", "src/lib.rs", "src/main.rs"],
"TemplateApp",
"{{ app_struct_identifier }}",
)?;
do_switch(
&["README.md"],
"# eframe template",
"# {{ crate_display_name }}",
)?;
do_switch(
&["README.md"],
"repo/github/emilk/eframe_template",
"repo/github/{{ github_repository_owner_and_name }}",
)?;
do_switch(
&["README.md"],
"emilk/eframe_template/workflows",
"{{ github_repository_owner_and_name }}/workflows",
)?;
do_switch(
&["README.md"],
"emilk/eframe_template/actions",
"{{ github_repository_owner_and_name }}/actions",
)?;
println!("Completed");
Ok(())
}

fn do_switch<P: std::fmt::Debug + AsRef<Path>>(
paths: &[P],
from: &str,
to: &str,
) -> anyhow::Result<()> {
for path in paths {
let contents = std::fs::read_to_string(path)
.with_context(|| format!("failed to read file contents of: {path:?}"))?;
let output = contents.replace(from, to);
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path)
.with_context(|| format!("failed to open file for writing: {path:?}"))?;
file.write_all(output.as_bytes())
.with_context(|| format!("failed to write changes to: {path:?}"))?;
}
Ok(())
}
36 changes: 0 additions & 36 deletions fill_template.sh

This file was deleted.

Loading