Skip to content

Commit

Permalink
Packaging (#527)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
flavioBachmann and ghaith authored Aug 1, 2022
1 parent ac8a601 commit 7f70105
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 9 additions & 3 deletions examples/plc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"files" : [
"examples/hw.st",
"examples/hello_world.st",
"examples/ExternalFunctions.st"
"examples/simple_program.st"
],
"compile_type" : "Shared",
"optimization" : "Default",
Expand All @@ -23,5 +21,13 @@
"examples/hello_world.st"
]
}
],
"package_commands" : [
"cd examples",
"mkdir CommandTestFolder",
"ls",
"pwd",
"cd CommandFolder",
"touch NewFile.txt"
]
}
15 changes: 4 additions & 11 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,19 @@ pub struct Proj {
pub files: Vec<String>,
pub compile_type: Option<FormatOption>,
pub optimization: Option<OptimizationLevel>,
pub target: Option<String>,
pub output: String,
pub error_format: ErrorFormat,
pub libraries: Option<Vec<Libraries>>,
pub sysroot: Option<String>,
pub package_commands: Option<Vec<String>>,
}

pub fn get_project_from_file(build_config: Option<String>) -> Result<Proj, Diagnostic> {
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);
Expand Down
10 changes: 10 additions & 0 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
error::Error,
fmt::{self, Display},
ops::Range,
};
Expand Down Expand Up @@ -91,6 +92,15 @@ pub enum ErrNo {
linker__generic_error,
}

impl<T: Error> From<T> 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 {
Expand Down
38 changes: 35 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
//!
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<String>) -> 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(())
}

Expand Down

0 comments on commit 7f70105

Please sign in to comment.