Skip to content

Commit

Permalink
Merge pull request #3 from jawahar273/web-asm
Browse files Browse the repository at this point in the history
Demo with web assembly
  • Loading branch information
jawahar273 authored Feb 9, 2024
2 parents 26556cf + 24a72c5 commit b59f537
Show file tree
Hide file tree
Showing 32 changed files with 978 additions and 1,408 deletions.
File renamed without changes.
74 changes: 74 additions & 0 deletions .github/workflows/gh.page.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: deploy to gh page
on:
push:
branches:
- web-asm
pull_request:

permissions:
contents: read
pages: write
id-token: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
defaults:
run:
working-directory: ./server/web
steps:
- name: Checkout Repository
uses: actions/checkout@v4
# - name: Change folder location
# id: web-folder
# run: |
# echo "path=./server/web" >> $GITHUB_OUTPUT
# exit 0
- name: Setup Node ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
cache-dependency-path: ./server/web
- name: Cache dependencies ⚡
id: cache_dependencies
uses: actions/cache@v3
with:
path: node_modules
key: node-modules-${{ hashFiles('package-lock.json') }}
- name: Setup Pages
uses: actions/configure-pages@v4
with:
# Automatically inject basePath in your Next.js configuration file and disable
# server side image optimization (https://nextjs.org/docs/api-reference/next/image#unoptimized).
#
# You may remove this line if you want to manage the configuration yourself.
static_site_generator: next
- name: Install dependencies
run: npm ci
- name: Update web assembly
run: cd ../.. && make ci-install && make js-asm
- name: Build with Next.js
run: npx next build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: "./server/web/out"

deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./server/web
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
workspace = { members = ["asm"] }
[package]
name = "timex"
version = "0.3.2-beta.1"
edition = "2021"
resolver = "2"
authors = ["Jawahar S <[email protected]>"]
license = "MIT"
license = "mit"
description = "Generate date times as event similar to Microsoft calender or Google calender based on the given parameters."
readme = "README.md"
homepage = "https://github.com/jawahar273/timex"
Expand All @@ -25,8 +26,6 @@ env_logger = "0.10.1"
log = "0.4.20"
serde = { version = "1.0.193", features = ["derive"] }
thiserror = "1.0.50"
wasm-bindgen = "0.2.89"
serde-wasm-bindgen = "0.6.3"
serde_json = "1.0.108"

# reduce web assembly size https://rustwasm.github.io/twiggy/
Expand Down
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,25 @@ pre-release:
relase:
cargo release commit --execute
cargo release tag --execute --no-confirm

ASM_PATH="./asm"
WEB_PATH="./server/web"
WEB_ASM_PATH="${WEB_PATH}/src/asm"

COMMIT_ID=$(shell git rev-parse --short HEAD)
ASM_FILE_NAME="asm-${COMMIT_ID}"

