alef 0.80.0

Opinionated polyglot binding generator for Rust libraries
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
use crate::core::backend::GeneratedFile;
use crate::core::config::ResolvedCrateConfig;
use crate::core::ir::ApiSurface;
use crate::core::template_versions::{maven, toolchain};
use crate::{scaffold::parse_author, scaffold::scaffold_meta, scaffold::xml_escape};
use anyhow::Context as _;
use minijinja::context;
use std::path::{Path, PathBuf};

/// Build `<dependency>` template values for host-native capsule (Language) passthrough.
/// Each `package` is a Maven `groupId:artifactId` coordinate (e.g.
/// `io.github.tree-sitter:jtreesitter`); `package_version` is the `<version>`.
fn java_capsule_dependencies(config: &ResolvedCrateConfig) -> Vec<minijinja::Value> {
    let mut deps: Vec<(String, String)> = config
        .java
        .as_ref()
        .map(|c| {
            c.capsule_types
                .values()
                .filter(|cap| !cap.package.is_empty())
                .map(|cap| (cap.package.clone(), cap.package_version.clone()))
                .collect()
        })
        .unwrap_or_default();
    deps.sort();
    deps.dedup();
    deps.iter()
        .map(|(coord, ver)| {
            let (group_id, artifact_id) = coord.split_once(':').unwrap_or((coord.as_str(), ""));
            context! {
                group_id => group_id,
                artifact_id => artifact_id,
                version => ver,
            }
        })
        .collect()
}

