Skip to content

Commit 3202c96

Browse files
author
RogueMacro
committed
Lots of changes:
- Package features - New command - Init command - Better logging - More dependency types - Local pkg dir instead of global - Probably more I forgot
1 parent c798a3b commit 3202c96

File tree

23 files changed

+970
-151
lines changed

23 files changed

+970
-151
lines changed

.gitignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
/target
44

5-
/dummy
6-
/dummy2
7-
/proj
8-
95
choco/tools/grill.exe
10-
*.nupkg
6+
*.nupkg
7+
8+
/testing_cmd

Cargo.lock

Lines changed: 67 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ description = "A package manager for the Beef Programming Language"
88

99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010
[dependencies]
11-
anyhow = "1.0.53"
11+
anyhow = { version = "1.0.53", features = ["backtrace"] }
1212
clap = { version = "3.0.10", features = ["cargo"] }
1313
console = "0.15.0"
1414
dialoguer = "0.10.2"
15+
either = "1.8.0"
1516
env_logger = "0.9.0"
1617
dirs = "4.0.0"
1718
git2 = "0.15.0"
1819
indicatif = "0.17.0-beta.1"
1920
itertools = "0.10.3"
21+
lazy_static = "1.4.0"
2022
log = "0.4.14"
2123
maplit = "1.0.2"
2224
multi_log = "0.1.2"

src/beef.rs

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::{
22
collections::{HashMap, HashSet},
3-
path::PathBuf,
3+
fs,
4+
path::{Path, PathBuf},
45
};
56

7+
use anyhow::{Context, Result};
68
use serde::{Deserialize, Serialize};
79

810
#[derive(Serialize, Deserialize, Debug)]
@@ -15,6 +17,7 @@ pub struct BeefSpace {
1517
pub projects: HashMap<String, ProjectEntry>,
1618
#[serde(default)]
1719
pub workspace_folders: HashMap<String, HashSet<String>>,
20+
pub workspace: Workspace,
1821

1922
#[serde(flatten)]
2023
pub other: HashMap<String, toml::Value>,
@@ -27,12 +30,13 @@ impl Default for BeefSpace {
2730
locked: Default::default(),
2831
projects: Default::default(),
2932
workspace_folders: Default::default(),
33+
workspace: Default::default(),
3034
other: Default::default(),
3135
}
3236
}
3337
}
3438

