Added selector for revanced cli

This commit is contained in:
Hydroxycarbamide 2022-09-04 16:39:56 +02:00
parent 59888b38dd
commit f60f96fa87
7 changed files with 110 additions and 76 deletions

View file

@ -25,6 +25,7 @@ module.exports = {
} }
], ],
rules: { rules: {
'import/no-mutable-exports': 'off', // to dismiss `export let prop = 'default'` warning
}, },
settings: { settings: {
'svelte3/typescript': () => require('typescript'), 'svelte3/typescript': () => require('typescript'),

View file

@ -3,21 +3,17 @@
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
use std::io::Cursor;
use std::path::PathBuf;
use std::io::copy;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::fs::File; use std::fs::File;
use std::io::{self, Write}; use std::io::{self, Write};
use tempdir::TempDir; use tempdir::TempDir;
use reqwest;
mod partial_download; mod partial_download;
fn main() { fn main() {
tauri::Builder::default() tauri::Builder::default()
.invoke_handler(tauri::generate_handler![java_version, download_revanced_cli]) .invoke_handler(tauri::generate_handler![java_version, use_revanced_cli])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }
@ -39,22 +35,23 @@ fn java_version() -> Result<String, String> {
} }
#[tauri::command] #[tauri::command]
fn download_revanced_cli() -> Result<String, String> { fn use_revanced_cli(
let response = match download_revanced_cli_in_tmp() { apk_url: String,
cli_url: String,
patches_url: String,
integrations_url: String,
device: String,
options: Vec<String>
) -> Result<String, String> {
let response = match use_revanced(cli_url) {
Ok(response) => response, Ok(response) => response,
Err(_) => return Err("Could not create temp dir".into()), Err(_) => return Err("Could not create temp dir".into()),
}; };
Ok(response.into()) Ok(response.into())
} }
fn download_revanced_cli_somewhere(tmp_dir_path: &Path) -> Result<String, String> { fn download_in_dir(tmp_dir_path: &Path, target: &str, filename: &str) -> Result<String, String> {
let target = "https://github.com/revanced/revanced-cli/releases/download/v2.9.5/revanced-cli-2.9.5-all.jar"; let fname = tmp_dir_path.join(filename);
let fname = tmp_dir_path.join("revanced_cli.jar");
let path = fname.clone();
let string_path = path
.to_str()
.expect("Could not read path");
let dest = { let dest = {
println!("will be located under: '{:?}'", fname); println!("will be located under: '{:?}'", fname);
match File::create(fname) { match File::create(fname) {
@ -62,41 +59,29 @@ fn download_revanced_cli_somewhere(tmp_dir_path: &Path) -> Result<String, String
Err(_) => return Err("Could not create file".to_owned()), Err(_) => return Err("Could not create file".to_owned()),
} }
}; };
partial_download::download(target, &dest).; partial_download::download(target, &dest)
.expect("Failed to download");
let stdout = match Command::new("java") Ok(format!("Success downloading {}", target))
.arg("-jar")
.arg(string_path)
.arg("--version")
.output() {
Ok(result) => result.stdout.clone(),
Err(_e) => return Err("revanced-cli version not found".into()),
};
let s = std::str::from_utf8(&stdout)
.expect("Could not read output");
println!("{}", s);
Ok(s.into())
} }
fn download_revanced_cli_in_tmp() -> Result<String, io::Error> { fn use_revanced(
cli_url: String
) -> Result<String, io::Error> {
let tmp_dir = TempDir::new("example")?; let tmp_dir = TempDir::new("example")?;
let file_path = tmp_dir.path().join("my-temporary-note.txt"); let file_path = tmp_dir.path().join("my-temporary-note.txt");
let mut tmp_file = File::create(file_path.clone())?;
writeln!(tmp_file, "Brian was here. Briefly.");
match file_path.to_str() { match file_path.to_str() {
None => println!("No file path for the file"), None => println!("No file path for the file"),
Some(path) => println!("{}", path) Some(path) => println!("{}", path)
} }
let response = match download_revanced_cli_somewhere(tmp_dir.path()) { let filename = "revanced_cli.jar";
let response = match download_in_dir(tmp_dir.path(), cli_url.as_str(), filename) {
Ok(response) => response, Ok(response) => response,
Err(errmsg) => panic!("{}", errmsg), Err(e) => e,
}; };
drop(tmp_file);
tmp_dir.close()?; tmp_dir.close()?;
Ok(response) Ok(response)
} }

View file

@ -1,9 +1,14 @@
<script lang="ts"> <script lang="ts">
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import svelteLogo from './assets/svelte.svg'; import type { Asset } from './lib/Asset';
import Counter from './lib/Counter.svelte'; import type { Release } from './lib/Release';
import type { Revanced } from './lib/Revanced';
import Select from './lib/Select.svelte';
let version: string = ''; let version: string = '';
let revancedCliReleases: Map<string, Release> | undefined;
let selected: string;
let versions: Array<Array<string>> | undefined;
const getJavaVersion = () => { const getJavaVersion = () => {
invoke('java_version') invoke('java_version')
.then((message: string) => { .then((message: string) => {
@ -11,8 +16,26 @@
}); });
}; };
const downloadRevancedCli = () => { const getRevanced: () => Revanced = () => {
invoke('download_revanced_cli') const cliUrl = revancedCliReleases
.get(selected)
.assets
.find((asset: Asset) => asset.name.match(/^revanced-cli(.*).jar$/gm))
.browser_download_url;
return ({
apkUrl: '',
cliUrl,
patchesUrl: '',
integrationsUrl: '',
device: '',
options: [],
});
};
const useRevancedCli = () => {
const args = JSON.parse(JSON.stringify(getRevanced()));
console.log(args);
invoke('use_revanced_cli', args)
.then((success: string) => { .then((success: string) => {
version = success; version = success;
}) })
@ -20,48 +43,30 @@
version = error; version = error;
}); });
}; };
const revancedCliReleasesUrl = 'https://api.github.com/repos/revanced/revanced-cli/releases';
fetch(revancedCliReleasesUrl)
.then((response) => response.json())
.then((data) => {
revancedCliReleases = new Map(data.map(
(release) => [release.name, release],
));
versions = Array.from(revancedCliReleases).map((value) => [value[0], value[1].name]);
});
</script> </script>
<main> <main>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite Logo" />
</a>
<a href="https://svelte.dev" target="_blank">
<img src={svelteLogo} class="logo svelte" alt="Svelte Logo" />
</a>
</div>
<h1>Vite + Svelte</h1>
<div class="card"> <div class="card">
<Counter />
<button on:click={getJavaVersion}>Get java version</button> <button on:click={getJavaVersion}>Get java version</button>
<button on:click={downloadRevancedCli}>Download revanced</button> <button on:click={useRevancedCli}>Download revanced</button>
<p>{version}</p> <p>{version}</p>
<Select
id="revanced-cli-versions"
defaultText={'Select the revanced-cli version'}
options={versions}
bind:selected={selected}
/>
</div> </div>
<p>
Check out <a href="https://github.com/sveltejs/kit#readme" target="_blank">SvelteKit</a>, the official Svelte app framework powered by Vite!
</p>
<p class="read-the-docs">
Click on the Vite and Svelte logos to learn more
</p>
</main> </main>
<style>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.svelte:hover {
filter: drop-shadow(0 0 2em #ff3e00aa);
}
.read-the-docs {
color: #888;
}
</style>

4
src/lib/Asset.ts Normal file
View file

@ -0,0 +1,4 @@
export interface Asset {
name: string
browser_download_url: string
}

6
src/lib/Release.ts Normal file
View file

@ -0,0 +1,6 @@
import type { Asset } from './Asset';
export interface Release {
name: string
assets: Asset[]
}

8
src/lib/Revanced.ts Normal file
View file

@ -0,0 +1,8 @@
export interface Revanced {
apkUrl: string,
cliUrl: string,
patchesUrl: string,
integrationsUrl: string,
device: string,
options: string[]
}

25
src/lib/Select.svelte Normal file
View file

@ -0,0 +1,25 @@
<script lang="ts">
export let id: string;
export let options: Array<Array<string>> | undefined;
export let defaultText: string;
export let selected: string;
</script>
<label
for={id}
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
>
{defaultText}
</label>
<select
bind:value={selected}
id={id}
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
>
<option selected>{defaultText}</option>
{#if options !== undefined}
{#each options as [key, value]}
<option value={key}>{value}</option>
{/each}
{/if}
</select>