pub(crate) fn scaffold_java(api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
    let meta = scaffold_meta(config);
    let name = config.java_artifact_id();
    let name = name.as_str();
    let version = &api.version;

    let repo_url = meta.configured_repository.as_deref().ok_or_else(|| {
        anyhow::anyhow!(
            "Java scaffold requires package metadata repository; set package_metadata.repository or scaffold.repository"
        )
    })?;
    if meta.authors.is_empty() {
        anyhow::bail!(
            "Java scaffold requires package metadata authors; set package_metadata.authors or scaffold.authors"
        );
    }
    let license = meta.license.as_deref().ok_or_else(|| {
        anyhow::anyhow!(
            "Java scaffold requires package metadata license; set package_metadata.license or scaffold.license"
        )
    })?;

    let scm = scm_urls(repo_url);

    let group_id = config.java_group_id();
    let source_root = group_id.split('.').next().unwrap_or("dev");

    let authors: Vec<minijinja::Value> = meta
        .authors
        .iter()
        .map(|a| {
            let (name, email) = parse_author(a);
            context! {
                name => xml_escape(name),
                email => xml_escape(email),
            }
        })
        .collect();

    let license_url = match license {
        "Elastic-2.0" => "https://www.elastic.co/licensing/elastic-license",
        "MIT" => "https://opensource.org/licenses/MIT",
        "Apache-2.0" => "https://www.apache.org/licenses/LICENSE-2.0",
        _ => "",
    };

    let content = crate::scaffold::template_env::render(
        "java_pom.xml.jinja",
        context! {
            group_id => group_id,
            name => name,
            version => version,
            description => meta.description,
            repository => repo_url,
            license => license,
            license_url => license_url,
            authors => authors,
            scm_connection => scm.connection,
            scm_developer_connection => scm.developer_connection,
            capsule_deps => java_capsule_dependencies(config),
            source_root => source_root,
            java_release => toolchain::JAVA_JVM_TARGET,
            junit_version => maven::JUNIT,
            maven_core_version => maven::MAVEN_CORE,
            maven_compiler_plugin_version => maven::MAVEN_COMPILER_PLUGIN,
            maven_surefire_plugin_version => maven::MAVEN_SUREFIRE_PLUGIN,
            maven_checkstyle_plugin_version => maven::MAVEN_CHECKSTYLE_PLUGIN,
            maven_source_plugin_version => maven::MAVEN_SOURCE_PLUGIN,
            maven_javadoc_plugin_version => maven::MAVEN_JAVADOC_PLUGIN,
            maven_gpg_plugin_version => maven::MAVEN_GPG_PLUGIN,
            maven_clean_plugin_version => maven::MAVEN_CLEAN_PLUGIN,
            maven_resources_plugin_version => maven::MAVEN_RESOURCES_PLUGIN,
            maven_jar_plugin_version => maven::MAVEN_JAR_PLUGIN,
            maven_install_plugin_version => maven::MAVEN_INSTALL_PLUGIN,
            maven_deploy_plugin_version => maven::MAVEN_DEPLOY_PLUGIN,
            maven_site_plugin_version => maven::MAVEN_SITE_PLUGIN,
            central_publishing_plugin_version => maven::CENTRAL_PUBLISHING_PLUGIN,
            versions_maven_plugin_version => maven::VERSIONS_MAVEN_PLUGIN,
            maven_enforcer_plugin_version => maven::MAVEN_ENFORCER_PLUGIN,
            jacoco_maven_plugin_version => maven::JACOCO_MAVEN_PLUGIN,
            checkstyle_version => maven::CHECKSTYLE,
            jspecify_version => maven::JSPECIFY,
            jackson_version => maven::JACKSON,
            assertj_version => maven::ASSERTJ,
        },
    );

    let checkstyle_xml = r#"<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
    "https://checkstyle.org/dtds/configuration_1_3.dtd">

<!-- Checkstyle handles correctness checks only. Formatting is handled by poly. -->
<module name="Checker">
  <property name="charset" value="UTF-8" />
  <property name="severity" value="error" />
  <property name="fileExtensions" value="java" />

  <module name="SuppressionFilter">
    <property name="file" value="checkstyle-suppressions.xml" />
    <property name="optional" value="true" />
  </module>

  <module name="LineLength">
    <!-- 200 accommodates the alef-emitted DefaultClient.java FFM call shims:
             the codegen chains arena allocation, MemorySegment marshalling, and
             error-result handling onto single lines that don't reflow cleanly.
             Tests and hand-written code stay well below this; the limit only
             gives the generator headroom. -->
    <property name="max" value="200" />
    <property name="ignorePattern" value="^package.*|^import.*|a]href|href|http://|https://|ftp://" />
  </module>

  <module name="TreeWalker">
    <!-- Naming Conventions (relaxed for FFI snake_case from Rust) -->
    <module name="ConstantName">
      <property name="format" value="^([A-Z][A-Z0-9]*(_[A-Z0-9]+)*|[a-z][a-zA-Z0-9_]*)$" />
    </module>
    <module name="PackageName" />
    <module name="TypeName" />

    <!-- Modifier Checks -->
    <module name="ModifierOrder" />
    <module name="RedundantModifier" />

    <!-- Imports -->
    <module name="UnusedImports" />

    <!-- Coding -->
    <module name="EmptyStatement" />
    <module name="EqualsHashCode" />
    <module name="SimplifyBooleanExpression" />
    <module name="SimplifyBooleanReturn" />

    <!-- Size Violations -->
    <module name="MethodLength">
      <property name="max" value="150" />
    </module>

    <!-- Misc -->
    <module name="ArrayTypeStyle" />
    <module name="UpperEll" />
  </module>
</module>
"#;

    let checkstyle_properties = "";

    let checkstyle_suppressions_xml = r#"<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
    "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
    "https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
  <!-- FFI constants -->
  <suppress checks="ConstantName" files=".*FFI\.java" />
  <suppress checks="MagicNumber" files=".*FFI\.java" />

  <!-- Allow star imports and magic numbers in test files -->
  <suppress checks="AvoidStarImport" files=".*Test\.java" />
  <suppress checks="MagicNumber" files=".*Test\.java" />
  <suppress checks="MethodLength" files=".*Test\.java" />
</suppressions>
"#;

    Ok(vec![
        GeneratedFile {
            path: PathBuf::from("packages/java/pom.xml"),
            content,
            generated_header: true,
        },
        GeneratedFile {
            path: PathBuf::from("packages/java/checkstyle.xml"),
            content: checkstyle_xml.to_string(),
            generated_header: false,
        },
        GeneratedFile {
            path: PathBuf::from("packages/java/checkstyle.properties"),
            content: checkstyle_properties.to_string(),
            generated_header: false,
        },
        GeneratedFile {
            path: PathBuf::from("packages/java/checkstyle-suppressions.xml"),
            content: checkstyle_suppressions_xml.to_string(),
            generated_header: false,
        },
        GeneratedFile {
            path: PathBuf::from("packages/java/versions-rules.xml"),
            content: r#"<?xml version="1.0" encoding="UTF-8"?>
<ruleset
  xmlns="http://mojo.codehaus.org/versions-maven-plugin/rules/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rules/2.0.0
                             https://www.mojohaus.org/versions/versions-maven-plugin/xsd/rule-2.0.0.xsd"
  comparisonMethod="maven"
>
  <ignoreVersions>
    <ignoreVersion type="regex">(?i).*[.-](alpha|beta|rc|cr|milestone|preview|ea|eap|snapshot).*</ignoreVersion>
    <ignoreVersion type="regex">(?i).*[.-]m\d+.*</ignoreVersion>
  </ignoreVersions>
</ruleset>
"#
            .to_string(),
            generated_header: false,
        },
    ])
}

