Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add cargo-generate workflow #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 23 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
[package]
name = "cosmic-app-template"
name = "{{ project-name }}"
version = "0.1.0"
authors = ["{{ authors }}"]
license = "{{ license }}"
edition = "2021"
repository = "{{ repository-url }}"
description = """
{{ description }}
"""

[dependencies]
futures-util = "0.3.31"
Expand All @@ -18,22 +24,22 @@ features = ["fluent-system", "desktop-requester"]
git = "https://github.com/pop-os/libcosmic.git"
# See https://github.com/pop-os/libcosmic/blob/master/Cargo.toml for available features.
features = [
# Accessibility support
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting came from a run of https://dprint.dev/ on md and toml files

"a11y",
# Uses cosmic-settings-daemon to watch for config file changes
"dbus-config",
# Support creating additional application windows.
"multi-window",
# On app startup, focuses an existing instance if the app is already open
"single-instance",
# Uses tokio as the executor for the runtime
"tokio",
# Windowing support for X11, Windows, Mac, & Redox
"winit",
# Add Wayland support to winit
"wayland",
# GPU-accelerated rendering
"wgpu",
# Accessibility support
"a11y",
# Uses cosmic-settings-daemon to watch for config file changes
"dbus-config",
# Support creating additional application windows.
"multi-window",
# On app startup, focuses an existing instance if the app is already open
"single-instance",
# Uses tokio as the executor for the runtime
"tokio",
# Windowing support for X11, Windows, Mac, & Redox
"winit",
# Add Wayland support to winit
"wayland",
# GPU-accelerated rendering
"wgpu",
]

# Uncomment to test a locally-cloned libcosmic
Expand Down
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
# COSMIC Application Template

A template for developing applications for the COSMIC™ desktop environment.
A template for developing applications for the COSMIC™ desktop environment using [libcosmic][libcosmic].

## Documentation

Refer to the [libcosmic API documentation][api-docs] and [book][book] for help with building applications with [libcosmic][libcosmic].

## Getting Started

Log into your GitHub account and click the "Use this template" button above. This will create a new repository in your account. Choose a name for this repository, and then clone it locally onto your system. Make the following changes after cloning it:
To generate a new project based on this template, [install `cargo generate`][cargo-generate-install] and run:

- In `Cargo.toml`, change the `name` and set your `license` and `repository`.
- Create a `LICENSE` file containing your chosen software license.
- Rename the file `i18n/en/cosmic_app_template.ftl` by replacing the `cosmic_app_template` portion with the new crate `name` you set in `Cargo.toml`.
- In `justfile`, change the `name` and `appid` variables with your own.
- In `src/app.rs`, change the `APP_ID` value in the `Application` implementation of the `AppModel`.
- In `src/app.rs`, change the `REPOSITORY` const with the URL to your application's git repository.
- In `res/app.desktop`, change the `Name=`, `Exec=`, and `Icon=` fields
- Set your license within the SPDX tags at the top of each source file
```bash
cargo generate gh:pop-os/cosmic-app-template
```

A [justfile](./justfile) is included by default with common recipes used by other COSMIC projects. Install from [casey/just][just]
Then follow the prompts to pre-populate and customize your project.

- `just` builds the application with the default `just build-release` recipe
- `just run` builds and runs the application
- `just install` installs the project into the system
- `just vendor` creates a vendored tarball
- `just build-vendored` compiles with vendored dependencies from that tarball
- `just check` runs clippy on the project to check for linter warnings
- `just check-json` can be used by IDEs that support LSP
### Manual Template Configuration

## Documentation
If you choose not to use `cargo generate`, you may still use this template.

Refer to the [libcosmic API documentation][api-docs] and [book][book] for help with building applications with [libcosmic][libcosmic].
Log into your GitHub account and navigate to [this template there][app-template] and click the "Use this template" button above. This will create a new repository in your account. Choose a name for this repository, and then clone it locally onto your system. After cloning it, you must:

- Manually replace all template fields matching `{{ <some field> }}` (regex `\{\{ .*\}\}`) in all source files.
- Create a `LICENSE` file containing your chosen software license.
- Rename the file `i18n/en/cosmic_app_template.ftl` by replacing the `cosmic_app_template` portion with the new crate `name` you set in `Cargo.toml`.
- Set your license within the SPDX tags at the top of each source file
- Replace this `README.md` with your own, optionally based on `README.md.liquid`, removing the postfix.
- Delete the `generate/` directory.

[api-docs]: https://pop-os.github.io/libcosmic/cosmic/
[book]: https://pop-os.github.io/libcosmic-book/
[libcosmic]: https://github.com/pop-os/libcosmic/
[just]: https://github.com/casey/just
[cargo-generate-install]: https://cargo-generate.github.io/cargo-generate/installation.html
[app-template]: https://github.com/pop-os/cosmic-app-template
16 changes: 16 additions & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[template]
cargo_generate_version = ">=0.22"

[placeholders]
description = { type = "string", prompt = "App description:", default = "An App built for COSMIC, based on the [`libcosmic` template](https://github.com/pop-os/cosmic-app-template/" }
repository-url = { type = "string", prompt = "Repository URL (https://*)", regex = "^https?:\\/\\/(www\\.)?[\\w\\-]+(\\.[\\w\\-]+)+[/#?]?.*$" }
app-id = { type = "string", prompt = "App ID (com.example.MyCosmicApp)", regex = "^[a-zA-Z0-9]+\\.[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$" }

