zoning 1.4.0

An import-topology gate: declare the zones a package's imports may cross, and judge the real import graph against them.
Documentation
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
//! Idempotent editor integration setup owned by the `zoning` executable.

use std::env;
use std::fs;
use std::io::IsTerminal as _;
use std::path::{Path, PathBuf};
use std::process::Command;

use serde::{Deserialize, Serialize};

use crate::Result;

const EXTENSION_ID: &str = "the-billy-company.zoning";

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
enum Editor {
    Cursor,
    Code,
    Zed,
    Neovim,
    Vim,
}

impl Editor {
    fn name(self) -> &'static str {
        match self {
            Self::Cursor => "Cursor",
            Self::Code => "VS Code",
            Self::Zed => "Zed",
            Self::Neovim => "Neovim",
            Self::Vim => "Vim",
        }
    }
}

#[derive(Deserialize, Serialize)]
struct Manifest {
    version: String,
    editors: Vec<Editor>,
}

/// Explicit setup operation.
#[derive(Clone, Copy, Debug)]
pub enum Action {
    /// Report detected and installed integrations.
    Status,
    /// Install every detected integration idempotently.
    Run,
    /// Reinstall missing or drifted owned files.
    Repair,
    /// Remove only integrations recorded in zoning's state manifest.
    Uninstall,
}

/// Run an explicit setup action and return human-readable status lines.
///
/// # Errors
/// Returns an error when editor state cannot be read or changed atomically.
pub fn execute(action: Action) -> Result<Vec<String>> {
    let home = home()?;
    execute_in(action, &home)
}

/// Run automatic first-use setup when mutation is safe.
pub fn auto() {
    if env::var_os("CI").is_some()
        || env::var_os("ZONING_NO_SETUP").is_some()
        || !std::io::stdin().is_terminal()
        || !std::io::stdout().is_terminal()
        || !std::io::stderr().is_terminal()
    {
        return;
    }
    let Ok(home) = home() else {
        return;
    };
    if state_path(&home).exists() || detect(&home).is_empty() {
        return;
    }
    if let Ok(lines) = execute_in(Action::Run, &home) {
        for line in lines {
            eprintln!("zone: {line}");
        }
    }
}

fn execute_in(action: Action, home: &Path) -> Result<Vec<String>> {
    let state = state_path(home);
    match action {
        Action::Status => Ok(status(home)),
        Action::Run | Action::Repair => {
            let editors = detect(home);
            if editors.is_empty() {
                return Ok(vec!["no supported editor detected; nothing changed".to_owned()]);
            }
            // The slow door here is an editor's own CLI (`cursor
            // --install-extension`, `--list-extensions`) — genuinely seconds,
            // not milliseconds — so the spinner covers exactly this loop and
            // stops before the lines it earned get printed.
            let spinner = crate::spinner::Spinner::start("zone is setting up your editor");
            let mut lines = Vec::new();
            for editor in &editors {
                install(home, *editor)?;
                lines.push(format!("{} integration ready", editor.name()));
            }
            write_atomic(
                &state,
                &serde_json::to_vec_pretty(&Manifest {
                    version: env!("CARGO_PKG_VERSION").to_owned(),
                    editors,
                })?,
            )?;
            spinner.stop();
            Ok(lines)
        }
        Action::Uninstall => {
            let manifest = read_manifest(&state)?;
            let Some(manifest) = manifest else {
                return Ok(vec!["no zoning-owned editor integration is recorded".to_owned()]);
            };
            for editor in manifest.editors {
                remove(home, editor)?;
            }
            if state.exists() {
                fs::remove_file(&state)
                    .map_err(|error| format!("cannot remove {}: {error}", state.display()))?;
            }
            Ok(vec!["removed zoning-owned editor integrations".to_owned()])
        }
    }
}

fn detect(home: &Path) -> Vec<Editor> {
    [
        (Editor::Cursor, command("cursor") || home.join(".cursor").exists()),
        (Editor::Code, command("code") || home.join(".vscode").exists()),
        (
            Editor::Zed,
            command("zed")
                || home.join(".config/zed").exists()
                || home.join("Library/Application Support/Zed").exists(),
        ),
        (Editor::Neovim, command("nvim") || home.join(".config/nvim").exists()),
        (Editor::Vim, command("vim") || home.join(".vim").exists()),
    ]
    .into_iter()
    .filter_map(|(editor, present)| present.then_some(editor))
    .collect()
}

