From 7f70105e7f3991365199793d098795514461b226 Mon Sep 17 00:00:00 2001 From: Flavio Schuricht <109070137+flavioBachmann@users.noreply.github.com> Date: Mon, 1 Aug 2022 09:08:53 +0200 Subject: [PATCH] Packaging (#527) * validate Pointer incl. tests * using cargo clippy for coding style as well as cargo fmt * multi-type declarations incl. tests * subcommand as well as simple build description file * improved subcommand * improved build description file * adding libraries and parsing them * documentation and minor improvements * Pull Request improvements * package_commands for build description file * changes after review Co-authored-by: Ghaith Hachem --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + examples/plc.json | 12 +++++++++--- src/build.rs | 15 ++++----------- src/diagnostics.rs | 10 ++++++++++ src/lib.rs | 38 +++++++++++++++++++++++++++++++++++--- 6 files changed, 66 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cae7820359..a54522e2bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -596,6 +596,7 @@ dependencies = [ "regex", "serde", "serde_json", + "shell-words", "thiserror", "toml", ] @@ -688,6 +689,12 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "shell-words" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" + [[package]] name = "similar" version = "2.1.0" diff --git a/Cargo.toml b/Cargo.toml index b1e0f9de1e..efc4b79874 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1" toml = "0.5" lazy_static = "1.4.0" +shell-words = "1.1.0" [dev-dependencies] num = "0.4" diff --git a/examples/plc.json b/examples/plc.json index 4dcbbf73ad..5988a12688 100644 --- a/examples/plc.json +++ b/examples/plc.json @@ -1,8 +1,6 @@ { "files" : [ - "examples/hw.st", - "examples/hello_world.st", - "examples/ExternalFunctions.st" + "examples/simple_program.st" ], "compile_type" : "Shared", "optimization" : "Default", @@ -23,5 +21,13 @@ "examples/hello_world.st" ] } + ], + "package_commands" : [ + "cd examples", + "mkdir CommandTestFolder", + "ls", + "pwd", + "cd CommandFolder", + "touch NewFile.txt" ] } \ No newline at end of file diff --git a/src/build.rs b/src/build.rs index c906e718fc..dce21c01a2 100644 --- a/src/build.rs +++ b/src/build.rs @@ -20,26 +20,19 @@ pub struct Proj { pub files: Vec, pub compile_type: Option, pub optimization: Option, + pub target: Option, pub output: String, pub error_format: ErrorFormat, pub libraries: Option>, + pub sysroot: Option, + pub package_commands: Option>, } pub fn get_project_from_file(build_config: Option) -> Result { let filepath = build_config.unwrap_or_else(|| String::from("plc.json")); //read from file - let content = fs::read_to_string(filepath); - - let content = match content { - Ok(file_content) => file_content, - Err(e) => { - return Err(Diagnostic::GeneralError { - message: e.to_string(), - err_no: ErrNo::general__io_err, - }) - } - }; + let content = fs::read_to_string(filepath)?; //convert file to Object let project = serde_json::from_str(&content); diff --git a/src/diagnostics.rs b/src/diagnostics.rs index a5a83fb984..870245ceaa 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -1,4 +1,5 @@ use std::{ + error::Error, fmt::{self, Display}, ops::Range, }; @@ -91,6 +92,15 @@ pub enum ErrNo { linker__generic_error, } +impl From for Diagnostic { + fn from(e: T) -> Self { + Diagnostic::GeneralError { + message: e.to_string(), + err_no: ErrNo::general__io_err, + } + } +} + impl Diagnostic { pub fn syntax_error(message: &str, range: SourceRange) -> Diagnostic { Diagnostic::SyntaxError { diff --git a/src/lib.rs b/src/lib.rs index 7c49606de2..0cc8648080 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ // Copyright (c) 2020 Ghaith Hachem and Mathias Rieder -//! A Structured Text LLVM Frontent +//! A St&ructured Text LLVM Frontent //! //! RuSTy is an [`ST`] Compiler using LLVM //! @@ -17,9 +17,10 @@ //! [`ST`]: https://en.wikipedia.org/wiki/Structured_text //! [`IEC61131-3`]: https://en.wikipedia.org/wiki/IEC_61131-3 //! [`IR`]: https://llvm.org/docs/LangRef.html -use std::fs; -use std::io::Write; +use std::io::{self, Write}; +use std::process::Command; use std::str::FromStr; +use std::{env, fs}; use build::{get_project_from_file, string_to_filepath}; use clap::ArgEnum; @@ -70,6 +71,8 @@ mod validation; #[cfg(test)] extern crate pretty_assertions; +extern crate shell_words; + #[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)] pub enum FormatOption { Static, @@ -674,8 +677,37 @@ pub fn build_with_subcommand(parameters: CompileParameters) -> Result<(), Diagno &target, config_options, )?; + + if let Some(commands) = project.package_commands { + execute_commands(commands)?; + } + } + } + Ok(()) +} + +fn execute_commands(commands: Vec) -> Result<(), Diagnostic> { + let root = env::current_dir()?; + for command in commands { + let args = shell_words::split(&command)?; + + if args[0].as_str() == "cd" { + io::stdout().write_all(&[b">>> ", args[0..2].join(" ").as_bytes(), b"\n"].concat())?; + + env::set_current_dir(args[1].as_str())?; + } else { + let output = Command::new(args[0].as_str()) + .args(args[1..args.len()].to_vec()) + .output()?; + + io::stdout().write_all(&[b">>> ", args.join(" ").as_bytes(), b"\n"].concat())?; + + if !output.stdout.is_empty() { + io::stdout().write_all(&output.stdout)?; + } } } + env::set_current_dir(root)?; Ok(()) }