demons 0.2.0

Run a project's development commands side-by-side in one terminal
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
use std::{
    collections::{BTreeMap, HashSet},
    env, fs,
    path::{Path, PathBuf},
    time::Duration,
};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

pub const CONFIG_FILE: &str = "demons.toml";
pub const DEFAULT_MULTI_CLICK_MS: u64 = 500;
pub const MIN_MULTI_CLICK_MS: u64 = 150;
pub const MAX_MULTI_CLICK_MS: u64 = 1000;
pub const MULTI_CLICK_STEP_MS: u64 = 50;

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Config {
    #[serde(default, skip_serializing_if = "Settings::is_default")]
    pub settings: Settings,
    #[serde(rename = "task")]
    pub tasks: Vec<Task>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct Settings {
    pub layout: Layout,
    pub leader: Leader,
    #[serde(
        default = "default_multi_click_ms",
        skip_serializing_if = "is_default_multi_click_ms"
    )]
    pub multi_click_ms: u64,
    #[serde(skip_serializing_if = "is_false")]
    pub logging: bool,
}

impl Settings {
    fn is_default(&self) -> bool {
        self == &Self::default()
    }
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            layout: Layout::Grid,
            leader: Leader::AltJ,
            multi_click_ms: DEFAULT_MULTI_CLICK_MS,
            logging: false,
        }
    }
}

fn default_multi_click_ms() -> u64 {
    DEFAULT_MULTI_CLICK_MS
}