/// The two known-stale `LineLength` max values `scaffold_java` emitted before the fix that
/// raised the ceiling to 200 for alef-emitted FFM call shims (`a95defbf5` raised 120 -> 140,
/// `6382afdf6` raised 140 -> 200). Each is paired with the immediately following
/// `ignorePattern` property line -- unique to this one `LineLength` module in the whole
/// template -- so the anchor can never coincidentally match `MethodLength`'s own unrelated
/// `max` property (150) or anything else in the file.
const STALE_LINE_LENGTH_120: &str = "<property name=\"max\" value=\"120\"/>\n        <property name=\"ignorePattern\" value=\"^package.*|^import.*|a]href|href|http://|https://|ftp://\"/>";
const STALE_LINE_LENGTH_140: &str = "<property name=\"max\" value=\"140\"/>\n        <property name=\"ignorePattern\" value=\"^package.*|^import.*|a]href|href|http://|https://|ftp://\"/>";
const FIXED_LINE_LENGTH: &str = "<property name=\"max\" value=\"200\"/>\n        <property name=\"ignorePattern\" value=\"^package.*|^import.*|a]href|href|http://|https://|ftp://\"/>";

/// Repair a pre-existing `packages/java/checkstyle.xml` whose `LineLength` ceiling is still
/// 120 or 140 -- the exact defect fixed across two commits (`a95defbf5`, `6382afdf6`) that
/// raised the limit to 200 so `mvn verify` stops failing on alef-emitted FFM call shims in
/// `DefaultClient.java` that cannot reflow within a shorter line.
///
/// `packages/java/checkstyle.xml` is `generated_header: false` (create-only), so a repo
/// scaffolded before either fix landed keeps checkstyle's default "error" severity on every
/// regenerated facade file that legitimately needs more than 120 or 140 columns -- a build
/// failure, not a warning. 120 and 140 are the *only* two values alef itself has ever emitted
/// as this property's default (see the fix history), so an exact match against either is a
/// safe positive-identification signature: a user who happened to configure `max` at precisely
/// one of alef's own two historical defaults either never touched the line, or chose the same
/// value alef would have -- either way, advancing it to alef's current correct value is the
/// right outcome, and any other value (a deliberate customisation) is left untouched. ~keep
pub(crate) fn migrate_java_checkstyle_line_length(base_dir: &Path, relative_path: &Path) -> anyhow::Result<bool> {
    let path = crate::cli::pipeline::generate::write::contained_output_path(base_dir, relative_path)?;
    let Ok(existing) = std::fs::read_to_string(&path) else {
        return Ok(false);
    };

    let stale = [STALE_LINE_LENGTH_120, STALE_LINE_LENGTH_140]
        .into_iter()
        .find(|candidate| existing.matches(candidate).count() == 1);
    let Some(stale) = stale else {
        return Ok(false);
    };

    let migrated = existing.replacen(stale, FIXED_LINE_LENGTH, 1);

    let parent = path.parent().context("checkstyle.xml path has no parent directory")?;
    let mut temporary = tempfile::NamedTempFile::new_in(parent)
        .with_context(|| format!("failed to create temporary file in {}", parent.display()))?;
    std::io::Write::write_all(&mut temporary, migrated.as_bytes())
        .with_context(|| format!("failed to write temporary file for {}", path.display()))?;
    temporary
        .persist(&path)
        .map_err(|error| error.error)
        .with_context(|| format!("failed to replace {}", path.display()))?;
    // Fires only after the replace above already succeeded: a completed self-heal, not an
    // outstanding problem. ~keep
    tracing::info!(
        path = %path.display(),
        "repaired pre-existing packages/java/checkstyle.xml: raised LineLength max to 200 \
         to accommodate alef-emitted FFM call shims"
    );
    Ok(true)
}

struct ScmUrls {
    connection: String,
    developer_connection: String,
}

