Skip to content

Commit

Permalink
Fixes to scaffold-tauri-app
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed May 20, 2024
1 parent 8019288 commit e8e8c3a
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"start": "AGENTS=2 npm run network",
"network": "npm run build:happ && BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) concurrently -k \"npm run local-services\" \"UI_PORT=1420 npm start -w ui\" \"npm run launch\"",
"android:network": "BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) concurrently -k \"npm run local-services\" \"UI_PORT=1420 npm start -w ui\" \"npm run tauri dev\" \"npm run tauri android dev\"",
"local-services": "hc run-local-services --bootstrap-interface $INTERNAL_IP --bootstrap-port $BOOTSTRAP_PORT --signal-interfaces $INTERNAL_IP --signal-port $SIGNAL_PORT",
"launch": "concurrently-repeat \"npm run tauri dev\" $AGENTS",
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"tauri": "BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) tauri"
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": ">=2.0.0-beta.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ pub fn run() {
}

fn internal_ip() -> String {
if cfg!(mobile) {
std::option_env!("INTERNAL_IP")
.expect("Environment variable INTERNAL_IP was not set")
.to_string()
} else {
String::from("localhost")
}
std::option_env!("INTERNAL_IP")
.expect("Environment variable INTERNAL_IP was not set")
.to_string()
}

fn bootstrap_url() -> Url2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
},
"bundle": {
"active": true,
"targets": "all"
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/[email protected]",
"icons/icon.icns",
"icons/icon.ico"
]
}
}
6 changes: 3 additions & 3 deletions crates/scaffold-tauri-app/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ set -e

DIR=$(pwd)

nix shell github:holochain/holochain#hc-scaffold --command bash -c "
nix shell --override-input versions "github:holochain/holochain?dir=versions/0_3_rc" github:holochain/holochain#hc-scaffold --command bash -c "
cd /tmp
rm -rf forum-scaffold-tauri-app
hc-scaffold web-app --template lit forum-scaffold-tauri-app --setup-nix true
hc-scaffold --template lit web-app forum-scaffold-tauri-app --setup-nix true -F
cd /tmp/forum-scaffold-tauri-app
nix flake update
nix develop --command bash -c \"npm i && hc scaffold dna forum && hc scaffold zome posts --integrity dnas/forum/zomes/integrity/ --coordinator dnas/forum/zomes/coordinator/\"
"

nix run .#scaffold-tauri-app -- --path /tmp/forum-scaffold-tauri-app --ui-package ui
nix run .#scaffold-tauri-app -- --path /tmp/forum-scaffold-tauri-app --ui-package ui --bundle-identifier org.myorg.myapp

cd /tmp/forum-scaffold-tauri-app

