dsx/dsx_server repo system first implementation
This commit is contained in:
@@ -10,14 +10,26 @@ use assembler::prelude::Assembler;
|
||||
use common::build::{BuildError, Builder};
|
||||
use compiler::Compiler;
|
||||
|
||||
pub struct BuildContext {
|
||||
pub project_dir: PathBuf,
|
||||
pub build_dir: PathBuf,
|
||||
pub artifact_dir: PathBuf,
|
||||
}
|
||||
|
||||
// ---------- build ----------------------------------------------------------
|
||||
pub fn build_project(cwd: &Path) -> Result<(), BuildError> {
|
||||
let config: DsxConfig = toml::from_str(&fs::read_to_string(cwd.join("Dsx.toml"))?)
|
||||
.map_err(|deser_err| {
|
||||
pub fn build_project(ctx: BuildContext) -> Result<(), BuildError> {
|
||||
// variables
|
||||
let binary_path = ctx.artifact_dir.join("out.dsb");
|
||||
let main_path = ctx.build_dir.join("main.dsa");
|
||||
let config_path = ctx.project_dir.join("Dsx.toml");
|
||||
|
||||
let src_dir = ctx.project_dir.join("src");
|
||||
|
||||
let config: DsxConfig =
|
||||
toml::from_str(&fs::read_to_string(&config_path)?).map_err(|deser_err| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, deser_err.to_string())
|
||||
})?;
|
||||
|
||||
let src_dir = cwd.join("src");
|
||||
if !src_dir.exists() {
|
||||
return Err(BuildError::Generic(String::from(
|
||||
"Source Directory does not exist",
|
||||
@@ -35,36 +47,38 @@ pub fn build_project(cwd: &Path) -> Result<(), BuildError> {
|
||||
let (has_dsa, has_dsc) = detect_source_language(&src_dir);
|
||||
|
||||
// create a build dir and copy all files across
|
||||
let build_dir = cwd.join("build");
|
||||
fs::create_dir_all(&build_dir)?;
|
||||
env::set_current_dir(&build_dir)?;
|
||||
|
||||
copy_recursively(&src_dir, &build_dir)?;
|
||||
fs::create_dir_all(&ctx.build_dir)?;
|
||||
copy_recursively(&src_dir, &ctx.build_dir)?;
|
||||
|
||||
if has_dsc {
|
||||
build_all_dsc(&build_dir)?;
|
||||
build_all_dsc(&ctx.build_dir)?;
|
||||
}
|
||||
|
||||
// Replace .dsc with .dsa only in include statements, recursively for each file.
|
||||
let mut sed_cmd = Command::new("bash");
|
||||
sed_cmd.args([
|
||||
|
||||
let status = Command::new("bash").args([
|
||||
"-c",
|
||||
&format!(
|
||||
"find \"{}\" -type f -name '*.dsa' -exec sed -i '/^include/ s/\\.dsc/.dsa/g' {{}} +",
|
||||
build_dir.display()
|
||||
ctx.build_dir.display()
|
||||
),
|
||||
]);
|
||||
run(&mut sed_cmd);
|
||||
]).status()?;
|
||||
|
||||
if !status.success() {
|
||||
return Err(BuildError::IoError(String::from(
|
||||
"Failed to execute build command command",
|
||||
)));
|
||||
}
|
||||
|
||||
// assemble result
|
||||
{
|
||||
fs::create_dir_all(cwd.join("artifacts"))?;
|
||||
let mut asm = Assembler::new("./main.dsa");
|
||||
fs::create_dir_all(&ctx.artifact_dir)?;
|
||||
let mut asm = Assembler::new(&main_path);
|
||||
asm.start(());
|
||||
asm.write_result("../artifacts/out.dsb")?;
|
||||
asm.write_result(&binary_path)?;
|
||||
}
|
||||
|
||||
println!("Build finished. Binary at {}/main.dsb", build_dir.display());
|
||||
println!("Build finished. Binary at {}", binary_path.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -152,11 +166,3 @@ fn build_all_dsc(path: &Path) -> Result<(), BuildError> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run a command and exit on failure.
|
||||
fn run(cmd: &mut Command) {
|
||||
let status = cmd.status().expect("failed to execute command");
|
||||
if !status.success() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ pub struct DsxConfig {
|
||||
pub description: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde(rename = "remote")]
|
||||
pub remote_url: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
|
||||
Reference in New Issue
Block a user