shared-context-engineering 0.3.0

Shared Context Engineering CLI
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
use sha2::{Digest, Sha256};
use std::{
    env,
    fmt::Write,
    fs,
    io::{self, Write as IoWrite},
    path::{Path, PathBuf},
    process::Command,
};

const TARGETS: &[TargetSpec] = &[
    TargetSpec {
        const_name: "OPENCODE_EMBEDDED_ASSETS",
        relative_root: "assets/generated/config/opencode",
    },
    TargetSpec {
        const_name: "CLAUDE_EMBEDDED_ASSETS",
        relative_root: "assets/generated/config/claude",
    },
    TargetSpec {
        const_name: "HOOK_EMBEDDED_ASSETS",
        relative_root: "assets/hooks",
    },
];

const MIGRATIONS_ROOT: &str = "migrations";
const GENERATED_MIGRATIONS_PATH: &str = "src/generated_migrations.rs";

struct TargetSpec {
    const_name: &'static str,
    relative_root: &'static str,
}

fn main() {
    if let Err(error) = generate_embedded_asset_manifest() {
        panic!("failed to generate setup embedded asset manifest: {error}");
    }

    if let Err(error) = generate_migration_manifest() {
        panic!("failed to generate embedded migration manifest: {error}");
    }

    emit_git_commit();
    emit_repo_version();
}

fn generate_migration_manifest() -> io::Result<()> {
    println!("cargo:rerun-if-changed=build.rs");

    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| invalid_data(&e))?);
    let migrations_root = manifest_dir.join(MIGRATIONS_ROOT);
    let destination_path = manifest_dir.join(GENERATED_MIGRATIONS_PATH);

    println!("cargo:rerun-if-changed={}", migrations_root.display());

    let mut databases = collect_migration_databases(&migrations_root)?;
    databases.sort_unstable_by(|a, b| a.directory_name.cmp(&b.directory_name));

    let mut output = String::new();
    output.push_str("// @generated by build.rs; do not edit by hand.\n");
    output.push_str("#![allow(dead_code)]\n\n");

    for database in &databases {
        output.push_str("#[rustfmt::skip]\n");
        output.push_str("pub static ");
        output.push_str(&database.const_name);
        output.push_str(": &[(&str, &str)] = &[\n");

        for migration in &database.migrations {
            println!(
                "cargo:rerun-if-changed={}",
                migration.absolute_path.display()
            );
            writeln!(
                output,
                "    (\"{}\", include_str!(\"{}\")),",
                escape_for_rust_string(&migration.id),
                escape_for_rust_string(&migration.include_path),
            )
            .expect("writing to String buffer should never fail");
        }

        output.push_str("];\n\n");
    }

    let trimmed = format!("{}\n", output.trim_end());
    write_if_changed(&destination_path, trimmed.as_bytes())
}

fn emit_git_commit() {
    println!("cargo:rerun-if-env-changed=SCE_GIT_COMMIT");

    if let Ok(commit) = env::var("SCE_GIT_COMMIT") {
        let commit = commit.trim();
        if !commit.is_empty() {
            println!("cargo:rustc-env=SCE_GIT_COMMIT={commit}");
            return;
        }
    }

    let manifest_dir = match env::var("CARGO_MANIFEST_DIR") {
        Ok(value) => PathBuf::from(value),
        Err(_) => return,
    };

    let repository_root = match manifest_dir.parent() {
        Some(path) => path.to_path_buf(),
        None => return,
    };

    let git_dir = repository_root.join(".git");
    println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
    println!(
        "cargo:rerun-if-changed={}",
        git_dir.join("packed-refs").display()
    );

    let output = Command::new("git")
        .args(["rev-parse", "--short=12", "HEAD"])
        .current_dir(&repository_root)
        .output();

    let Ok(output) = output else {
        return;
    };

    if !output.status.success() {
        return;
    }

    let Ok(commit) = String::from_utf8(output.stdout) else {
        return;
    };

    let commit = commit.trim();
    if !commit.is_empty() {
        println!("cargo:rustc-env=SCE_GIT_COMMIT={commit}");
    }
}