fn status(home: &Path) -> Vec<String> {
    let installed = read_manifest(&state_path(home)).ok().flatten();
    detect(home)
        .into_iter()
        .map(|editor| {
            let ready =
                installed.as_ref().is_some_and(|manifest| manifest.editors.contains(&editor))
                    && present(home, editor);
            format!(
                "{}: {}",
                editor.name(),
                if ready { "installed" } else { "detected, not installed" }
            )
        })
        .collect()
}

/// Whether `editor` actually carries zoning's integration right now.
///
/// Install for Cursor/Code takes one of two doors — the editor's own CLI, or
/// a manual file drop when no CLI is on `PATH` — and only the second leaves
/// an artifact under `installed_path`. Ask the same door install used, or
/// `status`/`repair` would call a CLI-verified install "not installed".
fn present(home: &Path, editor: Editor) -> bool {
    match editor {
        Editor::Cursor | Editor::Code => {
            let cli = if editor == Editor::Cursor { "cursor" } else { "code" };
            if env::var_os("HOME").is_some_and(|actual| Path::new(&actual) == home) && command(cli)
            {
                Command::new(cli).arg("--list-extensions").output().is_ok_and(|listed| {
                    String::from_utf8_lossy(&listed.stdout)
                        .lines()
                        .any(|extension| extension.eq_ignore_ascii_case(EXTENSION_ID))
                })
            } else {
                installed_path(home, editor).exists()
            }
        }
        Editor::Zed => fs::read_to_string(installed_path(home, editor))
            .is_ok_and(|settings| settings.contains("\"zoning\"")),
        Editor::Neovim | Editor::Vim => installed_path(home, editor).exists(),
    }
}

fn install(home: &Path, editor: Editor) -> Result<()> {
    match editor {
        Editor::Cursor | Editor::Code => install_graphical(home, editor),
        Editor::Zed => install_zed(home),
        Editor::Neovim | Editor::Vim => install_vim(home, editor),
    }
}

fn install_graphical(home: &Path, editor: Editor) -> Result<()> {
    let cli = if editor == Editor::Cursor { "cursor" } else { "code" };
    if env::var_os("HOME").is_some_and(|actual| Path::new(&actual) == home) && command(cli) {
        let vsix = home.join(".local/share/zoning/editor-setup/zoning.vsix");
        write_atomic(&vsix, VSIX)?;
        let install = Command::new(cli)
            .args(["--install-extension", &vsix.to_string_lossy(), "--force"])
            .output()
            .map_err(|error| format!("cannot run {cli}: {error}"))?;
        if !install.status.success() {
            return Err(format!(
                "{cli} could not install zoning: {}",
                String::from_utf8_lossy(&install.stderr).trim()
            )
            .into());
        }
        let listed = Command::new(cli)
            .arg("--list-extensions")
            .output()
            .map_err(|error| format!("cannot verify {cli}: {error}"))?;
        if !String::from_utf8_lossy(&listed.stdout)
            .lines()
            .any(|extension| extension.eq_ignore_ascii_case(EXTENSION_ID))
        {
            return Err(format!("{cli} did not report {EXTENSION_ID} after installation").into());
        }
        return Ok(());
    }
    let root = installed_path(home, editor);
    write_text(&root.join("package.json"), &vscode_manifest())?;
    write_text(&root.join("extension.js"), vscode_client())?;
    write_text(&root.join("language-configuration.json"), LANGUAGE_CONFIGURATION)?;
    write_text(&root.join("syntaxes/zoning.tmLanguage.json"), TEXTMATE_GRAMMAR)?;
    write_text(&root.join("icons/zoning.svg"), ICON)?;
    Ok(())
}

fn install_zed(home: &Path) -> Result<()> {
    let settings = if home.join("Library/Application Support/Zed").exists() {
        home.join("Library/Application Support/Zed/settings.json")
    } else {
        home.join(".config/zed/settings.json")
    };
    let existing = fs::read_to_string(&settings).unwrap_or_else(|_| "{}\n".to_owned());
    if existing.contains("\"zoning\"") {
        return Ok(());
    }
    let updated = add_zed_extension(&existing)?;
    write_text(&settings, &updated)
}

fn add_zed_extension(settings: &str) -> Result<String> {
    if settings.contains("\"zoning\"") {
        return Ok(settings.to_owned());
    }
    if let Some(key) = settings.find("\"auto_install_extensions\"") {
        let after = &settings[key..];
        let open = key + after.find('[').ok_or("Zed auto_install_extensions is not an array")?;
        let close =
            open + settings[open..].find(']').ok_or("Zed auto_install_extensions is not closed")?;
        let comma = if settings[open + 1..close].trim().is_empty() { "" } else { ", " };
        let mut updated = settings.to_owned();
        updated.insert_str(close, &format!("{comma}\"zoning\""));
        return Ok(updated);
    }
    let open = settings.find('{').ok_or("Zed settings are not a JSONC object")?;
    let mut updated = settings.to_owned();
    updated.insert_str(open + 1, "\n  \"auto_install_extensions\": [\"zoning\"],");
    Ok(updated)
}

