-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into upgrade-to-rc4
- Loading branch information
Showing
26 changed files
with
1,131 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# Code generation projects | ||
|
||
This directory contains 2 projects that are used in tests for code generation and serve as an example of | ||
how to use `ion-cli` code generator under the `generate` subcommand with an existing project. | ||
|
||
## Table of contents | ||
|
||
* [/input](#input) | ||
* [/schema](#schema) | ||
* [/java](#java) | ||
* [Gradle build process](#gradle-build-process) | ||
* [/rust](#rust) | ||
* [Cargo build process](#cargo-build-process) | ||
|
||
## /input | ||
|
||
This directory contains some good and bad test Ion files based on corresponding schema in `/schema`. | ||
|
||
## /schema | ||
|
||
This directory contains all the schema files used in testing code generation with `ion-cli` `generate` subcommand. | ||
|
||
## /java | ||
|
||
This directory contains a Java project called `code-gen-demo` which is a gradle project which has tests that uses the | ||
generated code based | ||
on schema file provided in `/schema` and test Ion file provided in `/input`. | ||
|
||
### Gradle build process | ||
|
||
To generate code as part of the build process of this project, a gradle build task is defined inside `build.gradle.kts`. | ||
This task performs following steps: | ||
|
||
- Gets the executable path for `ion-cli` through an environment variable `ION_CLI`. If the environment variable is not | ||
set then it uses the local executable named `ion`. | ||
- Sets the schema directory as `/schema` which will be used by `generate` subcommand to generate code for the schema | ||
files inside it. | ||
- Sets the path to output directory where the code will be generated and sets it as source directory. | ||
- It runs the `ion-cli` `generate` subcommand with the set schema directory and a namespace where the code will be | ||
generated. | ||
|
||
Following is a sample build task you can add in an existing gradle project to generate code for your schemas, | ||
|
||
``` | ||
val ionSchemaSourceCodeDir = "YOUR_SOURCE_SCHEMA_DIRECTORY" | ||
val generatedIonSchemaModelDir = "${layout.buildDirectory.get()}/generated/java" | ||
sourceSets { | ||
main { | ||
java.srcDir(generatedIonSchemaModelDir) | ||
} | ||
} | ||
tasks { | ||
val ionCodegen = create<Exec>("ionCodegen") { | ||
inputs.files(ionSchemaSourceCodeDir) | ||
outputs.file(generatedIonSchemaModelDir) | ||
val ionCli = System.getenv("ION_CLI") ?: "ion" | ||
commandLine(ionCli) | ||
.args( | ||
"beta", "generate", | ||
"-l", "java", | ||
"-n", "NAMESPACE_FOR_GENERATED_CODE", | ||
"-d", ionSchemaSourceCodeDir, | ||
"-o", generatedIonSchemaModelDir, | ||
) | ||
.workingDir(rootProject.projectDir) | ||
} | ||
withType<JavaCompile> { | ||
options.encoding = "UTF-8" | ||
if (JavaVersion.current() != JavaVersion.VERSION_1_8) { | ||
options.release.set(8) | ||
} | ||
dependsOn(ionCodegen) | ||
} | ||
} | ||
``` | ||
|
||
## /rust | ||
|
||
This directory contains a Rust project called `code-gen-demo` which is a cargo project which has tests that uses the | ||
generated code based | ||
on schema file provided in `/schema` and test Ion file provided in `/input`. | ||
|
||
### Cargo build process | ||
|
||
To generate code as part of the build process of this cargo project, a cargo build script is defined in `build.rs`. | ||
This task performs following steps: | ||
|
||
- Gets the executable path for `ion-cli` through an environment variable `ION_CLI`. If the environment variable is not | ||
set then it uses the local executable named `ion`. | ||
- Sets the schema directory as `/schema` which will be used by `generate` subcommand to generate code for the schema | ||
files inside it. | ||
- Sets the path to output directory where the code will be generated (e.g. `OUT_DIR`). | ||
- It runs the `ion-cli` `generate` subcommand with the set schema directory and a namespace where the code will be | ||
generated. | ||
|
||
Following is sample build script you can add in your existing cargo project to generate code using `ion-cli`. | ||
|
||
```rust | ||
fn main() { | ||
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
|
||
// Invokes the ion-cli executable using environment variable ION_CLI if present, otherwise uses local executable named `ion` | ||
let ion_cli = env::var("ION_CLI").unwrap_or("ion".to_string()); | ||
println!("cargo:warn=Running command: {}", ion_cli); | ||
let mut cmd = std::process::Command::new(ion_cli); | ||
cmd.arg("beta") | ||
.arg("generate") | ||
.arg("-l") | ||
.arg("rust") | ||
.arg("-d") | ||
.arg("YOUR_SOURCE_SCHEMA_DIRECTORY") | ||
.arg("-o") | ||
.arg(&out_dir); | ||
|
||
println!("cargo:warn=Running: {:?}", cmd); | ||
|
||
let output = cmd.output().expect("failed to execute process"); | ||
|
||
println!("status: {}", output.status); | ||
io::stdout().write_all(&output.stdout).unwrap(); | ||
io::stderr().write_all(&output.stderr).unwrap(); | ||
|
||
assert!(output.status.success()); | ||
|
||
println!("cargo:rerun-if-changed=input/"); | ||
println!("cargo:rerun-if-changed=schema/"); | ||
} | ||
``` |
9 changes: 9 additions & 0 deletions
9
code-gen-projects/input/bad/nested_struct/mismatched_sequence_type.ion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// nested struct with mismatched sequence type | ||
{ | ||
A: "hello", | ||
B: 12, | ||
C: { | ||
D: false, | ||
E: (1 2 3) // expected list | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
B: 12, | ||
C: { | ||
D: 1e0, // expected type: bool | ||
E: [1, 2, 3] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
{ | ||
A: "hello", | ||
B: 12, | ||
C: [1, 2, 3], | ||
C: (1 2 3), | ||
D: 10e2 | ||
} | ||
|
7 changes: 7 additions & 0 deletions
7
code-gen-projects/input/bad/struct_with_fields/mismatched_sequence_type.ion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// simple struct with type mismatched sequence type | ||
{ | ||
A: "hello", | ||
B: 12, | ||
C: ["foo", "bar", "baz"], // expected sexp | ||
D: 10e2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
{ | ||
A: "hello", | ||
B: false, // expected field type: int | ||
C: ["foo", "bar", "baz"], | ||
C: ("foo" "bar" "baz"), | ||
D: 10e2 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
B: 12, | ||
C: { | ||
D: false, | ||
E: [1, 2, 3] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
A: "hello", | ||
C: { | ||
D: false, | ||
E: [1, 2, 3] | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
code-gen-projects/input/good/struct_with_fields/empty_values.ion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// struct with empty list, empty string and zeros | ||
{ | ||
C: [], | ||
C: (), | ||
A: "", | ||
B: 0, | ||
D: 0e0, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
{ | ||
A: "hello", | ||
B: 12, | ||
C: ["foo", "bar", "baz"], | ||
C: ("foo" "bar" "baz"), | ||
D: 10e2 | ||
} |
2 changes: 1 addition & 1 deletion
2
code-gen-projects/input/good/struct_with_fields/valid_unordered_fields.ion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
// struct with unordered fields | ||
{ | ||
C: ["foo", "bar", "baz"], | ||
C: ("foo" "bar" "baz"), | ||
A: "hello", | ||
B: 12, | ||
D: 10e2, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ type::{ | |
B: int, | ||
C: { | ||
fields: { | ||
D: bool | ||
D: bool, | ||
E: { type: list, element: int } | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.