Skip to content

Commit a400e3f

Browse files
authored
Merge pull request #2388 from itowlson/templates-now-add-statis-fs-is-all-wrong
Fix `spin add static-fileserver` putting the asset directory in the wrong place
2 parents fc0baf4 + 9de1cf7 commit a400e3f

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

crates/templates/src/reader.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ pub(crate) enum RawExtraOutput {
5656
#[serde(deny_unknown_fields, rename_all = "snake_case")]
5757
pub(crate) struct RawCreateDir {
5858
pub path: String,
59+
pub at: Option<CreateLocation>,
60+
}
61+
62+
#[derive(Debug, Deserialize, Clone, Copy, Default)]
63+
#[serde(deny_unknown_fields, rename_all = "snake_case")]
64+
pub(crate) enum CreateLocation {
65+
#[default]
66+
Component,
67+
Manifest,
5968
}
6069

6170
pub(crate) fn parse_manifest_toml(text: impl AsRef<str>) -> anyhow::Result<RawTemplateManifest> {

crates/templates/src/run.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,20 @@ impl Run {
301301

302302
fn extra_operation(&self, extra: &ExtraOutputAction) -> anyhow::Result<RenderOperation> {
303303
match extra {
304-
ExtraOutputAction::CreateDirectory(_, template) => {
304+
ExtraOutputAction::CreateDirectory(_, template, at) => {
305+
let component_path = self.options.output_path.clone();
306+
let base_path = match at {
307+
crate::reader::CreateLocation::Component => component_path,
308+
crate::reader::CreateLocation::Manifest => match &self.options.variant {
309+
TemplateVariantInfo::NewApplication => component_path,
310+
TemplateVariantInfo::AddComponent { manifest_path } => manifest_path
311+
.parent()
312+
.map(|p| p.to_owned())
313+
.unwrap_or(component_path),
314+
},
315+
};
305316
Ok(RenderOperation::CreateDirectory(
306-
self.options.output_path.clone(),
317+
base_path,
307318
template.clone(),
308319
))
309320
}

crates/templates/src/template.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,19 @@ pub(crate) struct TemplateParameter {
118118
}
119119

120120
pub(crate) enum ExtraOutputAction {
121-
CreateDirectory(String, std::sync::Arc<liquid::Template>),
121+
CreateDirectory(
122+
String,
123+
std::sync::Arc<liquid::Template>,
124+
crate::reader::CreateLocation,
125+
),
122126
}
123127

124128
impl std::fmt::Debug for ExtraOutputAction {
125129
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126130
match self {
127-
Self::CreateDirectory(orig, _) => f.debug_tuple("CreateDirectory").field(orig).finish(),
131+
Self::CreateDirectory(orig, ..) => {
132+
f.debug_tuple("CreateDirectory").field(orig).finish()
133+
}
128134
}
129135
}
130136
}
@@ -501,7 +507,11 @@ impl ExtraOutputAction {
501507
liquid::Parser::new().parse(&create.path).with_context(|| {
502508
format!("Template error: output {id} is not a valid template")
503509
})?;
504-
Self::CreateDirectory(create.path.clone(), std::sync::Arc::new(path_template))
510+
Self::CreateDirectory(
511+
create.path.clone(),
512+
std::sync::Arc::new(path_template),
513+
create.at.unwrap_or_default(),
514+
)
505515
}
506516
})
507517
}

crates/templates/src/test_built_ins/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn new_fileserver_creates_assets_dir() -> anyhow::Result<()> {
6161
}
6262

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

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

117117
// Finally!
118118
assert!(
119-
app_dir.path().join("fs").exists(),
120-
"<app_dir>/fs should have been created"
119+
!app_dir.path().join("fs").exists(),
120+
"<app_dir>/fs should not have been created"
121121
);
122122
assert!(
123-
app_dir.path().join("fs").join("moarassets").exists(),
124-
"<app_dir>/fs/moarassets should have been created"
123+
app_dir.path().join("moarassets").exists(),
124+
"<app_dir>/moarassets should have been created"
125125
);
126126
Ok(())
127127
}

templates/static-fileserver/metadata/spin-template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ http-path = { type = "string", prompt = "HTTP path", default = "/static/...", pa
1616
files-path = { type = "string", prompt = "Directory containing the files to serve", default = "assets", pattern = "^\\S+$" }
1717

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

0 commit comments

Comments
 (0)