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
use crate::error::Error;
use handlebars::Handlebars;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;

const SHELL_WELCOME_MSG: &str = "# Hello, scripter! Here are some useful commands to begin with:

export NAME=\"{{name}}\"
export VAR=\"${VAR:-default}\"
cd ~/{{birthplace}}

{{#each content}}{{{this}}}
{{/each}}";

const JS_WELCOME_MSG: &str =
    "// Hello, scripter! Here are some information you may be intrested in:

const name = '{{name}}';
process.chdir(require('os').homedir());
{{#if birthplace}}process.chdir('{{birthplace}}');{{/if}}
let spawn = require('child_process').spawnSync;
spawn('test', [], { stdio: 'inherit' });

let writeFile = require('fs').writeFileSync;
writeFile('/dev/null', 'some content');

{{#each content}}{{{this}}}
{{/each}}";

const VORPAL_WELCOME_MSG: &str = "const name = '{{name}}';
process.chdir(require('os').homedir());
{{#if birthplace}}process.chdir('{{birthplace}}');{{/if}}

let Vorpal = require('vorpal');
let vorpal = new Vorpal();

vorpal.command('test <arg1> [arg2]', 'this is a teeeest!').action(args => {
    console.log(`arg1 = ${args.arg1}`);
    console.log(`arg2 = ${args.arg2}`);
    return Promise.resolve();
});

{{#each content}}{{{this}}}
{{/each}}

vorpal.delimiter('>').show();";

const TMUX_WELCOME_MSG: &str = "# Hello, scripter!
export NAME=\"{{name}}\"
export VAR=\"${VAR:-default}\"
cd ~/{{birthplace}}

tmux new-session -s $NAME -d \"{{{content.0}}}; $SHELL\" || exit 1
tmux split-window -h \"{{{content.1}}}; $SHELL\"
{{#if content.2}}tmux split-window -v \"{{{content.2}}}; $SHELL\"
{{/if}}
tmux -2 attach-session -d";

const RB_WELCOME_MSG: &str = "# Hello, scripter!
Dir.chdir(\"#{ENV['HOME']}/{{birthplace}}\")
NAME = '{{name}}'

{{#each content}}{{{this}}}
{{/each}} ";

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(transparent)]
pub struct ScriptType(String);
impl From<&str> for ScriptType {
    fn from(s: &str) -> Self {
        Self::from_str(s).unwrap()
    }
}
impl From<String> for ScriptType {
    fn from(s: String) -> Self {
        // TODO: 檢查
        ScriptType(s)
    }
}
impl AsRef<str> for ScriptType {
    fn as_ref(&self) -> &str {
        &self.0
    }
}
impl FromStr for ScriptType {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(s.to_owned().into())
    }
}
impl std::fmt::Display for ScriptType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl Default for ScriptType {
    fn default() -> Self {
        "sh".into()
    }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ScriptTypeConfig {
    pub ext: Option<String>,
    pub color: String,
    pub template: Vec<String>,
    pub cmd: Option<String>,
    args: Vec<String>,
    env: Vec<(String, String)>,
}
fn split(s: &str) -> Vec<String> {
    s.split("\n").map(|s| s.to_owned()).collect()
}
fn default_template() -> Vec<String> {
    vec![
        "{{#each content}}{{{this}}}".to_owned(),
        "{{/each}}".to_owned(),
    ]
}

impl ScriptTypeConfig {
    pub fn args(&self, info: &serde_json::Value) -> Result<Vec<String>, Error> {
        let reg = Handlebars::new();
        let mut args: Vec<String> = Vec::with_capacity(self.args.len());
        for c in self.args.iter() {
            let res = reg.render_template(c, &info)?;
            args.push(res);
        }
        Ok(args)
    }
    pub fn env(&self, info: &serde_json::Value) -> Result<Vec<(String, String)>, Error> {
        let reg = Handlebars::new();
        let mut env: Vec<(String, String)> = Vec::with_capacity(self.env.len());
        for (name, e) in self.env.iter() {
            let res = reg.render_template(e, &info)?;
            env.push((name.to_owned(), res));
        }
        Ok(env)
    }
    pub fn default_script_types() -> HashMap<ScriptType, ScriptTypeConfig> {
        let mut ret = HashMap::default();
        ret.insert(
            "sh".into(),
            ScriptTypeConfig {
                ext: Some("sh".to_owned()),
                color: "bright magenta".to_owned(),
                template: split(SHELL_WELCOME_MSG),
                cmd: Some("bash".to_owned()),
                args: vec!["{{path}}".to_owned()],
                env: vec![],
            },
        );
        ret.insert(
            "tmux".into(),
            ScriptTypeConfig {
                ext: Some("sh".to_owned()),
                color: "white".to_owned(),
                template: split(TMUX_WELCOME_MSG),
                cmd: Some("bash".to_owned()),
                args: vec!["{{path}}".to_owned()],
                env: vec![],
            },
        );
        ret.insert(
            "js".into(),
            ScriptTypeConfig {
                ext: Some("js".to_owned()),
                color: "bright cyan".to_owned(),
                template: split(JS_WELCOME_MSG),
                cmd: Some("node".to_owned()),
                args: vec!["{{path}}".to_owned()],
                env: vec![(
                    "NODE_PATH".to_owned(),
                    "{{{script_dir}}}/node_modules".to_owned(),
                )],
            },
        );
        ret.insert(
            "vorpal".into(),
            ScriptTypeConfig {
                ext: Some("js".to_owned()),
                color: "bright cyan".to_owned(),
                template: split(VORPAL_WELCOME_MSG),
                cmd: Some("node".to_owned()),
                args: vec!["{{path}}".to_owned()],
                env: vec![(
                    "NODE_PATH".to_owned(),
                    "{{{script_dir}}}/node_modules".to_owned(),
                )],
            },
        );
        ret.insert(
            "js-i".into(),
            ScriptTypeConfig {
                ext: Some("js".to_owned()),
                color: "bright cyan".to_owned(),
                template: split(JS_WELCOME_MSG),
                cmd: Some("node".to_owned()),
                args: vec!["-i".to_owned(), "-e".to_owned(), "{{{content}}}".to_owned()],
                env: vec![(
                    "NODE_PATH".to_owned(),
                    "{{{script_dir}}}/node_modules".to_owned(),
                )],
            },
        );
        ret.insert(
            "rb".into(),
            ScriptTypeConfig {
                ext: Some("rb".to_owned()),
                color: "bright red".to_owned(),
                template: split(RB_WELCOME_MSG),
                cmd: Some("ruby".to_owned()),
                args: vec!["{{path}}".to_owned()],
                env: vec![],
            },
        );

        ret.insert(
            "md".into(),
            ScriptTypeConfig {
                ext: Some("md".to_owned()),
                color: "bright black".to_owned(),
                template: default_template(),
                cmd: None,
                args: vec![],
                env: vec![],
            },
        );

        ret
    }
}