Expand Down
6 changes: 5 additions & 1 deletion crates/scaffold-tauri-app/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ struct Args {
#[clap(long)]
pub ui_package: Option<String>,

/// The bundle identifier for the Tauri app
#[clap(long)]
pub bundle_identifier: Option<String>,

/// The path of the file tree to modify.
#[clap(long, default_value = "./.")]
pub path: PathBuf,
Expand All @@ -34,7 +38,7 @@ fn internal_main() -> Result<()> {

let file_tree = file_tree_utils::load_directory_into_memory(&args.path)?;

let file_tree = scaffold_tauri_app(file_tree, args.ui_package)?;
let file_tree = scaffold_tauri_app(file_tree, args.ui_package, args.bundle_identifier)?;

let file_tree = MergeableFileSystemTree::<OsString, String>::from(file_tree);

Expand Down
25 changes: 22 additions & 3 deletions crates/scaffold-tauri-app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use dialoguer::Input;
use file_tree_utils::{dir_to_file_tree, map_file, FileTree, FileTreeError};
use handlebars::{no_escape, RenderErrorReason};
use holochain_scaffolding_utils::GetOrChooseWebAppManifestError;
Expand All @@ -8,6 +9,7 @@ use npm_scaffolding_utils::{
add_npm_dev_dependency_to_package, add_npm_script_to_package, choose_npm_package,
guess_or_choose_package_manager, NpmScaffoldingUtilsError,
};
use rust_scaffolding_utils::{add_member_to_workspace, RustScaffoldingUtilsError};
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
Expand Down Expand Up @@ -41,6 +43,9 @@ pub enum ScaffoldExecutableHappError {
#[error(transparent)]
NixScaffoldingUtilsError(#[from] NixScaffoldingUtilsError),

#[error(transparent)]
RustScaffoldingUtilsError(#[from] RustScaffoldingUtilsError),

#[error(transparent)]
DialoguerError(#[from] dialoguer::Error),

Expand All @@ -57,11 +62,13 @@ pub enum ScaffoldExecutableHappError {
#[derive(Serialize, Deserialize, Debug)]
struct ScaffoldExecutableHappData {
app_name: String,
identifier: String,
}

pub fn scaffold_tauri_app(
file_tree: FileTree,
ui_package: Option<String>,
bundle_identifier: Option<String>,
) -> Result<FileTree, ScaffoldExecutableHappError> {
// - Detect npm package manager
let package_manager = guess_or_choose_package_manager(&file_tree)?;
Expand All @@ -80,11 +87,23 @@ pub fn scaffold_tauri_app(
let h = register_case_helpers(h);
let h = register_merge(h);

let identifier: String = match bundle_identifier {
Some(i) => i,
None => Input::with_theme(&ColorfulTheme::default()).with_prompt(format!("Input the bundle identifier for your app (eg: org.myorg.{app_name}): ")).validate_with(|input| {
if input.contains("-") || input.contains("_") {
Err(String::from("The bundle identifier can only contain alphanumerical characters."))
} else {
Ok(())
}
}).interact_text()?
};

let mut file_tree = render_template_file_tree_and_merge_with_existing(
file_tree,
&h,
&template_file_tree,
&ScaffoldExecutableHappData {
identifier,
app_name: app_name.clone(),
},
)?;
Expand All @@ -99,9 +118,9 @@ pub fn scaffold_tauri_app(

map_file(
&mut file_tree,
&workspace_cargo_toml_path .as_path(),
&workspace_cargo_toml_path.clone().as_path(),
|cargo_toml_content| {
add_member_to_workspace(&(workspace_cargo_toml_path, cargo_toml_content), String::from("src-tauri"))
add_member_to_workspace(&(workspace_cargo_toml_path.clone(), cargo_toml_content), String::from("src-tauri"))
},
)?;

Expand Down Expand Up @@ -377,7 +396,7 @@ mod tests {
}
};

let repo = scaffold_tauri_app(repo, Some(String::from("package1"))).unwrap();
let repo = scaffold_tauri_app(repo, Some(String::from("package1")), Some(String::from("studio.darksoil.myapp"))).unwrap();

assert_eq!(
file_content(&repo, PathBuf::from("package.json").as_path()).unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ tauri = { version = "2.0.0-beta", features = [] }
tauri-plugin-holochain = { git = "https://github.com/darksoil-studio/tauri-plugin-holochain", branch = "main" }
holochain_types = { version = "0.3.1-rc" }
lair_keystore = { version = "0.4.0" }
holochain_client = { version = "0.5.0-dev" }

# Uncomment this crates dependency and add the appropriate holochain_client version
# compatible with holochain 0.3.x-rc when it's released to crates.io...
# holochain_client = { version = "0.5.0-dev" }
# ... and remove these git dependency
holochain_client = { git="https://github.com/guillemcordoba/holochain-client-rust", branch = "main" }

log = "0.4"
tauri-plugin-log = "2.0.0-beta"
url2 = "0.0.6"
app_dirs2 = "2.5.5"
tempdir = "0.3.7"
anyhow = "1"

[patch.crates-io]
# TODO: remove these patches once https://github.com/holochain/tx5/issues/87 is resolved
tx5-go-pion-sys = { git = "https://github.com/guillemcordoba/tx5", branch = "custom-go" }
tx5-go-pion-turn = { git = "https://github.com/guillemcordoba/tx5", branch = "custom-go" }
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use tauri_plugin_holochain::{HolochainPluginConfig, HolochainExt};
use url2::Url2;
use tauri::AppHandle;

const APP_ID: &'static str = "{{app_name}}";
const PRODUCTION_SIGNAL_URL: &'static str = "wss://signal.holo.host";
Expand Down Expand Up @@ -38,7 +39,7 @@ pub fn run() {

// After set up we can be sure our app is installed and up to date, so we can just open it
app.holochain()?
.main_window_builder("main", false, Some(String::from("{{app_name}}")), None)?
.main_window_builder(String::from("main"), false, Some(String::from("{{app_name}}")), None)?
.build()?;

Ok(())
Expand Down Expand Up @@ -68,7 +69,7 @@ async fn setup(handle: AppHandle) -> anyhow::Result<()> {
.holochain()?
.install_app(
String::from(APP_ID),
happ_bundle()
happ_bundle(),
HashMap::new(),
None,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"productName": "{{app_name}}",
"version": "0.0.0",
"identifier": "{{app_name}}",
"identifier": "{{identifier}}",
"build": {
"beforeBuildCommand": "pnpm -F ui build && pnpm happ:build",
"beforeBuildCommand": "npm run build && npm run build:happ",
"devUrl": "http://localhost:1420",
"frontendDist": "../ui/dist"
},
Expand All @@ -16,6 +16,13 @@
},
"bundle": {
"active": true,
"targets": "all"
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/[email protected]",
"icons/icon.icns",
"icons/icon.ico"
]
}
}
12 changes: 8 additions & 4 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ export default defineConfig({
text: "Android",
items: [
{
text: "Setup",
link: "/documentation/android/setup",
text: "Project Setup",
link: "/documentation/android/project-setup",
},
{
text: "Device Setup",
link: "/documentation/android/device-setup",
},
{
text: "Developing",
link: "/documentation/android/developing",
},
{
text: "Publishing",
link: "/documentation/android/publishing",
text: "Building for Production",
link: "/documentation/android/building-for-production",
},
],
},
Expand Down
21 changes: 21 additions & 0 deletions docs/documentation/android/building-for-production.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Building for Production

To build a production version of your app, you just need to run this very simple command:

::: code-group
```bash [npm]
npm run tauri android build
```

```bash [yarn]
yarn tauri android build
```

```bash [pnpm]
pnpm tauri android build
```
:::

Take into account that this will compile your app for all the different Android target architectures: it will take a long time.

That's it! When the command finishes, you can copy the resulting `Android App Bundle` and [publish it on the `Google Play Store`](https://developer.android.com/studio/publish), or the release method of your choosing.
33 changes: 31 additions & 2 deletions docs/documentation/android/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@

While developing a hApp, it's not that useful to just have one agent to test your hApp with. Instead, you usually need a couple of peers to be able to interact with one another.

The scaffolding setup
The scaffolding setup steps in [how to create an executable hApp](../how-to-create-an-executable-happ) and [how to create a holochain runtime](../how-to-create-a-holochain-runtime) create a new script in the top level `package.json` file called `android:network`. This script runs an agent in your local computer and another in an Android device, and enables communication between them.

Since we want to develop for the Android platform, we need to be inside the `androidDev` devShell:

```bash
nix develop .#androidDev
```

Run all the following commands inside this terminal shell.

Before running the `android:network` command, make sure your Android device is connected to your computer via a USB cable and accessible to the tauri tooling by running:

```bash
adb devices
```

You should see your device listed in the output of that command.

**Also make sure that your Android device is in the same wifi network as your computer**. This is necessary for them to see each other and connect in a small holochain network.

> [!NOTE]
> If you haven't setup your Android device yet, head over to [Android device setup](./device-setup) to do so.
We are now ready to run the command:

::: code-group
```bash [npm]
Expand All @@ -18,6 +41,12 @@ pnpm android:network
```
:::

If you want to see logs coming from your rust backend, you can run this command:

```bash
adb devices
adb logcat
```

---

That's it! Have fun while building your hApp. When you are satisfied with what you have and want to release a new production version of your app, go to [Building for Production](./building-for-production).
30 changes: 30 additions & 0 deletions docs/documentation/android/device-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Device Setup

1. In your Android device, enable the [developer options](https://developer.android.com/studio/debug/dev-options).

2. After you have enabled the developer options, [enable USB debbuging](https://developer.android.com/studio/debug/dev-options#Enable-debugging).

3. Connect your Android device to your computer with a USB cable, and confirm in your Android device that you allow USB debugging from this computer.

4. In the root folder of your repository, run:

```bash
nix develop .#androidDev
```

This is a replacement command for the usual `nix develop`, which includes `Android Studio`, and all the necessary tooling that you need for Android development. Every time you want to test or build for the Android platform, you will need to enter the nix devShell this way and then your command from inside of it.

> [!WARNING]
> The first time this is run, it will take some time. This is because nix has to download and build all the necessary Android tooling. After the first time, it will be almost instant.
5. Inside your `androidDev` devShell, run:

```bash
adb devices
```

If all the previous steps were successful, you should see your device in the list of devices.

---

That's it! You can now take a look at [developing for Android](./developing) to know what commands to use when targeting Android.
Loading

0 comments on commit e8e8c3a

Please sign in to comment.