fn install_vim(home: &Path, editor: Editor) -> Result<()> {
    let root = installed_path(home, editor);
    write_text(&root.join("ftdetect/zoning.vim"), VIM_DETECT)?;
    write_text(&root.join("syntax/zoning.vim"), VIM_SYNTAX)?;
    write_text(&root.join("ftplugin/zoning.vim"), VIM_PLUGIN)?;
    if editor == Editor::Neovim {
        write_text(&root.join("plugin/zoning.lua"), NEOVIM_LSP)?;
    }
    Ok(())
}

fn remove(home: &Path, editor: Editor) -> Result<()> {
    if editor == Editor::Zed {
        let path = installed_path(home, editor);
        let Ok(settings) = fs::read_to_string(&path) else {
            return Ok(());
        };
        let updated = settings
            .replace("\"zoning\", ", "")
            .replace(", \"zoning\"", "")
            .replace("\"zoning\"", "");
        return write_text(&path, &updated);
    }
    // Install shelled out to the editor's own CLI when one owns this HOME;
    // uninstall must reverse through the same door, or the extension the
    // editor thinks it manages outlives zoning's own bookkeeping.
    if matches!(editor, Editor::Cursor | Editor::Code) {
        let cli = if editor == Editor::Cursor { "cursor" } else { "code" };
        if env::var_os("HOME").is_some_and(|actual| Path::new(&actual) == home) && command(cli) {
            Command::new(cli)
                .args(["--uninstall-extension", EXTENSION_ID])
                .output()
                .map_err(|error| format!("cannot run {cli}: {error}"))?;
        }
    }
    let path = installed_path(home, editor);
    if path.exists() {
        fs::remove_dir_all(&path)
            .map_err(|error| format!("cannot remove {}: {error}", path.display()))?;
    }
    Ok(())
}

fn installed_path(home: &Path, editor: Editor) -> PathBuf {
    let version = env!("CARGO_PKG_VERSION");
    match editor {
        Editor::Cursor => home.join(format!(".cursor/extensions/{EXTENSION_ID}-{version}")),
        Editor::Code => home.join(format!(".vscode/extensions/{EXTENSION_ID}-{version}")),
        Editor::Zed if home.join("Library/Application Support/Zed").exists() => {
            home.join("Library/Application Support/Zed/settings.json")
        }
        Editor::Zed => home.join(".config/zed/settings.json"),
        Editor::Neovim => home.join(".local/share/nvim/site/pack/zoning/start/zoning"),
        Editor::Vim => home.join(".vim/pack/zoning/start/zoning"),
    }
}

fn state_path(home: &Path) -> PathBuf {
    home.join(".local/share/zoning/editor-setup/manifest.json")
}

fn read_manifest(path: &Path) -> Result<Option<Manifest>> {
    match fs::read(path) {
        Ok(bytes) => serde_json::from_slice(&bytes).map(Some).map_err(|error| {
            format!("cannot read zoning setup state {}: {error}", path.display()).into()
        }),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(format!("cannot read {}: {error}", path.display()).into()),
    }
}

fn write_text(path: &Path, text: &str) -> Result<()> {
    write_atomic(path, text.as_bytes())
}

fn write_atomic(path: &Path, bytes: &[u8]) -> Result<()> {
    let parent = path.parent().ok_or_else(|| format!("{} has no parent", path.display()))?;
    fs::create_dir_all(parent)
        .map_err(|error| format!("cannot create {}: {error}", parent.display()))?;
    let temporary = path.with_extension(format!("tmp-{}", std::process::id()));
    fs::write(&temporary, bytes)
        .map_err(|error| format!("cannot write {}: {error}", temporary.display()))?;
    fs::rename(&temporary, path)
        .map_err(|error| format!("cannot install {}: {error}", path.display()).into())
}

fn command(name: &str) -> bool {
    env::var_os("PATH")
        .is_some_and(|path| env::split_paths(&path).any(|dir| dir.join(name).is_file()))
}

fn home() -> Result<PathBuf> {
    env::var_os("HOME").map(PathBuf::from).ok_or_else(|| "HOME is not set".into())
}

