1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use super::PrepareRespond;
use crate::args::Subs;
use crate::color::Stylize;
use crate::config::Config;
use crate::env_pair::EnvPair;
use crate::error::{Contextable, Error, RedundantOpt, Result};
use crate::extract_msg::extract_env_from_content_help_aware;
use crate::path;
use crate::query::{
    self, do_list_query_with_handler, EditQuery, ListQuery, ListQueryHandler, ScriptQuery,
};
use crate::script::{IntoScriptName, ScriptInfo, ScriptName};
use crate::script_repo::{RepoEntry, ScriptRepo, Visibility};
use crate::script_type::{iter_default_templates, ScriptFullType, ScriptType};
use crate::tag::{Tag, TagSelector};
use fxhash::{FxHashMap as HashMap, FxHashSet as HashSet};
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
use std::process::Command;

pub struct EditTagArgs {
    pub content: TagSelector,
    /// 命令行參數裡帶著 tag 選項,例如 hs edit --tag some-tag edit
    pub explicit_tag: bool,
    /// 命令行參數裡帶著 select 選項,例如 hs --select some-tag edit
    pub explicit_select: bool,
}

pub async fn mv(
    entry: &mut RepoEntry<'_>,
    new_name: Option<ScriptName>,
    ty: Option<ScriptType>,
    tags: Option<TagSelector>,
) -> Result {
    if ty.is_some() || new_name.is_some() {
        let og_path = path::open_script(&entry.name, &entry.ty, Some(true))?;
        let new_name = new_name.as_ref().unwrap_or(&entry.name);
        let new_ty = ty.as_ref().unwrap_or(&entry.ty);
        let new_path = path::open_script(new_name, new_ty, None)?; // NOTE: 不判斷存在性,因為接下來要對新舊腳本同路徑的狀況做特殊處理
        if new_path != og_path {
            log::debug!("改動腳本檔案:{:?} -> {:?}", og_path, new_path);
            if new_path.exists() {
                return Err(Error::PathExist(new_path).context("移動成既存腳本"));
            }
            super::mv(&og_path, &new_path)?;
        } else {
            log::debug!("相同的腳本檔案:{:?},不做檔案處理", og_path);
        }
    }

    entry
        .update(|info| {
            if let Some(ty) = ty {
                info.ty = ty;
            }
            if let Some(name) = new_name {
                info.name = name.clone();
            }
            if let Some(tags) = tags {
                info.append_tags(tags);
            }
            info.write();
        })
        .await?;
    Ok(())
}

fn create<F: FnOnce(String) -> Error>(
    query: ScriptQuery,
    script_repo: &mut ScriptRepo,
    ty: &ScriptType,
    on_conflict: F,
) -> Result<(ScriptName, PathBuf)> {
    let name = query.into_script_name()?;
    log::debug!("打開新命名腳本:{:?}", name);
    if script_repo.get_mut(&name, Visibility::All).is_some() {
        return Err(on_conflict(name.to_string()));
    }

    let p =
        path::open_script(&name, ty, None).context(format!("打開新命名腳本失敗:{:?}", name))?;
    if p.exists() {
        if p.is_dir() {
            return Err(Error::PathExist(p).context("與目錄撞路徑"));
        }
        check_path_collision(&p, script_repo)?;
        log::warn!("編輯野生腳本!");
    } else {
        // NOTE: 創建資料夾
        if let Some(parent) = p.parent() {
            super::handle_fs_res(&[&p], create_dir_all(parent))?;
        }
    }
    Ok((name, p))
}