35-
#[derive(Serialize, Deserialize, Debug)]
39+
#[derive(Serialize, Deserialize, Debug, Default)]
3640
#[serde(rename_all = "PascalCase")]
3741
pub struct ProjectEntry {
3842
pub path: PathBuf,
@@ -41,13 +45,10 @@ pub struct ProjectEntry {
4145
pub other: HashMap<String, toml::Value>,
4246
}
4347

44-
impl Default for ProjectEntry {
45-
fn default() -> Self {
46-
Self {
47-
path: Default::default(),
48-
other: Default::default(),
49-
}
50-
}
48+
#[derive(Serialize, Deserialize, Debug, Default)]
49+
#[serde(rename_all = "PascalCase")]
50+
pub struct Workspace {
51+
pub startup_project: String,
5152
}
5253

5354
#[derive(Serialize, Deserialize, Debug)]
@@ -59,13 +60,68 @@ pub struct BeefProj {
5960
pub project: Project,
6061

6162
#[serde(flatten)]
62-
pub other: HashMap<String, toml::Value>,
63+
other: HashMap<String, toml::Value>,
64+
65+
#[serde(skip)]
66+
path: PathBuf,
6367
}
6468

65-
#[derive(Serialize, Deserialize, Debug)]
69+
impl BeefProj {
70+
pub fn new<P>(name: String, path: &P) -> BeefProj
71+
where
72+
P: AsRef<Path>,
73+
{
74+
let startup_object = format!("{}.Program", name);
75+
76+
Self {
77+
file_version: 1,
78+
dependencies: Default::default(),
79+
project: Project {
80+
name,
81+
target_type: String::from("BeefConsoleApplication"),
82+
startup_object,
83+
..Default::default()
84+
},
85+
other: Default::default(),
86+
path: path.as_ref().to_path_buf(),
87+
}
88+
}
89+
90+
pub fn from_file<P>(path: &P) -> Result<BeefProj>
91+
where
92+
P: AsRef<Path>,
93+
{
94+
let mut proj: Self = toml::from_str(&fs::read_to_string(&path)?).with_context(|| {
95+
format!("Failed to read project file '{}'", path.as_ref().display())
96+
})?;
97+
proj.path = path.as_ref().to_path_buf();
98+
Ok(proj)
99+
}
100+
101+
pub fn save(&self) -> Result<()> {
102+
fs::write(&self.path, toml::to_string(&self)?)
103+
.with_context(|| format!("Failed to write project file: '{}'", self.path.display()))
104+
}
105+
106+
pub fn path<P>(&mut self, path: &P) -> &mut Self
107+
where
108+
P: AsRef<Path>,
109+
{
110+
self.path = path.as_ref().to_path_buf();
111+
self
112+
}
113+
}
114+
115+
#[derive(Serialize, Deserialize, Debug, Default)]
66116
#[serde(rename_all = "PascalCase")]
67117
pub struct Project {
68118
pub name: String,
119+
#[serde(default)]
120+
pub target_type: String,
121+
#[serde(default)]
122+
pub startup_object: String,
123+
#[serde(default)]
124+
pub processor_macros: HashSet<String>,
69125

70126
#[serde(flatten)]
71127
pub other: HashMap<String, toml::Value>,

src/commands.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
pub mod init;
12
pub mod install;
23
pub mod list;
34
pub mod login;
45
pub mod make;
6+
pub mod new;
57
pub mod publish;
68
pub mod purge;
79
pub mod update;

src/commands/init.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::{fs, path::Path};
2+
3+
use crate::{beef::BeefProj, prelude::*};
4+
5+
pub fn cli() -> App {
6+
App::new("init")
7+
.about("Initialize an existing beef workspace with grill")
8+
.arg(
9+
Arg::new("path")
10+
.long("path")
11+
.value_name("PATH")
12+
.default_value("."),
13+
)
14+
}
15+
16+
pub fn exec(args: &ArgMatches) -> Result<()> {
17+
let path = Path::new(args.value_of("path").unwrap());
18+
let proj_path = path.join("BeefProj.toml");
19+
let name = if proj_path.exists() {
20+
BeefProj::from_file(&proj_path)?.project.name
21+
} else {
22+
fs::canonicalize(std::env::current_dir()?.join(path))?
23+
.file_name()
24+
.context("Invalid filename")?
25+
.to_string_lossy()
26+
.to_string()
27+
};
28+
29+
crate::ops::init::init(path, &name)
30+
}

src/commands/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
9090
let mut already_installed_prompt =
9191
"This package is already installed, do you want to update it?".to_owned();
9292
let has_manifest = if let Ok(file) = fs::read_to_string(manifest_path) {
93-
let manifest: Manifest = toml::from_str(&file)?;
93+
let manifest = Manifest::from_file(&file)?;
9494
pkg = manifest.package.name;
9595

9696
let pkg_path = paths::beeflib(&pkg);

src/commands/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn exec(args: &ArgMatches) -> Result<()> {
1414
let dir = if args.is_present("themes") {
1515
paths::themes()
1616
} else {
17-
paths::pkgs()
17+
paths::pkgs(".")
1818
};
1919

2020
for entry in fs::read_dir(dir)? {

src/commands/make.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@ pub fn cli() -> App {
1313
.help("Path to the workspace"),
1414
)
1515
.arg(Arg::new("quiet").long("quiet").short('q'))
16+
.arg(
17+
Arg::new("fix-pkg")
18+
.long("fix")
19+
.value_name("PACKAGE")
20+
.help("Run make for an installed package")
21+
.conflicts_with("path"),
22+
)
1623
}
1724

1825
pub fn exec(args: &ArgMatches) -> Result<()> {
19-
let path = PathBuf::from(args.value_of("path").unwrap());
20-
crate::ops::make(&path, args.is_present("quiet"))
26+
if let Some(pkg) = args.value_of("fix-pkg") {
27+
let pkg_path = crate::paths::pkg(args.value_of("path").unwrap(), pkg);
28+
if !pkg_path.exists() {
29+
bail!("Package '{}' is not installed. Did you include the right version? I.e. Dummy-1.2.3", pkg)
30+
}
31+
log::debug!("Fixing {}", pkg);
32+
crate::ops::install::prepare_pkg(&pkg_path, None)
33+
} else {
34+
let path = PathBuf::from(args.value_of("path").unwrap());
35+
crate::ops::make(&path, args.is_present("quiet"))
36+
}
2137
}

0 commit comments

Comments
 (0)