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

Detect launch processes #48

Merged
merged 5 commits into from
Jun 18, 2024
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
14 changes: 10 additions & 4 deletions buildpacks/dotnet/src/dotnet_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use thiserror::Error;
pub(crate) struct DotnetProject {
pub(crate) path: PathBuf,
pub(crate) target_framework: String,
#[allow(dead_code)]
pub(crate) project_type: ProjectType,
#[allow(dead_code)]
pub(crate) assembly_name: Option<String>,
pub(crate) assembly_name: String,
}

impl DotnetProject {
Expand All @@ -30,7 +28,15 @@ impl DotnetProject {
path: path.to_path_buf(),
target_framework: metadata.target_framework,
project_type,
assembly_name: metadata.assembly_name,
assembly_name: metadata
.assembly_name
.filter(|name| !name.is_empty())
.unwrap_or_else(|| {
path.file_stem()
.expect("path to have a file name")
.to_string_lossy()
.to_string()
}),
})
}
}
Expand Down
61 changes: 61 additions & 0 deletions buildpacks/dotnet/src/launch_process.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::dotnet_project::ProjectType;
use crate::dotnet_rid::RuntimeIdentifier;
use crate::dotnet_solution::DotnetSolution;
use libcnb::data::launch::{
Process, ProcessBuilder, ProcessType, ProcessTypeError, WorkingDirectory,
};

#[derive(thiserror::Error, Debug)]
pub(crate) enum LaunchProcessDetectionError {
#[error("Project has an invalid process type name: {0}")]
ProcessType(ProcessTypeError),
}

pub(crate) fn detect_solution_processes(
solution: &DotnetSolution,
configuration: &str,
rid: &RuntimeIdentifier,
) -> Result<Vec<Process>, LaunchProcessDetectionError> {
solution
.projects
.iter()
.filter(|project| {
matches!(
project.project_type,
ProjectType::WebApplication | ProjectType::ConsoleApplication
)
})
.map(|project| {
let executable_path = project
.path
.parent()
.expect("Project file should always have a parent directory")
.join("bin")
.join(configuration)
.join(&project.target_framework)
.join(rid.to_string())
.join("publish")
.join(&project.assembly_name);
let mut command = format!("{}", executable_path.to_string_lossy());

if project.project_type == ProjectType::WebApplication {
command.push_str(" --urls http://0.0.0.0:$PORT");
}

project
.assembly_name
.parse::<ProcessType>()
.map_err(LaunchProcessDetectionError::ProcessType)
.map(|process_type| {
ProcessBuilder::new(process_type, ["bash", "-c", &command])
.working_directory(WorkingDirectory::Directory(
executable_path
.parent()
.expect("Executable should always have a parent directory")
.to_path_buf(),
))
.build()
})
})
.collect::<Result<_, _>>()
}
31 changes: 28 additions & 3 deletions buildpacks/dotnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod dotnet_project;
mod dotnet_rid;
mod dotnet_solution;
mod global_json;
mod launch_process;
mod layers;
mod tfm;
mod utils;
Expand All @@ -12,12 +13,14 @@ use crate::dotnet_project::DotnetProject;
use crate::dotnet_rid::RuntimeIdentifier;
use crate::dotnet_solution::DotnetSolution;
use crate::global_json::GlobalJson;
use crate::launch_process::LaunchProcessDetectionError;
use crate::layers::sdk::SdkLayerError;
use crate::tfm::{ParseTargetFrameworkError, TargetFrameworkMoniker};
use crate::utils::StreamedCommandError;
use inventory::artifact::{Arch, Os};
use inventory::inventory::{Inventory, ParseInventoryError};
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::launch::LaunchBuilder;
use libcnb::data::layer_name;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
use libcnb::generic::{GenericMetadata, GenericPlatform};
Expand Down Expand Up @@ -138,11 +141,19 @@ impl Buildpack for DotnetBuildpack {
nuget_cache_layer.path(),
);

let configuration = String::from("Release"); // TODO: Make publish configuration configurable;
let runtime_identifier = dotnet_rid::get_runtime_identifier();
let launch_processes_result = launch_process::detect_solution_processes(
&solution,
&configuration,
&runtime_identifier,
);

utils::run_command_and_stream_output(
Command::from(PublishCommand {
path: solution.path,
configuration: String::from("Release"), // TODO: Make publish configuration configurable
runtime_identifier: dotnet_rid::get_runtime_identifier(),
configuration,
runtime_identifier,
verbosity_level: String::from("normal"),
})
.current_dir(&context.app_dir)
Expand All @@ -152,7 +163,19 @@ impl Buildpack for DotnetBuildpack {

layers::runtime::handle(&context, &sdk_layer.path())?;

BuildResultBuilder::new().build()
BuildResultBuilder::new()
.launch(
LaunchBuilder::new()
.processes(
launch_processes_result
// TODO: Failing to detect launch processes probably shouldn't cause a buildpack error.
// Handle errors in a way that provides helpful information to correct the issue, or
// instructions for writing a Procfile.
.map_err(DotnetBuildpackError::LaunchProcessDetection)?,
)
.build(),
)
.build()
}
}

Expand Down Expand Up @@ -292,6 +315,8 @@ enum DotnetBuildpackError {
PublishCommand(#[from] StreamedCommandError),
#[error("Error copying runtime files {0}")]
CopyRuntimeFilesToRuntimeLayer(io::Error),
#[error("Launch process detection error: {0}")]
LaunchProcessDetection(LaunchProcessDetectionError),
}

impl From<DotnetBuildpackError> for libcnb::Error<DotnetBuildpackError> {
Expand Down