struct EditListQueryHandler {
    anonymous_cnt: u32,
    named: HashMap<ScriptName, PathBuf>,
    ty: Option<ScriptFullType>,
    explicit_type: bool,
}
impl EditListQueryHandler {
    fn has_new_script(&self) -> bool {
        self.anonymous_cnt > 0 || !self.named.is_empty()
    }
    fn new(ty: Option<ScriptFullType>) -> Self {
        EditListQueryHandler {
            explicit_type: ty.is_some(),
            ty,
            named: Default::default(),
            anonymous_cnt: 0,
        }
    }
    fn get_or_default_type(&mut self) -> &ScriptFullType {
        if self.ty.is_none() {
            self.ty = Some(Default::default());
        }
        self.ty.as_ref().unwrap()
    }
}
// SAFETY: 實作永不改動 repo 本身
unsafe impl ListQueryHandler for EditListQueryHandler {
    type Item = EditQuery<ListQuery>;
    async fn handle_query<'a>(
        &mut self,
        query: ScriptQuery,
        repo: &'a mut ScriptRepo,
    ) -> Result<Option<RepoEntry<'a>>> {
        if self.explicit_type {
            let ty = self.get_or_default_type();
            let (name, path) = create(query, repo, &ty.ty, |_| {
                log::error!("與已存在的腳本撞名");
                RedundantOpt::Type.into()
            })?;
            self.named.insert(name, path);
            return Ok(None);
        }

        match query::do_script_query(&query, repo, false, false).await {
            Err(Error::DontFuzz) | Ok(None) => {
                let ty = self.get_or_default_type();
                let (name, path) = create(query, repo, &ty.ty, |name| {
                    log::error!("與被篩掉的腳本撞名");
                    Error::ScriptIsFiltered(name.to_string())
                })?;
                self.named.insert(name, path);
                Ok(None)
            }
            Ok(Some(entry)) => {
                log::debug!("打開既有命名腳本:{:?}", entry.name);
                // FIXME: 一旦 NLL 進化就修掉這段雙重詢問
                let n = entry.name.clone();
                return Ok(Some(repo.get_mut(&n, Visibility::All).unwrap()));
            }
            Err(e) => Err(e),
        }
    }
    fn handle_item(&mut self, item: Self::Item) -> Option<ListQuery> {
        match item {
            EditQuery::Query(query) => Some(query),
            EditQuery::NewAnonimous => {
                self.get_or_default_type();
                self.anonymous_cnt += 1;
                None
            }
        }
    }
    fn should_raise_dont_fuzz_on_empty() -> bool {
        false
    }
    fn should_return_all_on_empty() -> bool {
        false
    }
}

#[derive(Debug)]
pub struct EditResult<'a> {
    pub existing: Vec<RepoEntry<'a>>,
}
#[derive(Debug)]
pub struct CreateResult {
    pub ty: ScriptFullType,
    pub tags: Vec<Tag>,
    pub to_create: HashMap<ScriptName, PathBuf>,
}
impl CreateResult {
    pub fn new(
        ty: ScriptFullType,
        tags: Vec<Tag>,
        anonymous_cnt: u32,
        named: HashMap<ScriptName, PathBuf>,
    ) -> Result<CreateResult> {
        let iter = path::new_anonymous_name(
            anonymous_cnt,
            named.iter().filter_map(|(name, _)| {
                if let ScriptName::Anonymous(id) = name {
                    Some(*id)
                } else {
                    None
                }
            }),
        )
        .context("打開新匿名腳本失敗")?;

        let mut to_create = named;
        for name in iter {
            let path = path::open_script(&name, &ty.ty, None)?; // NOTE: new_anonymous_name 的邏輯已足以確保不會產生衝突的檔案,不檢查了!
            to_create.insert(name, path);
        }
        Ok(CreateResult {
            ty,
            tags,
            to_create,
        })
    }
    pub fn iter_path(&self) -> impl Iterator<Item = &Path> {
        self.to_create.iter().map(|(_, path)| path.as_ref())
    }
}

// XXX 到底幹嘛把新增和編輯的邏輯攪在一處呢…?
pub async fn edit_or_create(
    edit_query: Vec<EditQuery<ListQuery>>,
    script_repo: &'_ mut ScriptRepo,
    ty: Option<ScriptFullType>,
    tags: EditTagArgs,
) -> Result<(EditResult<'_>, Option<CreateResult>)> {
    let mut edit_query_handler = EditListQueryHandler::new(ty);
    let existing =
        do_list_query_with_handler(script_repo, edit_query, &mut edit_query_handler).await?;

    if existing.is_empty() && tags.explicit_select {
        return Err(RedundantOpt::Selector.into());
    }
    if !edit_query_handler.has_new_script() && tags.explicit_tag {
        return Err(RedundantOpt::Tag.into()); // TODO: why not follow `explicit_type` way?
    }
    if !edit_query_handler.has_new_script() && edit_query_handler.explicit_type {
        return Err(RedundantOpt::Type.into());
    }

    let edit_result = EditResult { existing };
    if edit_query_handler.has_new_script() {
        let create_result = CreateResult::new(
            edit_query_handler.ty.unwrap(),
            tags.content.into_allowed_iter().collect(),
            edit_query_handler.anonymous_cnt,
            edit_query_handler.named,
        )?;
        Ok((edit_result, Some(create_result)))
    } else {
        Ok((edit_result, None))
    }
}

