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 windows support, extend build script #12

Merged
merged 4 commits into from
Aug 15, 2023
Merged
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ jobs:
permissions:
contents: write

macOS:
macos:
uses: ./.github/workflows/macos.yml

windows:
uses: ./.github/workflows/windows.yml
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
path: ${{ env.REPO_NAME }}
- name: Setup ${{ env.REPO_NAME }}
run: |
${{ env.REPO_NAME }}/src/bindings/build.vsh
${{ env.REPO_NAME }}/src/bindings/build.vsh --silent
cp -r ${{ env.REPO_NAME }} ${{ env.MOD_PATH }}
- name: Cache
uses: actions/cache/save@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
path: ${{ env.REPO_NAME }}
- name: Setup ${{ env.REPO_NAME }}
run: |
${{ env.REPO_NAME }}/src/bindings/build.vsh
${{ env.REPO_NAME }}/src/bindings/build.vsh --silent
cp -r ${{ env.REPO_NAME }} ${{ env.MOD_PATH }}
- name: Cache
uses: actions/cache/save@v3
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: CI Windows

on:
workflow_call:

defaults:
run:
shell: bash

env:
REPO_NAME: ${{ github.event.repository.name }}
# Path where the module is installed with `v install <giturl>`
MOD_PATH: ~/.vmodules/webview

jobs:
setup:
runs-on: windows-latest
steps:
- name: Install V
uses: vlang/setup-v@v1.3
with:
check-latest: true
- name: Checkout ${{ env.REPO_NAME }}
uses: actions/checkout@v3
with:
path: ${{ env.REPO_NAME }}
- name: Setup ${{ env.REPO_NAME }}
run: |
${{ env.REPO_NAME }}/src/bindings/build.vsh --silent
cp -r ${{ env.REPO_NAME }} ${{ env.MOD_PATH }}
- name: Cache
uses: actions/cache/save@v3
with:
path: |
vlang
~/.vmodules
key: ${{ runner.os }}-${{ github.sha }}

build:
needs: setup
runs-on: windows-latest
strategy:
matrix:
compiler: [gcc]
steps:
- name: Restore Cache
uses: actions/cache/restore@v3
with:
path: |
vlang
~/.vmodules
key: ${{ runner.os }}-${{ github.sha }}
fail-on-cache-miss: true
- name: Setup V
uses: vlang/setup-v@v1.3
- name: Build
run: |
v -cg -cc ${{ matrix.compiler }} ${{ env.MOD_PATH }}/examples/emoji-picker/main.v
v -cg -cc ${{ matrix.compiler }} ${{ env.MOD_PATH }}/examples/v-js-interop/
v -cg -cc ${{ matrix.compiler }} ${{ env.MOD_PATH }}/examples/project-structure/
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,31 @@ Install the repository as V module

## Usage

> **Note**
> Depending on the installation method, default paths and imports look like:
>
> ```sh
> ~/.vmodules/webview/src/bindings/build.vsh # from source
> ~/.vmodules/ttytm/webview/src/bindings/build.vsh # vpm module
> ```

> ```v
> import webview // from source
> import ttytm.webview // vpm module
> ```

Before the first use, we need to build the webview C library to which we are going to bind.
Before the first use, build the webview C library to which the webview V module will bind.

```sh
# Run the included build script
# For installations from source
~/.vmodules/webview/src/bindings/build.vsh
# For installations as vpm module
~/.vmodules/ttytm/webview/src/bindings/build.vsh
# PowerShell `v run` might require to be prefixed with `v run`, e.g.:
v run $HOME/.vmodules/webview/bindings/build.vsh
```

### Usage Example

> **Note**
> When running and building on Windows, it is recommended to use `gcc` for compilation. E.g.:
>
> ```sh
> v -cc gcc run .
> ```

<br>