fn is_default_multi_click_ms(value: &u64) -> bool {
    *value == DEFAULT_MULTI_CLICK_MS
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum Layout {
    #[default]
    Grid,
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum Leader {
    #[serde(rename = "alt-j")]
    #[default]
    AltJ,
    #[serde(rename = "alt-backtick")]
    AltBacktick,
    Tab,
    CtrlB,
    CtrlQ,
    #[serde(rename = "ctrl-\\")]
    CtrlBackslash,
}

impl Leader {
    pub fn label(self) -> &'static str {
        match self {
            Self::AltJ => "Alt-J",
            Self::AltBacktick => "Alt-`",
            Self::Tab => "Tab",
            Self::CtrlB => "Ctrl-B",
            Self::CtrlQ => "Ctrl-Q",
            Self::CtrlBackslash => "Ctrl-\\",
        }
    }

    pub fn uses_escape_alt_encoding(self) -> bool {
        matches!(self, Self::AltJ | Self::AltBacktick)
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Task {
    pub name: String,
    pub command: TaskCommand,
    #[serde(default = "default_cwd", skip_serializing_if = "is_default_cwd")]
    pub cwd: PathBuf,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub env: BTreeMap<String, String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub depends_on: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start_delay: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub watch: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_on_change: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repeat: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum TaskCommand {
    Shell(String),
    Direct(Vec<String>),
}

impl TaskCommand {
    pub fn display(&self) -> String {
        match self {
            Self::Shell(command) => command.clone(),
            Self::Direct(parts) => parts.join(" "),
        }
    }

    fn is_empty(&self) -> bool {
        match self {
            Self::Shell(command) => command.trim().is_empty(),
            Self::Direct(parts) => parts.is_empty() || parts[0].trim().is_empty(),
        }
    }

    fn contains_nul(&self) -> bool {
        match self {
            Self::Shell(command) => command.contains('\0'),
            Self::Direct(parts) => parts.iter().any(|part| part.contains('\0')),
        }
    }
}

#[derive(Clone, Debug)]
pub struct LoadedConfig {
    pub path: PathBuf,
    pub root: PathBuf,
    pub config: Config,
}

impl LoadedConfig {
    pub fn load(path: PathBuf) -> Result<Self> {
        let path = absolute_path(path)?;
        let config = parse_file(&path)?;
        let root = path
            .parent()
            .context("config path has no parent directory")?
            .to_path_buf();
        let loaded = Self { path, root, config };
        loaded.validate()?;
        Ok(loaded)
    }

    pub fn load_unvalidated_or_default(path: PathBuf) -> Result<Self> {
        let path = absolute_path(path)?;
        let config = if path.is_file() {
            parse_file(&path)?
        } else {
            Config::default()
        };
        let root = path
            .parent()
            .context("config path has no parent directory")?
            .to_path_buf();
        Ok(Self { path, root, config })
    }

    pub fn validate(&self) -> Result<()> {
        validate_for_path(&self.config, &self.path)
    }

    pub fn save(&self) -> Result<()> {
        self.validate()?;
        if let Some(parent) = self.path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("failed to create config directory {}", parent.display())
            })?;
        }
        let text = toml::to_string_pretty(&self.config)
            .with_context(|| format!("failed to serialize config {}", self.path.display()))?;
        fs::write(&self.path, text)
            .with_context(|| format!("failed to write config {}", self.path.display()))
    }

    pub fn task_cwd(&self, task: &Task) -> PathBuf {
        if task.cwd.is_absolute() {
            task.cwd.clone()
        } else {
            self.root.join(&task.cwd)
        }
    }
}

pub fn parse_file(path: &Path) -> Result<Config> {
    let source = fs::read_to_string(path)
        .with_context(|| format!("failed to read config {}", path.display()))?;
    toml::from_str(&source).with_context(|| format!("failed to parse config {}", path.display()))
}

pub fn validate_for_path(config: &Config, path: &Path) -> Result<()> {
    if config.tasks.is_empty() {
        bail!("{} must define at least one [[task]]", path.display());
    }
    if config.settings.logging {
        bail!(
            "{}: settings.logging is reserved for a future release and cannot be enabled",
            path.display()
        );
    }
    if !(MIN_MULTI_CLICK_MS..=MAX_MULTI_CLICK_MS).contains(&config.settings.multi_click_ms) {
        bail!(
            "{}: settings.multi_click_ms must be between {MIN_MULTI_CLICK_MS} and {MAX_MULTI_CLICK_MS}",
            path.display()
        );
    }

    let root = path
        .parent()
        .context("config path has no parent directory")?;
    let mut names = HashSet::new();
    let mut name_to_index = BTreeMap::new();
    for (index, task) in config.tasks.iter().enumerate() {
        let label = format!("task #{}", index + 1);
        if task.name.trim().is_empty() {
            bail!("{}: {label} has an empty name", path.display());
        }
        if task.name.trim() != task.name || task.name.chars().any(char::is_control) {
            bail!(
                "{}: task name {:?} has leading/trailing whitespace or control characters",
                path.display(),
                task.name
            );
        }
        if !names.insert(task.name.as_str()) {
            bail!("{}: duplicate task name {:?}", path.display(), task.name);
        }
        name_to_index.insert(task.name.as_str(), index);
        if task.command.is_empty() {
            bail!(
                "{}: task {:?} has an empty command",
                path.display(),
                task.name
            );
        }
        if task.command.contains_nul() {
            bail!(
                "{}: task {:?} command contains a NUL byte",
                path.display(),
                task.name
            );
        }
        for (key, value) in &task.env {
            if key.is_empty()
                || key.contains(['=', '\0'])
                || key.contains(char::is_whitespace)
                || value.contains('\0')
            {
                bail!(
                    "{}: task {:?} has an invalid environment entry for key {:?}",
                    path.display(),
                    task.name,
                    key
                );
            }
        }

        let mut dependencies = HashSet::new();
        for dependency in &task.depends_on {
            if dependency.trim().is_empty()
                || dependency.trim() != dependency
                || dependency.chars().any(char::is_control)
            {
                bail!(
                    "{}: task {:?} has an invalid dependency name {:?}",
                    path.display(),
                    task.name,
                    dependency
                );
            }
            if dependency == &task.name {
                bail!(
                    "{}: task {:?} cannot depend on itself",
                    path.display(),
                    task.name
                );
            }
            if !dependencies.insert(dependency.as_str()) {
                bail!(
                    "{}: task {:?} repeats dependency {:?}",
                    path.display(),
                    task.name,
                    dependency
                );
            }
        }
        if let Some(delay) = task.start_delay.as_deref() {
            parse_start_delay(delay).with_context(|| {
                format!(
                    "{}: task {:?} has invalid start_delay {:?}",
                    path.display(),
                    task.name,
                    delay
                )
            })?;
        }

        let cwd = if task.cwd.is_absolute() {
            task.cwd.clone()
        } else {
            root.join(&task.cwd)
        };
        if !cwd.is_dir() {
            bail!(
                "{}: cwd for task {:?} is not a directory: {}",
                path.display(),
                task.name,
                cwd.display()
            );
        }

        if task.watch.is_some() || task.run_on_change.is_some() || task.repeat.is_some() {
            bail!(
                "{}: task {:?} uses watch, run_on_change, or repeat; these fields are reserved for \
                 a future release",
                path.display(),
                task.name
            );
        }
    }
    for task in &config.tasks {
        for dependency in &task.depends_on {
            if !name_to_index.contains_key(dependency.as_str()) {
                bail!(
                    "{}: task {:?} depends on unknown task {:?}",
                    path.display(),
                    task.name,
                    dependency
                );
            }
        }
    }
    reject_dependency_cycles(config, &name_to_index, path)?;
    Ok(())
}

pub fn parse_start_delay(value: &str) -> Result<Duration> {
    let value = value.trim();
    if value.is_empty() {
        bail!("delay cannot be empty");
    }
    let (number, unit) = split_duration(value)?;
    let amount: u64 = number
        .parse()
        .with_context(|| format!("invalid delay amount {number:?}"))?;
    if amount == 0 {
        return Ok(Duration::ZERO);
    }
    let millis = match unit {
        "" | "s" => amount.checked_mul(1000).context("delay is too large")?,
        "ms" => amount,
        "m" => amount.checked_mul(60_000).context("delay is too large")?,
        "h" => amount
            .checked_mul(3_600_000)
            .context("delay is too large")?,
        _ => bail!("delay unit must be one of ms, s, m, h"),
    };
    Ok(Duration::from_millis(millis))
}

fn split_duration(value: &str) -> Result<(&str, &str)> {
    let unit_start = value
        .find(|character: char| !character.is_ascii_digit())
        .unwrap_or(value.len());
    let number = &value[..unit_start];
    let unit = &value[unit_start..];
    if number.is_empty() || !number.chars().all(|character| character.is_ascii_digit()) {
        bail!("delay must start with a number");
    }
    if unit.is_empty()
        || unit
            .chars()
            .all(|character| character.is_ascii_alphabetic())
    {
        Ok((number, unit))
    } else {
        bail!("delay unit must use letters only");
    }
}

fn reject_dependency_cycles(
    config: &Config,
    name_to_index: &BTreeMap<&str, usize>,
    path: &Path,
) -> Result<()> {
    let mut states = vec![VisitState::Unvisited; config.tasks.len()];
    let mut stack = Vec::new();
    for index in 0..config.tasks.len() {
        visit_dependency(index, config, name_to_index, &mut states, &mut stack, path)?;
    }
    Ok(())
}

fn visit_dependency(
    index: usize,
    config: &Config,
    name_to_index: &BTreeMap<&str, usize>,
    states: &mut [VisitState],
    stack: &mut Vec<usize>,
    path: &Path,
) -> Result<()> {
    match states[index] {
        VisitState::Visited => return Ok(()),
        VisitState::Visiting => {
            let task = &config.tasks[index];
            bail!(
                "{}: task dependency cycle includes {:?}",
                path.display(),
                task.name
            );
        }
        VisitState::Unvisited => {}
    }
    states[index] = VisitState::Visiting;
    stack.push(index);
    for dependency in &config.tasks[index].depends_on {
        let Some(&dependency_index) = name_to_index.get(dependency.as_str()) else {
            continue;
        };
        if states[dependency_index] == VisitState::Visiting {
            let start = stack
                .iter()
                .position(|candidate| *candidate == dependency_index)
                .unwrap_or(0);
            let mut cycle = stack[start..]
                .iter()
                .map(|candidate| config.tasks[*candidate].name.as_str())
                .collect::<Vec<_>>();
            cycle.push(config.tasks[dependency_index].name.as_str());
            bail!(
                "{}: task dependency cycle: {}",
                path.display(),
                cycle.join(" -> ")
            );
        }
        visit_dependency(dependency_index, config, name_to_index, states, stack, path)?;
    }
    stack.pop();
    states[index] = VisitState::Visited;
    Ok(())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum VisitState {
    Unvisited,
    Visiting,
    Visited,
}

pub fn discover(start: &Path) -> Result<Option<PathBuf>> {
    let mut directory = absolute_path(start.to_path_buf())?;
    if directory.is_file() {
        directory.pop();
    }

    loop {
        let candidate = directory.join(CONFIG_FILE);
        if candidate.is_file() {
            return Ok(Some(candidate));
        }
        if !directory.pop() {
            return Ok(None);
        }
    }
}

pub fn explicit_or_discover(explicit: Option<PathBuf>, start: &Path) -> Result<Option<PathBuf>> {
    match explicit {
        Some(path) => Ok(Some(absolute_path(path)?)),
        None => discover(start),
    }
}

fn absolute_path(path: PathBuf) -> Result<PathBuf> {
    if path.is_absolute() {
        Ok(path)
    } else {
        Ok(env::current_dir()
            .context("failed to determine current directory")?
            .join(path))
    }
}

fn default_cwd() -> PathBuf {
    PathBuf::from(".")
}

fn is_default_cwd(path: &Path) -> bool {
    path == Path::new(".")
}

fn is_false(value: &bool) -> bool {
    !value
}

#[cfg(test)]
mod tests {
    use std::{fs, time::Duration};

    use tempfile::tempdir;

    use super::*;

    #[test]
    fn discovers_closest_config() {
        let temp = tempdir().unwrap();
        let root = temp.path();
        let nested = root.join("a/b");
        fs::create_dir_all(&nested).unwrap();
        fs::write(root.join(CONFIG_FILE), "").unwrap();
        fs::write(root.join("a").join(CONFIG_FILE), "").unwrap();

        assert_eq!(
            discover(&nested).unwrap(),
            Some(root.join("a").join(CONFIG_FILE))
        );
    }

    #[test]
    fn rejects_unknown_keys() {
        let error = toml::from_str::<Config>(
            r#"
                surprise = true
                [[task]]
                name = "test"
                command = "echo ok"
            "#,
        )
        .unwrap_err();

        assert!(error.to_string().contains("unknown field"));
    }

    #[test]
    fn parses_shell_and_direct_commands() {
        let config: Config = toml::from_str(
            r#"
                [[task]]
                name = "shell"
                command = "echo shell"

                [[task]]
                name = "direct"
                command = ["echo", "direct"]
            "#,
        )
        .unwrap();

        assert!(matches!(
            config.tasks[0].command,
            TaskCommand::Shell(ref command) if command == "echo shell"
        ));
        assert!(matches!(
            config.tasks[1].command,
            TaskCommand::Direct(ref command) if command == &["echo", "direct"]
        ));
    }

    #[test]
    fn allows_empty_direct_arguments() {
        let config: Config = toml::from_str(
            r#"
                [[task]]
                name = "direct"
                command = ["printf", "%s", ""]
            "#,
        )
        .unwrap();

        assert!(!config.tasks[0].command.is_empty());
    }

    #[test]
    fn validates_relative_working_directories_and_unique_names() {
        let temp = tempdir().unwrap();
        fs::create_dir(temp.path().join("web")).unwrap();
        let path = temp.path().join(CONFIG_FILE);
        let valid: Config = toml::from_str(
            r#"
                [[task]]
                name = "server"
                command = "echo server"

                [[task]]
                name = "web"
                command = "echo web"
                cwd = "web"
            "#,
        )
        .unwrap();
        validate_for_path(&valid, &path).unwrap();

        let duplicate: Config = toml::from_str(
            r#"
                [[task]]
                name = "server"
                command = "echo one"

                [[task]]
                name = "server"
                command = "echo two"
            "#,
        )
        .unwrap();
        assert!(validate_for_path(&duplicate, &path).is_err());
    }

    #[test]
    fn validates_dependencies_and_start_delay() {
        let temp = tempdir().unwrap();
        let path = temp.path().join(CONFIG_FILE);
        let valid: Config = toml::from_str(
            r#"
                [[task]]
                name = "server"
                command = "echo server"

                [[task]]
                name = "web"
                command = "echo web"
                depends_on = ["server"]
                start_delay = "3s"
            "#,
        )
        .unwrap();

        validate_for_path(&valid, &path).unwrap();
        assert_eq!(
            parse_start_delay("500ms").unwrap(),
            Duration::from_millis(500)
        );
        assert_eq!(parse_start_delay("2").unwrap(), Duration::from_secs(2));
    }

    #[test]
    fn rejects_unknown_dependency_and_cycles() {
        let temp = tempdir().unwrap();
        let path = temp.path().join(CONFIG_FILE);
        let unknown: Config = toml::from_str(
            r#"
                [[task]]
                name = "web"
                command = "echo web"
                depends_on = ["server"]
            "#,
        )
        .unwrap();
        assert!(validate_for_path(&unknown, &path).is_err());

        let cycle: Config = toml::from_str(
            r#"
                [[task]]
                name = "api"
                command = "echo api"
                depends_on = ["web"]

                [[task]]
                name = "web"
                command = "echo web"
                depends_on = ["api"]
            "#,
        )
        .unwrap();
        let error = validate_for_path(&cycle, &path).unwrap_err().to_string();
        assert!(error.contains("dependency cycle"));
    }

    #[test]
    fn defaults_to_alt_j_leader() {
        let config: Config = toml::from_str(
            r#"
                [[task]]
                name = "server"
                command = "echo ready"
            "#,
        )
        .unwrap();

        assert_eq!(config.settings.leader, Leader::AltJ);
        assert_eq!(config.settings.multi_click_ms, DEFAULT_MULTI_CLICK_MS);
    }

    #[test]
    fn parses_alt_backtick_leader() {
        let config: Config = toml::from_str(
            r#"
                [settings]
                leader = "alt-backtick"

                [[task]]
                name = "server"
                command = "echo ready"
            "#,
        )
        .unwrap();

        assert_eq!(config.settings.leader, Leader::AltBacktick);
        assert_eq!(config.settings.leader.label(), "Alt-`");
    }

    #[test]
    fn validates_multi_click_timing() {
        let temp = tempdir().unwrap();
        let path = temp.path().join(CONFIG_FILE);
        let config: Config = toml::from_str(
            r#"
                [settings]
                multi_click_ms = 400

                [[task]]
                name = "server"
                command = "echo ready"
            "#,
        )
        .unwrap();
        validate_for_path(&config, &path).unwrap();

        let invalid: Config = toml::from_str(
            r#"
                [settings]
                multi_click_ms = 20

                [[task]]
                name = "server"
                command = "echo ready"
            "#,
        )
        .unwrap();
        assert!(validate_for_path(&invalid, &path).is_err());
    }
}