Skip to content

Commit

Permalink
Only clear target files that were added by server-wrapper
Browse files Browse the repository at this point in the history
This will allow targeting single files such as `server.properties` without clearing the whole server directory. It's also a bit safer in general!
  • Loading branch information
Gegy committed Aug 7, 2022
1 parent 06e9063 commit 73ae938
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ game-configs = { github = "NucleoidMC/Game-Configs" }
```

The basic structure of the destinations file involves the definition of multiple named definitions, where the name can be arbitrary.
Each destination declares a target path where all files will be copied into. Importantly, **this directory will be cleared**! Make sure to not add anything important to the directory.
Each destination declares a target path where all files will be copied into.

Destinations furthermore can declare multiple named sources, where the names are also arbitrary.
The purpose of separate sources is to provide different transform procedures to files. For example, loading from GitHub Actions may require unzipping the artifacts file and selecting a specific file.
Expand Down
16 changes: 14 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,23 @@ pub struct Reference {

impl Reference {
pub async fn copy_to<P: AsRef<Path>>(&self, root: P) -> io::Result<()> {
let target_path = root.as_ref().join(&self.name);
fs::copy(&self.path, target_path).await?;
fs::copy(&self.path, self.resolve_target_path(root)).await?;
Ok(())
}

pub async fn remove_from<P: AsRef<Path>>(&self, root: P) -> io::Result<()> {
let target = self.resolve_target_path(root);
if target.exists() {
fs::remove_file(target).await
} else {
Ok(())
}
}

fn resolve_target_path<P: AsRef<Path>>(&self, root: P) -> PathBuf {
root.as_ref().join(&self.name)
}

pub fn changed(&self) -> bool { self.changed }
}

Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async fn prepare_destination(ctx: &Context, destination_name: &str, destination:
.map(|s| s.to_owned())
.collect();

cache.drop_stale(cache_keys);
let stale_files = cache.drop_stale(cache_keys).await?;

for (_, source_set) in &destination.sources {
for (key, source) in &source_set.sources {
Expand All @@ -159,22 +159,26 @@ async fn prepare_destination(ctx: &Context, destination_name: &str, destination:
Ok(PreparedDestination {
root: destination.path.clone(),
cache_files,
stale_files,
})
}

struct PreparedDestination {
root: PathBuf,
cache_files: Vec<(String, cache::Reference)>,
stale_files: Vec<cache::Reference>,
}

impl PreparedDestination {
async fn apply(&self) -> Result<()> {
if self.root.exists() {
fs::remove_dir_all(&self.root).await?;
for reference in &self.stale_files {
reference.remove_from(&self.root).await?;
}
} else {
fs::create_dir_all(&self.root).await?;
}

fs::create_dir_all(&self.root).await?;

for (_, reference) in &self.cache_files {
reference.copy_to(&self.root).await?;
}
Expand Down

0 comments on commit 73ae938

Please sign in to comment.