Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Run prebuild script hook in build dir
Browse files Browse the repository at this point in the history
Resolves #54 #55
  • Loading branch information
andylokandy committed May 31, 2019
1 parent 2fd2c97 commit 4501ea6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
37 changes: 13 additions & 24 deletions src/lib/build/job.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::{
compile_bin, compile_doc, compile_lib, context::BuildContext, run_script, Target, Targets,
};
use super::{compile_bin, compile_doc, compile_lib, context::BuildContext, Target, Targets};
use crate::{
retrieve::cache::{Binary, BuildHash, OutputLayout, Source},
util::{
Expand Down Expand Up @@ -206,19 +204,6 @@ impl JobQueue {
let mut res: Option<Binary> = None;
let has_lib = ts.has_lib();

// First, if there's a prebuild script, execute it
if let Some(s) = source.meta().scripts.get("prebuild") {
shell.println(
style("Running").dim(),
format!("prebuild script > {}", s),
Verbosity::Verbose,
);
shell.println_plain(
fmt_multiple(&run_script(source.path(), s)?),
Verbosity::Normal,
);
}

for t in ts.0.to_vec() {
match t {
Target::Lib(cg) => {
Expand All @@ -228,14 +213,16 @@ impl JobQueue {
"target" => cg,
"summary" => source.summary()
);
let out = compile_lib(&source, cg, &deps, &layout, bcx)
.with_context(|e| {
format!(
"Couldn't build library target for {}\n{}",
source.pretty_summary(),
e
)
})?;
let out = compile_lib(
&source, cg, &deps, &layout, bcx, shell,
)
.with_context(|e| {
format!(
"Couldn't build library target for {}\n{}",
source.pretty_summary(),
e
)
})?;

res = if job_index == NodeIndex::new(0)
&& root_ol.is_some()
Expand Down Expand Up @@ -267,6 +254,7 @@ impl JobQueue {
&deps,
&layout,
bcx,
shell,
)
.with_context(|e| {
format!(
Expand Down Expand Up @@ -312,6 +300,7 @@ impl JobQueue {
&deps,
&layout,
bcx,
shell,
)
.with_context(|e| {
format!(
Expand Down
33 changes: 30 additions & 3 deletions src/lib/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ use self::{
use crate::{
retrieve::cache::{Binary, OutputLayout, Source},
util::{
clear_dir, copy_dir, copy_dir_iter, errors::Res, fmt_output, generate_ipkg,
shell::OutputGroup, valid_file,
clear_dir, copy_dir, copy_dir_iter,
errors::Res,
fmt_multiple, fmt_output, generate_ipkg,
shell::{OutputGroup, Shell, Verbosity},
valid_file,
},
};
use console::style;
use failure::{bail, format_err, ResultExt};
use itertools::Itertools;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
Expand Down Expand Up @@ -135,6 +139,7 @@ pub fn compile_lib(
deps: &[&Binary],
layout: &OutputLayout,
bcx: &BuildContext,
shell: Shell,
) -> Res<OutputGroup> {
let lib_target = source.meta().targets.lib.clone().ok_or_else(|| {
format_err!(
Expand Down Expand Up @@ -175,6 +180,8 @@ pub fn compile_lib(
clear_dir(&layout.build.join("lib"))?;
copy_dir_iter(src_walker, &src_path, &layout.build.join("lib"))?;

run_prebuild_script(source, &layout.build.join("lib"), shell)?;

let invocation = CompileInvocation {
deps,
targets: &targets,
Expand Down Expand Up @@ -238,6 +245,7 @@ pub fn compile_bin(
deps: &[&Binary],
layout: &OutputLayout,
bcx: &BuildContext,
shell: Shell,
) -> Res<(OutputGroup, Option<PathBuf>)> {
if bcx.compiler.flavor().is_idris2() {
bail!("The Idris 2 compiler currently can't build executables")
Expand All @@ -260,13 +268,16 @@ pub fn compile_bin(
clear_dir(&layout.build.join("bin"))?;
copy_dir(&src_path, &layout.build.join("bin"), false)?;

run_prebuild_script(source, &layout.build.join("bin"), shell)?;

// Check extension etc
let target_path = if let Some(ext) = target_path.extension() {
if ext != OsStr::new("idr") && ext != OsStr::new("lidr") {
let mod_name = &*target_path
.with_extension("")
.to_string_lossy()
.replace("/", ".");
.replace("/", ".")
.replace("\\", ".");
make_main_file(mod_name, &*ext.to_string_lossy(), &layout.build.join("bin"))?
} else {
target_path
Expand Down Expand Up @@ -424,6 +435,22 @@ pub fn run_script(root: &Path, cmd: &str) -> Res<OutputGroup> {
Ok(res.into())
}

pub fn run_prebuild_script(source: &Source, root: &Path, shell: Shell) -> Res<()> {
if let Some(s) = source.meta().scripts.get("prebuild") {
shell.println(
style("Running").dim(),
format!("prebuild script > {}", s),
Verbosity::Verbose,
);
shell.println_plain(
fmt_multiple(&run_script(root, s)?),
Verbosity::Normal,
);
}

Ok(())
}

fn make_main_file(module: &str, fun: &str, parent: &Path) -> Res<PathBuf> {
let rstr: String = thread_rng().sample_iter(&Alphanumeric).take(8).collect();
let fname = format!("elba-{}.idr", rstr);
Expand Down

0 comments on commit 4501ea6

Please sign in to comment.