Skip to content

Commit a7e0eb6

Browse files
author
RogueMacro
committed
buildscripts
1 parent 36aa4d8 commit a7e0eb6

File tree

13 files changed

+315
-73
lines changed

13 files changed

+315
-73
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "grill"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
authors = ["William Tetlie <william.tetlie@gmail.com>"]
66
license-file = "LICENSE"

src/commands.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ pub mod make;
66
pub mod new;
77
pub mod publish;
88
pub mod purge;
9+
pub mod rebuild;
910
pub mod update;

src/commands/rebuild.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use std::{borrow::Cow, fs, path::Path};
2+
3+
use anyhow::Result;
4+
use indicatif::{ProgressBar, ProgressStyle};
5+
6+
use crate::{paths, prelude::*};
7+
8+
pub fn cli() -> App {
9+
App::new("rebuild")
10+
.about("Run the Build script for this package")
11+
.arg(
12+
Arg::new("all")
13+
.long("all")
14+
.short('a')
15+
.help("Rebuild all packages")
16+
.conflicts_with("pkgs"),
17+
)
18+
.arg(
19+
Arg::new("pkgs")
20+
.multiple_values(true)
21+
.help("Rebuild the specified packages"),
22+
)
23+
.arg(Arg::new("quiet").long("quiet").short('q'))
24+
}
25+
26+
pub fn exec(args: &ArgMatches) -> Result<()> {
27+
let quiet = args.is_present("quiet");
28+
29+
if args.is_present("all") {
30+
for path in fs::read_dir(paths::pkgs("."))? {
31+
rebuild(&path?.path(), quiet)?;
32+
}
33+
} else if let Some(mut pkgs) = args.values_of("pkgs") {
34+
for dir in fs::read_dir(paths::pkgs("."))? {
35+
let dir = dir?;
36+
let dir_name = dir.file_name();
37+
let ident = dir_name.to_string_lossy();
38+
let (pkg, _) = ident.rsplit_once('-').context("Invalid file name")?;
39+
40+
if pkgs.any(|v| v == ident || v == pkg) {
41+
rebuild(&dir.path(), quiet)?;
42+
}
43+
}
44+
}
45+
46+
rebuild(Path::new("."), quiet)
47+
}
48+
49+
fn rebuild(path: &Path, quiet: bool) -> Result<()> {
50+
let file_name = if path.ends_with(".") {
51+
Cow::Owned(
52+
std::env::current_dir()?
53+
.file_name()
54+
.context("Invalid file name")?
55+
.to_string_lossy()
56+
.to_string(),
57+
)
58+
} else {
59+
path.file_name()
60+
.context("Invalid file name")?
61+
.to_string_lossy()
62+
};
63+
64+
let spinner = if !quiet {
65+
let spinner = ProgressBar::new_spinner()
66+
.with_message(format!("{}", file_name))
67+
.with_style(
68+
ProgressStyle::default_spinner()
69+
.template("{prefix:>12} {msg} {spinner}")?
70+
.tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈✔"),
71+
);
72+
spinner.enable_steady_tick(std::time::Duration::from_millis(100));
73+
Some(spinner)
74+
} else {
75+
None
76+
};
77+
78+
crate::ops::rebuild(path, spinner.as_ref())?;
79+
80+
if let Some(spinner) = spinner {
81+
spinner.set_prefix(console::style("Finished").bright().green().to_string());
82+
spinner.finish();
83+
}
84+
85+
Ok(())
86+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn cli() -> App {
2323
.subcommand(commands::new::cli())
2424
.subcommand(commands::publish::cli())
2525
.subcommand(commands::purge::cli())
26+
.subcommand(commands::rebuild::cli())
2627
.subcommand(commands::update::cli())
2728
}
2829

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn main() -> Result<()> {
1818
"new" => grill::commands::new::exec(args),
1919
"publish" => grill::commands::publish::exec(args),
2020
"purge" => grill::commands::purge::exec(args),
21+
"rebuild" => grill::commands::rebuild::exec(args),
2122
"update" => grill::commands::update::exec(args),
2223
_ => bail!("Unkown command: {}", cmd),
2324
},

src/manifest.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
1111
#[serde(rename_all = "PascalCase")]
1212
pub struct Manifest {
1313
pub package: Package,
14+
pub buildscript: Option<Buildscript>,
1415
#[serde(default)]
1516
pub dependencies: HashMap<String, Dependency>,
1617
#[serde(default)]
@@ -80,6 +81,12 @@ pub struct Package {
8081
pub corlib: bool,
8182
}
8283

84+
#[derive(Serialize, Deserialize, Debug)]
85+
#[serde(rename_all = "PascalCase")]
86+
pub struct Buildscript {
87+
pub path: PathBuf,
88+
}
89+
8390
#[derive(Serialize, Deserialize, Debug)]
8491
#[serde(untagged)]
8592
pub enum Dependency {

src/ops.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
pub mod beefbuild;
12
pub mod init;
23
pub mod install;
34
pub mod make;
5+
pub mod rebuild;
46

7+
pub use init::*;
58
pub use install::*;
69
pub use make::*;
10+
pub use rebuild::*;

src/ops/beefbuild.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::{
2+
env,
3+
path::{Path, PathBuf},
4+
process::{Command, Output},
5+
};
6+
7+
use anyhow::{Context, Result};
8+
9+
pub fn run<P>(path: P) -> Result<Output>
10+
where
11+
P: AsRef<Path>,
12+
{
13+
build(&path)?;
14+
15+
let mut command = create_command(path)?;
16+
command.arg("-run");
17+
Ok(command.output()?)
18+
}
19+
20+
pub fn build<P>(path: P) -> Result<Output>
21+
where
22+
P: AsRef<Path>,
23+
{
24+
Ok(create_command(path)?.output()?)
25+
}
26+
27+
fn create_command<P>(path: P) -> Result<Command>
28+
where
29+
P: AsRef<Path>,
30+
{
31+
let beefpath =
32+
env::var("BeefPath").context("BeefPath not found. Are you sure Beef is installed?")?;
33+
let mut exe = PathBuf::from(beefpath);
34+
exe.push("bin");
35+
exe.push("BeefBuild");
36+
37+
let mut command = Command::new(exe);
38+
command.arg(format!("-workspace={}", path.as_ref().to_string_lossy()));
39+
Ok(command)
40+
}

src/ops/install.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ use crate::{
1818
/// Returns the path to the installed package and a bool indicating
1919
/// if the package was downloaded.
2020
pub fn install<S, C>(
21+
ws: &Path,
2122
pkg: S,
2223
version: &Version,
2324
index: Option<&Index>,
2425
progress_callback: C,
25-
) -> Result<(PathBuf, bool)>
26+
) -> Result<(PathBuf, PathBuf, bool)>
2627
where
2728
S: AsRef<str>,
2829
C: FnMut(git2::Progress<'_>),
@@ -49,29 +50,31 @@ where
4950
.with_context(|| format!("{} is not a version of '{}'", version, pkg))?;
5051

5152
let ident = format!("{}-{}", pkg, version);
52-
let path = paths::pkg(".", &ident);
53+
let path = paths::pkg(ws, Path::new(&ident));
5354
if path.exists() {
54-
return Ok((path, false));
55+
return Ok((paths::pkg(".", &ident), path, false));
5556
}
5657

57-
let (path, _) = install_git(
58+
let (relative_path, full_path, _) = install_git(
59+
ws,
5860
&entry.url,
5961
Some(&metadata.rev),
6062
Some(&ident),
6163
progress_callback,
6264
)?;
6365

64-
Ok((path, true))
66+
Ok((relative_path, full_path, true))
6567
}
6668

67-
/// Returns the path to the installed package and the revision
68-
/// that was checked out.
69+
/// Returns the path to the installed package, first relative to the workspace,
70+
/// then relative to the working directory. Last is the revision that was checked out.
6971
pub fn install_git<C>(
72+
ws: &Path,
7073
url: &Url,
7174
rev: Option<&str>,
7275
pkg_ident: Option<&String>,
7376
mut progress_callback: C,
74-
) -> Result<(PathBuf, String)>
77+
) -> Result<(PathBuf, PathBuf, String)>
7578
where
7679
C: FnMut(git2::Progress<'_>),
7780
{
@@ -121,26 +124,28 @@ where
121124
// Dropping the repository gives us access to the directory
122125
}
123126

124-
let path = pkg_ident
125-
.map(|ident| paths::pkg(".", ident))
127+
let relative_path = pkg_ident
128+
.map(|ident| paths::pkg("", ident))
126129
.unwrap_or_else(|| {
127130
let mut pkg = url.host().unwrap().to_string();
128131
pkg.push_str(&url.path().replace('/', "-").replace(".git", ""));
129132
pkg = format!("{}-{}", pkg, checkout_rev);
130-
paths::pkg(".", &pkg)
133+
paths::pkg("", &pkg)
131134
});
132135

133-
if path.exists() {
134-
return Ok((path, checkout_rev));
136+
let full_path = ws.join(&relative_path);
137+
138+
if full_path.exists() {
139+
return Ok((relative_path, full_path, checkout_rev));
135140
}
136141

137-
fs::rename(paths::tmp(), &path).context("Failed to move tmp folder")?;
142+
fs::rename(paths::tmp(), &full_path).context("Failed to move tmp folder")?;
138143

139144
if pkg_ident.is_some() {
140-
prepare_pkg(&path, pkg_ident.map(String::as_str))?;
145+
prepare_pkg(&full_path, pkg_ident.map(String::as_str))?;
141146
}
142147

143-
Ok((path, checkout_rev))
148+
Ok((relative_path, full_path, checkout_rev))
144149
}
145150

146151
pub fn prepare_pkg(path: &Path, ident: Option<&str>) -> Result<()> {

0 commit comments

Comments
 (0)