fn run(
    script_path: &Path,
    info: &ScriptInfo,
    remaining: &[String],
    hs_tmpl_val: &super::TmplVal<'_>,
    remaining_envs: &[EnvPair],
) -> Result<()> {
    let conf = Config::get();
    let ty = &info.ty;

    let script_conf = conf.get_script_conf(ty)?;
    let cmd_str = if let Some(cmd) = &script_conf.cmd {
        cmd
    } else {
        return Err(Error::PermissionDenied(vec![script_path.to_path_buf()]));
    };

    let env = conf.gen_env(hs_tmpl_val, true)?;
    let ty_env = script_conf.gen_env(hs_tmpl_val)?;

    let pre_run_script = prepare_pre_run(None)?;
    let (cmd, shebang) = super::shebang_handle::handle(&pre_run_script)?;
    let args = shebang
        .iter()
        .map(|s| s.as_ref())
        .chain(std::iter::once(pre_run_script.as_os_str()))
        .chain(remaining.iter().map(|s| s.as_ref()));

    let set_cmd_envs = |cmd: &mut Command| {
        cmd.envs(ty_env.iter().map(|(a, b)| (a, b)));
        cmd.envs(env.iter().map(|(a, b)| (a, b)));
        cmd.envs(remaining_envs.iter().map(|p| (&p.key, &p.val)));
    };

    let mut cmd = super::create_cmd(cmd, args);
    set_cmd_envs(&mut cmd);

    let stat = super::run_cmd(cmd)?;
    log::info!("預腳本執行結果:{:?}", stat);
    if !stat.success() {
        // TODO: 根據返回值做不同表現
        let code = stat.code().unwrap_or_default();
        return Err(Error::PreRunError(code));
    }

    let args = script_conf.args(hs_tmpl_val)?;
    let full_args = args
        .iter()
        .map(|s| s.as_str())
        .chain(remaining.iter().map(|s| s.as_str()));

    let mut cmd = super::create_cmd(&cmd_str, full_args);
    set_cmd_envs(&mut cmd);

    let stat = super::run_cmd(cmd)?;
    log::info!("程式執行結果:{:?}", stat);
    if !stat.success() {
        let code = stat.code().unwrap_or_default();
        Err(Error::ScriptError(code))
    } else {
        Ok(())
    }
}
pub async fn run_n_times(
    repeat: u64,
    dummy: bool,
    entry: &mut RepoEntry<'_>,
    mut args: Vec<String>,
    res: &mut Vec<Error>,
    use_previous: bool,
    error_no_previous: bool,
    dir: Option<PathBuf>,
) -> Result {
    log::info!("執行 {:?}", entry.name);
    super::hijack_ctrlc_once();

    let mut env_vec = vec![];
    if use_previous {
        let dir = super::option_map_res(dir, |d| path::normalize_path(d))?;
        let historian = &entry.get_env().historian;
        match historian.previous_args(entry.id, dir.as_deref()).await? {
            None if error_no_previous => {
                return Err(Error::NoPreviousArgs);
            }
            None => log::warn!("無前一次參數,當作空的"),
            Some((arg_str, envs_str)) => {
                log::debug!("撈到前一次呼叫的參數 {}", arg_str);
                let mut prev_arg_vec: Vec<String> =
                    serde_json::from_str(&arg_str).context(format!("反序列失敗 {}", arg_str))?;
                env_vec =
                    serde_json::from_str(&envs_str).context(format!("反序列失敗 {}", envs_str))?;
                prev_arg_vec.extend(args.into_iter());
                args = prev_arg_vec;
            }
        }
    }

    let here = path::normalize_path(".").ok();
    let script_path = path::open_script(&entry.name, &entry.ty, Some(true))?;
    let content = super::read_file(&script_path)?;

    if !Config::get_no_caution()
        && Config::get().caution_tags.select(&entry.tags, &entry.ty) == Some(true)
    {
        let ty = super::get_display_type(&entry.ty);
        let name = &entry.name;
        let msg = format!(
            "{} requires extra caution. Are you sure?",
            name.stylize().color(ty.color()).bold()
        );
        let yes = super::prompt(msg, false)?;
        if !yes {
            return Err(Error::Caution);
        }
    }

    let mut hs_env_desc = vec![];
    for (need_save, line) in extract_env_from_content_help_aware(&content) {
        hs_env_desc.push(line.to_owned());
        if need_save {
            EnvPair::process_line(line, &mut env_vec);
        }
    }
    EnvPair::sort(&mut env_vec);
    let env_record = serde_json::to_string(&env_vec)?;

    let run_id = entry
        .update(|info| info.exec(content, &args, env_record, here))
        .await?;

    if dummy {
        log::info!("--dummy 不用真的執行,提早退出");
        return Ok(());
    }
    // Start packing hs tmpl val
    // SAFETY: 底下所有對 `entry` 的借用,都不會被更後面的 `entry.update` 影響
    let mut hs_tmpl_val = super::TmplVal::new();
    let hs_name = entry.name.key();
    let hs_name = hs_name.as_ref() as *const str;
    let hs_tags = &entry.tags as *const HashSet<Tag>;
    let content = entry.exec_time.as_ref().unwrap().data().unwrap().0.as_str() as *const str;
    hs_tmpl_val.path = Some(&script_path);
    hs_tmpl_val.run_id = Some(run_id);
    hs_tmpl_val.tags = unsafe { &*hs_tags }.iter().map(|t| t.as_ref()).collect();
    hs_tmpl_val.env_desc = hs_env_desc;
    hs_tmpl_val.name = Some(unsafe { &*hs_name });
    hs_tmpl_val.content = Some(unsafe { &*content });
    // End packing hs tmpl val

    for _ in 0..repeat {
        let run_res = run(&script_path, &*entry, &args, &hs_tmpl_val, &env_vec);
        let ret_code: i32;
        match run_res {
            Err(Error::ScriptError(code)) => {
                ret_code = code;
                res.push(run_res.unwrap_err());
            }
            Err(e) => return Err(e),
            Ok(_) => ret_code = 0,
        }
        entry
            .update(|info| info.exec_done(ret_code, run_id))
            .await?;
    }
    Ok(())
}