fn emit_repo_version() {
    let manifest_dir = match env::var("CARGO_MANIFEST_DIR") {
        Ok(value) => PathBuf::from(value),
        Err(_) => return,
    };

    let repository_root = match manifest_dir.parent() {
        Some(path) => path.to_path_buf(),
        None => return,
    };

    let version_path = repository_root.join(".version");
    println!("cargo:rerun-if-changed={}", version_path.display());

    let Ok(bytes) = fs::read(&version_path) else {
        return;
    };

    let version = String::from_utf8_lossy(&bytes);
    let version = version.trim();
    if !version.is_empty() {
        println!("cargo:rustc-env=SCE_VERSION={version}");
    }
}

fn generate_embedded_asset_manifest() -> io::Result<()> {
    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| invalid_data(&e))?);
    let out_dir = PathBuf::from(env::var("OUT_DIR").map_err(|e| invalid_data(&e))?);
    let destination_path = out_dir.join("setup_embedded_assets.rs");

    let mut output = String::new();

    for target in TARGETS {
        let source_root = manifest_dir.join(target.relative_root);
        println!("cargo:rerun-if-changed={}", source_root.display());

        let mut files = Vec::new();
        collect_files(&source_root, &source_root, &mut files)?;
        files.sort_unstable_by(|a, b| a.relative_path.cmp(&b.relative_path));

        writeln!(
            output,
            "pub static {}: &[EmbeddedAsset] = &[",
            target.const_name
        )
        .expect("writing to String buffer should never fail");

        for file in &files {
            println!("cargo:rerun-if-changed={}", file.absolute_path.display());
            let bytes = fs::read(&file.absolute_path)?;
            let sha256 = compute_sha256(&bytes);
            writeln!(
                output,
                "    EmbeddedAsset {{ relative_path: \"{}\", bytes: {}, sha256: {} }},",
                escape_for_rust_string(&file.relative_path),
                format_byte_literal("&[", &bytes),
                format_byte_literal("[", &sha256),
            )
            .expect("writing to String buffer should never fail");
        }

        output.push_str("];\n\n");
    }

    let mut output_file = fs::File::create(destination_path)?;
    output_file.write_all(output.as_bytes())
}

#[derive(Debug)]
struct MigrationDatabase {
    directory_name: String,
    const_name: String,
    migrations: Vec<MigrationFile>,
}

#[derive(Debug)]
struct MigrationFile {
    absolute_path: PathBuf,
    id: String,
    include_path: String,
    sort_prefix: u64,
}

fn collect_migration_databases(migrations_root: &Path) -> io::Result<Vec<MigrationDatabase>> {
    let mut databases = Vec::new();

    for entry in fs::read_dir(migrations_root)? {
        let entry = entry?;

        if !entry.file_type()?.is_dir() {
            continue;
        }

        let directory_path = entry.path();
        let directory_name = entry
            .file_name()
            .to_str()
            .ok_or_else(|| invalid_data(&"non-UTF-8 migration directory names are not supported"))?
            .to_owned();

        println!("cargo:rerun-if-changed={}", directory_path.display());

        let mut migrations = collect_migration_files(&directory_name, &directory_path)?;
        migrations.sort_unstable_by(|a, b| {
            a.sort_prefix
                .cmp(&b.sort_prefix)
                .then_with(|| a.id.cmp(&b.id))
        });

        databases.push(MigrationDatabase {
            const_name: migration_const_name(&directory_name)?,
            directory_name,
            migrations,
        });
    }

    Ok(databases)
}