```v ignore
import webview
import webview // For installations from source
// import ttytm.webview // For installations as vpm module

struct App {
w &webview.Webview
Expand Down Expand Up @@ -108,4 +109,4 @@ Extended examples can be found in the [examples](https://github.com/ttytm/webvie
An application example that uses this webview binding with SvelteKit for the UI is [emoji-mart-desktop](https://github.com/ttytm/emoji-mart-desktop).

The overview of exported functions is accessible in the repositories [`src/lib.v`](https://github.com/ttytm/webview/blob/master/src/lib.v)
file and on its [vdoc site](https://ttytm.github.io/webview/).
file and on its [vdoc site](https://ttytm.github.io/webview/webview.html).
1 change: 1 addition & 0 deletions examples/project-structure/src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn main() {
settings: struct {true}
}
app.bind()
app.w.set_title('V webview examples')
app.w.set_size(800, 600, .@none)
app.w.navigate('file://${@VMODROOT}/ui/index.html')
app.w.run()
Expand Down
1 change: 1 addition & 0 deletions examples/v-js-interop/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn main() {
w: webview.create(debug: true)
settings: struct {true}
}
app.w.set_title('V webview examples')
app.w.set_size(800, 600, .@none)
// The first string arg names the functions for JS usage. E.g. use JS's `camelCase` convention if you prefer it.
app.w.bind('connect', connect, &app)
Expand Down
1 change: 1 addition & 0 deletions src/bindings/bindings.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module bindings

#flag linux -DWEBVIEW_GTK -lstdc++
#flag darwin -DWEBVIEW_COCOA -framework WebKit -stdlib=libc++ -lstdc++
#flag windows -DWEBVIEW_EDGE -static -ladvapi32 -lole32 -lshell32 -lshlwapi -luser32 -lversion -lstdc++

#flag @VMODROOT/src/bindings/webview.o
#include "@VMODROOT/src/bindings/webview.h"
Expand Down
103 changes: 92 additions & 11 deletions src/bindings/build.vsh
Original file line number Diff line number Diff line change
@@ -1,34 +1,115 @@
#!/usr/bin/env -S v

import cli
import os
import time
import net.http

const (
lib_url = 'https://raw.githubusercontent.com/webview/webview/master'
lib_dir = '${@VMODROOT}/src/bindings'
)

fn main() {
println('Building webview library...')
fn spinner(ch chan bool) {
runes := [`-`, `\\`, `|`, `/`]
mut pos := 0
for {
mut finished := false
ch.try_pop(mut finished)
if finished {
print('\r')
return
}
if pos == runes.len - 1 {
pos = 0
} else {
pos += 1
}
print('\r${runes[pos]}')
flush()
time.sleep(100 * time.millisecond)
}
}

execute('rm ${lib_dir}/webview.*')
[if windows]
fn download_webview2() {
http.download_file('https://www.nuget.org/api/v2/package/Microsoft.Web.WebView2',
'${lib_dir}/webview2.zip') or { panic(err) }
unzip_res := execute('powershell -command Expand-Archive -LiteralPath ${lib_dir}/webview2.zip -DestinationPath ${lib_dir}/webview2')
if unzip_res.exit_code != 0 {
eprintln(unzip_res.output)
exit(1)
}
}

fn download(silent bool) {
println('Downloading...')
dl_spinner := chan bool{cap: 1}
if !silent {
spawn spinner(dl_spinner)
}
http.download_file('${lib_url}/webview.h', '${lib_dir}/webview.h') or { panic(err) }
http.download_file('${lib_url}/webview.cc', '${lib_dir}/webview.cc') or { panic(err) }
$if windows {
download_webview2()
}
dl_spinner <- true
}

fn build(silent bool) {
mut cmd := 'g++ -c ${lib_dir}/webview.cc -o ${lib_dir}/webview.o'
cmd += $if darwin && amd64 { ' -std=c++11' } $else { ' -std=c++17' }
$if linux {
cmd += ' $(pkg-config --cflags gtk+-3.0 webkit2gtk-4.0)'
} $else $if windows {
defer {
// Cleanup
rm('${lib_dir}/webview2.zip') or {}
rmdir_all('${lib_dir}/webview2') or {}
}
cmd += ' -I${lib_dir}/webview2/build/native/include'
}
$if darwin && amd64 {
cmd += ' -std=c++11'
} $else {
cmd += ' -std=c++17'
println('Building...')
build_spinner := chan bool{cap: 1}
if !silent {
spawn spinner(build_spinner)
}
comp_res := execute(cmd)
if comp_res.exit_code != 0 {
eprintln(comp_res.output)
build_res := execute(cmd)
build_spinner <- true
if build_res.exit_code != 0 {
eprintln(build_res.output)
exit(1)
}
println('\rSuccessfully built webview library.')
}

println('Finished.')
fn run(cmd cli.Command) ! {
// Remove old library files
execute('rm ${lib_dir}/webview.*')
silent := cmd.flags[0].get_bool()!
download(silent)
time.sleep(100 * time.millisecond)
build(silent)
}

mut cmd := cli.Command{
name: 'build.vsh'
posix_mode: true
required_args: 0
pre_execute: fn (cmd cli.Command) ! {
if cmd.args.len > cmd.required_args {
eprintln('Unknown commands ${cmd.args}.\n')
cmd.execute_help()
exit(0)
}
}
execute: run
flags: [
cli.Flag{
flag: .bool
name: 'silent'
global: true
},
]
}
cmd.parse(os.args)