fn scm_urls(repository: &str) -> ScmUrls {
    let normalized = repository.trim_end_matches(".git");
    let without_scheme = normalized
        .strip_prefix("https://")
        .or_else(|| normalized.strip_prefix("http://"))
        .unwrap_or(normalized);
    let (host, path) = without_scheme.split_once('/').unwrap_or((without_scheme, ""));
    let suffix = if path.is_empty() {
        String::new()
    } else {
        format!("/{path}.git")
    };

    ScmUrls {
        connection: format!("scm:git:git://{host}{suffix}"),
        developer_connection: format!("scm:git:ssh://git@{host}{suffix}"),
    }
}

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

    fn checkstyle_with_line_length(max_property: &str) -> String {
        format!(
            "<?xml version=\"1.0\"?>\n<module name=\"Checker\">\n    <module name=\"LineLength\">\n        {max_property}\n        <property name=\"ignorePattern\" value=\"^package.*|^import.*|a]href|href|http://|https://|ftp://\"/>\n    </module>\n    <module name=\"TreeWalker\">\n        <module name=\"MethodLength\">\n            <property name=\"max\" value=\"150\"/>\n        </module>\n    </module>\n</module>\n"
        )
    }

    #[test]
    fn should_raise_line_length_to_200_when_stale_at_120() {
        let dir = tempfile::tempdir().expect("tempdir");
        let pkg_dir = dir.path().join("packages/java");
        std::fs::create_dir_all(&pkg_dir).expect("create packages/java");
        let stale = checkstyle_with_line_length("<property name=\"max\" value=\"120\"/>");
        std::fs::write(pkg_dir.join("checkstyle.xml"), &stale).expect("write stale checkstyle.xml");

        let relative_path = Path::new("packages/java/checkstyle.xml");
        let changed = migrate_java_checkstyle_line_length(dir.path(), relative_path).expect("must not error");
        assert!(
            changed,
            "a checkstyle.xml stuck at the old LineLength ceiling must be reported as changed"
        );

        let on_disk = std::fs::read_to_string(pkg_dir.join("checkstyle.xml")).expect("read migrated file");
        assert!(on_disk.contains("<property name=\"max\" value=\"200\"/>"));
        assert!(!on_disk.contains("value=\"120\""));
        assert!(
            on_disk.contains("<property name=\"max\" value=\"150\"/>"),
            "MethodLength's unrelated max property must survive untouched"
        );

        let changed_again = migrate_java_checkstyle_line_length(dir.path(), relative_path).expect("must not error");
        assert!(
            !changed_again,
            "second pass over an already-migrated file must be a no-op"
        );
    }

    #[test]
    fn should_raise_line_length_to_200_when_stale_at_140() {
        let dir = tempfile::tempdir().expect("tempdir");
        let pkg_dir = dir.path().join("packages/java");
        std::fs::create_dir_all(&pkg_dir).expect("create packages/java");
        let stale = checkstyle_with_line_length("<property name=\"max\" value=\"140\"/>");
        std::fs::write(pkg_dir.join("checkstyle.xml"), &stale).expect("write stale checkstyle.xml");

        let relative_path = Path::new("packages/java/checkstyle.xml");
        let changed = migrate_java_checkstyle_line_length(dir.path(), relative_path).expect("must not error");
        assert!(changed);

        let on_disk = std::fs::read_to_string(pkg_dir.join("checkstyle.xml")).expect("read migrated file");
        assert!(on_disk.contains("<property name=\"max\" value=\"200\"/>"));
        assert!(!on_disk.contains("value=\"140\""));
    }

    #[test]
    fn should_not_touch_a_checkstyle_with_a_customised_line_length() {
        let dir = tempfile::tempdir().expect("tempdir");
        let pkg_dir = dir.path().join("packages/java");
        std::fs::create_dir_all(&pkg_dir).expect("create packages/java");
        let customised = checkstyle_with_line_length("<property name=\"max\" value=\"100\"/>");
        std::fs::write(pkg_dir.join("checkstyle.xml"), &customised).expect("write customised checkstyle.xml");

        let relative_path = Path::new("packages/java/checkstyle.xml");
        let changed = migrate_java_checkstyle_line_length(dir.path(), relative_path).expect("must not error");
        assert!(
            !changed,
            "a LineLength max outside alef's two known historical defaults must never be touched"
        );

        let on_disk = std::fs::read_to_string(pkg_dir.join("checkstyle.xml")).expect("read file");
        assert_eq!(
            on_disk, customised,
            "customised checkstyle.xml must survive byte-for-byte"
        );
    }

    #[test]
    fn should_not_touch_a_checkstyle_already_at_200() {
        let dir = tempfile::tempdir().expect("tempdir");
        let pkg_dir = dir.path().join("packages/java");
        std::fs::create_dir_all(&pkg_dir).expect("create packages/java");
        let current = checkstyle_with_line_length("<property name=\"max\" value=\"200\"/>");
        std::fs::write(pkg_dir.join("checkstyle.xml"), &current).expect("write current checkstyle.xml");

        let relative_path = Path::new("packages/java/checkstyle.xml");
        let changed = migrate_java_checkstyle_line_length(dir.path(), relative_path).expect("must not error");
        assert!(!changed);
    }

    #[test]
    fn migrate_java_checkstyle_is_a_no_op_when_file_does_not_exist() {
        let dir = tempfile::tempdir().expect("tempdir");
        let relative_path = Path::new("packages/java/checkstyle.xml");
        let changed = migrate_java_checkstyle_line_length(dir.path(), relative_path).expect("must not error");
        assert!(!changed);
        assert!(!dir.path().join(relative_path).exists());
    }
}