Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kerwin612 committed May 18, 2023
0 parents commit af391a6
Show file tree
Hide file tree
Showing 38 changed files with 4,513 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Kerwin Bryant

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# PortableRunner

**Mount the portable directory as a consistent user directory. Then, based on this user directory, run your program.**
>***Note, run with administrator privileges.***
**help**
```cmd
PortableRunner [Target Path] [Link Path] [Home name]
args:
Target Path: Specifies the physical drive and path that you want to assign to a virtual drive.
Link Path: Specifies the virtual drive and path to which you want to assign a path.
Home name: The subdirectory name of the <Link Path> directory, Will be specified as the value of %HOME%, which defaults to [.home].
```

**example**

* No parameters, double click to open
![1](./images/1.png)

* Input parameters
![2](./images/2.png)

* Enter to enter the main window
![3](./images/3.png)

* Enter the command and press Enter to execute it. The command running environment is based on:
```cmd
HOME=X:\work\.home
HOMEDRIVE=X:
HOMEPATH=X:\work\.home
LOCALAPPDATA=X:\work\.home\AppData\Local
PORTABLE_RUNNER_ENV_LINK_PATH=X:\work
PORTABLE_RUNNER_ENV_TARGET_PATH=E:\test
TEMP=X:\work\.home\AppData\Local\Temp
TMP=X:\work\.home\AppData\Local\Temp
USERPROFILE=X:\work\.home
...
```

### .profile
`%HOME%/[.profile.cmd|.profile.bat]`
> **One of these two files is automatically executed when the program starts (if the file exists)**
### .pd.json
`%HOME%/.pd.json`
> **Configuration file, currently supports shortcuts configuration**
**example**
```json
{
"shortcuts": {
"open-url": "msedge",
"open-file": "explorer",
"open-home": "explorer %HOME%",
"reboot": "shutdown /r /f /t 0",
"shutdown": "shutdown /s /f /t 0"
}
}
```
* entering [open-home], [explorer %HOME%] will be executed
* entering [open-url github.com], [msedge github.com] will be executed

2 changes: 2 additions & 0 deletions env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
8 changes: 8 additions & 0 deletions env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "portable_runner_env"
version = "0.0.1"
authors = ["kerwin612 <kerwin612@qq.com>"]

[dependencies]
random-string = "1.0.0"
mount_dir = "0.0.4"
106 changes: 106 additions & 0 deletions env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
extern crate mount_dir;
extern crate random_string;

use std::env::{set_var};
use std::process::Command;
use std::fs::{remove_dir_all};
use std::path::{Component, Path};
use std::os::windows::process::CommandExt;
use std::io::{Error, BufRead, BufReader, ErrorKind};

use random_string::generate;

pub fn mount(tpath: &str, lpath: &str, hpath: &str, force: bool) -> Result<bool, Error> {

if ! Path::new(&tpath).exists() {
return Err(Error::new(ErrorKind::NotFound, format!("[{}] not found", &tpath)));
}

if Path::new(&lpath).exists() {
if force {
match remove_dir_all(&lpath) {
Err(e) => return Err(e),
_ => (),
}
} else {
return Err(Error::new(ErrorKind::AlreadyExists, format!("[{}] already exists", &lpath)));
}
}

let _hpath = format!("{}\\{}", lpath, hpath);

mount_dir::mount(tpath, lpath, force)?;

let app_data = format!("{}\\AppData", &_hpath);
let roaming_app_data = format!("{}\\Roaming", &app_data);
let local_app_data = format!("{}\\Local", &app_data);
let temp = format!("{}\\Temp", &local_app_data);

set_var("TMP", &temp);
set_var("TEMP", &temp);
set_var("HOME", &_hpath);
set_var("HOMEPATH", &_hpath);
set_var("USERPROFILE", &_hpath);
set_var("HOMEDRIVE", get_disk(lpath));
set_var("APPDATA", &roaming_app_data);
set_var("LOCALAPPDATA", &local_app_data);
set_var("PORTABLE_RUNNER_ENV_LINK_PATH", &lpath);
set_var("PORTABLE_RUNNER_ENV_TARGET_PATH", &tpath);

let mut profile_path = format!("{}\\.profile.cmd", &_hpath);
if ! Path::new(&profile_path).exists() {
profile_path = format!("{}\\.profile.bat", &_hpath);
}
if Path::new(&profile_path).exists() {
let env_flag = format!(".env.{}.tmp", generate(32, "1234567890"));
let output = Command::new("CMD").args(["/D", "/C", &profile_path, "&", "ECHO", &env_flag, "&", "SET"]).creation_flags(0x08000000).output().expect("process failed to execute");
let reader = BufReader::new(&*output.stdout);
let mut is_env = false;
for line in reader.lines() {
let l = line.unwrap();
if ! is_env {
if env_flag.eq(l.trim()) {
is_env = true;
}
continue;
}
match l.trim().split_once('=') {
Some((key, value)) => {
set_var(key, value);
}
None => ()
}
}
}

return Ok(true);
}

pub fn unmount(link: &str) -> bool {
return mount_dir::unmount(link);
}

fn get_disk(path: &str) -> &str {
match Path::new(path).components().next().unwrap() {
Component::Prefix(prefix_component) => {
return prefix_component.as_os_str().to_str().unwrap();
}
_ => unreachable!(),
}
}

#[cfg(test)]
mod it_works {
use super::*;
use std::env;

#[test]
fn test_for_mount() {
assert_eq!(mount(env::current_dir().unwrap().as_path().to_str().unwrap(), "T:\\work", "home", true).unwrap(), true);
}

#[test]
fn test_for_unmount() {
assert_eq!(unmount("T:\\work"), true);
}
}
24 changes: 24 additions & 0 deletions gui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
3 changes: 3 additions & 0 deletions gui/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"]
}
7 changes: 7 additions & 0 deletions gui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tauri + Vanilla

This template should help get you started developing with Tauri in vanilla HTML, CSS and Javascript.

## Recommended IDE Setup

- [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)
12 changes: 12 additions & 0 deletions gui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "gui",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"tauri": "tauri"
},
"devDependencies": {
"@tauri-apps/cli": "^1.2.2"
}
}
4 changes: 4 additions & 0 deletions gui/src-tauri/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/

Loading

0 comments on commit af391a6

Please sign in to comment.