1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use anyhow::{Context, Error};
use codespan_reporting::{
files::SimpleFile,
term::{termcolor::StandardStream, Config, termcolor::ColorChoice},
};
use hotg_rune_codegen::{
Compilation, DefaultEnvironment, GitSpecifier, RuneProject, Verbosity,
};
use hotg_rune_syntax::{hir::Rune, yaml::Document, Diagnostics};
use std::path::{Path, PathBuf};
use once_cell::sync::Lazy;
#[derive(Debug, Clone, PartialEq, structopt::StructOpt)]
pub struct Build {
#[structopt(parse(from_os_str), default_value = "Runefile")]
runefile: PathBuf,
#[structopt(short, long, parse(from_os_str))]
output: Option<PathBuf>,
#[structopt(long, env)]
cache_dir: Option<PathBuf>,
#[structopt(short, long, env)]
current_dir: Option<PathBuf>,
#[structopt(short, long)]
name: Option<String>,
#[structopt(short, long, conflicts_with = "verbose")]
quiet: bool,
#[structopt(short, long, conflicts_with = "quiet")]
verbose: bool,
#[structopt(long)]
debug: bool,
}
impl Build {
pub fn execute(self, color: ColorChoice) -> Result<(), Error> {
let verbosity =
Verbosity::from_quiet_and_verbose(self.quiet, self.verbose)
.context(
"The --verbose and --quiet flags can't be used together",
)?;
let rune = analyze(&self.runefile, color)?;
let current_directory = self.current_directory()?;
let name = self.name()?;
let working_directory = self
.cache_dir
.unwrap_or_else(|| Path::new(&*DEFAULT_CACHE_DIR).join(&name));
let dest = self.output.unwrap_or_else(|| {
current_directory.join(&name).with_extension("rune")
});
log::debug!(
"Compiling {} in \"{}\"",
name,
working_directory.display()
);
let compilation = Compilation {
name,
rune,
current_directory,
working_directory,
verbosity,
rune_project: locate_rune_dependencies(),
optimized: !self.debug,
};
let mut env = DefaultEnvironment::for_compilation(&compilation)
.with_build_info(crate::version::version().clone());
let blob = hotg_rune_codegen::generate_with_env(compilation, &mut env)
.context("Rune compilation failed")?;
log::debug!("Generated {} bytes", blob.len());
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent).with_context(|| {
format!(
"Unable to create the \"{}\" directory",
parent.display()
)
})?;
}
std::fs::write(&dest, &blob).with_context(|| {
format!("Unable to write to \"{}\"", dest.display())
})?;
log::info!("The Rune was written to \"{}\"", dest.display());
Ok(())
}
fn current_directory(&self) -> Result<PathBuf, Error> {
if let Some(dir) = &self.current_dir {
return Ok(dir.clone());
}
if let Some(parent) =
self.runefile.parent().and_then(|p| p.canonicalize().ok())
{
return Ok(parent);
}
std::env::current_dir()
.context("Unable to determine the current directory")
}
fn name(&self) -> Result<String, Error> {
if let Some(name) = &self.name {
return Ok(name.clone());
}
let current_dir = self.current_directory()?;
if let Some(name) = current_dir.file_name().and_then(|n| n.to_str()) {
return Ok(name.to_string());
}
Err(Error::msg("Unable to determine the Rune's name"))
}
}
fn locate_rune_dependencies() -> RuneProject {
if let Some(root_dir) = rune_repo_root() {
RuneProject::Disk(root_dir)
} else if let Some(git) = crate::version::version()
.version_control
.as_ref()
.and_then(|v| v.git())
{
RuneProject::Git {
repo: RuneProject::GITHUB_REPO.into(),
specifier: GitSpecifier::Commit(git.commit_id.clone()),
}
} else {
RuneProject::Git {
repo: RuneProject::GITHUB_REPO.into(),
specifier: GitSpecifier::Tag(String::from("nightly")),
}
}
}
fn rune_repo_root() -> Option<PathBuf> {
let current_dir = std::env::current_dir().unwrap();
for parent in current_dir.ancestors() {
if parent.join(".git").exists()
&& parent.join("images").exists()
&& parent.join("proc-blocks").exists()
{
return Some(parent.to_path_buf());
}
}
None
}
static DEFAULT_CACHE_DIR: Lazy<String> = Lazy::new(|| {
let cache_dir = dirs::cache_dir()
.or_else(|| dirs::home_dir())
.unwrap_or_else(|| PathBuf::from("."));
cache_dir.join("runes").to_string_lossy().into_owned()
});
pub(crate) fn analyze(
runefile: &Path,
color: ColorChoice,
) -> Result<Rune, Error> {
let src = std::fs::read_to_string(runefile).with_context(|| {
format!("Unable to read \"{}\"", runefile.display())
})?;
let file = SimpleFile::new(runefile.display().to_string(), &src);
log::debug!("Parsing \"{}\"", runefile.display());
let mut diags = Diagnostics::new();
let parsed =
Document::parse(&src).context("Unable to parse the Runefile")?;
let rune = hotg_rune_syntax::analyse(&parsed, &mut diags);
let mut writer = StandardStream::stdout(color);
let config = Config::default();
for diag in &diags {
codespan_reporting::term::emit(&mut writer, &config, &file, diag)
.context("Unable to print the diagnostic")?;
}
if diags.has_errors() {
anyhow::bail!("Aborting compilation due to errors.");
}
Ok(rune)
}