[hooks]
pre = [
"generate/license.rhai",
"generate/rename.rhai",
]
post = [
"generate/cleanup.rhai",
]
6 changes: 6 additions & 0 deletions generate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Cargo Generate Artifacts

See usage and docs for Rhai scripts:

- https://cargo-generate.github.io/cargo-generate/templates/scripting.rhai-extensions.html
- https://rhai.rs/book/index.html
2 changes: 2 additions & 0 deletions generate/cleanup.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Cleanup cargo generate artifacts
file::delete("generate");
20 changes: 20 additions & 0 deletions generate/license.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This example script asks the users selection of license

let license = variable::prompt("Which license do you want to use?",
"MPL-2.0",
["MPL-2.0",
"MIT",
"AGPL-3.0",
"Apache-2.0",
"Unlicense",
"(Other)",
"None"
]
);

if license == "(Other)" {
license = variable::prompt("Please define *a valid* identifier from <https://github.com/spdx/license-list-data/blob/v3.20/licenses.md>")
}

print(`📝 Remeber to add a correct LICENSE file for ${license} - found at <https://github.com/spdx/license-list-data/blob/v3.20/licenses.md> `);
variable::set("license", license);
6 changes: 6 additions & 0 deletions generate/rename.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Rename and move files

let project_name = variable::get("project-name");
file::rename("i18n/en/cosmic_app_template.ftl", `i18n/en/${project_name}.ftl`);

file::rename("generate/template-README.md", "README.md");
26 changes: 26 additions & 0 deletions generate/template-README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# {{ project-name | title_case }}

{{ description }}

> Built using the [COSMIC™ App Template](https://github.com/pop-os/cosmic-app-template)

## Getting Started

A [justfile](./justfile) is included by default with common recipes used by other COSMIC projects. Install from [casey/just][just]

```sh
# Builds the application with the default `just build-release` recipe
just

# Lists all commands
just --list
```

## Documentation

Refer to the [libcosmic API documentation][api-docs] and [book][book] for help with building applications with [libcosmic][libcosmic].

[api-docs]: https://pop-os.github.io/libcosmic/cosmic/
[book]: https://pop-os.github.io/libcosmic-book/
[libcosmic]: https://github.com/pop-os/libcosmic/
[just]: https://github.com/casey/just
2 changes: 1 addition & 1 deletion i18n.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fallback_language = "en"

[fluent]
assets_dir = "i18n"
assets_dir = "i18n"
6 changes: 3 additions & 3 deletions i18n/en/cosmic_app_template.ftl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
app-title = COSMIC App Template
about = About
app-title = {{ project-name | title_case }}
about = Built using COSMIC
view = View
welcome = Welcome to COSMIC! ✨
welcome = Welcome to the {{ project-name | title_case }} COSMIC App! ✨
page-id = Page { $num }
8 changes: 4 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name := 'cosmic-app-template'
appid := 'com.example.CosmicAppTemplate'
name := '{{ project-name }}'
app-id := '{{ app-id }}'

rootdir := ''
prefix := '/usr'
Expand All @@ -9,15 +9,15 @@ base-dir := absolute_path(clean(rootdir / prefix))
bin-src := 'target' / 'release' / name
bin-dst := base-dir / 'bin' / name

desktop := appid + '.desktop'
desktop := app-id + '.desktop'
desktop-src := 'res' / desktop
desktop-dst := clean(rootdir / prefix) / 'share' / 'applications' / desktop

icons-src := 'res' / 'icons' / 'hicolor'
icons-dst := clean(rootdir / prefix) / 'share' / 'icons' / 'hicolor'

icon-svg-src := icons-src / 'scalable' / 'apps' / 'icon.svg'
icon-svg-dst := icons-dst / 'scalable' / 'apps' / appid + '.svg'
icon-svg-dst := icons-dst / 'scalable' / 'apps' / app-id + '.svg'

# Default recipe which runs `just build-release`
default: build-release
Expand Down
6 changes: 3 additions & 3 deletions res/app.desktop
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[Desktop Entry]
Name={{APP_NAME}}
Exec={{APP_BINARY}} %F
Name={{ project-name | title_case }}
Exec={{ project-name }} %F
Terminal=false
Type=Application
StartupNotify=true
Icon={{APP_ID}}
Icon={{ app_id }}
Categories=COSMIC;
Keywords=
MimeType=
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: {{LICENSE}}
// SPDX-License-Identifier: {{ license }}

use crate::config::Config;
use crate::fl;
Expand All @@ -11,7 +11,7 @@ use cosmic::{cosmic_theme, theme, Application, ApplicationExt, Apply, Element};
use futures_util::SinkExt;
use std::collections::HashMap;

const REPOSITORY: &str = "https://github.com/pop-os/cosmic-app-template";
const REPOSITORY: &str = "{{ repository-url }}";
const APP_ICON: &[u8] = include_bytes!("../res/icons/hicolor/scalable/apps/icon.svg");

/// The application model stores app-specific state used to describe its interface and
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Application for AppModel {
type Message = Message;

/// Unique identifier in RDNN (reverse domain name notation) format.
const APP_ID: &'static str = "com.example.CosmicAppTemplate";
const APP_ID: &'static str = "{{ app-id }}";

fn core(&self) -> &Core {
&self.core
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: {{LICENSE}}
// SPDX-License-Identifier: {{ license }}

use cosmic::cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry};

Expand Down
2 changes: 1 addition & 1 deletion src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: {{LICENSE}}
// SPDX-License-Identifier: {{ license }}

//! Provides localization support for this crate.

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: {{LICENSE}}
// SPDX-License-Identifier: {{ license }}

mod app;
mod config;
Expand Down