Skip to content

Commit

Permalink
Platform-specific improvements (#127)
Browse files Browse the repository at this point in the history
* Use native-windows-gui crate to manage tray icon
Adds log file support on Windows

* Log file location now works like other paths

* Removed context builder

* Context --> App

* Removed mount URLs from App

* Switch to a nicer crate for forking daemon

* Handle errors from notify_ready

* Add application icon to all Windows Polaris executables, not just those created by the release script

* Add build.rs to release tarball

* Create PID file parent directory if necessary
  • Loading branch information
agersant authored Dec 31, 2020
1 parent 7edcc38 commit 4ad8d92
Show file tree
Hide file tree
Showing 20 changed files with 377 additions and 661 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ TestConfig.toml

# Runtime artifacts
*.sqlite
polaris.log
/thumbnails

# Release process artifacts (usually runs on CI)
Expand Down
70 changes: 52 additions & 18 deletions Cargo.lock

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

12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ name = "polaris"
version = "0.0.0"
authors = ["Antoine Gersant <[email protected]>"]
edition = "2018"
build = "build.rs"

[features]
default = ["bundle-sqlite"]
bundle-sqlite = ["libsqlite3-sys"]
ui = ["uuid", "winapi"]
ui = ["native-windows-gui", "native-windows-derive"]

[dependencies]
actix-files = { version = "0.4" }
Expand Down Expand Up @@ -59,12 +60,15 @@ default_features = false
features = ["bmp", "gif", "jpeg", "png"]

[target.'cfg(windows)'.dependencies]
uuid = { version="0.8", optional = true }
winapi = { version = "0.3.3", features = ["winuser", "libloaderapi", "shellapi", "errhandlingapi"], optional = true }
native-windows-gui = {version = "1.0.7", default-features = false, features = ["cursor", "image-decoder", "message-window", "menu", "tray-notification"], optional = true }
native-windows-derive = {version = "1.0.2", optional = true }

[target.'cfg(unix)'.dependencies]
daemonize = "0.4.1"
sd-notify = "0.1.0"
unix-daemonize = "0.1.2"

[target.'cfg(windows)'.build-dependencies]
winres = "0.1"

[dev-dependencies]
headers = "0.3"
Expand Down
9 changes: 9 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[cfg(windows)]
fn main() {
let mut res = winres::WindowsResource::new();
res.set_icon("./res/windows/application/icon_polaris_512.ico");
res.compile().unwrap();
}

#[cfg(unix)]
fn main() {}
2 changes: 1 addition & 1 deletion res/unix/release_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ echo "Creating output directory"
mkdir -p release/tmp/polaris

echo "Copying package files"
cp -r web docs/swagger src migrations test-data Cargo.toml Cargo.lock rust-toolchain res/unix/Makefile release/tmp/polaris
cp -r web docs/swagger src migrations test-data build.rs Cargo.toml Cargo.lock rust-toolchain res/unix/Makefile release/tmp/polaris

echo "Creating tarball"
tar -zc -C release/tmp -f release/polaris.tar.gz polaris
Expand Down
15 changes: 0 additions & 15 deletions res/windows/application/application.manifest

This file was deleted.

7 changes: 0 additions & 7 deletions res/windows/application/application.rc

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed res/windows/application/icon_polaris_outline_64.ico
Binary file not shown.
8 changes: 2 additions & 6 deletions res/windows/release_script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ if (!(Test-Path env:POLARIS_VERSION)) {
throw "POLARIS_VERSION environment variable is not defined"
}

"Compiling resource file"
$rc_exe = Join-Path "C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64" RC.exe
& $rc_exe /fo res\windows\application\application.res res\windows\application\application.rc

""
"Compiling executable"
# TODO: Uncomment the following once Polaris can do variable expansion of %LOCALAPPDATA%
Expand All @@ -17,8 +13,8 @@ $rc_exe = Join-Path "C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64
# $env:POLARIS_LOG_DIR = "$INSTALL_DIR"
# $env:POLARIS_CACHE_DIR = "$INSTALL_DIR"
# $env:POLARIS_PID_DIR = "$INSTALL_DIR"
cargo rustc --release --features "ui" -- -C link-args="/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup res\windows\application\application.res"
cargo rustc --release -- -o ".\target\release\polaris-cli.exe" -C link-args="res\windows\application\application.res"
cargo rustc --release --features "ui" -- -o ".\target\release\polaris.exe"
cargo rustc --release -- -o ".\target\release\polaris-cli.exe"

""
"Creating output directory"
Expand Down
74 changes: 74 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::fs;
use std::path::PathBuf;

use crate::db::DB;
use crate::paths::Paths;

pub mod config;
pub mod ddns;
pub mod index;
Expand All @@ -10,3 +16,71 @@ pub mod vfs;

#[cfg(test)]
pub mod test;

#[derive(Clone)]
pub struct App {
pub port: u16,
pub auth_secret: settings::AuthSecret,
pub web_dir_path: PathBuf,
pub swagger_dir_path: PathBuf,
pub db: DB,
pub index: index::Index,
pub config_manager: config::Manager,
pub ddns_manager: ddns::Manager,
pub lastfm_manager: lastfm::Manager,
pub playlist_manager: playlist::Manager,
pub settings_manager: settings::Manager,
pub thumbnail_manager: thumbnail::Manager,
pub user_manager: user::Manager,
pub vfs_manager: vfs::Manager,
}

impl App {
pub fn new(port: u16, paths: Paths) -> anyhow::Result<Self> {
let db = DB::new(&paths.db_file_path)?;
fs::create_dir_all(&paths.web_dir_path)?;
fs::create_dir_all(&paths.swagger_dir_path)?;

let thumbnails_dir_path = paths.cache_dir_path.join("thumbnails");

let vfs_manager = vfs::Manager::new(db.clone());
let settings_manager = settings::Manager::new(db.clone());
let auth_secret = settings_manager.get_auth_secret()?;
let ddns_manager = ddns::Manager::new(db.clone());
let user_manager = user::Manager::new(db.clone(), auth_secret);
let index = index::Index::new(db.clone(), vfs_manager.clone(), settings_manager.clone());
let config_manager = config::Manager::new(
settings_manager.clone(),
user_manager.clone(),
vfs_manager.clone(),
ddns_manager.clone(),
);
let playlist_manager = playlist::Manager::new(db.clone(), vfs_manager.clone());
let thumbnail_manager = thumbnail::Manager::new(thumbnails_dir_path);
let lastfm_manager = lastfm::Manager::new(index.clone(), user_manager.clone());

if let Some(config_path) = paths.config_file_path {
let config = config::Config::from_path(&config_path)?;
config_manager.apply(&config)?;
}

let auth_secret = settings_manager.get_auth_secret()?;

Ok(Self {
port,
auth_secret,
web_dir_path: paths.web_dir_path,
swagger_dir_path: paths.swagger_dir_path,
index,
config_manager,
ddns_manager,
lastfm_manager,
playlist_manager,
settings_manager,
thumbnail_manager,
user_manager,
vfs_manager,
db,
})
}
}
4 changes: 0 additions & 4 deletions src/app/thumbnail/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ impl Manager {
}
}

