Skip to content

Commit

Permalink
Use nuget.org if no NuGet.config file specified
Browse files Browse the repository at this point in the history
  • Loading branch information
winterqt committed Feb 9, 2022
1 parent e941e31 commit d9a2838
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Generates a Nix expression for `buildDotnetModule`, with support for non `nuget.

## Usage

Similar to the `nuget-to-nix` command available in Nixpkgs, you'll need a directory of packages. This can be achieved with `dotnet restore` (see [here](https://github.com/NixOS/nixpkgs/blob/3ecddf791da4d893beb35fb09eb9da55b326f4fb/pkgs/build-support/build-dotnet-module/default.nix#L142) for an example). Additionally, you'll need a path to the `NuGet.config` file for your package.
Similar to the `nuget-to-nix` command available in Nixpkgs, you'll need a directory of packages. This can be achieved with `dotnet restore` (see [here](https://github.com/NixOS/nixpkgs/blob/3ecddf791da4d893beb35fb09eb9da55b326f4fb/pkgs/build-support/build-dotnet-module/default.nix#L142) for an example). Optionally, you can specify a path to the `NuGet.config` file for your package.

Once you have these, the tool can be invoked like so:
```
$ nuget2nix --directory /path/to/packages --nuget-config /path/to/NuGet.config
$ nuget2nix --directory /path/to/packages <--nuget-config /path/to/NuGet.config>
```

On completion, the Nix expression to pass to `nugetDeps` is output to stdout.
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,18 @@ async fn main() -> anyhow::Result<()> {
let mut args = Arguments::from_env();

let dir: Utf8PathBuf = args.value_from_str("--directory")?;
let nuget_config: Utf8PathBuf = args.value_from_str("--nuget-config")?;
let nuget_config = args.value_from_str::<_, Utf8PathBuf>("--nuget-config");

let mut repos = Vec::new();

if let Ok(nuget_config) = nuget_config {
repos = get_repos(nuget_config.as_std_path()).await?;
}

if repos.is_empty() {
repos.push(Arc::new(NuGet::nuget_org().await?));
}

let repos = get_repos(nuget_config.as_std_path()).await?;
let mut packages = Vec::new();

for mut path in glob(dir.join("**/*.nuspec").as_str())?.map(Result::unwrap) {
Expand Down
6 changes: 6 additions & 0 deletions src/nuget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use dashmap::DashMap;
use serde::Deserialize;
use url::Url;

const NUGET_ORG_INDEX_URL: &str = "https://api.nuget.org/v3/index.json";

pub struct NuGet {
client: reqwest::Client,
package_base_address: Url,
Expand All @@ -28,6 +30,10 @@ impl NuGet {
})
}

pub async fn nuget_org() -> anyhow::Result<NuGet> {
NuGet::new(Url::parse(NUGET_ORG_INDEX_URL)?).await
}

pub async fn exists(&self, package: &str, version: &str) -> bool {
if !self.package_cache.contains_key(package) {
async fn get(this: &NuGet, package: &str) -> Option<Vec<String>> {
Expand Down

0 comments on commit d9a2838

Please sign in to comment.