Skip to content

Commit

Permalink
upgrade happyx CLI, update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Dec 14, 2023
1 parent b9bb6eb commit 295a1b6
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 143 deletions.
83 changes: 81 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

## Install

```shell
nimble install happyx-native
```

or via GitHub:
```shell
nimble install https://github.com/HapticX/happyx-native
```
Expand All @@ -22,13 +27,87 @@ nimble install https://github.com/HapticX/happyx-native
- Support for Android;


## Project Initialization

To init project you should use this command:
```shell
hpx-native init --name ProjectName
```

This command will automatically initialize your project.

It also fetches `ANDROID_SDK_ROOT` from environment (need for android compilation).

## Building

To build your project you should move into project folder
```shell
cd ProjectName
```

and just build it!
```shell
hpx-native build
```
This automatically builds your project for your OS as target platform.

### Cross-Compilation

To compile for other OS use
```shell
hpx-native build --target linux
```

Possible values:
| OS | value | aliases |
| :-- | :--: | :--: |
| Windows | windows | win |
| Linux | linux | unix |
| MacOS | macosx | mac, macos |
| Android | android | - |

### Android compilation

You should have:
- Android Studio with SDK and NDK;
- Gradle >= 7.5;
- Nim >= 2.0.0;

By default `hpx-native build --target android` supports all android architectures.
If you want to disable some architectures then use:
```shell
hpx-native build --target android --no-x86_64
```

Possible architectures
| Architecture | Disable Argument |
| :-- | :--: |
| x86 | `--no-x86` |
| x86_64 | `--no-x86_64` |
| armeabi-v7a | `--no-armeabi-v7a` |
| arm64-v8a | `--no-arm64-v8a` |

If you doesn't want to use gradle building then use
```shel
hpx-native build --target android --no-gradle
```
This command will build only `.so` libraries.


### Building Assets

HappyX Native supports "building" assets - all resources from app directory (by default `/assets`) and all subdirectories are "sewn" into executable file.

This way you can distribute your application over the network with only one executable file.

> This option can be disabled via `--no-build-assets`
## Browsers

If you want choose other browser instead of default - use these flags:

| browser | flag |
| :-----: | :-------------: |
| Default browser | uses by default |
| Chrome | `-d:chrome` |
| Chrome | uses by default |
| Edge | `-d:edge` |
| Yandex | `-d:yandex` |
8 changes: 5 additions & 3 deletions happyx_native.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

description = "Macro-oriented web-framework compiles to native written with ♥"
author = "HapticX"
version = "0.1.0"
version = "0.2.0"
license = "MIT"
srcDir = "src"
installExt = @["nim", "gradle", "properties", "ico"]
installExt = @["nim", "gradle", "properties"]
installFiles = @["happyx_native/android/tmpl/gradlew"]
installDirs = @[
"happyx_native/android/",
"happyx_native/assets/",
]
namedBin["happyx_native/hpxnative"] = "hpx-native"

Expand All @@ -17,8 +18,9 @@ namedBin["happyx_native/hpxnative"] = "hpx-native"
requires "nim >= 1.6.14"
# CLI
requires "cligen >= 1.6.14"
requires "illwill#2fe96f5c5a6e216e84554d92090ce3d47460667a"
# HappyX
requires "happyx#head"
# JVM, Android
requires "jnim#head"
# windows executable
requires "rcedit"
9 changes: 3 additions & 6 deletions src/happyx_native.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ when not defined(docgen):
elif defined(edge):
import happyx_native/core/finder/edge
export edge
elif defined(chrome):
else:
import happyx_native/core/finder/chrome
export chrome
else:
import happyx_native/core/finder/[default, chrome]
export default, chrome
else:
import happyx_native/core/finder/[chrome, yandex, edge, default]
export chrome, yandex, edge, default
import happyx_native/core/finder/[chrome, yandex, edge]
export chrome, yandex, edge

when defined(export2android):
import happyx_native/android/core
Expand Down
67 changes: 45 additions & 22 deletions src/happyx_native/app/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ when defined(export2android):
export
macros, mimetypes