pub fn get_directory(&self) -> &Path {
&self.thumbnails_dir_path
}

pub fn get_thumbnail(&self, image_path: &Path, thumbnailoptions: &Options) -> Result<PathBuf> {
match self.retrieve_thumbnail(image_path, thumbnailoptions) {
Some(path) => Ok(path),
Expand Down
13 changes: 3 additions & 10 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use diesel::r2d2::{self, ConnectionManager, PooledConnection};
use diesel::sqlite::SqliteConnection;
use diesel::RunQueryDsl;
use diesel_migrations;
use std::path::{Path, PathBuf};
use std::path::Path;

mod schema;

Expand All @@ -16,7 +16,6 @@ embed_migrations!("migrations");
#[derive(Clone)]
pub struct DB {
pool: r2d2::Pool<ConnectionManager<SqliteConnection>>,
location: PathBuf,
}

#[derive(Debug)]
Expand All @@ -42,22 +41,16 @@ impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error>

impl DB {
pub fn new(path: &Path) -> Result<DB> {
std::fs::create_dir_all(&path.parent().unwrap())?;
let manager = ConnectionManager::<SqliteConnection>::new(path.to_string_lossy());
let pool = diesel::r2d2::Pool::builder()
.connection_customizer(Box::new(ConnectionCustomizer {}))
.build(manager)?;
let db = DB {
pool: pool,
location: path.to_owned(),
};
let db = DB { pool: pool };
db.migrate_up()?;
Ok(db)
}

pub fn location(&self) -> &Path {
&self.location
}

pub fn connect(&self) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>> {
self.pool.get().map_err(Error::new)
}
Expand Down
Loading

0 comments on commit 4ad8d92

Please sign in to comment.