fn vscode_manifest() -> String {
    format!(
        r#"{{
  "name": "zoning",
  "displayName": "Zoning",
  "publisher": "the-billy-company",
  "version": "{}",
  "engines": {{"vscode": "^1.85.0"}},
  "main": "./extension.js",
  "activationEvents": ["onLanguage:zoning"],
  "contributes": {{
    "languages": [{{"id": "zoning", "aliases": ["Zoning"], "extensions": [".zone"], "configuration": "./language-configuration.json", "icon": {{"light": "./icons/zoning.svg", "dark": "./icons/zoning.svg"}}}}],
    "grammars": [{{"language": "zoning", "scopeName": "source.zoning", "path": "./syntaxes/zoning.tmLanguage.json"}}]
  }}
}}"#,
        env!("CARGO_PKG_VERSION")
    )
}

fn vscode_client() -> &'static str {
    "'use strict';\nconst vscode = require('vscode');\nconst cp = require('child_process');\nexports.activate = context => { const child = cp.spawn('zoning', ['lsp', '--stdio']); context.subscriptions.push({dispose: () => child.kill()}); };\nexports.deactivate = () => undefined;\n"
}

const LANGUAGE_CONFIGURATION: &str =
    r#"{"comments":{"lineComment":"//"},"brackets":[["{","}"],["[","]"]]}"#;
const TEXTMATE_GRAMMAR: &str = r#"{"scopeName":"source.zoning","patterns":[{"name":"comment.line.double-slash.zoning","match":"//.*$"},{"name":"keyword.control.zoning","match":"\\b(package|root|language|facade|zones|seal|keep|use|forbid|cycles|limit|reach|variance|because|exclude)\\b"},{"name":"string.quoted.double.zoning","begin":"\"","end":"\""}]}"#;
const ICON: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill="currentColor" d="M2 3h12v3H2zm2 4h10v3H4zm2 4h8v3H6z"/></svg>"#;
const VIM_DETECT: &str = "autocmd BufRead,BufNewFile *.zone setfiletype zoning\n";
const VIM_SYNTAX: &str = "syntax keyword zoningKeyword package root language facade zones seal keep use forbid cycles limit reach variance because exclude\nsyntax region zoningString start=/\"/ end=/\"/\nsyntax match zoningComment /\\/\\/.*/\nhighlight default link zoningKeyword Keyword\nhighlight default link zoningString String\nhighlight default link zoningComment Comment\n";
const VIM_PLUGIN: &str =
    "setlocal commentstring=//\\ %s\nsetlocal foldmethod=marker\nsetlocal foldmarker={,}\n";
const NEOVIM_LSP: &str = "vim.api.nvim_create_autocmd('FileType', { pattern = 'zoning', callback = function(args) vim.lsp.start({ name = 'zoning', cmd = {'zoning', 'lsp', '--stdio'}, root_dir = vim.fs.root(args.buf, {'.git', 'contract'}) or vim.uv.cwd() }) end })\n";
const VSIX: &[u8] = include_bytes!("setup/payload/zoning.vsix");

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn zed_edit_is_surgical_and_idempotent() -> Result<()> {
        let original = "{\n  // mine\n  \"theme\": \"A\"\n}\n";
        let once = add_zed_extension(original)?;
        assert!(once.contains("// mine"));
        assert!(once.contains("\"auto_install_extensions\": [\"zoning\"]"));
        assert_eq!(add_zed_extension(&once)?, once);
        Ok(())
    }

    #[test]
    fn fake_home_install_repair_and_uninstall_are_owned() -> Result<()> {
        let thread_name = std::thread::current().name().unwrap_or("test").replace(':', "_");
        let home = std::env::temp_dir().join(format!(
            "zoning-setup-{}-{}",
            std::process::id(),
            thread_name
        ));
        if home.exists() {
            fs::remove_dir_all(&home)?;
        }
        fs::create_dir_all(&home)?;
        let editors = vec![Editor::Cursor, Editor::Code, Editor::Zed, Editor::Neovim, Editor::Vim];
        for editor in &editors {
            install(&home, *editor)?;
            assert!(installed_path(&home, *editor).exists());
        }
        write_atomic(
            &state_path(&home),
            &serde_json::to_vec(&Manifest {
                version: env!("CARGO_PKG_VERSION").to_owned(),
                editors: editors.clone(),
            })?,
        )?;

        let result = execute_in(Action::Uninstall, &home)?;
        assert_eq!(result, ["removed zoning-owned editor integrations"]);
        assert!(!state_path(&home).exists());
        for editor in editors {
            let path = installed_path(&home, editor);
            if editor == Editor::Zed {
                assert!(!fs::read_to_string(path)?.contains("\"zoning\""));
            } else {
                assert!(!path.exists());
            }
        }
        Ok(fs::remove_dir_all(home)?)
    }
}