Skip to content

Commit

Permalink
feat(csharp): allow configurable sdk version (#766)
Browse files Browse the repository at this point in the history
Co-authored-by: Milo <[email protected]>
Closes #323
  • Loading branch information
patrickserrano authored Jan 30, 2023
1 parent e15e422 commit 5a1b93e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[alias]
generate_plan_tests = "test --package nixpacks --lib --test generate_plan_tests"
generate-plan-tests = "test --package nixpacks --lib --test generate_plan_tests"
snapshot = "insta test --review -- --test generate_plan_tests"
lint-fix = "clippy --fix --allow-dirty --allow-staged --all-targets --all-features -- -D warnings"
5 changes: 5 additions & 0 deletions docs/pages/docs/providers/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ title: C#

CSharp is detected if any `*.csproj*` files are found.

The SDK version can be overridden by

- Setting the `NIXPACKS_CSHARP_SDK_VERSION` environment variable
- Setting the version in a `global.json` file

## Install

```
Expand Down
5 changes: 5 additions & 0 deletions examples/csharp-api/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "7.0.0"
}
}
95 changes: 93 additions & 2 deletions src/providers/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ use crate::nixpacks::{
},
};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct CSharpSdk {
pub version: Option<String>,
}

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct CSharpGlobalJson {
pub sdk: Option<CSharpSdk>,
}

pub struct CSharpProvider {}

Expand All @@ -23,8 +34,9 @@ impl Provider for CSharpProvider {
Ok(!app.find_files("*.csproj")?.is_empty())
}

fn get_build_plan(&self, app: &App, _env: &Environment) -> Result<Option<BuildPlan>> {
let setup = Phase::setup(Some(vec![Pkg::new("dotnet-sdk")]));
fn get_build_plan(&self, app: &App, env: &Environment) -> Result<Option<BuildPlan>> {
let sdk = CSharpProvider::get_sdk_version(app, env);
let setup = Phase::setup(Some(vec![Pkg::new(sdk?.as_str())]));
let install = Phase::install(Some("dotnet restore".to_string()));
let build = Phase::build(Some(format!(
"dotnet publish --no-restore -c Release -o {ARTIFACT_DIR}"
Expand Down Expand Up @@ -57,3 +69,82 @@ impl Provider for CSharpProvider {
Ok(Some(plan))
}
}

impl CSharpProvider {
fn get_sdk_version(app: &App, env: &Environment) -> Result<String> {
// First check for an SDK version environment variable
if let Some(version) = env.get_config_variable("CSHARP_SDK_VERSION") {
if let Some((major, minor)) = &version[0..3].split_once('.') {
return Ok(format!("dotnet-sdk_{major}_{minor}"));
}
}

// Then check for a global.json and see if we can get the sdk version from there
if app.includes_file("global.json") {
let global_json: CSharpGlobalJson = app.read_json("global.json")?;

if let Some(sdk) = global_json.sdk {
if let Some(version) = sdk.version {
if let Some((major, minor)) = &version[0..3].split_once('.') {
return Ok(format!("dotnet-sdk_{major}_{minor}"));
}
}
}
}

// Fall back to default sdk
Ok("dotnet-sdk".to_string())
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::nixpacks::{app::App, environment::Environment};
use std::collections::BTreeMap;

#[test]
fn test_no_version() -> Result<()> {
let expected_sdk_name = "dotnet-sdk";
assert_eq!(
CSharpProvider::get_sdk_version(
&App::new("./examples/csharp-cli")?,
&Environment::default()
)?,
expected_sdk_name
);

Ok(())
}

#[test]
fn test_global_json() -> Result<()> {
let expected_sdk_name = "dotnet-sdk_7_0";
assert_eq!(
CSharpProvider::get_sdk_version(
&App::new("./examples/csharp-api")?,
&Environment::default()
)?,
expected_sdk_name
);

Ok(())
}

#[test]
fn test_version_from_environment_variable() -> Result<()> {
let expected_sdk_name = "dotnet-sdk_6_0";
assert_eq!(
CSharpProvider::get_sdk_version(
&App::new("./examples/csharp-cli")?,
&Environment::new(BTreeMap::from([(
"NIXPACKS_CSHARP_SDK_VERSION".to_string(),
"6.0.0".to_string()
)]))
)?,
expected_sdk_name
);

Ok(())
}
}
2 changes: 1 addition & 1 deletion tests/snapshots/generate_plan_tests__csharp_api.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ expression: plan
"setup": {
"name": "setup",
"nixPkgs": [
"dotnet-sdk"
"dotnet-sdk_7_0"
],
"nixOverlays": [],
"nixpkgsArchive": "[archive]"
Expand Down

0 comments on commit 5a1b93e

Please sign in to comment.