# @rnx-kit/build

[![Build](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml/badge.svg)](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml)
[![npm version](https://img.shields.io/npm/v/@rnx-kit/build)](https://www.npmjs.com/package/@rnx-kit/build)

🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧

### This tool is EXPERIMENTAL - USE WITH CAUTION

🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧🚧

An experimental tool for building your apps in the cloud.

## Requirements

- [Node.js](https://nodejs.org/en/download/) LTS 14.18 or greater

| Feature                      | Android | iOS | macOS | Windows |
| :--------------------------- | :-----: | :-: | :---: | :-----: |
| Build with Azure DevOps      |   🚧    | 🚧  |  🚧   |   🚧    |
| Build with GitHub Actions    |    ✓    |  ✓  |   ✓   |    ✓    |
| Launch in device (local)     |   ✓¹    | ✓²  |   ✓   |    ✓    |
| Launch in emulator/simulator |   ✓¹    | ✓²  |   -   |    -    |
| Launch from QR code          |    ✓    | 🚧  |   -   |    -    |

1. Requires [Android Studio](https://developer.android.com/studio)
2. Requires [Xcode](https://developer.apple.com/xcode/)

## Usage

```sh
npm run rnx-build --platform <platform>
```

### `-p`, `--platform`

Target platform to build for.

Supported platforms are `android`, `ios`, `macos`, `windows`.

### `--deploy` (optional)

Where builds should be deployed from. For how to configure your app distribution
service, please see [Distribution](#distribution).

Supported deployment methods are `remote-first`, `local-only`.

Defaults to `remote-first`.

### `--device-type` (optional)

Target device type. This is currently only implemented for iOS.

Supported device types are `device`, `emulator`, `simulator`.

Specifying `emulator`/`simulator` implies `--deploy local-only`.

Defaults to `simulator`.

### `--package-manager` (optional)

Binary name of the package manager used in the current repo.

Defaults to `npm`, `pnpm`, or `yarn` if detected.

### `--project-root` (optional)

Path to the root of the project.

Defaults to current working directory.

### `--scheme` (optional)

The workspace scheme to build (iOS and macOS only).

Defaults to `ReactTestApp`.

## Distribution

### Android: Local Deployment

In order to launch the build artifact on device, you need to install Android
Studio. Once installed, go into **Preferences** ❭ **Appearance & Behavior** ❭
**System Settings** ❭ **Android SDK**, and install **Android SDK Build-Tools**
and **Android SDK Platform-Tools**.

If you want to run apps on the Android Emulator, follow the instructions here:
[Run apps on the Android Emulator](https://developer.android.com/studio/run/emulator).

### iOS: Install Signing Certificate and Provisioning Profile

In order to launch the build artifact on device, you need to install Apple
signing certificate and provisioning profile on your host of choice.

For GitHub, please follow the steps to create the four secrets here:
[Creating secrets for your certificate and provisioning profile](https://docs.github.com/en/actions/deployment/deploying-xcode-applications/installing-an-apple-certificate-on-macos-runners-for-xcode-development#creating-secrets-for-your-certificate-and-provisioning-profile)

### Plugins

`@rnx-kit/build`'s remote app distribution is enabled by plugins. A plugin is
configured by the `rnx-kit.build.distribution` key in `package.json`. It takes a
tuplet, the module name and an options object. For instance, the Firebase plugin
may be configured like below:

```json
{
  ...
  "rnx-kit": {
    "build": {
      "distribution": [
        "@rnx-kit/build-plugin-firebase",
        {
          "appId": {
            "android": "1:1234567890:android:0a1b2c3d4e5f67890",
            "ios": "1:1234567890:android:0a1b2c3d4e5f67890"
          }
        }
      ]
    },
  }
}
```

The options object is passed to the plugin on load:

```js
import type { DistributionPlugin, Platform } from "@rnx-kit/build";

type Config = {
  appId: string | Partial<Record<Platform, string>>;
};

module.exports = (config: Partial<Config>): DistributionPlugin => {
  ...
};
```

## Assumptions

### Folder Structure

Workflows currently assume that your project has a folder structure similar to
the one generated by `react-native init`, e.g.:

    app
    ├── android
    │   ├── gradlew
    │   └── gradlew.bat
    ├── ios
    │   └── Podfile
    ├── macos
    │   └── Podfile
    ├── windows
    │   └── App.sln
    ├── package.json
    └── src

## Contributors' Notes

### TODO

- [x] Figure out how to push/restore local Git state
- [x] Figure out how to trigger workflow
- [x] Figure out how to fetch job state
- [x] Figure out how to push artifacts
  - [x] Android
  - [x] iOS
  - [x] macOS
  - [x] Windows
- [x] Figure out how to download artifacts
- [x] Figure out how to install artifacts
  - [x] Android emulator
  - [x] Android device
  - [x] iOS simulator
  - [x] iOS device (need to figure out how to sign on CI)
    - https://docs.github.com/en/actions/deployment/deploying-xcode-applications/installing-an-apple-certificate-on-macos-runners-for-xcode-development
  - [x] macOS
  - [x] Windows
- [x] Miscellaneous cleanup
  - [x] Implement proper CLI
  - [x] Download build artifacts to platform specific folders
  - [x] iOS: Detect workspace to build
- [x] Cancel build job when user ctrl+c in the terminal
- [x] Add `init` or `install` command to copy the correct workflow file to
      user's repo
- [x] Replace yauzl with something more native
- [x] Windows: Install currently only works on Windows 11, we need to support 10
- [x] Build artifacts are currently hard-coded to look for ReactTestApp
- [ ] Verify downloaded build artifacts using checksum
- [ ] Figure out appropriate storage for auth tokens
- [ ] Figure out how to install artifacts with QR code
  - [x] Android
  - [ ] iOS
- [ ] Figure out caching
- [ ] Figure out how to skip native build when cached
- [x] Implement a plugin system for distribution
- [ ] Figure out how a plugin can extend a workflow
- [ ] Implement a plugin system for remotes (i.e. ADO, GitHub)
