mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-09-18 13:54:53 +08:00
feat(bat): 完善工作流调度与 dashboard RPC
补全资源拉取、解析、翻译、重打包和本地化发布命令,支持单次、限定次数与周期调度。移除 TUI 计划并通过 schedule.* RPC 暴露给 bat-api dashboard。 Closes #43
This commit is contained in:
@@ -8,22 +8,26 @@ use bat_infrastructure::DEFAULT_DOWNLOAD_CONCURRENCY;
|
||||
use bat_infrastructure::{
|
||||
apply_patch_file, apply_unityfs_field_patch_file, apply_unityfs_string_field_patch_file,
|
||||
apply_unityfs_text_asset_patch_file, changed_endpoint_urls, diff_extended_snapshot,
|
||||
gc_orphan_staging, lexical_absolute, open_append_file, read_download_manifest_at,
|
||||
export_translation_workbench, gc_orphan_staging, lexical_absolute,
|
||||
localized_text_asset_patches, open_append_file, read_download_manifest_at,
|
||||
read_file_no_symlink, read_localized_patch_manifest_at, read_localized_version_state,
|
||||
read_parse_cache_at, read_snapshot, read_textunit_index_at, read_version_state,
|
||||
redact_proxy_url, resolve_curl_proxy, validate_output_root, validate_runtime_state_dir,
|
||||
write_file_atomic, CurlProxyConfig, CurlProxyMode, OfficialEndpointMarkerRole,
|
||||
OfficialFailedVersionRecord, OfficialResourceHashVerification, OfficialResourceVerification,
|
||||
OfficialServerInfoSource, OfficialTextUnitQuery, OfficialTextUnitTaskQuery,
|
||||
OfficialUpdateConfig, OfficialUpdateProgress, OfficialUpdateReport, OfficialUpdateService,
|
||||
OfficialUpdateSnapshot, OfficialUpdateStatus, OfficialVerificationSummary,
|
||||
OfficialVersionRecord, OfficialVersionState, PatchApplyKind, PatchApplyParams,
|
||||
PatchApplyReport, ReleaseFlowStatusCode, SqliteResourceRepository,
|
||||
SqliteTranslationTaskRepository, TranslationTaskStatus, UnityFsFieldPatchParams,
|
||||
UnityFsPatchReport, UnityFsStringFieldPatchParams, UnityFsTextAssetPatchParams,
|
||||
LOCALIZED_CURRENT_LINK, LOCALIZED_PATCH_MANIFEST_FILE, LOCALIZED_VERSIONS_DIR,
|
||||
LOCALIZED_VERSION_STATE_FILE, MAX_DOWNLOAD_CONCURRENCY, MIN_DOWNLOAD_CONCURRENCY,
|
||||
OFFICIAL_PARSE_CACHE_FILE, OFFICIAL_TEXTUNIT_INDEX_FILE, PRIVATE_FILE_MODE,
|
||||
read_parse_cache_at, read_snapshot, read_textunit_index_at, read_translation_workbench,
|
||||
read_version_state, redact_proxy_url, repack_bundle, resolve_curl_proxy, set_translation,
|
||||
validate_output_root, validate_runtime_state_dir, write_file_atomic,
|
||||
write_official_textunit_queues, CurlProxyConfig, CurlProxyMode, LocalizedPatchConfig,
|
||||
LocalizedPatchReport, LocalizedPatchService, OfficialEndpointMarkerRole,
|
||||
OfficialFailedVersionRecord, OfficialParseCacheService, OfficialParseConfig,
|
||||
OfficialResourceHashVerification, OfficialResourceVerification, OfficialServerInfoSource,
|
||||
OfficialTextUnitQuery, OfficialTextUnitTaskQuery, OfficialUpdateConfig, OfficialUpdateProgress,
|
||||
OfficialUpdateReport, OfficialUpdateService, OfficialUpdateSnapshot, OfficialUpdateStatus,
|
||||
OfficialVerificationSummary, OfficialVersionRecord, OfficialVersionState, PatchApplyKind,
|
||||
PatchApplyParams, PatchApplyReport, ReleaseFlowStatusCode, RepackReport,
|
||||
SqliteResourceRepository, SqliteTranslationTaskRepository, TranslationTaskStatus,
|
||||
UnityFsFieldPatchParams, UnityFsPatchReport, UnityFsStringFieldPatchParams,
|
||||
UnityFsTextAssetPatchParams, LOCALIZED_CURRENT_LINK, LOCALIZED_PATCH_MANIFEST_FILE,
|
||||
LOCALIZED_VERSIONS_DIR, LOCALIZED_VERSION_STATE_FILE, MAX_DOWNLOAD_CONCURRENCY,
|
||||
MIN_DOWNLOAD_CONCURRENCY, OFFICIAL_PARSE_CACHE_FILE, OFFICIAL_TEXTUNIT_INDEX_FILE,
|
||||
PRIVATE_FILE_MODE,
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -46,16 +50,23 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
mod patch_commands;
|
||||
#[path = "readonly_query.rs"]
|
||||
mod readonly_query;
|
||||
#[path = "schedule_commands.rs"]
|
||||
mod schedule_commands;
|
||||
#[path = "task_registry.rs"]
|
||||
mod task_registry;
|
||||
#[path = "translation_query.rs"]
|
||||
mod translation_query;
|
||||
#[path = "workflow_commands.rs"]
|
||||
mod workflow_commands;
|
||||
use patch_commands::{
|
||||
is_write_patch_command, run_write_patch_command, validate_write_patch_options,
|
||||
};
|
||||
#[cfg(test)]
|
||||
use readonly_query::run_readonly_query_command_with_rpc;
|
||||
use readonly_query::{run_readonly_query_command, validate_readonly_query_options};
|
||||
use schedule_commands::{
|
||||
run_schedule_add, run_schedule_list, run_schedule_remove, run_schedule_run, run_schedule_update,
|
||||
};
|
||||
use task_registry::{
|
||||
run_task_worker, CancelOutcome, DaemonTaskContext, TaskJob, TaskKind, TaskRegistry,
|
||||
};
|
||||
@@ -67,6 +78,9 @@ use translation_query::{
|
||||
build_translation_handoff_report, build_translation_tasks_report, textunit_query_json,
|
||||
update_translation_task_status_report,
|
||||
};
|
||||
use workflow_commands::{
|
||||
run_parse_once, run_publish_localized, run_repack, run_translate_once, run_translation_set,
|
||||
};
|
||||
|
||||
const EXIT_ERROR: i32 = 1;
|
||||
const EXIT_LOCKED: i32 = 75;
|
||||
@@ -168,6 +182,52 @@ fn run() -> anyhow::Result<i32> {
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::Pull => {
|
||||
run_repeated_workflow(&options, "pull", |options| {
|
||||
run_sync_command_foreground(options, "pull")
|
||||
})?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::Parse => {
|
||||
run_repeated_workflow(&options, "parse", run_parse_once)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::Translate => {
|
||||
run_repeated_workflow(&options, "translate", run_translate_once)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::TranslationSet => {
|
||||
run_translation_set(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::Repack => {
|
||||
run_repack(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::PublishLocalized => {
|
||||
run_repeated_workflow(&options, "publish-localized", run_publish_localized)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::ScheduleList => {
|
||||
run_schedule_list(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::ScheduleAdd => {
|
||||
run_schedule_add(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::ScheduleUpdate => {
|
||||
run_schedule_update(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::ScheduleRemove => {
|
||||
run_schedule_remove(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::ScheduleRun => {
|
||||
run_schedule_run(&options)?;
|
||||
Ok(0)
|
||||
}
|
||||
CliCommand::Status => {
|
||||
let _control_lock = DaemonControlLock::acquire(&options.state_dir)?;
|
||||
print_daemon_status(&options.state_dir, options.output_format)?;
|
||||
@@ -233,6 +293,34 @@ fn run() -> anyhow::Result<i32> {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_repeated_workflow(
|
||||
options: &CliOptions,
|
||||
command_name: &'static str,
|
||||
mut operation: impl FnMut(&CliOptions) -> anyhow::Result<()>,
|
||||
) -> anyhow::Result<()> {
|
||||
let max_runs = if options.watch {
|
||||
None
|
||||
} else {
|
||||
Some(options.run_count.unwrap_or(1))
|
||||
};
|
||||
let mut completed_runs = 0usize;
|
||||
loop {
|
||||
operation(options)?;
|
||||
completed_runs += 1;
|
||||
if max_runs.is_some_and(|limit| completed_runs >= limit) {
|
||||
return Ok(());
|
||||
}
|
||||
if options.progress {
|
||||
eprintln!(
|
||||
"{command_name} 下一轮将在 {} 后执行(已完成 {} 轮)",
|
||||
format_duration(options.interval),
|
||||
completed_runs
|
||||
);
|
||||
}
|
||||
thread::sleep(options.interval);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct ErrorReport<'a> {
|
||||
status: &'a str,
|
||||
@@ -248,10 +336,31 @@ struct CliOptions {
|
||||
command: CliCommand,
|
||||
output_format: OutputFormat,
|
||||
watch: bool,
|
||||
run_count: Option<usize>,
|
||||
interval_explicit: bool,
|
||||
daemon: bool,
|
||||
daemon_child: bool,
|
||||
state_dir: PathBuf,
|
||||
output_explicit: bool,
|
||||
resource_root: Option<PathBuf>,
|
||||
translation_file: Option<PathBuf>,
|
||||
translation_id: Option<String>,
|
||||
translation_text: Option<String>,
|
||||
translation_text_file: Option<PathBuf>,
|
||||
localized_release_id: Option<String>,
|
||||
repack_spec: Option<PathBuf>,
|
||||
schedule_group: Option<String>,
|
||||
schedule_action: Option<String>,
|
||||
schedule_id: Option<String>,
|
||||
schedule_at_unix: Option<u64>,
|
||||
schedule_delay: Option<Duration>,
|
||||
schedule_every: Option<Duration>,
|
||||
schedule_count: Option<usize>,
|
||||
schedule_args: Vec<String>,
|
||||
schedule_clear_args: bool,
|
||||
schedule_clear_every: bool,
|
||||
schedule_enabled: Option<bool>,
|
||||
schedule_option_explicit: bool,
|
||||
sync_option_explicit: bool,
|
||||
proxy_option_explicit: bool,
|
||||
interval: Duration,
|
||||
@@ -309,10 +418,31 @@ impl Default for CliOptions {
|
||||
command: CliCommand::Run,
|
||||
output_format: OutputFormat::Human,
|
||||
watch: false,
|
||||
run_count: None,
|
||||
interval_explicit: false,
|
||||
daemon: false,
|
||||
daemon_child: false,
|
||||
state_dir: PathBuf::from(DEFAULT_DAEMON_STATE_DIR),
|
||||
output_explicit: false,
|
||||
resource_root: None,
|
||||
translation_file: None,
|
||||
translation_id: None,
|
||||
translation_text: None,
|
||||
translation_text_file: None,
|
||||
localized_release_id: None,
|
||||
repack_spec: None,
|
||||
schedule_group: None,
|
||||
schedule_action: None,
|
||||
schedule_id: None,
|
||||
schedule_at_unix: None,
|
||||
schedule_delay: None,
|
||||
schedule_every: None,
|
||||
schedule_count: None,
|
||||
schedule_args: Vec::new(),
|
||||
schedule_clear_args: false,
|
||||
schedule_clear_every: false,
|
||||
schedule_enabled: None,
|
||||
schedule_option_explicit: false,
|
||||
sync_option_explicit: false,
|
||||
proxy_option_explicit: false,
|
||||
interval: Duration::from_secs(DEFAULT_WATCH_INTERVAL_SECONDS),
|
||||
@@ -372,6 +502,17 @@ enum OutputFormat {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum CliCommand {
|
||||
Run,
|
||||
Pull,
|
||||
Parse,
|
||||
Translate,
|
||||
TranslationSet,
|
||||
Repack,
|
||||
PublishLocalized,
|
||||
ScheduleList,
|
||||
ScheduleAdd,
|
||||
ScheduleUpdate,
|
||||
ScheduleRemove,
|
||||
ScheduleRun,
|
||||
Status,
|
||||
Stop,
|
||||
Restart,
|
||||
@@ -831,6 +972,11 @@ const RPC_METHOD_RESOURCE_REPAIR: &str = "resource.repair";
|
||||
const RPC_METHOD_RESOURCE_MANIFEST: &str = "resource.manifest";
|
||||
const RPC_METHOD_RESOURCE_INDEX: &str = "resource.index";
|
||||
const RPC_METHOD_RESOURCE_LIST: &str = "resource.list";
|
||||
const RPC_METHOD_SCHEDULE_LIST: &str = "schedule.list";
|
||||
const RPC_METHOD_SCHEDULE_ADD: &str = "schedule.add";
|
||||
const RPC_METHOD_SCHEDULE_UPDATE: &str = "schedule.update";
|
||||
const RPC_METHOD_SCHEDULE_REMOVE: &str = "schedule.remove";
|
||||
const RPC_METHOD_SCHEDULE_RUN: &str = "schedule.run";
|
||||
const RPC_METHOD_PARSE_STATUS: &str = "parse.status";
|
||||
const RPC_METHOD_PARSE_TEXT_UNITS: &str = "parse.text_units";
|
||||
const RPC_METHOD_PARSE_ERRORS: &str = "parse.errors";
|
||||
@@ -1616,6 +1762,77 @@ fn dispatch_rpc_method(
|
||||
"resource.state",
|
||||
build_resource_state_report(state_dir),
|
||||
),
|
||||
RPC_METHOD_SCHEDULE_LIST => rpc_envelope_from_result(
|
||||
request_id,
|
||||
RPC_METHOD_SCHEDULE_LIST,
|
||||
schedule_commands::schedule_list_report(state_dir),
|
||||
),
|
||||
RPC_METHOD_SCHEDULE_ADD => {
|
||||
let params = match rpc_struct_params::<schedule_commands::ScheduleMutationRequest>(
|
||||
request.params.as_ref(),
|
||||
RPC_METHOD_SCHEDULE_ADD,
|
||||
) {
|
||||
Ok(params) => params,
|
||||
Err(error) => return rpc_envelope_error(request_id, error),
|
||||
};
|
||||
rpc_envelope_from_result(
|
||||
request_id,
|
||||
RPC_METHOD_SCHEDULE_ADD,
|
||||
schedule_commands::schedule_add_report(state_dir, params),
|
||||
)
|
||||
}
|
||||
RPC_METHOD_SCHEDULE_UPDATE => {
|
||||
let params = match rpc_struct_params::<schedule_commands::ScheduleMutationRequest>(
|
||||
request.params.as_ref(),
|
||||
RPC_METHOD_SCHEDULE_UPDATE,
|
||||
) {
|
||||
Ok(params) => params,
|
||||
Err(error) => return rpc_envelope_error(request_id, error),
|
||||
};
|
||||
rpc_envelope_from_result(
|
||||
request_id,
|
||||
RPC_METHOD_SCHEDULE_UPDATE,
|
||||
schedule_commands::schedule_update_report(state_dir, params),
|
||||
)
|
||||
}
|
||||
RPC_METHOD_SCHEDULE_REMOVE => {
|
||||
let params = match rpc_struct_params::<schedule_commands::ScheduleMutationRequest>(
|
||||
request.params.as_ref(),
|
||||
RPC_METHOD_SCHEDULE_REMOVE,
|
||||
) {
|
||||
Ok(params) => params,
|
||||
Err(error) => return rpc_envelope_error(request_id, error),
|
||||
};
|
||||
rpc_envelope_from_result(
|
||||
request_id,
|
||||
RPC_METHOD_SCHEDULE_REMOVE,
|
||||
schedule_commands::schedule_remove_report(state_dir, params),
|
||||
)
|
||||
}
|
||||
RPC_METHOD_SCHEDULE_RUN => {
|
||||
let params = request
|
||||
.params
|
||||
.as_ref()
|
||||
.map(|value| serde_json::from_value(value.clone()))
|
||||
.transpose()
|
||||
.map_err(|error| {
|
||||
ApiError::new(
|
||||
ErrorCode::RPC_INVALID_PARAMS,
|
||||
RPC_METHOD_SCHEDULE_RUN,
|
||||
format!("params 无效:{error}"),
|
||||
)
|
||||
});
|
||||
let params = match params {
|
||||
Ok(Some(params)) => params,
|
||||
Ok(None) => schedule_commands::ScheduleRunRequest::default(),
|
||||
Err(error) => return rpc_envelope_error(request_id, error),
|
||||
};
|
||||
rpc_envelope_from_result(
|
||||
request_id,
|
||||
RPC_METHOD_SCHEDULE_RUN,
|
||||
schedule_commands::schedule_run_report(state_dir, params),
|
||||
)
|
||||
}
|
||||
RPC_METHOD_RESOURCE_SYNC => {
|
||||
let force = rpc_bool_param(request.params.as_ref(), "force").unwrap_or(false);
|
||||
enqueue_task_envelope(tasks, TaskKind::Sync, force, request_id)
|
||||
@@ -3837,6 +4054,42 @@ trait HumanReport {
|
||||
fn print_human(&self) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
impl HumanReport for serde_json::Value {
|
||||
fn print_human(&self) -> anyhow::Result<()> {
|
||||
print_human_json_value(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl HumanReport for RepackReport {
|
||||
fn print_human(&self) -> anyhow::Result<()> {
|
||||
print_title("UnityFS 重打包");
|
||||
print_field("命令", self.command);
|
||||
print_field("状态", self.status);
|
||||
print_path_field("源 bundle", &self.source_bundle);
|
||||
print_path_field("目标 bundle", &self.target_bundle);
|
||||
print_field("操作数", self.operation_count);
|
||||
print_field("源字节", self.source_bytes);
|
||||
print_field("目标字节", self.target_bytes);
|
||||
print_field("源 BLAKE3", &self.source_blake3);
|
||||
print_field("目标 BLAKE3", &self.target_blake3);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl HumanReport for LocalizedPatchReport {
|
||||
fn print_human(&self) -> anyhow::Result<()> {
|
||||
print_title("汉化 release 发布");
|
||||
print_path_field("版本目录", &self.version_path);
|
||||
print_path_field("current", &self.current_path);
|
||||
print_path_field("状态文件", &self.state_path);
|
||||
print_path_field("patch manifest", &self.patch_manifest_path);
|
||||
print_field("变更文件数", self.files.len());
|
||||
print_field("TextAsset 操作数", self.manifest.text_asset_operation_count);
|
||||
print_field("校验文件数", self.integrity.verified_changed_file_count);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn print_human_json_value(value: &serde_json::Value) -> anyhow::Result<()> {
|
||||
if value.get("running").is_some() && value.get("state_dir").is_some() {
|
||||
print_title("后台状态");
|
||||
@@ -6180,6 +6433,11 @@ fn parse_args_with_env(
|
||||
|
||||
while let Some(flag) = args.next() {
|
||||
match flag.as_str() {
|
||||
"res" | "resource" | "resources" => parse_resource_command(&mut args, &mut options)?,
|
||||
"parse" => parse_parse_command(&mut args, &mut options)?,
|
||||
"i18n" | "tr" | "translation" | "translate" => {
|
||||
parse_translation_command(&mut args, &mut options)?
|
||||
}
|
||||
"status" => {
|
||||
ensure_command_not_set(options.command, "status")?;
|
||||
options.command = CliCommand::Status;
|
||||
@@ -6313,6 +6571,29 @@ fn parse_args_with_env(
|
||||
PathBuf::from(next_option_value(&mut args, &flag)?);
|
||||
options.output_explicit = true;
|
||||
}
|
||||
"--resource-root" => {
|
||||
options.resource_root = Some(PathBuf::from(next_option_value(&mut args, &flag)?));
|
||||
}
|
||||
"--translation-file" | "--workbench" => {
|
||||
options.translation_file =
|
||||
Some(PathBuf::from(next_option_value(&mut args, &flag)?));
|
||||
}
|
||||
"--translation-id" => {
|
||||
options.translation_id = Some(next_option_value(&mut args, &flag)?);
|
||||
}
|
||||
"--translated-text" => {
|
||||
options.translation_text = Some(next_option_value(&mut args, &flag)?);
|
||||
}
|
||||
"--translated-file" => {
|
||||
options.translation_text_file =
|
||||
Some(PathBuf::from(next_option_value(&mut args, &flag)?));
|
||||
}
|
||||
"--localized-release-id" => {
|
||||
options.localized_release_id = Some(next_option_value(&mut args, &flag)?);
|
||||
}
|
||||
"--repack-spec" => {
|
||||
options.repack_spec = Some(PathBuf::from(next_option_value(&mut args, &flag)?));
|
||||
}
|
||||
"--import-repository" => {
|
||||
options.config.import_repository = true;
|
||||
options.sync_option_explicit = true;
|
||||
@@ -6334,6 +6615,18 @@ fn parse_args_with_env(
|
||||
"--state-dir" | "--pid-dir" => {
|
||||
options.state_dir = PathBuf::from(next_option_value(&mut args, &flag)?);
|
||||
}
|
||||
"--run-count" | "--repeat" => {
|
||||
let value = next_option_value(&mut args, &flag)?
|
||||
.parse::<usize>()
|
||||
.map_err(|error| anyhow::anyhow!("{flag} 无效:{error}"))?;
|
||||
if value == 0 {
|
||||
return Err(anyhow::anyhow!("{flag} 必须大于 0"));
|
||||
}
|
||||
options.run_count = Some(value);
|
||||
}
|
||||
"--once" => {
|
||||
options.run_count = Some(1);
|
||||
}
|
||||
"--snapshot" => {
|
||||
options.config.snapshot_path =
|
||||
Some(PathBuf::from(next_option_value(&mut args, &flag)?));
|
||||
@@ -6415,6 +6708,7 @@ fn parse_args_with_env(
|
||||
}
|
||||
"--interval" => {
|
||||
options.interval = parse_duration(&next_option_value(&mut args, &flag)?)?;
|
||||
options.interval_explicit = true;
|
||||
options.sync_option_explicit = true;
|
||||
}
|
||||
"--interval-seconds" => {
|
||||
@@ -6422,8 +6716,66 @@ fn parse_args_with_env(
|
||||
.parse::<u64>()
|
||||
.map_err(|error| anyhow::anyhow!("{flag} 的秒数无效:{error}"))?;
|
||||
options.interval = Duration::from_secs(seconds);
|
||||
options.interval_explicit = true;
|
||||
options.sync_option_explicit = true;
|
||||
}
|
||||
"--schedule-id" | "--id" => {
|
||||
options.schedule_id = Some(next_option_value(&mut args, &flag)?);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-action" | "--action" => {
|
||||
options.schedule_action = Some(next_option_value(&mut args, &flag)?);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-at-unix" => {
|
||||
let value = next_option_value(&mut args, &flag)?
|
||||
.parse::<u64>()
|
||||
.map_err(|error| anyhow::anyhow!("{flag} 无效:{error}"))?;
|
||||
options.schedule_at_unix = Some(value);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-delay" => {
|
||||
options.schedule_delay =
|
||||
Some(parse_duration(&next_option_value(&mut args, &flag)?)?);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-every" => {
|
||||
options.schedule_every =
|
||||
Some(parse_duration(&next_option_value(&mut args, &flag)?)?);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-count" => {
|
||||
let value = next_option_value(&mut args, &flag)?
|
||||
.parse::<usize>()
|
||||
.map_err(|error| anyhow::anyhow!("{flag} 无效:{error}"))?;
|
||||
if value == 0 {
|
||||
return Err(anyhow::anyhow!("--schedule-count 必须大于 0"));
|
||||
}
|
||||
options.schedule_count = Some(value);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-arg" => {
|
||||
options
|
||||
.schedule_args
|
||||
.push(next_option_value(&mut args, &flag)?);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-clear-args" => {
|
||||
options.schedule_clear_args = true;
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-clear-every" => {
|
||||
options.schedule_clear_every = true;
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-enabled" => {
|
||||
options.schedule_enabled = Some(true);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--schedule-disabled" => {
|
||||
options.schedule_enabled = Some(false);
|
||||
options.schedule_option_explicit = true;
|
||||
}
|
||||
"--error-retry" => {
|
||||
options.error_retry_interval =
|
||||
parse_duration(&next_option_value(&mut args, &flag)?)?;
|
||||
@@ -6741,7 +7093,7 @@ fn parse_args_with_env(
|
||||
options.progress = false;
|
||||
options.banner = false;
|
||||
}
|
||||
CliCommand::Refresh | CliCommand::Verify | CliCommand::Repair => {
|
||||
CliCommand::Pull | CliCommand::Refresh | CliCommand::Verify | CliCommand::Repair => {
|
||||
if !options.config.auto_discover
|
||||
&& options.config.server_info_source.is_none()
|
||||
&& options.config.connection_group.is_none()
|
||||
@@ -6756,6 +7108,52 @@ fn parse_args_with_env(
|
||||
options.config.repair = false;
|
||||
}
|
||||
}
|
||||
CliCommand::Parse | CliCommand::Translate | CliCommand::PublishLocalized => {
|
||||
if options.daemon || options.daemon_child {
|
||||
return Err(anyhow::anyhow!(
|
||||
"parse/translate/publish-localized 使用 --watch 或 schedule,不支持 daemon"
|
||||
));
|
||||
}
|
||||
if options.config.dry_run {
|
||||
return Err(anyhow::anyhow!(
|
||||
"parse/translate/publish-localized 不能使用 --dry-run"
|
||||
));
|
||||
}
|
||||
}
|
||||
CliCommand::TranslationSet | CliCommand::Repack => {
|
||||
if options.watch || options.daemon || options.daemon_child {
|
||||
return Err(anyhow::anyhow!("translation set/repack 只支持单次执行"));
|
||||
}
|
||||
if options.config.force || options.sync_option_explicit {
|
||||
return Err(anyhow::anyhow!("translation set/repack 不接受资源同步选项"));
|
||||
}
|
||||
options.progress = false;
|
||||
options.banner = false;
|
||||
}
|
||||
CliCommand::ScheduleList
|
||||
| CliCommand::ScheduleAdd
|
||||
| CliCommand::ScheduleUpdate
|
||||
| CliCommand::ScheduleRemove
|
||||
| CliCommand::ScheduleRun => {
|
||||
if options.daemon || options.daemon_child {
|
||||
return Err(anyhow::anyhow!("schedule 命令不能使用 daemon"));
|
||||
}
|
||||
if options.output_explicit
|
||||
|| options.proxy_option_explicit
|
||||
|| (options.config.force && !matches!(options.command, CliCommand::ScheduleRun))
|
||||
|| options.config.dry_run
|
||||
|| tools_are_non_default(&options.config, &options.env_baseline_config)
|
||||
{
|
||||
return Err(anyhow::anyhow!(
|
||||
"schedule 命令只接受 --state-dir、--json/--human、调度选项和 schedule run 的 --watch/--interval/--force"
|
||||
));
|
||||
}
|
||||
if options.interval_explicit && !matches!(options.command, CliCommand::ScheduleRun) {
|
||||
return Err(anyhow::anyhow!("--interval 只适用于 schedule run 的轮询"));
|
||||
}
|
||||
options.progress = false;
|
||||
options.banner = false;
|
||||
}
|
||||
CliCommand::Restart | CliCommand::Reload => {
|
||||
if (options.sync_option_explicit
|
||||
|| options.output_explicit
|
||||
@@ -6787,6 +7185,35 @@ fn parse_args_with_env(
|
||||
if options.error_retry_interval.is_zero() {
|
||||
return Err(anyhow::anyhow!("watch 失败重试间隔必须大于 0"));
|
||||
}
|
||||
if options.run_count.is_some_and(|count| count > 1)
|
||||
&& !options.interval_explicit
|
||||
&& matches!(
|
||||
options.command,
|
||||
CliCommand::Pull
|
||||
| CliCommand::Parse
|
||||
| CliCommand::Translate
|
||||
| CliCommand::PublishLocalized
|
||||
)
|
||||
{
|
||||
return Err(anyhow::anyhow!(
|
||||
"--run-count 大于 1 时必须显式指定 --interval"
|
||||
));
|
||||
}
|
||||
if matches!(
|
||||
options.command,
|
||||
CliCommand::Pull | CliCommand::Parse | CliCommand::Translate | CliCommand::PublishLocalized
|
||||
) {
|
||||
if options.watch && options.run_count.is_some() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"--watch 与 --run-count 不能同时指定;周期执行请使用 --watch"
|
||||
));
|
||||
}
|
||||
if options.interval_explicit && !options.watch && options.run_count.unwrap_or(1) <= 1 {
|
||||
return Err(anyhow::anyhow!(
|
||||
"--interval 需要配合 --watch 或 --run-count 大于 1"
|
||||
));
|
||||
}
|
||||
}
|
||||
if (options.watch || options.daemon || options.daemon_child)
|
||||
&& !options.quiet_up_to_date_explicit
|
||||
{
|
||||
@@ -6818,6 +7245,109 @@ fn parse_args_with_env(
|
||||
Ok(options)
|
||||
}
|
||||
|
||||
fn parse_resource_command(
|
||||
args: &mut impl Iterator<Item = String>,
|
||||
options: &mut CliOptions,
|
||||
) -> anyhow::Result<()> {
|
||||
let action = next_option_value(args, "resource")?;
|
||||
match action.as_str() {
|
||||
"pull" => {
|
||||
ensure_command_not_set(options.command, "resource pull")?;
|
||||
options.command = CliCommand::Pull;
|
||||
}
|
||||
"refresh" => {
|
||||
ensure_command_not_set(options.command, "resource refresh")?;
|
||||
options.command = CliCommand::Refresh;
|
||||
}
|
||||
"verify" => {
|
||||
ensure_command_not_set(options.command, "resource verify")?;
|
||||
options.command = CliCommand::Verify;
|
||||
}
|
||||
"repair" => {
|
||||
ensure_command_not_set(options.command, "resource repair")?;
|
||||
options.command = CliCommand::Repair;
|
||||
}
|
||||
"status" => {
|
||||
ensure_command_not_set(options.command, "resource status")?;
|
||||
options.command = CliCommand::ResourceIndex;
|
||||
}
|
||||
"index" => {
|
||||
ensure_command_not_set(options.command, "resource index")?;
|
||||
options.command = CliCommand::ResourceIndex;
|
||||
}
|
||||
"schedule" => {
|
||||
options.schedule_group = Some("res".to_string());
|
||||
parse_schedule_command(args, options)?
|
||||
}
|
||||
other => return Err(anyhow::anyhow!("未知 resource 二级命令:{other}")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_parse_command(
|
||||
args: &mut impl Iterator<Item = String>,
|
||||
options: &mut CliOptions,
|
||||
) -> anyhow::Result<()> {
|
||||
let action = next_option_value(args, "parse")?;
|
||||
let command = match action.as_str() {
|
||||
"run" => CliCommand::Parse,
|
||||
"status" => CliCommand::ParseStatus,
|
||||
"text-units" => CliCommand::ParseTextUnits,
|
||||
"errors" => CliCommand::ParseErrors,
|
||||
"schedule" => {
|
||||
options.schedule_group = Some("parse".to_string());
|
||||
return parse_schedule_command(args, options);
|
||||
}
|
||||
"repack" => CliCommand::Repack,
|
||||
other => return Err(anyhow::anyhow!("未知 parse 二级命令:{other}")),
|
||||
};
|
||||
ensure_command_not_set(options.command, &format!("parse {action}"))?;
|
||||
options.command = command;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_translation_command(
|
||||
args: &mut impl Iterator<Item = String>,
|
||||
options: &mut CliOptions,
|
||||
) -> anyhow::Result<()> {
|
||||
let action = next_option_value(args, "translation")?;
|
||||
let command = match action.as_str() {
|
||||
"run" => CliCommand::Translate,
|
||||
"export" => CliCommand::Translate,
|
||||
"set" => CliCommand::TranslationSet,
|
||||
"publish" => CliCommand::PublishLocalized,
|
||||
"tasks" => CliCommand::TranslationTasks,
|
||||
"handoff" => CliCommand::TranslationHandoff,
|
||||
"status" => CliCommand::LocalizedStatus,
|
||||
"schedule" => {
|
||||
options.schedule_group = Some("i18n".to_string());
|
||||
return parse_schedule_command(args, options);
|
||||
}
|
||||
other => return Err(anyhow::anyhow!("未知 translation 二级命令:{other}")),
|
||||
};
|
||||
ensure_command_not_set(options.command, &format!("translation {action}"))?;
|
||||
options.command = command;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_schedule_command(
|
||||
args: &mut impl Iterator<Item = String>,
|
||||
options: &mut CliOptions,
|
||||
) -> anyhow::Result<()> {
|
||||
let action = next_option_value(args, "schedule")?;
|
||||
let command = match action.as_str() {
|
||||
"list" => CliCommand::ScheduleList,
|
||||
"add" => CliCommand::ScheduleAdd,
|
||||
"update" => CliCommand::ScheduleUpdate,
|
||||
"remove" | "delete" => CliCommand::ScheduleRemove,
|
||||
"run" => CliCommand::ScheduleRun,
|
||||
other => return Err(anyhow::anyhow!("未知 schedule 二级命令:{other}")),
|
||||
};
|
||||
ensure_command_not_set(options.command, &format!("schedule {action}"))?;
|
||||
options.command = command;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_command_not_set(command: CliCommand, next: &str) -> anyhow::Result<()> {
|
||||
if command == CliCommand::Run {
|
||||
Ok(())
|
||||
@@ -6902,6 +7432,15 @@ fn print_usage(binary: &str) {
|
||||
eprintln!(" {binary} <COMMAND> [OPTIONS]");
|
||||
eprintln!();
|
||||
eprintln!("Commands:");
|
||||
eprintln!(" res pull Pull official resources once or repeatedly");
|
||||
eprintln!(" res schedule Manage resource pull schedules (CLI/RPC/dashboard)");
|
||||
eprintln!(" parse run Parse current official release");
|
||||
eprintln!(" parse repack Repack a UnityFS bundle from a JSON spec");
|
||||
eprintln!(" i18n run Refresh offline translation work");
|
||||
eprintln!(" i18n export Export an editable translation workbench");
|
||||
eprintln!(" i18n set Update one translation workbench entry");
|
||||
eprintln!(" i18n publish Publish a localized release");
|
||||
eprintln!(" i18n schedule Manage translation schedules");
|
||||
eprintln!(" refresh Run one update check, or ask a live daemon to refresh");
|
||||
eprintln!(" verify Verify remote plan, local manifest, and official seed hashes");
|
||||
eprintln!(" repair Redownload resources that fail local verification");
|
||||
@@ -6930,6 +7469,10 @@ fn print_usage(binary: &str) {
|
||||
eprintln!(" {binary} --auto-discover --dry-run");
|
||||
eprintln!(" {binary} --auto-discover --watch");
|
||||
eprintln!(" {binary} --auto-discover --daemon");
|
||||
eprintln!(" {binary} res pull --auto-discover --run-count 3 --interval 1h");
|
||||
eprintln!(" {binary} parse run --force --resource-root /tmp/bat-release");
|
||||
eprintln!(" {binary} i18n export --translation-file /tmp/bat-workbench.json");
|
||||
eprintln!(" {binary} i18n publish --translation-file /tmp/bat-workbench.json --force");
|
||||
eprintln!(" {binary} status");
|
||||
eprintln!(" {binary} refresh --force --json");
|
||||
eprintln!();
|
||||
@@ -6971,6 +7514,15 @@ fn print_usage(binary: &str) {
|
||||
eprintln!(" --force Force download/refresh");
|
||||
eprintln!(" --audit-local | --no-audit-local Enable/disable local manifest audit");
|
||||
eprintln!(" --repair | --no-repair Enable/disable automatic repair");
|
||||
eprintln!(" --run-count <N> Run pull/parse/translate/publish N times");
|
||||
eprintln!(" --once Explicitly select one run");
|
||||
eprintln!(" --resource-root <DIR> Use an explicit published official release root");
|
||||
eprintln!(" --translation-file <PATH> Translation workbench JSON file");
|
||||
eprintln!(" --translation-id <ID> TextUnit ID for i18n set");
|
||||
eprintln!(" --translated-text <TEXT> Inline translation for i18n set");
|
||||
eprintln!(" --translated-file <PATH> UTF-8 translation file for i18n set");
|
||||
eprintln!(" --localized-release-id <ID> Explicit localized publication ID");
|
||||
eprintln!(" --repack-spec <PATH> UnityFS batch repack JSON spec");
|
||||
eprintln!();
|
||||
eprintln!("Read-only queries:");
|
||||
eprintln!(" --offset <N> Query offset for resource-index/parse-text-units/parse-errors/translation-tasks");
|
||||
@@ -7031,6 +7583,16 @@ fn print_usage(binary: &str) {
|
||||
eprintln!(" --quiet-up-to-date Suppress clean up-to-date reports");
|
||||
eprintln!(" --no-quiet-up-to-date Always print reports");
|
||||
eprintln!(" --tail <N> Log lines for logs command (default: 200)");
|
||||
eprintln!(" --schedule-id <ID> Schedule identifier");
|
||||
eprintln!(" --schedule-action <ACTION> Schedule action (pull/run/repack/publish)");
|
||||
eprintln!(" --schedule-at-unix <SECONDS> First execution time");
|
||||
eprintln!(" --schedule-delay <DURATION> Delay first execution from now");
|
||||
eprintln!(" --schedule-every <DURATION> Period between executions");
|
||||
eprintln!(" --schedule-count <N> Bounded execution count");
|
||||
eprintln!(" --schedule-arg <ARG> Argument passed to scheduled child command");
|
||||
eprintln!(" --schedule-clear-args Clear args during schedule update");
|
||||
eprintln!(" --schedule-clear-every Convert a periodic plan to one-shot");
|
||||
eprintln!(" --schedule-enabled/--schedule-disabled Enable/disable a schedule");
|
||||
eprintln!();
|
||||
eprintln!("Output:");
|
||||
eprintln!(" --human Human-readable output (default)");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::*;
|
||||
use crate::app::schedule_commands::read_schedule_file;
|
||||
|
||||
fn parse(values: &[&str]) -> anyhow::Result<CliOptions> {
|
||||
parse_args_from(values.iter().map(|value| value.to_string()))
|
||||
@@ -136,6 +137,254 @@ fn env_invalid_values_error() {
|
||||
assert!(parse(&["bat", "--download-concurrency", "257"]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grouped_workflow_commands_use_short_top_level_aliases() {
|
||||
let options = parse(&[
|
||||
"bat",
|
||||
"res",
|
||||
"pull",
|
||||
"--run-count",
|
||||
"3",
|
||||
"--interval",
|
||||
"10s",
|
||||
"--download-concurrency",
|
||||
"8",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(options.command, CliCommand::Pull);
|
||||
assert_eq!(options.run_count, Some(3));
|
||||
assert_eq!(options.interval, Duration::from_secs(10));
|
||||
assert!(options.interval_explicit);
|
||||
|
||||
let options = parse(&[
|
||||
"bat",
|
||||
"parse",
|
||||
"run",
|
||||
"--resource-root",
|
||||
"/tmp/official-release",
|
||||
"--force",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(options.command, CliCommand::Parse);
|
||||
assert_eq!(
|
||||
options.resource_root,
|
||||
Some(PathBuf::from("/tmp/official-release"))
|
||||
);
|
||||
assert!(options.config.force);
|
||||
|
||||
let options = parse(&[
|
||||
"bat",
|
||||
"i18n",
|
||||
"set",
|
||||
"--translation-file",
|
||||
"/tmp/workbench.json",
|
||||
"--translation-id",
|
||||
"unit-1",
|
||||
"--translated-text",
|
||||
"你好",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(options.command, CliCommand::TranslationSet);
|
||||
assert_eq!(
|
||||
options.translation_file,
|
||||
Some(PathBuf::from("/tmp/workbench.json"))
|
||||
);
|
||||
assert_eq!(options.translation_id.as_deref(), Some("unit-1"));
|
||||
|
||||
let options = parse(&[
|
||||
"bat",
|
||||
"parse",
|
||||
"repack",
|
||||
"--repack-spec",
|
||||
"/tmp/repack.json",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(options.command, CliCommand::Repack);
|
||||
assert_eq!(options.repack_spec, Some(PathBuf::from("/tmp/repack.json")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grouped_command_long_aliases_and_schedule_options_are_accepted() {
|
||||
let options = parse(&[
|
||||
"bat",
|
||||
"resources",
|
||||
"schedule",
|
||||
"add",
|
||||
"--schedule-id",
|
||||
"nightly-pull",
|
||||
"--schedule-action",
|
||||
"pull",
|
||||
"--schedule-delay",
|
||||
"5m",
|
||||
"--schedule-every",
|
||||
"1h",
|
||||
"--schedule-count",
|
||||
"4",
|
||||
"--schedule-arg",
|
||||
"--auto-discover",
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(options.command, CliCommand::ScheduleAdd);
|
||||
assert_eq!(options.schedule_group.as_deref(), Some("res"));
|
||||
assert_eq!(options.schedule_id.as_deref(), Some("nightly-pull"));
|
||||
assert_eq!(options.schedule_action.as_deref(), Some("pull"));
|
||||
assert_eq!(options.schedule_delay, Some(Duration::from_secs(300)));
|
||||
assert_eq!(options.schedule_every, Some(Duration::from_secs(3600)));
|
||||
assert_eq!(options.schedule_count, Some(4));
|
||||
assert_eq!(options.schedule_args, vec!["--auto-discover"]);
|
||||
|
||||
let options = parse(&["bat", "translate", "schedule", "run", "--force"]).unwrap();
|
||||
assert_eq!(options.command, CliCommand::ScheduleRun);
|
||||
assert_eq!(options.schedule_group.as_deref(), Some("i18n"));
|
||||
assert!(options.config.force);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_workflow_requires_explicit_interval_after_first_run() {
|
||||
assert!(parse(&["bat", "parse", "run", "--run-count", "2"]).is_err());
|
||||
assert!(parse(&["bat", "parse", "run", "--run-count", "0"]).is_err());
|
||||
assert!(parse(&["bat", "parse", "run", "--watch", "--run-count", "2"]).is_err());
|
||||
assert!(parse(&["bat", "parse", "run", "--interval", "1m"]).is_err());
|
||||
assert!(parse(&[
|
||||
"bat",
|
||||
"parse",
|
||||
"run",
|
||||
"--run-count",
|
||||
"2",
|
||||
"--interval",
|
||||
"1m",
|
||||
])
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schedule_crud_persists_and_updates_a_workflow_plan() {
|
||||
let temp = tempfile::TempDir::new().unwrap();
|
||||
let state_dir = temp.path().to_string_lossy().to_string();
|
||||
let add = parse(&[
|
||||
"bat",
|
||||
"res",
|
||||
"schedule",
|
||||
"add",
|
||||
"--state-dir",
|
||||
&state_dir,
|
||||
"--schedule-id",
|
||||
"pull-once",
|
||||
"--schedule-action",
|
||||
"pull",
|
||||
"--schedule-delay",
|
||||
"1s",
|
||||
"--schedule-every",
|
||||
"1h",
|
||||
"--schedule-count",
|
||||
"2",
|
||||
"--schedule-arg",
|
||||
"--auto-discover",
|
||||
])
|
||||
.unwrap();
|
||||
run_schedule_add(&add).unwrap();
|
||||
let file = read_schedule_file(temp.path()).unwrap();
|
||||
assert_eq!(file.schedules.len(), 1);
|
||||
assert_eq!(file.schedules[0].id, "pull-once");
|
||||
assert_eq!(file.schedules[0].remaining_runs, Some(2));
|
||||
assert_eq!(file.schedules[0].interval_seconds, Some(3600));
|
||||
assert_eq!(file.schedules[0].args, vec!["--auto-discover"]);
|
||||
|
||||
let update = parse(&[
|
||||
"bat",
|
||||
"res",
|
||||
"schedule",
|
||||
"update",
|
||||
"--state-dir",
|
||||
&state_dir,
|
||||
"--schedule-id",
|
||||
"pull-once",
|
||||
"--schedule-disabled",
|
||||
"--schedule-every",
|
||||
"1h",
|
||||
])
|
||||
.unwrap();
|
||||
run_schedule_update(&update).unwrap();
|
||||
let file = read_schedule_file(temp.path()).unwrap();
|
||||
assert!(!file.schedules[0].enabled);
|
||||
assert_eq!(file.schedules[0].interval_seconds, Some(3600));
|
||||
|
||||
let clear_every = parse(&[
|
||||
"bat",
|
||||
"res",
|
||||
"schedule",
|
||||
"update",
|
||||
"--state-dir",
|
||||
&state_dir,
|
||||
"--schedule-id",
|
||||
"pull-once",
|
||||
"--schedule-clear-every",
|
||||
"--schedule-count",
|
||||
"1",
|
||||
])
|
||||
.unwrap();
|
||||
run_schedule_update(&clear_every).unwrap();
|
||||
let file = read_schedule_file(temp.path()).unwrap();
|
||||
assert_eq!(file.schedules[0].interval_seconds, None);
|
||||
assert_eq!(file.schedules[0].remaining_runs, Some(1));
|
||||
|
||||
let remove = parse(&[
|
||||
"bat",
|
||||
"res",
|
||||
"schedule",
|
||||
"remove",
|
||||
"--state-dir",
|
||||
&state_dir,
|
||||
"--schedule-id",
|
||||
"pull-once",
|
||||
])
|
||||
.unwrap();
|
||||
run_schedule_remove(&remove).unwrap();
|
||||
assert!(read_schedule_file(temp.path())
|
||||
.unwrap()
|
||||
.schedules
|
||||
.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dispatch_schedule_crud_uses_shared_state_file() {
|
||||
let temp = tempfile::TempDir::new().unwrap();
|
||||
let state_dir = temp.path();
|
||||
let control = new_daemon_control();
|
||||
|
||||
let add = dispatch_rpc_method(
|
||||
&rpc_request(
|
||||
"schedule.add",
|
||||
Some(serde_json::json!({
|
||||
"id": "rpc-pull",
|
||||
"group": "res",
|
||||
"action": "pull",
|
||||
"delay_seconds": 60,
|
||||
"every_seconds": 3600,
|
||||
"count": 2
|
||||
})),
|
||||
),
|
||||
state_dir,
|
||||
&control,
|
||||
&test_task_context(),
|
||||
"req-schedule-add".to_string(),
|
||||
);
|
||||
let add = serde_json::to_value(add).unwrap();
|
||||
assert_eq!(add["ok"], true);
|
||||
assert_eq!(add["data"]["schedule"]["id"], "rpc-pull");
|
||||
|
||||
let list = dispatch_rpc_method(
|
||||
&rpc_request("schedule.list", None),
|
||||
state_dir,
|
||||
&control,
|
||||
&test_task_context(),
|
||||
"req-schedule-list".to_string(),
|
||||
);
|
||||
let list = serde_json::to_value(list).unwrap();
|
||||
assert_eq!(list["ok"], true);
|
||||
assert_eq!(list["data"]["schedules"][0]["id"], "rpc-pull");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cli_download_concurrency_is_preserved_for_daemon_child() {
|
||||
let options = parse(&["bat", "--download-concurrency", "4"]).unwrap();
|
||||
|
||||
@@ -0,0 +1,689 @@
|
||||
use super::*;
|
||||
|
||||
const SCHEDULES_FILE_NAME: &str = "bat-schedules.json";
|
||||
const SCHEDULE_LOCK_FILE_NAME: &str = "bat-schedule.lock";
|
||||
const SCHEDULES_SCHEMA_VERSION: u32 = 1;
|
||||
static SCHEDULE_FILE_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ScheduleFileLock {
|
||||
path: PathBuf,
|
||||
pid: u32,
|
||||
}
|
||||
|
||||
impl ScheduleFileLock {
|
||||
fn acquire(state_dir: &Path) -> anyhow::Result<Self> {
|
||||
validate_runtime_state_dir(state_dir).map_err(anyhow::Error::msg)?;
|
||||
fs::create_dir_all(state_dir)?;
|
||||
let path = state_dir.join(SCHEDULE_LOCK_FILE_NAME);
|
||||
let pid = std::process::id();
|
||||
for attempt in 0..=1 {
|
||||
let mut options = OpenOptions::new();
|
||||
options.write(true).create_new(true);
|
||||
#[cfg(unix)]
|
||||
options.mode(PRIVATE_FILE_MODE);
|
||||
match options.open(&path) {
|
||||
Ok(mut file) => {
|
||||
file.write_all(pid.to_string().as_bytes())?;
|
||||
return Ok(Self { path, pid });
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
|
||||
if attempt == 0 && remove_recoverable_pid_lock(&path)? {
|
||||
continue;
|
||||
}
|
||||
return Err(anyhow::anyhow!(
|
||||
"调度计划已被锁定:{};{}",
|
||||
path.display(),
|
||||
describe_pid_lock_owner(&path)?
|
||||
));
|
||||
}
|
||||
Err(error) => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"获取调度计划锁失败 {}:{error}",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(anyhow::anyhow!("获取调度计划锁失败"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ScheduleFileLock {
|
||||
fn drop(&mut self) {
|
||||
let expected = self.pid.to_string();
|
||||
if fs::symlink_metadata(&self.path)
|
||||
.map(|metadata| metadata.file_type().is_symlink())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if fs::read_to_string(&self.path)
|
||||
.map(|contents| contents.trim() == expected)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
let _ = fs::remove_file(&self.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub(super) struct ScheduleFile {
|
||||
pub(super) schema_version: u32,
|
||||
pub(super) schedules: Vec<ScheduleEntry>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub(super) struct ScheduleEntry {
|
||||
pub(super) id: String,
|
||||
pub(super) group: String,
|
||||
pub(super) action: String,
|
||||
pub(super) args: Vec<String>,
|
||||
pub(super) next_run_unix_seconds: u64,
|
||||
pub(super) interval_seconds: Option<u64>,
|
||||
pub(super) remaining_runs: Option<usize>,
|
||||
pub(super) enabled: bool,
|
||||
pub(super) created_unix_seconds: u64,
|
||||
pub(super) updated_unix_seconds: u64,
|
||||
pub(super) last_run_unix_seconds: Option<u64>,
|
||||
pub(super) last_status: Option<String>,
|
||||
pub(super) last_error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub(super) struct ScheduleMutationRequest {
|
||||
#[serde(default, alias = "schedule_id")]
|
||||
pub(super) id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) group: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) action: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) args: Vec<String>,
|
||||
#[serde(default, alias = "at_unix_seconds", alias = "schedule_at_unix")]
|
||||
pub(super) next_run_unix_seconds: Option<u64>,
|
||||
#[serde(default, alias = "schedule_delay_seconds")]
|
||||
pub(super) delay_seconds: Option<u64>,
|
||||
#[serde(default, alias = "schedule_every_seconds")]
|
||||
pub(super) every_seconds: Option<u64>,
|
||||
#[serde(default, alias = "schedule_count")]
|
||||
pub(super) count: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub(super) clear_args: bool,
|
||||
#[serde(default)]
|
||||
pub(super) clear_every: bool,
|
||||
#[serde(default)]
|
||||
pub(super) enabled: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub(super) struct ScheduleRunRequest {
|
||||
#[serde(default, alias = "schedule_id")]
|
||||
pub(super) id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) force: bool,
|
||||
}
|
||||
|
||||
pub(super) fn run_schedule_list(options: &CliOptions) -> anyhow::Result<()> {
|
||||
print_json_value(
|
||||
options.output_format,
|
||||
&schedule_list_report(&options.state_dir)?,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_schedule_add(options: &CliOptions) -> anyhow::Result<()> {
|
||||
validate_schedule_command_options(options, false)?;
|
||||
let request = schedule_request_from_options(options);
|
||||
print_json_value(
|
||||
options.output_format,
|
||||
&schedule_add_report(&options.state_dir, request)?,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_schedule_update(options: &CliOptions) -> anyhow::Result<()> {
|
||||
validate_schedule_command_options(options, true)?;
|
||||
let request = schedule_request_from_options(options);
|
||||
print_json_value(
|
||||
options.output_format,
|
||||
&schedule_update_report(&options.state_dir, request)?,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_schedule_remove(options: &CliOptions) -> anyhow::Result<()> {
|
||||
validate_schedule_command_options(options, true)?;
|
||||
let request = schedule_request_from_options(options);
|
||||
print_json_value(
|
||||
options.output_format,
|
||||
&schedule_remove_report(&options.state_dir, request)?,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_schedule_run(options: &CliOptions) -> anyhow::Result<()> {
|
||||
validate_schedule_command_options(options, true)?;
|
||||
loop {
|
||||
let request = ScheduleRunRequest {
|
||||
id: options.schedule_id.clone(),
|
||||
force: options.config.force,
|
||||
};
|
||||
print_json_value(
|
||||
options.output_format,
|
||||
&schedule_run_report(&options.state_dir, request)?,
|
||||
)?;
|
||||
if !options.watch {
|
||||
return Ok(());
|
||||
}
|
||||
thread::sleep(options.interval);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn schedule_list_report(state_dir: &Path) -> anyhow::Result<serde_json::Value> {
|
||||
let _guard = SCHEDULE_FILE_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|poison| poison.into_inner());
|
||||
let _schedule_lock = ScheduleFileLock::acquire(state_dir)?;
|
||||
let file = read_schedule_file(state_dir)?;
|
||||
Ok(serde_json::json!({
|
||||
"command": "schedule-list",
|
||||
"status": "ok",
|
||||
"state_file": schedule_file_path(state_dir),
|
||||
"schedules": file.schedules,
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) fn schedule_add_report(
|
||||
state_dir: &Path,
|
||||
request: ScheduleMutationRequest,
|
||||
) -> anyhow::Result<serde_json::Value> {
|
||||
let _guard = SCHEDULE_FILE_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|poison| poison.into_inner());
|
||||
let _schedule_lock = ScheduleFileLock::acquire(state_dir)?;
|
||||
let mut file = read_schedule_file(state_dir)?;
|
||||
let id = request
|
||||
.id
|
||||
.as_deref()
|
||||
.ok_or_else(|| anyhow::anyhow!("schedule add 必须指定 --schedule-id"))?;
|
||||
if file.schedules.iter().any(|entry| entry.id == id) {
|
||||
return Err(anyhow::anyhow!("schedule 已存在:{id}"));
|
||||
}
|
||||
let now = unix_seconds_now();
|
||||
let entry = build_schedule_entry(&request, now)?;
|
||||
file.schedules.push(entry.clone());
|
||||
write_schedule_file(state_dir, &file)?;
|
||||
Ok(schedule_result_value(
|
||||
state_dir,
|
||||
"schedule-add",
|
||||
"created",
|
||||
&entry,
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn schedule_update_report(
|
||||
state_dir: &Path,
|
||||
request: ScheduleMutationRequest,
|
||||
) -> anyhow::Result<serde_json::Value> {
|
||||
let _guard = SCHEDULE_FILE_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|poison| poison.into_inner());
|
||||
let _schedule_lock = ScheduleFileLock::acquire(state_dir)?;
|
||||
validate_schedule_mutation(&request, true)?;
|
||||
let id = request
|
||||
.id
|
||||
.as_deref()
|
||||
.ok_or_else(|| anyhow::anyhow!("schedule update 必须指定 --schedule-id"))?;
|
||||
let mut file = read_schedule_file(state_dir)?;
|
||||
let entry = file
|
||||
.schedules
|
||||
.iter_mut()
|
||||
.find(|entry| entry.id == id)
|
||||
.ok_or_else(|| anyhow::anyhow!("schedule 不存在:{id}"))?;
|
||||
if let Some(group) = request
|
||||
.group
|
||||
.as_deref()
|
||||
.map(normalize_schedule_group)
|
||||
.transpose()?
|
||||
{
|
||||
if group != entry.group {
|
||||
return Err(anyhow::anyhow!(
|
||||
"schedule {} 属于 {},不能从 {} 二级命令更新",
|
||||
id,
|
||||
entry.group,
|
||||
group
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(action) = request.action.as_deref() {
|
||||
validate_schedule_action(entry.group.as_str(), action)?;
|
||||
entry.action = action.to_string();
|
||||
}
|
||||
if let Some(at) = request.next_run_unix_seconds {
|
||||
entry.next_run_unix_seconds = at;
|
||||
}
|
||||
if let Some(delay) = request.delay_seconds {
|
||||
entry.next_run_unix_seconds = unix_seconds_now().saturating_add(delay);
|
||||
}
|
||||
if let Some(every) = request.every_seconds {
|
||||
entry.interval_seconds = Some(nonzero_seconds(
|
||||
Duration::from_secs(every),
|
||||
"--schedule-every",
|
||||
)?);
|
||||
}
|
||||
if request.clear_every {
|
||||
entry.interval_seconds = None;
|
||||
}
|
||||
if let Some(count) = request.count {
|
||||
entry.remaining_runs = Some(count);
|
||||
}
|
||||
if request.clear_args {
|
||||
entry.args.clear();
|
||||
}
|
||||
if !request.args.is_empty() {
|
||||
validate_schedule_args(&request.args)?;
|
||||
entry.args = request.args.clone();
|
||||
}
|
||||
if let Some(enabled) = request.enabled {
|
||||
entry.enabled = enabled;
|
||||
}
|
||||
if entry.interval_seconds.is_none() && request.clear_every && request.count.is_none() {
|
||||
entry.remaining_runs = Some(1);
|
||||
}
|
||||
validate_schedule_entry_shape(entry)?;
|
||||
entry.updated_unix_seconds = unix_seconds_now();
|
||||
let updated = entry.clone();
|
||||
write_schedule_file(state_dir, &file)?;
|
||||
Ok(schedule_result_value(
|
||||
state_dir,
|
||||
"schedule-update",
|
||||
"updated",
|
||||
&updated,
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn schedule_remove_report(
|
||||
state_dir: &Path,
|
||||
request: ScheduleMutationRequest,
|
||||
) -> anyhow::Result<serde_json::Value> {
|
||||
let _guard = SCHEDULE_FILE_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|poison| poison.into_inner());
|
||||
let _schedule_lock = ScheduleFileLock::acquire(state_dir)?;
|
||||
let id = request
|
||||
.id
|
||||
.as_deref()
|
||||
.ok_or_else(|| anyhow::anyhow!("schedule remove 必须指定 --schedule-id"))?;
|
||||
let mut file = read_schedule_file(state_dir)?;
|
||||
let before = file.schedules.len();
|
||||
file.schedules.retain(|entry| entry.id != id);
|
||||
if file.schedules.len() == before {
|
||||
return Err(anyhow::anyhow!("schedule 不存在:{id}"));
|
||||
}
|
||||
write_schedule_file(state_dir, &file)?;
|
||||
Ok(serde_json::json!({
|
||||
"command": "schedule-remove",
|
||||
"status": "removed",
|
||||
"id": id,
|
||||
"state_file": schedule_file_path(state_dir),
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) fn schedule_run_report(
|
||||
state_dir: &Path,
|
||||
request: ScheduleRunRequest,
|
||||
) -> anyhow::Result<serde_json::Value> {
|
||||
let _guard = SCHEDULE_FILE_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|poison| poison.into_inner());
|
||||
let _schedule_lock = ScheduleFileLock::acquire(state_dir)?;
|
||||
let now = unix_seconds_now();
|
||||
let selected_id = request.id.as_deref();
|
||||
let mut file = read_schedule_file(state_dir)?;
|
||||
let mut results = Vec::new();
|
||||
for index in 0..file.schedules.len() {
|
||||
let due = {
|
||||
let entry = &file.schedules[index];
|
||||
entry.enabled
|
||||
&& (request.force || entry.next_run_unix_seconds <= now)
|
||||
&& selected_id.is_none_or(|id| id == entry.id)
|
||||
};
|
||||
if !due {
|
||||
continue;
|
||||
}
|
||||
let entry = &mut file.schedules[index];
|
||||
let id = entry.id.clone();
|
||||
let command = schedule_child_command(entry, state_dir);
|
||||
let started = unix_seconds_now();
|
||||
if let Some(remaining) = entry.remaining_runs.as_mut() {
|
||||
*remaining = remaining.saturating_sub(1);
|
||||
}
|
||||
entry.last_run_unix_seconds = Some(started);
|
||||
entry.updated_unix_seconds = started;
|
||||
entry.enabled = entry.remaining_runs != Some(0);
|
||||
entry.next_run_unix_seconds = entry
|
||||
.interval_seconds
|
||||
.map(|seconds| started.saturating_add(seconds))
|
||||
.unwrap_or(started);
|
||||
write_schedule_file(state_dir, &file)?;
|
||||
|
||||
let status = Command::new(&command[0]).args(&command[1..]).status();
|
||||
let (status_label, error) = match status {
|
||||
Ok(status) if status.success() => ("completed".to_string(), None),
|
||||
Ok(status) => (
|
||||
"failed".to_string(),
|
||||
Some(format!("子命令退出码:{}", status.code().unwrap_or(-1))),
|
||||
),
|
||||
Err(error) => ("failed".to_string(), Some(error.to_string())),
|
||||
};
|
||||
let (next_run_unix_seconds, enabled) = {
|
||||
let entry = &mut file.schedules[index];
|
||||
entry.last_status = Some(status_label.clone());
|
||||
entry.last_error = error.clone();
|
||||
entry.updated_unix_seconds = unix_seconds_now();
|
||||
(entry.next_run_unix_seconds, entry.enabled)
|
||||
};
|
||||
write_schedule_file(state_dir, &file)?;
|
||||
results.push(serde_json::json!({
|
||||
"id": id,
|
||||
"command": command,
|
||||
"status": status_label,
|
||||
"error": error,
|
||||
"next_run_unix_seconds": next_run_unix_seconds,
|
||||
"enabled": enabled,
|
||||
}));
|
||||
}
|
||||
if selected_id.is_some() && results.is_empty() {
|
||||
let status = match file
|
||||
.schedules
|
||||
.iter()
|
||||
.find(|entry| Some(entry.id.as_str()) == selected_id)
|
||||
{
|
||||
None => "not_found",
|
||||
Some(entry) if !entry.enabled => "disabled",
|
||||
Some(_) => "not_due",
|
||||
};
|
||||
return Ok(serde_json::json!({
|
||||
"command": "schedule-run",
|
||||
"status": status,
|
||||
"now_unix_seconds": now,
|
||||
"executed": [],
|
||||
}));
|
||||
}
|
||||
Ok(serde_json::json!({
|
||||
"command": "schedule-run",
|
||||
"status": "completed",
|
||||
"now_unix_seconds": now,
|
||||
"executed": results,
|
||||
}))
|
||||
}
|
||||
|
||||
fn build_schedule_entry(
|
||||
request: &ScheduleMutationRequest,
|
||||
now: u64,
|
||||
) -> anyhow::Result<ScheduleEntry> {
|
||||
validate_schedule_mutation(request, false)?;
|
||||
let group = request
|
||||
.group
|
||||
.as_deref()
|
||||
.map(normalize_schedule_group)
|
||||
.transpose()?
|
||||
.ok_or_else(|| anyhow::anyhow!("schedule 命令缺少所属一级命令"))?;
|
||||
let action = request
|
||||
.action
|
||||
.as_deref()
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| default_schedule_action(&group).to_string());
|
||||
validate_schedule_action(&group, &action)?;
|
||||
validate_schedule_args(&request.args)?;
|
||||
let next_run = schedule_next_run(request, now)?;
|
||||
let interval_seconds = request
|
||||
.every_seconds
|
||||
.map(|value| nonzero_seconds(Duration::from_secs(value), "--schedule-every"))
|
||||
.transpose()?;
|
||||
let remaining_runs = request
|
||||
.count
|
||||
.or_else(|| interval_seconds.is_none().then_some(1));
|
||||
if interval_seconds.is_none() && remaining_runs.is_some_and(|count| count > 1) {
|
||||
return Err(anyhow::anyhow!(
|
||||
"--schedule-count 大于 1 时必须指定 --schedule-every"
|
||||
));
|
||||
}
|
||||
Ok(ScheduleEntry {
|
||||
id: request
|
||||
.id
|
||||
.clone()
|
||||
.ok_or_else(|| anyhow::anyhow!("schedule add 必须指定 --schedule-id"))?,
|
||||
group,
|
||||
action,
|
||||
args: request.args.clone(),
|
||||
next_run_unix_seconds: next_run,
|
||||
interval_seconds,
|
||||
remaining_runs,
|
||||
enabled: request.enabled.unwrap_or(true),
|
||||
created_unix_seconds: now,
|
||||
updated_unix_seconds: now,
|
||||
last_run_unix_seconds: None,
|
||||
last_status: None,
|
||||
last_error: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn validate_schedule_mutation(
|
||||
request: &ScheduleMutationRequest,
|
||||
update: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
if request.next_run_unix_seconds.is_some() && request.delay_seconds.is_some() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"next_run_unix_seconds 与 delay_seconds 只能指定一个"
|
||||
));
|
||||
}
|
||||
if request.every_seconds.is_some() && request.clear_every {
|
||||
return Err(anyhow::anyhow!("every_seconds 与 clear_every 只能指定一个"));
|
||||
}
|
||||
if request.count == Some(0) {
|
||||
return Err(anyhow::anyhow!("count 必须大于 0"));
|
||||
}
|
||||
if request.clear_args && !update {
|
||||
return Err(anyhow::anyhow!("clear_args 只适用于 schedule update"));
|
||||
}
|
||||
if request.clear_every && !update {
|
||||
return Err(anyhow::anyhow!("clear_every 只适用于 schedule update"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn schedule_next_run(request: &ScheduleMutationRequest, now: u64) -> anyhow::Result<u64> {
|
||||
match (request.next_run_unix_seconds, request.delay_seconds) {
|
||||
(Some(_), Some(_)) => Err(anyhow::anyhow!(
|
||||
"--schedule-at-unix 与 --schedule-delay 只能指定一个"
|
||||
)),
|
||||
(Some(at), None) => Ok(at),
|
||||
(None, Some(delay)) => Ok(now.saturating_add(delay)),
|
||||
(None, None) => Ok(now),
|
||||
}
|
||||
}
|
||||
|
||||
fn schedule_child_command(entry: &ScheduleEntry, state_dir: &Path) -> Vec<String> {
|
||||
let executable = env::current_exe().unwrap_or_else(|_| PathBuf::from("bat"));
|
||||
let mut command = vec![
|
||||
executable.to_string_lossy().into_owned(),
|
||||
entry.group.clone(),
|
||||
entry.action.clone(),
|
||||
];
|
||||
command.extend(entry.args.iter().cloned());
|
||||
if !entry.args.iter().any(|arg| arg == "--state-dir") {
|
||||
command.push("--state-dir".to_string());
|
||||
command.push(state_dir.to_string_lossy().into_owned());
|
||||
}
|
||||
command.push("--no-banner".to_string());
|
||||
command.push("--no-progress".to_string());
|
||||
command
|
||||
}
|
||||
|
||||
fn validate_schedule_command_options(
|
||||
options: &CliOptions,
|
||||
allow_empty: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
if !allow_empty && options.schedule_group.is_none() {
|
||||
return Err(anyhow::anyhow!("schedule 命令缺少所属一级命令"));
|
||||
}
|
||||
if options.watch && !matches!(options.command, CliCommand::ScheduleRun) {
|
||||
return Err(anyhow::anyhow!("只有 schedule run 支持 --watch"));
|
||||
}
|
||||
if options.interval.is_zero() {
|
||||
return Err(anyhow::anyhow!("schedule 轮询间隔必须大于 0"));
|
||||
}
|
||||
if options.schedule_every.is_some_and(|value| value.is_zero()) {
|
||||
return Err(anyhow::anyhow!("--schedule-every 必须大于 0"));
|
||||
}
|
||||
if options.schedule_delay.is_some_and(|value| value.is_zero()) {
|
||||
return Err(anyhow::anyhow!("--schedule-delay 必须大于 0"));
|
||||
}
|
||||
if options.schedule_count == Some(0) {
|
||||
return Err(anyhow::anyhow!("--schedule-count 必须大于 0"));
|
||||
}
|
||||
if options.schedule_clear_args && !matches!(options.command, CliCommand::ScheduleUpdate) {
|
||||
return Err(anyhow::anyhow!(
|
||||
"--schedule-clear-args 只适用于 schedule update"
|
||||
));
|
||||
}
|
||||
if options.schedule_clear_every && !matches!(options.command, CliCommand::ScheduleUpdate) {
|
||||
return Err(anyhow::anyhow!(
|
||||
"--schedule-clear-every 只适用于 schedule update"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_schedule_action(group: &str, action: &str) -> anyhow::Result<()> {
|
||||
let valid = match group {
|
||||
"res" => matches!(action, "pull" | "refresh" | "verify" | "repair"),
|
||||
"parse" => matches!(action, "run" | "repack"),
|
||||
"i18n" => matches!(action, "run" | "export" | "publish"),
|
||||
_ => false,
|
||||
};
|
||||
if valid {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow::anyhow!(
|
||||
"不支持的 schedule action:group={group}, action={action}"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_schedule_entry_shape(entry: &ScheduleEntry) -> anyhow::Result<()> {
|
||||
if entry.interval_seconds.is_none() && entry.remaining_runs.is_some_and(|count| count > 1) {
|
||||
return Err(anyhow::anyhow!(
|
||||
"非周期 schedule 不能保留多次执行次数;请设置 --schedule-every"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn default_schedule_action(group: &str) -> &'static str {
|
||||
match group {
|
||||
"res" => "pull",
|
||||
"parse" => "run",
|
||||
"i18n" => "run",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_schedule_args(args: &[String]) -> anyhow::Result<()> {
|
||||
if let Some(arg) = args
|
||||
.iter()
|
||||
.find(|arg| arg.starts_with("--schedule-") || matches!(arg.as_str(), "--id" | "--action"))
|
||||
{
|
||||
return Err(anyhow::anyhow!(
|
||||
"schedule 子命令参数不能嵌套调度控制选项:{arg}"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn nonzero_seconds(value: Duration, flag: &str) -> anyhow::Result<u64> {
|
||||
let seconds = value.as_secs();
|
||||
if seconds == 0 {
|
||||
return Err(anyhow::anyhow!("{flag} 必须至少为 1s"));
|
||||
}
|
||||
Ok(seconds)
|
||||
}
|
||||
|
||||
fn schedule_request_from_options(options: &CliOptions) -> ScheduleMutationRequest {
|
||||
ScheduleMutationRequest {
|
||||
id: options.schedule_id.clone(),
|
||||
group: options.schedule_group.clone(),
|
||||
action: options.schedule_action.clone(),
|
||||
args: options.schedule_args.clone(),
|
||||
next_run_unix_seconds: options.schedule_at_unix,
|
||||
delay_seconds: options.schedule_delay.map(|value| value.as_secs()),
|
||||
every_seconds: options.schedule_every.map(|value| value.as_secs()),
|
||||
count: options.schedule_count,
|
||||
clear_args: options.schedule_clear_args,
|
||||
clear_every: options.schedule_clear_every,
|
||||
enabled: options.schedule_enabled,
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_schedule_group(group: &str) -> anyhow::Result<String> {
|
||||
let normalized = match group {
|
||||
"res" | "resource" | "resources" => "res",
|
||||
"parse" => "parse",
|
||||
"i18n" | "tr" | "translation" | "translate" => "i18n",
|
||||
other => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"schedule 不支持的一级命令:{other}(支持 res、parse、i18n)"
|
||||
))
|
||||
}
|
||||
};
|
||||
Ok(normalized.to_string())
|
||||
}
|
||||
|
||||
fn schedule_result_value(
|
||||
state_dir: &Path,
|
||||
command: &'static str,
|
||||
status: &'static str,
|
||||
entry: &ScheduleEntry,
|
||||
) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"command": command,
|
||||
"status": status,
|
||||
"schedule": entry,
|
||||
"state_file": schedule_file_path(state_dir),
|
||||
})
|
||||
}
|
||||
|
||||
fn schedule_file_path(state_dir: &Path) -> PathBuf {
|
||||
state_dir.join(SCHEDULES_FILE_NAME)
|
||||
}
|
||||
|
||||
pub(super) fn read_schedule_file(state_dir: &Path) -> anyhow::Result<ScheduleFile> {
|
||||
let path = schedule_file_path(state_dir);
|
||||
let Some(bytes) = read_file_no_symlink(&path, "调度计划文件").map_err(anyhow::Error::msg)?
|
||||
else {
|
||||
return Ok(ScheduleFile {
|
||||
schema_version: SCHEDULES_SCHEMA_VERSION,
|
||||
schedules: Vec::new(),
|
||||
});
|
||||
};
|
||||
let file: ScheduleFile = serde_json::from_slice(&bytes)?;
|
||||
if file.schema_version != SCHEDULES_SCHEMA_VERSION {
|
||||
return Err(anyhow::anyhow!(
|
||||
"不支持的调度计划 schema:{},当前版本={}",
|
||||
file.schema_version,
|
||||
SCHEDULES_SCHEMA_VERSION
|
||||
));
|
||||
}
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
fn write_schedule_file(state_dir: &Path, file: &ScheduleFile) -> anyhow::Result<()> {
|
||||
validate_runtime_state_dir(state_dir).map_err(anyhow::Error::msg)?;
|
||||
let path = schedule_file_path(state_dir);
|
||||
let bytes = serde_json::to_vec_pretty(file)?;
|
||||
write_file_atomic(
|
||||
&path,
|
||||
&bytes,
|
||||
bat_infrastructure::STATE_FILE_MODE,
|
||||
"调度计划文件",
|
||||
)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
use super::*;
|
||||
|
||||
pub(super) fn run_parse_once(options: &CliOptions) -> anyhow::Result<()> {
|
||||
let (resource_root, release_id) = current_official_release(options)?;
|
||||
let parse_config =
|
||||
OfficialParseConfig::new(&resource_root, options.config.unzip_command.clone())
|
||||
.with_force(options.config.force);
|
||||
let parse_report = OfficialParseCacheService::new()
|
||||
.run(&parse_config)
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
let queue_report =
|
||||
write_official_textunit_queues(&resource_root).map_err(anyhow::Error::msg)?;
|
||||
let data = serde_json::json!({
|
||||
"official_release_id": release_id,
|
||||
"resource_root": resource_root,
|
||||
"forced": options.config.force,
|
||||
"parse": parse_report,
|
||||
"translation_queue": queue_report,
|
||||
});
|
||||
print_report(
|
||||
options.output_format,
|
||||
&CommandReport {
|
||||
command: "parse",
|
||||
status: "completed",
|
||||
message: "官方资源解析已执行",
|
||||
data,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_translate_once(options: &CliOptions) -> anyhow::Result<()> {
|
||||
let (resource_root, release_id) = current_official_release(options)?;
|
||||
let queue = write_official_textunit_queues(&resource_root).map_err(anyhow::Error::msg)?;
|
||||
let exported = options
|
||||
.translation_file
|
||||
.as_ref()
|
||||
.map(|path| {
|
||||
export_translation_workbench(&resource_root, release_id.clone(), path).map(
|
||||
|workbench| {
|
||||
serde_json::json!({
|
||||
"path": path,
|
||||
"entry_count": workbench.entries.len(),
|
||||
})
|
||||
},
|
||||
)
|
||||
})
|
||||
.transpose()?;
|
||||
let data = serde_json::json!({
|
||||
"official_release_id": release_id,
|
||||
"resource_root": resource_root,
|
||||
"queue": queue,
|
||||
"workbench": exported,
|
||||
"provider": "offline",
|
||||
"note": "当前 translate 只生成/刷新离线队列和可编辑工作台,不调用外部翻译 provider",
|
||||
});
|
||||
print_report(
|
||||
options.output_format,
|
||||
&CommandReport {
|
||||
command: "translate",
|
||||
status: "queued",
|
||||
message: "翻译离线队列已刷新",
|
||||
data,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_translation_set(options: &CliOptions) -> anyhow::Result<()> {
|
||||
let path = options
|
||||
.translation_file
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("translation-set 必须指定 --translation-file"))?;
|
||||
let text = match (&options.translation_text, &options.translation_text_file) {
|
||||
(Some(_), Some(_)) => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"--translated-text 与 --translated-file 只能指定一个"
|
||||
))
|
||||
}
|
||||
(Some(text), None) => text.clone(),
|
||||
(None, Some(path)) => String::from_utf8(
|
||||
read_file_no_symlink(path, "翻译文本文件")
|
||||
.map_err(anyhow::Error::msg)?
|
||||
.ok_or_else(|| anyhow::anyhow!("翻译文本文件不存在:{}", path.display()))?,
|
||||
)?,
|
||||
(None, None) => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"translation-set 必须指定 --translated-text 或 --translated-file"
|
||||
))
|
||||
}
|
||||
};
|
||||
let entry_id = options
|
||||
.translation_id
|
||||
.as_deref()
|
||||
.ok_or_else(|| anyhow::anyhow!("translation-set 必须指定 --translation-id"))?;
|
||||
let entry = set_translation(path, entry_id, text)?;
|
||||
let data = serde_json::json!({
|
||||
"translation_file": path,
|
||||
"entry": entry,
|
||||
});
|
||||
print_report(
|
||||
options.output_format,
|
||||
&CommandReport {
|
||||
command: "translation-set",
|
||||
status: "updated",
|
||||
message: "翻译工作台条目已更新",
|
||||
data,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn run_repack(options: &CliOptions) -> anyhow::Result<()> {
|
||||
let spec = options
|
||||
.repack_spec
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("repack 必须指定 --repack-spec"))?;
|
||||
let report = repack_bundle(spec)?;
|
||||
print_report(options.output_format, &report)
|
||||
}
|
||||
|
||||
pub(super) fn run_publish_localized(options: &CliOptions) -> anyhow::Result<()> {
|
||||
let translation_file = options
|
||||
.translation_file
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("publish-localized 必须指定 --translation-file"))?;
|
||||
let (resource_root, official_release_id) = current_official_release(options)?;
|
||||
let workbench = read_translation_workbench(translation_file)?;
|
||||
if workbench.official_release_id != official_release_id {
|
||||
return Err(anyhow::anyhow!(
|
||||
"翻译工作台 release={} 与当前官方 release={} 不一致;请重新导出",
|
||||
workbench.official_release_id,
|
||||
official_release_id
|
||||
));
|
||||
}
|
||||
let expected_root = lexical_absolute(&resource_root).map_err(anyhow::Error::msg)?;
|
||||
if workbench.official_resource_root != expected_root {
|
||||
return Err(anyhow::anyhow!(
|
||||
"翻译工作台资源根目录与当前 release 不一致;请重新导出"
|
||||
));
|
||||
}
|
||||
let patches = localized_text_asset_patches(&resource_root, &workbench)?;
|
||||
let localized_release_id = options.localized_release_id.clone().or_else(|| {
|
||||
options
|
||||
.config
|
||||
.force
|
||||
.then(|| format!("{}-manual-{}", official_release_id, unix_seconds_now()))
|
||||
});
|
||||
let mut config = LocalizedPatchConfig::new(
|
||||
resource_root,
|
||||
options.config.localized_output_root.clone(),
|
||||
official_release_id,
|
||||
patches,
|
||||
)
|
||||
.with_force(options.config.force);
|
||||
if let Some(release_id) = localized_release_id {
|
||||
config = config.with_localized_release_id(release_id);
|
||||
}
|
||||
let report = LocalizedPatchService::new().publish(&config)?;
|
||||
print_report(options.output_format, &report)
|
||||
}
|
||||
|
||||
fn current_official_release(options: &CliOptions) -> anyhow::Result<(PathBuf, String)> {
|
||||
let state = read_version_state(&options.config.version_state_path())?;
|
||||
let resource_root = if let Some(resource_root) = options.resource_root.clone() {
|
||||
lexical_absolute(&resource_root).map_err(anyhow::Error::msg)?
|
||||
} else {
|
||||
state
|
||||
.as_ref()
|
||||
.and_then(|state| state.current_completed_version.as_ref())
|
||||
.map(|record| record.resource_root.clone())
|
||||
.unwrap_or(active_official_resource_root(&options.config.output_root)?)
|
||||
};
|
||||
let release_id = state
|
||||
.as_ref()
|
||||
.and_then(|state| state.current_completed_version.as_ref())
|
||||
.filter(|_| options.resource_root.is_none())
|
||||
.map(|record| record.id.clone())
|
||||
.or_else(|| {
|
||||
resource_root
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_string)
|
||||
})
|
||||
.ok_or_else(|| anyhow::anyhow!("无法从当前官方资源根目录确定 release id"))?;
|
||||
if read_download_manifest_at(&resource_root)
|
||||
.map_err(anyhow::Error::msg)?
|
||||
.is_none()
|
||||
{
|
||||
return Err(anyhow::anyhow!(
|
||||
"当前官方 release 缺少官方下载 manifest:{}",
|
||||
resource_root.display()
|
||||
));
|
||||
}
|
||||
Ok((resource_root, release_id))
|
||||
}
|
||||
Reference in New Issue
Block a user