js-asm:
echo "version: ${COMMIT_ID}" && \
cd ${ASM_PATH} && \
rm -rf "${ASM_PATH}/pkg" && \
wasm-pack build --target web --out-name ${ASM_FILE_NAME} && \
cd .. && \
rm -rf "${WEB_ASM_PATH}/*" && \
cp -a ${ASM_PATH}/pkg/* ${WEB_ASM_PATH} && \
echo "export const VERSION = \"${COMMIT_ID}\"" | cat > "${WEB_ASM_PATH}/version.ts"


ci-install:
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

20 changes: 20 additions & 0 deletions asm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
# Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
debug/
wasm32-unknown-unknown
wasm32-wasi
release/
pkg
.rust_info.json
15 changes: 15 additions & 0 deletions asm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "asm"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["rlib", "cdylib"]

[dependencies]
chrono = "0.4.31"
wasm-bindgen = "0.2.90"
timex = { path = "../"}
serde-wasm-bindgen = "0.6.3"
62 changes: 62 additions & 0 deletions asm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use chrono::{DateTime, Utc};
use timex::{
model::ScheduleDetails,
schedule_date_times,
};
use wasm_bindgen::{prelude::wasm_bindgen, *};


#[wasm_bindgen]
pub fn show_detail_in_display(_detail: JsValue) -> std::result::Result<JsValue, JsValue> {
let detail: ScheduleDetails = match serde_wasm_bindgen::from_value(_detail) {
Err(e) => return Err(e.to_string().into()),
std::result::Result::Ok(v) => v,
};

return std::result::Result::Ok(format!("{}", detail).into());
}

#[wasm_bindgen]
pub fn find_schedule_date_time(
_detail: JsValue,
previous_scheduled_date: String,
start_range_date: String,
end_range_date: String,
) -> std::result::Result<Vec<JsValue>, JsValue> {
let mut _previous_scheduled_date: DateTime<Utc>;
let detail: ScheduleDetails = serde_wasm_bindgen::from_value(_detail)?;

match DateTime::parse_from_rfc3339(&previous_scheduled_date) {
std::result::Result::Ok(v) => _previous_scheduled_date = v.with_timezone(&Utc),
Err(e) => return Err(e.to_string().into()),
};

let mut _start_range_date: DateTime<Utc>;

match DateTime::parse_from_rfc3339(&start_range_date) {
std::result::Result::Ok(v) => _start_range_date = v.with_timezone(&Utc),
Err(e) => return Err(e.to_string().into()),
};

let mut _end_range_date: DateTime<Utc>;

match DateTime::parse_from_rfc3339(&end_range_date) {
std::result::Result::Ok(v) => _end_range_date = v.with_timezone(&Utc),
Err(e) => return Err(e.to_string().into()),
};

let t = schedule_date_times(
&detail,
_previous_scheduled_date,
_start_range_date,
_end_range_date,
);
match t {
std::result::Result::Ok(v) => std::result::Result::Ok(
v.into_iter()
.map(|x| x.to_rfc3339().into())
.collect::<Vec<_>>(),
),
Err(e) => Err(e.to_string().into()),
}
}
10 changes: 5 additions & 5 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions server/Cargo.toml

This file was deleted.

19 changes: 11 additions & 8 deletions server/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
CORE_PATH="./core"
CORE_PATH="."
ASM_PATH="../asm"
WEB_PATH="./server/web"



install:
Expand Down Expand Up @@ -32,10 +35,10 @@ go-grpc:
--proto_path=. \
./proto/api/v1/*.proto

# asm:
# rm -rf ./web/pkg
# cd ${CORE_PATH} && \
# wasm-pack build --target web && \
# cd .. && \
# mv ${CORE_PATH}/pkg ./server/web/


asm:
cd .. && \
cd asm && \
CARGO_TARGET_DIR=./ cargo build --target wasm32-wasi --release && \
cd .. && \
cp ./asm/wasm32-wasi/release/asm.wasm ./server/
1 change: 1 addition & 0 deletions server/web/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/src/asm/*
6 changes: 5 additions & 1 deletion server/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const nextConfig = {
experimental: {
typedRoutes: true,
},
output: 'standalone',
output: 'export',
assetPrefix: process.env.NEXT_PUBLIC_BASE_PATH || '/timex',
basePath: process.env.NEXT_PUBLIC_BASE_PATH || '/timex',
// + assetPrefix = process.env.NEXT_PUBLIC_BASE_PATH || ''
};


module.exports = nextConfig;
8 changes: 8 additions & 0 deletions server/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion server/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"prettier": "prettier -w ."
},
"dependencies": {
"asm": "./src/asm",
"@hookform/resolvers": "^3.3.3",
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-dialog": "^1.0.5",
Expand Down Expand Up @@ -55,4 +56,4 @@
"tailwindcss": "^3.3.0",
"typescript": "^5.3.3"
}
}
}
2 changes: 0 additions & 2 deletions server/web/public/pkg/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions server/web/public/pkg/package.json

This file was deleted.

Loading

0 comments on commit b59f537

Please sign in to comment.