Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spin add static-fileserver putting the asset directory in the wrong place #2388

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/templates/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ pub(crate) enum RawExtraOutput {
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub(crate) struct RawCreateDir {
pub path: String,
pub at: Option<CreateLocation>,
}

#[derive(Debug, Deserialize, Clone, Copy, Default)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub(crate) enum CreateLocation {
#[default]
Component,
Manifest,
}

pub(crate) fn parse_manifest_toml(text: impl AsRef<str>) -> anyhow::Result<RawTemplateManifest> {
Expand Down
15 changes: 13 additions & 2 deletions crates/templates/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,20 @@ impl Run {

fn extra_operation(&self, extra: &ExtraOutputAction) -> anyhow::Result<RenderOperation> {
match extra {
ExtraOutputAction::CreateDirectory(_, template) => {
ExtraOutputAction::CreateDirectory(_, template, at) => {
let component_path = self.options.output_path.clone();
let base_path = match at {
crate::reader::CreateLocation::Component => component_path,
crate::reader::CreateLocation::Manifest => match &self.options.variant {
TemplateVariantInfo::NewApplication => component_path,
TemplateVariantInfo::AddComponent { manifest_path } => manifest_path
.parent()
.map(|p| p.to_owned())
.unwrap_or(component_path),
},
};
Ok(RenderOperation::CreateDirectory(
self.options.output_path.clone(),
base_path,
template.clone(),
))
}
Expand Down
16 changes: 13 additions & 3 deletions crates/templates/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ pub(crate) struct TemplateParameter {
}

pub(crate) enum ExtraOutputAction {
CreateDirectory(String, std::sync::Arc<liquid::Template>),
CreateDirectory(
String,
std::sync::Arc<liquid::Template>,
crate::reader::CreateLocation,
),
}

impl std::fmt::Debug for ExtraOutputAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CreateDirectory(orig, _) => f.debug_tuple("CreateDirectory").field(orig).finish(),
Self::CreateDirectory(orig, ..) => {
f.debug_tuple("CreateDirectory").field(orig).finish()
}
}
}
}
Expand Down Expand Up @@ -501,7 +507,11 @@ impl ExtraOutputAction {
liquid::Parser::new().parse(&create.path).with_context(|| {
format!("Template error: output {id} is not a valid template")
})?;
Self::CreateDirectory(create.path.clone(), std::sync::Arc::new(path_template))
Self::CreateDirectory(
create.path.clone(),
std::sync::Arc::new(path_template),
create.at.unwrap_or_default(),
)
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions crates/templates/src/test_built_ins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn new_fileserver_creates_assets_dir() -> anyhow::Result<()> {
}

#[tokio::test]
async fn add_fileserver_creates_assets_dir() -> anyhow::Result<()> {
async fn add_fileserver_creates_assets_dir_next_to_manifest() -> anyhow::Result<()> {
let built_ins_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let built_ins_src = TemplateSource::File(built_ins_dir);

Expand Down Expand Up @@ -116,12 +116,12 @@ async fn add_fileserver_creates_assets_dir() -> anyhow::Result<()> {

// Finally!
assert!(
app_dir.path().join("fs").exists(),
"<app_dir>/fs should have been created"
!app_dir.path().join("fs").exists(),
"<app_dir>/fs should not have been created"
);
assert!(
app_dir.path().join("fs").join("moarassets").exists(),
"<app_dir>/fs/moarassets should have been created"
app_dir.path().join("moarassets").exists(),
"<app_dir>/moarassets should have been created"
);
Ok(())
}
2 changes: 1 addition & 1 deletion templates/static-fileserver/metadata/spin-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ http-path = { type = "string", prompt = "HTTP path", default = "/static/...", pa
files-path = { type = "string", prompt = "Directory containing the files to serve", default = "assets", pattern = "^\\S+$" }

[outputs]
create_asset_directory = { action = "create_dir", path = "{{ files-path }}" }
create_asset_directory = { action = "create_dir", path = "{{ files-path }}", at = "manifest" }
Loading