Skip to content

Commit

Permalink
Feat: Splitting into module (#2)
Browse files Browse the repository at this point in the history
* Started Split

* Fixed up packages

* finishd package publishing

* Cleaned up last remaining problems

* Moved everything back to root directory so decky cli can build it

* fixed last remaining errors
  • Loading branch information
CEbbinghaus authored Oct 26, 2023
1 parent 9faf1c1 commit 2113e05
Show file tree
Hide file tree
Showing 29 changed files with 3,576 additions and 401 deletions.
38 changes: 34 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
on:
pull_request:
push:
branches:
- master
pull_request_target:
branches: ['*']
branches: master

jobs:
build:
Expand Down Expand Up @@ -47,3 +45,35 @@ jobs:
with:
name: "MicroSDeck"
path: release/MicroSDeck/*

deploy:
if: github.ref == 'refs/heads/master'
needs: build
name: Deploy Package
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Checkout
if: ${{ !env.ACT }}
uses: actions/checkout@v4

- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: https://npm.pkg.github.com/

- uses: pnpm/action-setup@v2
with:
version: 8.5.1
- run: |
cd lib
pnpm install
pnpm build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}


1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@cebbinghaus:registry=https://npm.pkg.github.com
2 changes: 1 addition & 1 deletion backend/Cargo.lock

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

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "backend"
version = "0.9.4"
version = "0.9.5"
edition = "2021"
license = "GPL-2.0"
authors = ["Christopher-Robin Ebbinghaus <git@cebbinghaus.com>"]
Expand Down
65 changes: 31 additions & 34 deletions backend/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ use crate::{
ds::Store,
dto::{Game, MicroSDCard},
err::Error,
sdcard::{get_card_cid, is_card_inserted},
sdcard::{get_card_cid, is_card_inserted}, env::PACKAGE_VERSION,
};
use actix_web::{delete, get, post, web, HttpResponse, Responder, Result};
use serde::Deserialize;
use std::{ops::Deref, sync::Arc};
use tokio::sync::broadcast::Sender;

pub(crate) fn config(cfg: &mut web::ServiceConfig) {
cfg.service(listen)
cfg.service(health)
.service(version)
.service(listen)
.service(save)
.service(get_current_card)
.service(get_current_card_id)
.service(get_current_card_and_games)
.service(get_games_on_current_card)
.service(create_card)
.service(delete_card)
.service(list_cards)
Expand All @@ -22,14 +26,15 @@ pub(crate) fn config(cfg: &mut web::ServiceConfig) {
.service(delete_game)
.service(list_games)
.service(get_game)
.service(list_games_on_card)
.service(list_games_for_card)
.service(list_cards_for_game)
.service(list_cards_with_games)
.service(get_cards_for_game)
.service(get_games_on_current_card)
.service(set_name_for_card)
.service(create_link)
.service(health)
.service(save);
.service(create_link);
}

#[get("/version")]
pub(crate) async fn version() -> impl Responder {
HttpResponse::Ok().body(PACKAGE_VERSION)
}

#[get("/health")]
Expand All @@ -44,13 +49,13 @@ pub(crate) async fn listen(sender: web::Data<Sender<()>>) -> Result<impl Respond
}


#[get("/ListCardsWithGames")]
#[get("/list")]
pub(crate) async fn list_cards_with_games(datastore: web::Data<Arc<Store>>) -> impl Responder {
web::Json(datastore.list_cards_with_games())
}

#[get("/ListGamesOnCard/{card_id}")]
pub(crate) async fn list_games_on_card(
#[get("/list/games/{card_id}")]
pub(crate) async fn list_games_for_card(
card_id: web::Path<String>,
datastore: web::Data<Arc<Store>>,
) -> Result<impl Responder> {
Expand All @@ -60,35 +65,17 @@ pub(crate) async fn list_games_on_card(
}
}

#[get("/GetCardsForGame/{uid}")]
pub(crate) async fn get_cards_for_game(
uid: web::Path<String>,
#[get("/list/cards/{game_id}")]
pub(crate) async fn list_cards_for_game(
game_id: web::Path<String>,
datastore: web::Data<Arc<Store>>,
) -> Result<impl Responder> {
match datastore.get_cards_for_game(&uid) {
match datastore.get_cards_for_game(&game_id) {
Ok(value) => Ok(web::Json(value)),
Err(err) => Err(actix_web::Error::from(err)),
}
}

#[derive(Deserialize)]
pub struct SetNameForCardBody {
id: String,
name: String,
}

#[post("/SetNameForCard")]
pub(crate) async fn set_name_for_card(
body: web::Json<SetNameForCardBody>,
datastore: web::Data<Arc<Store>>,
) -> Result<impl Responder> {
datastore.update_card(&body.id, |card| {
card.name = body.name.clone();
Ok(())
})?;
Ok(HttpResponse::Ok())
}

#[get("/current")]
pub(crate) async fn get_current_card_and_games(datastore: web::Data<Arc<Store>>) -> Result<impl Responder> {
if !is_card_inserted() {
Expand Down Expand Up @@ -242,6 +229,16 @@ pub(crate) async fn create_link(
Ok(HttpResponse::Ok())
}

#[post("/unlink")]
pub(crate) async fn delete_link(
body: web::Json<LinkBody>,
datastore: web::Data<Arc<Store>>,
) -> Result<impl Responder> {
datastore.unlink(&body.game_id, &body.card_id)?;

Ok(HttpResponse::Ok())
}

#[post("/save")]
pub(crate) async fn save(datastore: web::Data<Arc<Store>>) -> Result<impl Responder> {
datastore.write_to_file()?;
Expand Down
40 changes: 20 additions & 20 deletions backend/src/ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ impl StoreData {
Ok(())
}

pub fn unlink(&mut self, a_id: &str, b_id: &str) -> Result<(), Error> {
let game_key = self.node_ids.get(a_id);
let card_key = self.node_ids.get(b_id);
let (game_key, card_key) = game_key
.zip(card_key)
.ok_or_else(|| Error::from_str("Either Game or Card could not be found"))?;

self.nodes[*game_key].links.remove(card_key);
self.nodes[*card_key].links.remove(game_key);

Ok(())
}

pub fn remove_item(&mut self, id: &str) -> Result<(), Error> {
let element_key = self
.node_ids
Expand All @@ -119,19 +132,6 @@ impl StoreData {
Ok(())
}

pub fn remove_game_from_card(&mut self, game_id: &str, card_id: &str) -> Result<(), Error> {
let game_key = self.node_ids.get(game_id);
let card_key = self.node_ids.get(card_id);
let (game_key, card_key) = game_key
.zip(card_key)
.ok_or_else(|| Error::from_str("Either Game or Card could not be found"))?;

self.nodes[*game_key].links.remove(card_key);
self.nodes[*card_key].links.remove(game_key);

Ok(())
}

pub fn contains_element(&self, card_id: &str) -> bool {
self.node_ids.contains_key(card_id)
}
Expand Down Expand Up @@ -354,17 +354,17 @@ impl Store {
Ok(())
}

pub fn remove_element(&self, game_id: &str) -> Result<(), Error> {
self.data.write().unwrap().remove_item(game_id)?;
pub fn unlink(&self, a_id: &str, b_id: &str) -> Result<(), Error> {
self.data
.write()
.unwrap()
.unlink(a_id, b_id)?;
self.try_write_to_file();
Ok(())
}

pub fn remove_game_from_card(&self, game_id: &str, card_id: &str) -> Result<(), Error> {
self.data
.write()
.unwrap()
.remove_game_from_card(game_id, card_id)?;
pub fn remove_element(&self, game_id: &str) -> Result<(), Error> {
self.data.write().unwrap().remove_item(game_id)?;
self.try_write_to_file();
Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions backend/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::path::Path;
use log::warn;

pub const PORT: u16 = 12412; // TODO replace with something unique

pub const PACKAGE_NAME: &'static str = env!("CARGO_PKG_NAME");
pub const PACKAGE_VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub const PACKAGE_AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");

const TEMPDIR: &'static str = "/tmp/MicroSDeck";

pub fn get_data_dir() -> String {
Expand Down
8 changes: 1 addition & 7 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod steam;

use crate::api::config;
use crate::ds::Store;
use crate::env::get_file_path_and_create_directory;
use crate::env::*;
use crate::log::Logger;
use crate::watch::start_watch;
use ::log::{info, trace, error};
Expand All @@ -28,12 +28,6 @@ use std::sync::Arc;

static LOGGER: Lazy<Logger> = Lazy::new(|| Logger::new().expect("Logger to be created"));

const PORT: u16 = 12412; // TODO replace with something unique

const PACKAGE_NAME: &'static str = env!("CARGO_PKG_NAME");
const PACKAGE_VERSION: &'static str = env!("CARGO_PKG_VERSION");
const PACKAGE_AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");

pub fn init() -> Result<(), ::log::SetLoggerError> {
::log::set_logger(&*LOGGER).map(|()| ::log::set_max_level(LevelFilter::Trace))
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn read_msd_directory(datastore: &Store) -> Result<(), Error> {
.iter()
.filter(|v| !games.iter().any(|g| g.appid == v.uid))
{
datastore.remove_game_from_card(&deleted_game.uid, &cid)?
datastore.unlink(&deleted_game.uid, &cid)?
}

for game in games.iter() {
Expand Down
9 changes: 4 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ mkdir -p build

if [[ "$*" != *"--skip-backend"* ]]; then
echo "Building backend..."
cd ./backend && ./build.sh && cd ..
cd backend && ./build.sh && cd ..
fi

if [[ "$*" != *"--skip-frontend"* ]]; then
echo "Building frontend..."
# pnpm does not like local dependencies, and doesn't install them unless forced to install everything
rm -rf ./node_modules && pnpm install && pnpm run build
cd src && pnpm install && pnpm run build && cd ..
fi

echo "Collecting outputs into /build folder"
cp -r dist build/
cp -r src/dist build/
cp -r bin build/
cp main.py build/
cp plugin.json build/
cp README.md build/
cp package.json build/
cp src/package.json build/

if [[ "$*" != *"--skip-copy"* ]]; then
echo "Copying build folder to local plugin directory"
Expand Down
13 changes: 0 additions & 13 deletions index.html

This file was deleted.

33 changes: 33 additions & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@cebbinghaus/microsdeck",
"version": "0.9.5",
"description": "",
"keywords": [],
"author": "CEbbinghaus",
"license": "GPLv2",

"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],

"scripts": {
"build": "tsc"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"repository": {
"type": "git",
"url": "https://github.com/CEbbinghaus/MicroSDeck.git",
"directory": "src/lib"
},
"devDependencies": {
"@types/react": "16.14.0",
"lipe": "^0.3.3"
},
"dependencies": {
"typescript": "^4.9.5"
}
}
Loading

0 comments on commit 2113e05

Please sign in to comment.