fn collect_migration_files(
    directory_name: &str,
    directory_path: &Path,
) -> io::Result<Vec<MigrationFile>> {
    let mut migrations = Vec::new();

    for entry in fs::read_dir(directory_path)? {
        let entry = entry?;
        let path = entry.path();

        if entry.file_type()?.is_dir()
            || path.extension().and_then(|value| value.to_str()) != Some("sql")
        {
            continue;
        }

        let id = path
            .file_stem()
            .and_then(|value| value.to_str())
            .ok_or_else(|| invalid_data(&"non-UTF-8 migration filenames are not supported"))?
            .to_owned();

        migrations.push(MigrationFile {
            absolute_path: path,
            include_path: format!("../{MIGRATIONS_ROOT}/{directory_name}/{id}.sql"),
            sort_prefix: migration_sort_prefix(&id)?,
            id,
        });
    }

    Ok(migrations)
}

fn migration_sort_prefix(id: &str) -> io::Result<u64> {
    let prefix = id
        .split_once('_')
        .map(|(prefix, _)| prefix)
        .ok_or_else(|| invalid_data(&format!("migration filename '{id}' must contain '_'")))?;

    if prefix.is_empty() || !prefix.chars().all(|character| character.is_ascii_digit()) {
        return Err(invalid_data(&format!(
            "migration filename '{id}' must start with a numeric prefix"
        )));
    }

    prefix.parse::<u64>().map_err(|error| invalid_data(&error))
}

fn migration_const_name(directory_name: &str) -> io::Result<String> {
    let mut const_name = String::new();

    for character in directory_name.chars() {
        if character.is_ascii_alphanumeric() {
            const_name.push(character.to_ascii_uppercase());
        } else if character == '-' || character == '_' {
            const_name.push('_');
        } else {
            return Err(invalid_data(&format!(
                "migration directory '{directory_name}' contains unsupported character '{character}'"
            )));
        }
    }

    if const_name.is_empty() {
        return Err(invalid_data(&"migration directory name cannot be empty"));
    }

    const_name.push_str("_MIGRATIONS");
    Ok(const_name)
}

#[derive(Debug)]
struct SourceFile {
    absolute_path: PathBuf,
    relative_path: String,
}

fn collect_files(
    base_root: &Path,
    current_dir: &Path,
    output: &mut Vec<SourceFile>,
) -> io::Result<()> {
    for entry in fs::read_dir(current_dir)? {
        let entry = entry?;
        let path = entry.path();

        if entry.file_type()?.is_dir() {
            collect_files(base_root, &path, output)?;
            continue;
        }

        let relative_path = path
            .strip_prefix(base_root)
            .map_err(|_| invalid_data(&"failed to strip source root from file path"))?;

        let relative_path = normalize_relative_path(relative_path)?;

        output.push(SourceFile {
            absolute_path: path,
            relative_path,
        });
    }

    Ok(())
}

fn normalize_relative_path(path: &Path) -> io::Result<String> {
    let normalized = path
        .to_str()
        .ok_or_else(|| invalid_data(&"non-UTF-8 config paths are not supported"))?
        .replace('\\', "/");

    if normalized.is_empty() {
        return Err(invalid_data(&"relative path cannot be empty"));
    }

    if normalized.starts_with('/') {
        return Err(invalid_data(&"relative path must not start with '/'"));
    }

    Ok(normalized)
}

fn escape_for_rust_string(value: &str) -> String {
    value.replace('\\', "\\\\").replace('"', "\\\"")
}

fn compute_sha256(bytes: &[u8]) -> [u8; 32] {
    let digest = Sha256::digest(bytes);
    digest.into()
}

fn format_byte_literal(prefix: &str, bytes: &[u8]) -> String {
    format!(
        "{prefix}{}]",
        bytes
            .iter()
            .map(|byte| format!("0x{byte:02x}"))
            .collect::<Vec<_>>()
            .join(", ")
    )
}

fn write_if_changed(path: &Path, bytes: &[u8]) -> io::Result<()> {
    if fs::read(path).is_ok_and(|existing| existing == bytes) {
        return Ok(());
    }

    fs::write(path, bytes)
}

fn invalid_data<E: ToString>(error: &E) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, error.to_string())
}