Skip to content

Commit

Permalink
Merge pull request #33 from xoofx/improve-new
Browse files Browse the repository at this point in the history
Improve `dotnet-releaser new` command
  • Loading branch information
xoofx authored Mar 4, 2022
2 parents 604724d + 0a8e720 commit 0e4220b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
9 changes: 6 additions & 3 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,12 @@ Arguments:
dotnet-releaser.toml TOML configuration file path to create. Default is: dotnet-releaser.toml
Options:
--project <project_file> A - relative - path to project file (csproj, vbproj, fsproj)
--user <GitHub_user/org> The GitHub user/org where the packages will be published
--repo <GitHub_repo> The GitHub repo name where the packages will be published
--project <project_file> A - relative - path to a solution file (.sln) or project file (.csproj, .fsproj, .vbproj). By default, it will try to find a solution file where this command is run or where the output dotnet-releaser.toml file
is specified.
--user <GitHub_user/org> The GitHub user/org where the packages will be published. If not specified, it will try to detect automatically if there is a git repository configured from the folder (and parents) of the TOML configuration
file, and extract any git remote that could give this information.
--repo <GitHub_repo> The GitHub repo name where the packages will be published. If not specified, it will try to detect automatically if there is a git repository configured from the folder (and parents) of the TOML configuration
file, and extract any git remote that could give this information.
--force Force overwriting the existing TOML configuration file.
-?|-h|--help Show help information.
```
Expand Down
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ By default, `dotnet-releaser` will:
> See the [user guide](https://github.com/xoofx/dotnet-releaser/blob/main/doc/readme.md) on how to setup this differently for your application.
## Getting Started

- Create a `dotnet-releaser.toml` at the same level you have your .NET solution. Most projects won't need more than this kind of configuration:
- Install `dotnet-releaser` as a global .NET tool.
```
dotnet tool install --global dotnet-releaser"
```
- Go to a folder where you have your solution `.sln` file or your project file (`.csproj`, `.fsproj`, `.vbproj`) and run:
```
dotnet releaser new
```
- It should create a `dotnet-releaser.toml` at the same level than your solution with a content like:
```toml
[msbuild]
project = "Tonlyn.sln"
[github]
user = "xoofx"
repo = "Tomlyn"
```
- Install `dotnet-releaser` as a global .NET tool.
```
dotnet tool install --global dotnet-releaser"
```
- If you want to try a full build locally:
```
dotnet-releaser build --force dotnet-releaser.toml
Expand Down
67 changes: 64 additions & 3 deletions src/dotnet-releaser/ReleaserApp.New.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using LibGit2Sharp;

namespace DotNetReleaser;

public partial class ReleaserApp
{
// (1) (2)
// [email protected]:xoofx/dotnet-releaser.git
private static readonly Regex GitUrlRegex = new Regex(@":(.+)/(.+)\.git$");

private async Task<bool> CreateConfigurationFile(string? destinationFilePath, string projectFile, string? user, string? repo, bool force)
private async Task<bool> CreateConfigurationFile(string? destinationFilePath, string? projectFile, string? user, string? repo, bool force)
{
destinationFilePath ??= Path.Combine(Environment.CurrentDirectory, "dotnet-releaser.toml");
destinationFilePath = Path.Combine(Environment.CurrentDirectory, destinationFilePath);
Expand All @@ -18,12 +24,67 @@ private async Task<bool> CreateConfigurationFile(string? destinationFilePath, st
return false;
}

// If projectFile is null, try to find a sln or project files
var folder = Path.GetFullPath(Path.GetDirectoryName(destinationFilePath)!);
if (projectFile is null)
{
projectFile = Directory.GetFiles(folder).FirstOrDefault(x => x.EndsWith(".sln"));
string kind = "Solution";
if (projectFile is null)
{
projectFile = Directory.GetFiles(folder).FirstOrDefault(x => x.EndsWith(".csproj") || x.EndsWith(".fsproj") || x.EndsWith(".vbproj"));
kind = "Project";
if (projectFile is null)
{
Error($"Unable to find a solution file (.sln) or project files (.csproj, .fsproj, .vbproj) in the current folder `{folder}`");
return false;
}
}
projectFile = Path.GetFileName(projectFile);
Info($"{kind} file detected: {projectFile}");
}

// Try to detect the user/repo
var repositoryPath = Repository.Discover(Path.GetDirectoryName(destinationFilePath));
if (repositoryPath is not null && user is null && repo is null)
{
var repository = new Repository(repositoryPath);
foreach (var remote in repository.Network.Remotes)
{
var url = remote.Url;
if (url.Contains('@'))
{
var match = GitUrlRegex.Match(url);
if (match.Success)
{
user = match.Groups[1].Value;
repo = match.Groups[2].Value;
Info($"git user/repo detected: {user}/{repo}");
break;
}
}
else if (url.StartsWith("http"))
{
// https://github.com/xoofx/dotnet-releaser.git
var uri = new Uri(url);
var path = uri.PathAndQuery;
user = Path.GetFileName(Path.GetDirectoryName(path));
repo = Path.GetFileNameWithoutExtension(path);
Info($"git user/repo detected: {user}/{repo}");
break;
}
}
}

user ??= "github_user_or_org_here";
repo ??= "github_repo_here";

var configAsText = $@"# configuration file for dotnet-releaser
[msbuild]
project = ""{projectFile.Replace('\\', '/')}""
[github]
user = ""{user ?? "github_user_or_org_here"}""
repo = ""{repo ?? "github_repo_here"}""
user = ""{user}""
repo = ""{repo}""
";

// Normalize the output
Expand Down
6 changes: 3 additions & 3 deletions src/dotnet-releaser/ReleaserApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ public static async Task<int> Run(string[] args)
{
newCommand.Description = "Create a dotnet-releaser TOML configuration file for a specified project.";
var configurationFileArg = AddTomlConfigurationArgument(newCommand, true);
var projectOption = newCommand.Option<string>("--project <project_file>", "A - relative - path to project file (csproj, vbproj, fsproj)", CommandOptionType.SingleValue).IsRequired();
var userOption = newCommand.Option<string>("--user <GitHub_user/org>", "The GitHub user/org where the packages will be published", CommandOptionType.SingleValue);
var repoOption = newCommand.Option<string>("--repo <GitHub_repo>", "The GitHub repo name where the packages will be published", CommandOptionType.SingleValue);
var projectOption = newCommand.Option<string>("--project <project_file>", "A - relative - path to a solution file (.sln) or project file (.csproj, .fsproj, .vbproj). By default, it will try to find a solution file where this command is run or where the output dotnet-releaser.toml file is specified.", CommandOptionType.SingleValue);
var userOption = newCommand.Option<string>("--user <GitHub_user/org>", "The GitHub user/org where the packages will be published. If not specified, it will try to detect automatically if there is a git repository configured from the folder (and parents) of the TOML configuration file, and extract any git remote that could give this information.", CommandOptionType.SingleValue);
var repoOption = newCommand.Option<string>("--repo <GitHub_repo>", "The GitHub repo name where the packages will be published. If not specified, it will try to detect automatically if there is a git repository configured from the folder (and parents) of the TOML configuration file, and extract any git remote that could give this information.", CommandOptionType.SingleValue);
var forceOption = newCommand.Option<bool>("--force", "Force overwriting the existing TOML configuration file.", CommandOptionType.NoValue);

newCommand.OnExecuteAsync(async token =>
Expand Down

0 comments on commit 0e4220b

Please sign in to comment.