pub fn prepare_pre_run(content: Option<&str>) -> Result<PathBuf>
Examples found in repository?
src/util/init_repo.rs (line 72)
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
pub async fn init_repo(args: RootArgs, need_journal: bool) -> Result<ScriptRepo> {
    let RootArgs {
        no_trace,
        humble,
        archaeology,
        select,
        toggle,
        recent,
        timeless,
        ..
    } = args;

    let conf = Config::get();

    let recent = if timeless {
        None
    } else {
        recent.or(conf.recent).map(|recent| RecentFilter {
            recent,
            archaeology,
        })
    };

    // TODO: 測試 toggle 功能,以及名字不存在的錯誤
    let tag_group = {
        let mut toggle: HashSet<_> = toggle.into_iter().collect();
        let mut tag_group = conf.get_tag_selector_group(&mut toggle);
        if let Some(name) = toggle.into_iter().next() {
            return Err(Error::TagSelectorNotFound(name));
        }
        for select in select.into_iter() {
            tag_group.push(select);
        }
        tag_group
    };

    let (env, init) = init_env(need_journal).await?;
    let mut repo = ScriptRepo::new(recent, env, &tag_group)
        .await
        .context("載入腳本倉庫失敗")?;
    if no_trace {
        repo.no_trace();
    } else if humble {
        repo.humble();
    }

    if init {
        log::info!("初次使用,載入好用工具和預執行腳本");
        main_util::load_utils(&mut repo).await?;
        main_util::prepare_pre_run(None)?;
        main_util::load_templates()?;
    }

    Ok(repo)
}
More examples
Hide additional examples
src/util/main_util.rs (line 163)
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
fn run(
    script_path: &Path,
    info: &ScriptInfo,
    remaining: &[String],
    hs_tmpl_val: &serde_json::Value,
    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)?;
    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(())
    }
}