Skip to content

Commit

Permalink
Remove InAppDirCacheLayer in favor of struct api
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Sep 9, 2024
1 parent 20fc090 commit d86fda2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 85 deletions.
1 change: 0 additions & 1 deletion commons/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod app_cache_collection;
mod clean;
mod config;
mod error;
mod in_app_dir_cache_layer;

pub use self::app_cache::{build, PathState};
pub use self::app_cache::{AppCache, CacheState};
Expand Down
34 changes: 31 additions & 3 deletions commons/src/cache/app_cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::cache::clean::{lru_clean, FilesWithSize};
use crate::cache::in_app_dir_cache_layer::InAppDirCacheLayer;
use crate::cache::{CacheConfig, CacheError, KeepPath};
use byte_unit::{AdjustedByte, Byte};
use fs_extra::dir::CopyOptions;
use libcnb::build::BuildContext;
use libcnb::data::layer::LayerName;
use libcnb::layer::{CachedLayerDefinition, InvalidMetadataAction, RestoredLayerAction};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::path::PathBuf;

Expand Down Expand Up @@ -249,11 +250,33 @@ pub fn build<B: libcnb::Buildpack>(
let layer_name = create_layer_name(&context.app_dir, &path)?;
let create_state = layer_name_cache_state(&context.layers_dir, &layer_name);

let metadata = InAppDirCacheLayerMetadata {
app_dir_path: path.clone(),
};

let layer = context
.handle_layer(layer_name, InAppDirCacheLayer::new(path.clone()))
.cached_layer(
layer_name,
CachedLayerDefinition {
launch: false,
build: true,
invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
restored_layer_action: &|old: &InAppDirCacheLayerMetadata, _| {
if old.app_dir_path == metadata.app_dir_path {
Ok(RestoredLayerAction::KeepLayer)
} else {
Ok(RestoredLayerAction::DeleteLayer)
}
},
},
)
.map_err(|error| CacheError::InternalLayerError(format!("{error:?}")))?;

let cache = layer.path;
layer
.write_metadata(metadata)
.map_err(|error| CacheError::InternalLayerError(format!("{error:?}")))?;

let cache = layer.path();

Ok(AppCache {
path,
Expand All @@ -264,6 +287,11 @@ pub fn build<B: libcnb::Buildpack>(
})
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub(crate) struct InAppDirCacheLayerMetadata {
app_dir_path: PathBuf,
}

/// Copy contents of application path into the cache
///
/// This action preserves the contents in the application path.
Expand Down
81 changes: 0 additions & 81 deletions commons/src/cache/in_app_dir_cache_layer.rs

This file was deleted.

0 comments on commit d86fda2

Please sign in to comment.