pub async fn load_utils(script_repo: &mut ScriptRepo) -> Result {
    for u in hyper_scripter_util::get_all().iter() {
        log::info!("載入小工具 {}", u.name);
        let name = u.name.to_owned().into_script_name()?;
        if script_repo.get_mut(&name, Visibility::All).is_some() {
            log::warn!("已存在的小工具 {:?},跳過", name);
            continue;
        }
        let ty = u.ty.parse()?;
        let tags: Vec<Tag> = if u.is_hidden {
            vec!["util".parse().unwrap(), "hide".parse().unwrap()]
        } else {
            vec!["util".parse().unwrap()]
        };
        let p = path::open_script(&name, &ty, Some(false))?;

        // NOTE: 創建資料夾
        if let Some(parent) = p.parent() {
            super::handle_fs_res(&[&p], create_dir_all(parent))?;
        }

        let entry = script_repo
            .entry(&name)
            .or_insert(ScriptInfo::builder(0, name, ty, tags.into_iter()).build())
            .await?;
        super::prepare_script(&p, &*entry, None, true, &[u.content])?;
    }
    Ok(())
}

pub fn prepare_pre_run(content: Option<&str>) -> Result<PathBuf> {
    let p = path::get_home().join(path::HS_PRE_RUN);
    if content.is_some() || !p.exists() {
        let content = content.unwrap_or_else(|| include_str!("hs_prerun"));
        log::info!("寫入預執行腳本 {:?} {}", p, content);
        super::write_file(&p, content)?;
    }
    Ok(p)
}

pub fn load_templates() -> Result {
    for (ty, tmpl) in iter_default_templates() {
        let tmpl_path = path::get_template_path(&ty)?;
        if tmpl_path.exists() {
            continue;
        }
        super::write_file(&tmpl_path, tmpl)?;
    }
    Ok(())
}

/// 判斷是否需要寫入主資料庫(script_infos 表格)
pub fn need_write(arg: &Subs) -> bool {
    use Subs::*;
    match arg {
        Edit { .. } => true,
        CP { .. } => true,
        RM { .. } => true,
        LoadUtils { .. } => true,
        MV {
            ty,
            tags,
            new,
            origin: _,
        } => {
            // TODO: 好好測試這個
            ty.is_some() || tags.is_some() || new.is_some()
        }
        _ => false,
    }
}

pub async fn after_script(
    entry: &mut RepoEntry<'_>,
    path: &Path,
    prepare_resp: Option<&PrepareRespond>,
) -> Result {
    let mut record_write = true;
    match prepare_resp {
        None => {
            log::debug!("不執行後處理");
        }
        Some(PrepareRespond { is_new, time }) => {
            let modified = super::file_modify_time(path)?;
            if time >= &modified {
                if *is_new {
                    log::info!("新腳本未變動,應刪除之");
                    return Err(Error::EmptyCreate);
                } else {
                    log::info!("舊腳本未變動,不記錄寫事件(只記讀事件)");
                    record_write = false;
                }
            }
        }
    }
    if record_write {
        entry.update(|info| info.write()).await?;
    }
    Ok(())
}

fn check_path_collision(p: &Path, script_repo: &mut ScriptRepo) -> Result {
    for script in script_repo.iter_mut(Visibility::All) {
        let script_p = path::open_script(&script.name, &script.ty, None)?;
        if &script_p == p {
            return Err(Error::PathExist(script_p).context("與既存腳本撞路徑"));
        }
    }
    Ok(())
}