Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DTDL Parser #50

Merged
merged 28 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/actions/install-rust-toolchain/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Install Rust Toolchain
description: |
Installs the Rust toolchain.
Relies on the fact that rustup show will install a toolchain. The installed version is based on a toolchain file, or stable if no such file is found.
inputs:
components:
description: A list of additional components to install
required: false
default: null
runs:
using: "composite"
steps:
- name: Install Rust Toolchain
# A note on using rustup show to do this (from https://rust-lang.github.io/rustup/overrides.html):
# To verify which toolchain is active, you can use rustup show,
# which will also try to install the corresponding toolchain if the current one has not been installed [...].
# (Please note that this behavior is subject to change, as detailed in issue #1397 [https://github.com/rust-lang/rustup/issues/1397].)
run: rustup show
shell: bash
- name: Install Additional Components
run: rustup component add ${{ inputs.components }}
shell: bash
if: ${{ inputs.components != null }}
4 changes: 2 additions & 2 deletions .github/workflows/notice-generation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
uses: actions/checkout@v3
with:
submodules: recursive
- name: Install stable toolchain
run: rustup show
- name: Install toolchain
uses: ./.github/actions/install-rust-toolchain
- name: Cache Dependencies
uses: Swatinem/rust-cache@v1
- name: Generate the Notice
Expand Down
21 changes: 14 additions & 7 deletions .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ jobs:
submodules: recursive
- name: Install protobuf-compiler
run: sudo apt-get install -y protobuf-compiler
- name: Install stable Rust toolchain
run: |
rustup show
rustup component add rustfmt clippy
- name: Install .NET 7.0
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0
- name: Install Rust toolchain
uses: ./.github/actions/install-rust-toolchain
with:
components: clippy rustfmt
- name: Cache Dependencies
uses: Swatinem/rust-cache@v2
- run: cargo check --workspace
- run: cargo clippy --all-targets --all-features --workspace --no-deps -- -D warnings
- run: cargo fmt --all -- --check
- name: Run doctest only
# we run doctest here as cargo tarpaulin (our test runner)
uses: actions-rs/cargo@v1
with:
command: test
Expand All @@ -52,8 +55,12 @@ jobs:
submodules: recursive
- name: Install protobuf-compiler
run: sudo apt-get install -y protobuf-compiler
- name: Install stable Rust toolchain
run: rustup show
- name: Install .NET 7.0
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0
- name: Install Rust toolchain
uses: ./.github/actions/install-rust-toolchain
- name: Cache Dependencies
uses: Swatinem/rust-cache@v2
- name: Build
Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
- [Cloning the Repo](#cloning-the-repo)
- [Developer Notes](#developer-notes)
- [JSON-LD Crate](#json-ld-crate)
- [DTDL Parser](#dtdl-parser)
- [Building](#building)
- [Running the Tests](#running-the-tests)
- [Running the Samples](#running-the-samples)
Expand Down Expand Up @@ -96,10 +95,6 @@ However, it currently has a build issue that is discussed [here](https://github.
To work around this issue you will need to use git clone to obtain the source from [here](https://github.com/blast-hardcheese/json-ld)
and checkout its "resolve-issue-40" branch. It should be cloned to a directory that is a sibling to ibeji.

### <a name="dtdl-parser">DTDL Parser</a>

There is no existing DTDL Parser for Rust, so we have provided a minimalistic one for DTDL v2 that is based on the [JavaScript DTDL Parser](https://github.com/Azure/azure-sdk-for-js/tree/%40azure/dtdl-parser_1.0.0-beta.2/sdk/digitaltwins/dtdl-parser).

## <a name="building">Building</a>

Once you have installed the prerequisites, go to your enlistment's root directory and run:
Expand Down
8 changes: 3 additions & 5 deletions core/invehicle-digital-twin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
// Module references behind feature flags. Add any necessary module references here.
// Start: Module references.

// Add a new feature to any() so the use statement is active for the feature.
// ex. #[cfg(any(feature = "feature_1", feature = "feature_2"))]
#[cfg(feature = "managed_subscribe")]
use common::grpc_interceptor::GrpcInterceptorLayer;

#[cfg(feature = "managed_subscribe")]
use managed_subscribe::managed_subscribe_module::ManagedSubscribeModule;

// End: Module references.

#[allow(unused_imports)]
use common::grpc_interceptor::GrpcInterceptorLayer;

use common::grpc_server::GrpcServer;
use core_protobuf_data_access::chariott::service_discovery::core::v1::service_registry_client::ServiceRegistryClient;
use core_protobuf_data_access::chariott::service_discovery::core::v1::{
Expand Down
23 changes: 23 additions & 0 deletions dtdl-tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# DTDL Tools

DTDL is fundamental to Ibeji. These tools will help developers to use DTDL to build their own in-vehicle digital twin model.

## DTDL Validator

DTDL Validator is an application that validates DTDL files. It uses the .NET DTDLParser.

The DTDL Validator application is built by Cargo. It can be found here: ibeji/target/debug/dtdl-validator/bin/Debug/net7.0/dtdl-validator.
It takes two command line arguments:

* -d {directory name} The directory that contains the DTDL files.
* -e {file extension} The file extension used by the DTDL files. The default is "json".

The CI/CD pipeline automatically validates DTDL files found under the ibeji/digital-twin-model/dtdl directory via dtdl-tools
test suite. Additonal directories containing DTDL files can also be checked by addining new test cases based on the one for
the ibeji/digital-twin-model/dtdl directory.

If you wish to manually run the the DTDL Validator application, then install it from Cargo's out directory to a custom directory by
using the ibeji/dtdl-tools/scripts/install-dtdl-validator.sh script. This script takes two command line arguments:

* -s {source directory} The Cargo out directory where the application was built. The default is the current directory.
* -d {destination directory} The custom directory where you want the application installed. The default is "$HOME/.cargo/bin/dtdl-validator".
10 changes: 8 additions & 2 deletions dtdl-tools/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::env;
use std::io::{self, Write};
use std::process::Command;

// This build script builds all the .NET projects in this digital_twins_connector folder.
// This build script builds all the .NET projects in this dtdl-tools folder.
// Running 'cargo build' will build the .NET projects and the Rust crates.
fn main() {
const DOTNET_COMMAND: &str = "dotnet";
const DOTNET_BUILD_ARG: &str = "build";

const CSPROJ_PATHS: &[&str] = &["src/dtdl-validator/dtdl-validator.csproj"];
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");

for csproj in CSPROJ_PATHS {
let output =
Expand All @@ -19,5 +21,9 @@ fn main() {
if !output.status.success() {
panic!("Failed to run due to {output:?}");
}

io::stdout().write_all(&output.stdout).unwrap();
}

println!("cargo:rerun-if-changed={CARGO_MANIFEST_DIR}/src/dtdl-validator");
}
42 changes: 42 additions & 0 deletions dtdl-tools/scripts/install-dtdl-validator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

set -e

# Default values
src_dir="."
dst_dir="$HOME/.cargo/bin/dtdl-validator"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be the default since it's managed by cargo and we're not using cargo to do this installation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as is for now until we have a better option.


# Parse command line options
while getopts "s:d:" opt; do
case ${opt} in
s)
src_dir="$OPTARG"
;;
d)
dst_dir="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
echo "Usage: $0 [-s source_directory] [-d destination_directory]" 1>&2
exit 1
;;
esac
done

# Check if dtdl-validator exists in the source directory
if [[ ! -f "$src_dir/dtdl-validator" ]]; then
echo "Error: dtdl-validator must exist in the source directory." 1>&2
exit 1
fi

# Create the destination directory if it does not exist
mkdir -p "$dst_dir"

# Copy dtdl-validator, its config file and all assocaited dll files to the destination directory
cp "$src_dir/dtdl-validator" "$dst_dir"
cp "$src_dir"/dtdl-validator.runtimeconfig.json "$dst_dir"
cp "$src_dir"/*.dll "$dst_dir"

echo "dtdl-validator has been successfully installed in $dst_dir"

exit 0
6 changes: 3 additions & 3 deletions dtdl-tools/src/dtdl-validator/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<BaseOutputPath>../../../target/debug/dtdl-validator/bin</BaseOutputPath>
<BaseIntermediateOutputPath>../../../target/debug/dtdl-validator/obj</BaseIntermediateOutputPath>
<MSBuildProjectExtensionsPath>../../../target/debug/dtdl-validator/obj</MSBuildProjectExtensionsPath>
<!--BaseOutputPath and BaseIntermediateOutputPath are adjusted when the project is run by a Cargo build. -->
<BaseOutputPath Condition="$(OUT_DIR) != ''">$(OUT_DIR)/$(BaseOutputPath)</BaseOutputPath>
<BaseIntermediateOutputPath Condition="$(OUT_DIR) != ''">$(OUT_DIR)/$(BaseIntermediateOutputPath)</BaseIntermediateOutputPath>
</PropertyGroup>
</Project>
11 changes: 5 additions & 6 deletions dtdl-tools/src/dtdl-validator/DtdlValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ static int ValidateDtdl(DirectoryInfo directory, String extension)

static async Task<int> Main(string[] args)
{
var directoryOption =
new Option<DirectoryInfo>(
"-d",
getDefaultValue: () => new DirectoryInfo(Directory.GetCurrentDirectory()),
var directoryArgument =
new Argument<DirectoryInfo>(
"directory",
description: "The directory that contains the DTDL files.");
var extensionOption =
new Option<string>(
Expand All @@ -93,13 +92,13 @@ static async Task<int> Main(string[] args)
int exitCode = EXIT_SUCCESS;

var cmd = new RootCommand();
cmd.AddOption(directoryOption);
cmd.AddArgument(directoryArgument);
cmd.AddOption(extensionOption);
cmd.SetHandler((directory, extension) =>
{
exitCode = ValidateDtdl(directory!, extension!);
},
directoryOption, extensionOption);
directoryArgument, extensionOption);

await cmd.InvokeAsync(args);

Expand Down
10 changes: 8 additions & 2 deletions dtdl-tools/src/dtdl-validator/dtdl-validator.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- For help on using Microsoft.NET.Sdk in project files, see https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview -->

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -7,8 +9,12 @@
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />

<!-- https://www.nuget.org/packages/DTDLParser -->
<!-- The web page for the DTDLParser can be found here: https://www.nuget.org/packages/DTDLParser -->
<PackageReference Include="DTDLParser" Version="1.0.52" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent"> <!-- Condition="Exists('$(OutputPath)/dtdl-validator')" -->
<Message Text="You can now install the dtdl-validator in the directory $(HOME)/.cargo/bin/dtdl-validator by running:" Importance="high" />
<Message Text="$(CARGO_MANIFEST_DIR)/scripts/install-dtdl-validator.sh -s $(OutputPath)" Importance="high" />
</Target>
</Project>
21 changes: 10 additions & 11 deletions dtdl-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

// This lib.rs is for running the .NET unit tests in this digital_twins_connector folder.
// This lib.rs is for running the .NET unit tests in this dtdl-tools folder.
// Running 'cargo test' will run all the .NET unit tests and the Rust unit tests.
#[cfg(test)]
mod digital_twins_connector_dotnet_tests {
mod dtdl_tools_tests {
use std::env;
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;

// The manifest directory is the directory that contains the Cargo.toml file for this crate.
const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
// The out directory is the directory that contains the build artifacts.
const OUT_DIR: &str = env!("OUT_DIR");

const DTDL_VALIDATOR_FILENAME: &str = "dtdl-validator";
const DTDL_VALIDATOR_BIN_DIR: &str = "target/debug/dtdl-validator/bin/Debug/net7.0";
const DTDL_VALIDATOR_DIR_OPTION: &str = "-d";
const DTDL_VALIDATOR_BIN_DIR: &str = "Debug/net7.0";
const DTDL_VALIDATOR_EXT_OPTION: &str = "-e";

/// Validate DTDL files.
Expand All @@ -24,13 +24,12 @@ mod digital_twins_connector_dotnet_tests {
/// * `directory` - The directory that contains the DTDL files that you wish to validate.
/// * `extension` - The file extension that the DTDL files use.
fn validate_dtdl_files(directory: &str, extension: &str) -> bool {
let dtdl_validator_command_path = Path::new(MANIFEST_DIR)
.join("..")
.join(DTDL_VALIDATOR_BIN_DIR)
.join(DTDL_VALIDATOR_FILENAME);
let dtdl_validator_command_path =
Path::new(OUT_DIR).join(DTDL_VALIDATOR_BIN_DIR).join(DTDL_VALIDATOR_FILENAME);

println!("dtdl_validator_command_path = '{:?}", dtdl_validator_command_path);

let dtdl_validator_output = Command::new(dtdl_validator_command_path)
.arg(DTDL_VALIDATOR_DIR_OPTION)
.arg(directory)
.arg(DTDL_VALIDATOR_EXT_OPTION)
.arg(extension)
Expand Down