when defined(buildAssets):
import mimetypes
export macros, mimetypes

export
happyx,
osproc,
Expand Down Expand Up @@ -151,7 +155,7 @@ macro fetchFiles*(directory: static[string]): untyped =
result.add(ident"_files")


template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,
template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5123,
x: int = 512, y: int = 128, w: int = 720, h: int = 320,
appMode: bool = true, title: string = "",
resizeable: bool = true, establish: bool = true
Expand All @@ -172,14 +176,24 @@ template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,
when defined(export2android):
static:
# Compile main
echo staticExec(
var data = staticExec(
"nim js -d:danger --opt:size " & getScriptDir() / appDirectory / "main.nim"
)
echo data
else:
# Compile main
echo execCmdEx(
"nim js -d:danger --opt:size " & getCurrentDir() / appDirectory / "main.nim"
)
when defined(buildAssets):
static:
# Compile main
var data = staticExec(
"nim js -d:danger --opt:size " & getScriptDir() / appDirectory / "main.nim"
)
else:
# Compile main
var data = execCmdEx(
"nim js -d:danger --opt:size " & getCurrentDir() / appDirectory / "main.nim"
)
echo data.output
assert data.exitCode == 0
when appMode:
var arguments: seq[string] = @[]
arguments.add "--enable-gpu"
Expand All @@ -191,10 +205,8 @@ template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,
spawn openYandex(port, arguments)
elif defined(edge):
spawn openEdge(port, arguments)
elif defined(chrome):
spawn openChrome(port, arguments)
else:
spawn openDefaultBrowserApp(port, arguments)
spawn openChrome(port, arguments)
else:
spawn openDefaultBrowser("http://127.0.0.1:" & $port & "/#/")

Expand Down Expand Up @@ -226,7 +238,7 @@ template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,

get "/":
outHeaders["Cache-Control"] = "no-store"
when defined(export2android):
when defined(export2android) or defined(buildAssets):
var data = getIndexHtml(appDirectory)
else:
let f = openAsync(getCurrentDir() / appDirectory / "index.html")
Expand Down Expand Up @@ -288,13 +300,22 @@ template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,
websocketClient = wsClient

wsClosed:
await handleWebSocketErr()
when not defined(guiApp):
await handleWebSocketErr()
else:
quit(QuitSuccess)

wsMismatchProtocol:
await handleWebSocketErr()
when not defined(guiApp):
await handleWebSocketErr()
else:
quit(QuitSuccess)

wsError:
await handleWebSocketErr()
when not defined(guiApp):
await handleWebSocketErr()
else:
quit(QuitSuccess)

ws "/ws":
let
Expand All @@ -304,14 +325,16 @@ template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,
try:
await callNim(procName, params)
except:
echo "Error from Javascript call to Nim."
echo "Function: " & procName
echo "Parameters: " & $params
echo fmt"ERROR [{getCurrentException().name}]"
echo fmt"Message: " & getCurrentExceptionMsg()
when not defined(guiApp):
echo "Error from Javascript call to Nim."
echo "Function: " & procName
echo "Parameters: " & $params
echo fmt"ERROR [{getCurrentException().name}]"
echo fmt"Message: " & getCurrentExceptionMsg()
discard

get "/{f:path}":
when defined(export2android):
when defined(export2android) or defined(buildAssets):
var
files = fetchFiles(getProjectPath() / appDirectory)
splitted = f.split('.')
Expand All @@ -332,9 +355,9 @@ template nativeAppImpl*(appDirectory: string = "/assets", port: int = 5000,


template nativeApp*(appDirectory: string = "/assets", port: int = 5123,
x: int = 512, y: int = 128, w: int = 720, h: int = 320,
appMode: bool = true, title: string = "",
resizeable: bool = true, establish: bool = true
x: int = 512, y: int = 128, w: int = 720, h: int = 320,
appMode: bool = true, title: string = "",
resizeable: bool = true, establish: bool = true
) {.dirty.} =
when defined(export2android):
proc startAndroidApp*() =
Expand Down
Loading

0 comments on commit 295a1b6

Please sign in to comment.