Skip to content

Commit

Permalink
feat: support service file name template
Browse files Browse the repository at this point in the history
  • Loading branch information
soul committed Dec 16, 2023
1 parent 7d196a5 commit 881c9a8
Show file tree
Hide file tree
Showing 24 changed files with 945 additions and 1,028 deletions.
13 changes: 0 additions & 13 deletions .eslintignore

This file was deleted.

31 changes: 0 additions & 31 deletions .eslintrc.cjs

This file was deleted.

5 changes: 5 additions & 0 deletions src-tauri/src/services/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub struct UpdateConfigRequest {
pub request_template: Option<String>,
pub header_template: Option<String>,
pub request_path: Option<String>,
pub file_name_template: Option<String>
}

// 更新类型配置
Expand Down Expand Up @@ -193,6 +194,10 @@ pub async fn update_config(
config_json.header_template = Some(header_template.clone());
}

if let Some(file_name_template) = &form.file_name_template {
config_json.file_name_template = Some(file_name_template.clone());
}

if let Some(request_path) = &form.request_path {
config_json.request_path = Some(PathBuf::from(request_path));
config_json.request_full_path = Some(join_path(
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/structs/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct ConfigJson {
pub request_full_path: Option<PathBuf>,
pub request_template: Option<String>,
pub header_template: Option<String>,
pub file_name_template : Option<String>
}

impl ConfigJson {
Expand All @@ -54,6 +55,7 @@ impl ConfigJson {
request_full_path:None,
request_template: None,
header_template: None,
file_name_template: None
}
}
}
Expand Down
55 changes: 36 additions & 19 deletions src-tauri/src/structs/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct Request {
pub request_template: Option<String>,
pub header_template: Option<String>,
pub request_full_path: Option<PathBuf>,
// My_$1 : dirname => My_dirname.ts
pub file_name_template: Option<String>,
}

impl Request {
Expand All @@ -47,6 +49,7 @@ impl Request {
request_template: config_json.request_template,
header_template: config_json.header_template,
request_full_path: config_json.request_full_path,
file_name_template: config_json.file_name_template,
}
}

Expand Down Expand Up @@ -87,15 +90,8 @@ impl Request {
}

pub fn write_ts(&self, path: &PathBuf) {
let types_root_path = self.context.types_full_path.clone().unwrap();
let sub_path = match path == &types_root_path {
true => None,
false => Some(path.strip_prefix(&types_root_path).unwrap().to_path_buf()),
};

println!("sub_path: {:?}", sub_path);

let read_res = fs::read_dir(path).unwrap();
let sub_path = self.get_type_relative_path(path);
let write_path = self.get_write_path(&sub_path);

let mut ts_string = match &self.header_template {
Some(template) => format!("{}\n", template.clone()),
Expand All @@ -105,14 +101,7 @@ impl Request {
let mut import_list: Vec<String> = vec![];
let mut export_list: Vec<String> = vec![];

let write_path = match &sub_path {
Some(sub_path) => {
self.request_full_path.clone().unwrap().join(&sub_path)
},
None => self.request_full_path.clone().unwrap().join("index"),
};

for dirs in read_res.into_iter() {
for dirs in fs::read_dir(path).unwrap().into_iter() {
let dir = dirs.unwrap();
let file_type = dir.file_type().unwrap();
let file_path = dir.path();
Expand Down Expand Up @@ -151,12 +140,40 @@ impl Request {

let parent = write_path.parent().unwrap();

println!("{:#?}", write_path);

fs::create_dir_all(parent).unwrap();

fs::write(format!("{}.ts", write_path.to_str().unwrap()), ts_string).unwrap();
}

// 获取 type 文件的相对路径
fn get_type_relative_path(&self, path: &PathBuf) -> Option<PathBuf> {
let types_root_path = self.context.types_full_path.clone().unwrap();
match path == &types_root_path {
true => None,
false => Some(path.strip_prefix(&types_root_path).unwrap().to_path_buf()),
}
}

// 获取写入 service 文件的路径
fn get_write_path(&self, sub_path: &Option<PathBuf>) -> PathBuf {
match &sub_path {
Some(sub_path) => {
let mut write_path = self.request_full_path.clone().unwrap().join(&sub_path);

if let Some(template) = self.file_name_template.clone() {
if let Some(file_name) = write_path.file_name() {
let real_file_name = template.replace("$1", file_name.to_str().unwrap());
write_path.set_file_name(real_file_name);
}
}

write_path
}
None => self.request_full_path.clone().unwrap().join("index"),
}
}

// 检查生成service的 type 文件是否有 Request/Response interface
fn check_file(&self, file_path: &PathBuf, file_name_without_ext: &String) -> bool {
let req = format!("{}Request", file_name_without_ext);
let resp = format!("{}Response", file_name_without_ext);
Expand Down
145 changes: 71 additions & 74 deletions src/lib/components/ProcessingModal.svelte
Original file line number Diff line number Diff line change
@@ -1,92 +1,89 @@
<script lang="ts">
import { listen } from "@tauri-apps/api/event";
import { Modal, ProgressBar } from "carbon-components-svelte";
import { onDestroy, onMount } from "svelte";
import { runningTask, processingModalOpen, processingModalTotal } from "../store";
import { request } from "@/utils";
import type { SuccessResponse } from "@/types/public";
import { toast } from "@zerodevx/svelte-toast";
import { toastTheme } from "@/consts";
import { tweened } from "svelte/motion";
import { cubicOut } from "svelte/easing";
import { listen } from '@tauri-apps/api/event';
import { Modal, ProgressBar } from 'carbon-components-svelte';
import { onDestroy, onMount } from 'svelte';
import { runningTask, processingModalOpen, processingModalTotal } from '../store';
import { request } from '@/utils';
import type { SuccessResponse } from '@/types/public';
import { toast } from '@zerodevx/svelte-toast';
import { toastTheme } from '@/consts';
import { tweened } from 'svelte/motion';
import { cubicOut } from 'svelte/easing';
const progress = tweened(0, {
duration: 400,
easing: cubicOut,
});
const progress = tweened(0, {
duration: 400,
easing: cubicOut
});
let log_area: HTMLDivElement;
let log_area: HTMLDivElement;
let log_list: {
msg: string;
is_success: boolean;
}[] = [];
let log_list: {
msg: string;
is_success: boolean;
}[] = [];
let unlistenLog: () => void;
let unlistenLog: () => void;
onMount(async () => {
unlistenLog = await listen<{
msg: string;
success_number: number;
is_success: boolean;
}>("log", (event) => {
log_list.push({
msg: event.payload.msg,
is_success: event.payload.is_success,
});
log_list = log_list;
log_area.scrollTop = log_area.scrollHeight
progress.set(event.payload.success_number);
});
});
onMount(async () => {
unlistenLog = await listen<{
msg: string;
success_number: number;
is_success: boolean;
}>('log', (event) => {
log_list.push({
msg: event.payload.msg,
is_success: event.payload.is_success
});
log_list = log_list;
log_area.scrollTop = log_area.scrollHeight;
progress.set(event.payload.success_number);
});
});
onDestroy(() => {
unlistenLog();
});
onDestroy(() => {
unlistenLog();
});
function onClose() {
const over = log_list.length === $processingModalTotal;
function onClose() {
const over = log_list.length === $processingModalTotal;
if (!over) {
request("pause")
.then((res: SuccessResponse<null>) => {
toast.push(JSON.stringify(res.message), toastTheme.success);
$processingModalOpen = false;
})
.catch((e) => {
toast.push(JSON.stringify(e), toastTheme.error);
});
}
if (!over) {
request('pause')
// @ts-expect-error
.then((res: SuccessResponse<null>) => {
toast.push(JSON.stringify(res.message), toastTheme.success);
$processingModalOpen = false;
})
.catch((e) => {
toast.push(JSON.stringify(e), toastTheme.error);
});
}
log_list = [];
$processingModalTotal = 0;
progress.set(0);
runningTask.update(() => false);
}
log_list = [];
$processingModalTotal = 0;
progress.set(0);
runningTask.update(() => false);
}
</script>

<Modal
bind:open={$processingModalOpen}
modalHeading="Log"
preventCloseOnClickOutside
passiveModal
on:close={onClose}
bind:open={$processingModalOpen}
modalHeading="Log"
preventCloseOnClickOutside
passiveModal
on:close={onClose}
>
<div bind:this={log_area} style="max-height:300px;overflow-y:auto;">
{#each log_list as log}
{#if log.is_success}
<p>{log.msg}</p>
{:else}
<p style="color:crimson">{log.msg}</p>
{/if}
{/each}
</div>
<div bind:this={log_area} style="max-height:300px;overflow-y:auto;">
{#each log_list as log}
{#if log.is_success}
<p>{log.msg}</p>
{:else}
<p style="color:crimson">{log.msg}</p>
{/if}
{/each}
</div>

<ProgressBar
value={$progress}
max={$processingModalTotal}
labelText="进度条"
/>
<ProgressBar value={$progress} max={$processingModalTotal} labelText="进度条" />
</Modal>

<style>
Expand Down
Loading

0 comments on commit 881c9a8

Please sign in to comment.