Skip to content

Commit

Permalink
read version from virtual fs
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson committed Sep 27, 2023
1 parent dd22519 commit 2d2f866
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
13 changes: 9 additions & 4 deletions bin/src/config/project/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{mem::MaybeUninit, path::Path};
use git2::Repository;
use hemtt_common::version::Version;
use serde::{Deserialize, Serialize};
use vfs::VfsPath;

use crate::error::Error;

Expand Down Expand Up @@ -35,15 +36,15 @@ impl Options {
///
/// Returns an error if the version is not a valid semver version
/// or a points to a file that does not contain a valid version macro
pub fn get(&self) -> Result<Version, Error> {
pub fn get(&self, vfs: Option<&VfsPath>) -> Result<Version, Error> {
// Check for a cached version
unsafe {
if INIT {
return Ok(VERSION.assume_init_ref().clone());
}
}

let mut version = self._get()?;
let mut version = self._get(vfs)?;

if let Some(length) = self.git_hash() {
let repo = Repository::discover(".")?;
Expand All @@ -67,7 +68,7 @@ impl Options {
}
}

fn _get(&self) -> Result<Version, Error> {
fn _get(&self, vfs: Option<&VfsPath>) -> Result<Version, Error> {
// Check for a defined major version
if let Some(major) = self.major {
trace!("reading version from project.toml");
Expand All @@ -93,7 +94,11 @@ impl Options {
// Check for a path to a version macro file
if path.exists() {
trace!("checking for version macro in {:?}", path);
let content = std::fs::read_to_string(path)?;
let content = if let Some(vfs) = vfs {
vfs.join(&binding)?.read_to_string()?
} else {
std::fs::read_to_string(path)?
};
return Version::try_from_script_version(&content).map_err(Into::into);
}
error!("could not find version macro file: {:?}", path);
Expand Down
2 changes: 1 addition & 1 deletion bin/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Context {
return Err(Error::ConfigNotFound);
}
let config = Configuration::from_file(&path)?;
let version = config.version().get();
let version = config.version().get(None);
if let Err(Error::Git(_)) = version {
error!("Failed to find a git repository with at least one commit, if you are not using git add the following to your project.toml");
println!("\n[version]\ngit_hash = 0\n");
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn release(ctx: &Context) -> Result<(), Error> {
output.set_file_name(format!(
"{}-{}.zip",
ctx.config().prefix(),
ctx.config().version().get()?
ctx.config().version().get(Some(ctx.workspace().vfs()))?
));
info!("Created release: {}", output.display());
output
Expand Down
6 changes: 5 additions & 1 deletion bin/src/modules/hook/libraries/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ impl RhaiProject {
Self {
name: ctx.config().name().to_string(),
prefix: ctx.config().prefix().to_string(),
version: ctx.config().version().get().unwrap(),
version: ctx
.config()
.version()
.get(Some(ctx.workspace().vfs()))
.unwrap(),
// addons: ctx.addons().to_vec(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/hook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod time;
pub fn scope(ctx: &Context, vfs: bool) -> Result<Scope, Error> {
let mut scope = Scope::new();
scope.push_constant("HEMTT_VERSION", env!("CARGO_PKG_VERSION"));
let version = ctx.config().version().get()?;
let version = ctx.config().version().get(Some(ctx.workspace().vfs()))?;
scope.push_constant("HEMTT_PROJECT_VERSION", version.to_string());
scope.push_constant("HEMTT_PROJECT_VERSION_MAJOR", version.major());
scope.push_constant("HEMTT_PROJECT_VERSION_MINOR", version.minor());
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/pbo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum Collapse {
}

pub fn build(ctx: &Context, collapse: Collapse) -> Result<(), Error> {
let version = ctx.config().version().get()?;
let version = ctx.config().version().get(Some(ctx.workspace().vfs()))?;
let git_hash = {
Repository::discover(".").map_or(None, |repo| {
repo.revparse_single("HEAD").map_or(None, |rev| {
Expand Down
2 changes: 1 addition & 1 deletion bin/src/modules/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn get_authority(ctx: &Context, suffix: Option<&str>) -> Result<String, Error> {
|| ctx.config().prefix().to_string(),
std::string::ToString::to_string
),
ctx.config().version().get()?
ctx.config().version().get(Some(ctx.workspace().vfs()))?
);
if let Some(suffix) = suffix {
authority.push_str(&format!("_{suffix}"));
Expand Down

0 comments on commit 2d2f866

Please sign in to comment.